ARDUINO ASSIGNMENT 2
Abdull Rehman Ali
ME-10-A
260165
Submitted to: MAM SADIA QAMAR
PARKING SENSOR
Components
Circuit diagram
Outputs
OBJECT
DISTANCE
LED
when distance is greater than 60 (100 in this case), the led
shows green colour
when distance is between 60 and 40 (50 in this case), the
led shows blue colour
When distance is less than 40 (30 in this case), the led
shows red color
Construction
I used an ultrasonic sensor hc-sr04. This sensor has 4 legs,
• Vcc: +5vdc
• Trig: trigger (input)
• Echo: echo (output)
• Gnd: gnd
After the connections of sensor were made with Arduino, I took a RGB led bulb and connect its pins to
Arduino (cathode to gnd and colour pins to I/O pins of Arduino). Then I assigned pins number
according to the connections made. That’s pretty much my construction, let’s move on to working.
Working
Initially all the LED are set at low output. A pulse of 10 microseconds is generated for the Trigger pin of
sensor by changing its value from low to high and back to low with 5 microsecond delay. Then input is
recorded from echo pin inside an integer “time” by using “pulseIn” command. A pulseIn reads a pulse
(either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH,
starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in
microseconds or 0 if no complete pulse was received within the timeout. Once the time is recorded, it
is converted to distance by using simple formula “ Distance = Speed x Time ”. we recorded the time
and multiply it with speed of sound (0.0343). this time is basically the total time ultrasonic signal takes
to hit the object and return to sensor so, we divide it with 2. The we simply use “if” command and set
the outputs of LED color according to the distance of the object from sensor. such as:
❖ Distance > 60 cm LED turns GREEN
❖ Distance > 40 cm but < 60 cm LED turns BLUE
❖ Distance < 40 cm LED turns RED
However, there is a limitation in current sensor I used, it works at minimum distance of 2.5 cm. any
object smaller then 2 cm and within 2.5 cm range of sensor will not activate it. And circuit will gives
green color.
Object too close to sensor cannot activate sensor that’s why
sensor is not giving distance output and LED shows green
color.
Code
int trigPin = 7;
int echoPin = 6;
int redPin = 3;
int bluePin = 2;
int greenPin = 1;
int time, distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
void loop() {
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
time = pulseIn(echoPin, HIGH);
// Convert the time into a distance
distance = (time/2)* 0.0343 ; // Divide by 29.1 or multiply by 0.0343
if (distance > 60){ // If the distance is larger than 60cm Turn On Green Leds
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
if (distance > 40 && distance < 60) { // If the distance is larger than 40cm and under 60 Turn On blue
Leds
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
if (distance < 40){ // If the distance is less than 40cm Turn On red Led
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
delay(250);