Introduction: OBSTACLE LED ALARM
I bought a new HC-SR04 and I couldn't get it out of the box, and I felt many might be facing this problem.So I found a solution in this.
Step 1: HOW DOES THIS WORK?
So before starting this project let's know how this works'.
The RX of the ping sensor(HC-SR04) keeps sending ultrasonic waves. When an obstacle comes on its way the ultrasonic waves turn their back and are received by RX of the ping sensor, which causes the RED COLOR LIGHT EMITTING DIODE to turn on.
Attachments
Step 2: COMPONENTS REQUIRED
HARDWARE:
- 1 x ARDUINO (I used the uno rev3)
- 1 x BREADBOARD
- 1 x 560 ohm RESISTOR
- 1 x HC-SR04(ULTRASONIC SENSOR)
- 1 x RED LIGHT EMITTING DIODE
- 8 x MALE TO MALE JUMPER CABLES
SOFTWARE:
ARDUINO IDE
Step 3: CONNECTIONS
First insert the HC-SR04 ping sensor and the led into the breadboard, then connect the following:
- LED VCC to pin D-11 of arduino
- LED GND TO ARDUINO GND via 560 ohm resistor
- echo pin to D-13
-trig pin to D-12
Attachments
Step 4: CODE
After the circuits upload the following code:
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT); }
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) { // This is where the LED On/Off happens digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off digitalWrite(led2,LOW); }
else { digitalWrite(led,LOW); digitalWrite(led2,HIGH); } if (distance >= 200 || distance <= 0){ Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm"); }
delay(500); }