LED Distance Indicator using Ultrasonic Sensor (HC-SR04) and Arduino.
How ultrasonic sensor work?
The ultrasonic sensor continues to transmit the signal by trig pin and receives the signal by echo pin. Hc-sr04 transmit the frequency of a 40khz sound wave and when an object detects then reflects the signal to an echo pin. The time between the transmission and reception of the signal allows us to calculate the distance to an object.
1. Arduino Uno
2. Ultrasonic sensor Hc-sr04
3. 5 x LEDs.
4. 5 x 220ohm resistor.
5. Breadboard.
6. Jumper wires
Circuit diagram:
Connect Ultrasonic sensor ( Hc-sr04 ) pins to Arduino Uno pins.
1. Connect ultrasonic sensor Trig Pin to Arduino digital Pin 2.
2. Connect ultrasonic sensor Echo Pin to Arduino digital Pin 3.
3. Connect ultrasonic sensor VCC Pin to Arduino 5v Pin.
4. Connect the ultrasonic sensor GND Pin to the Arduino digital GND Pin.
Connect LEDs pins to Arduino uno pins.
1. Connect LED1(+) Pin to Arduino Digital Pin 8.
2. Connect LED2(+) Pin to Arduino Digital Pin 7.
3. Connect LED3(+) Pin to Arduino Digital Pin 6.
4. Connect LED4(+) Pin to Arduino Digital Pin 5.
5. Connect LED5(+) Pin to Arduino Digital Pin 4.
8. Connect all LED Negative Terminal ( - ) to Arduino GND
The Code:
//arduino ultrasonic sensor hc-sr04 led projects code. /* Code: * by Amir Philip const int trig = 2; const int echo = 3; const int LED1 = 8; const int LED2 = 7; const int LED3 = 6; const int LED4 = 5; const int LED5 = 4; int duration = 0; int distance = 0; void setup() { pinMode(trig , OUTPUT); pinMode(echo , INPUT);
pinMode(LED4 , OUTPUT); pinMode(LED5 , OUTPUT); pinMode(LED6 , OUTPUT); pinMode(LED7 , OUTPUT); Serial.begin(9600); } void loop() { digitalWrite(trig , HIGH); delayMicroseconds(1000); digitalWrite(trig , LOW); duration = pulseIn(echo , HIGH); distance = (duration/2) / 28.5 ; Serial.println(distance); if ( distance <= 7 ) { digitalWrite(LED1, HIGH); } else { digitalWrite(LED1, LOW); } if ( distance <= 14 ) { digitalWrite(LED2, HIGH); } else { digitalWrite(LED2, LOW); } if ( distance <= 21 ) { digitalWrite(LED3, HIGH); } else { digitalWrite(LED3, LOW); } if ( distance <= 28 ) { digitalWrite(LED4, HIGH); } else { digitalWrite(LED4, LOW); } if ( distance <= 35 ) { digitalWrite(LED5, HIGH); } else { digitalWrite(LED5, LOW); } if ( distance <= 42 ) { digitalWrite(LED6, HIGH); } else { digitalWrite(LED6, LOW); } if ( distance <= 49 ) { digitalWrite(LED7, HIGH); } else { digitalWrite(LED7, LOW); } }
For more you can watch the video toturial..
0 Comments