Introduction: Ultrasonic Distance Sensor Arduino HC-SR04

About: bachelors degree in Electrical Engineering, love developing hardware systems on both microcontrollers and PLC.

Hello Ladies and gentlemen :), I have a beginner tutorial Arduino project on the HC-SR04, this can actually be used as a security measure. Like showing how close someone or something is standing close to your door.if the person is standing a fair distance away the LED is GREEN, else if closer BLUE else if too close RED. This is exactly the program software logic. So Let's go!

Step 1: Required Electrical Components

Here is a list of electrical parts one needs to complete this project
-Arduino board
-RGB led(I am using a common cathode but single LEDs can work as well)
-3 resistors.
When you have all these, connect them

Step 2: Connection

Supply +5v to the breadboard from the Arduino +5v and do the same for the ground.
Mount the HC-SR04 on the breadboard. Remember from the digital electronics class the + rule vertical pins are linked together on the board rung and horizontal pins linked together in on he power rung.
Connect the sensor to the GND and +5v.
Connect the echo-pin of the HC-SR04 to pin 5 on the Arduino and trig-pin to pin 6 of the Arduino board.
Mount the LED on the breadboard. Connect resistors to the RGB pins and connect them to pin 7,8 and 9 of the Arduino respectively and connect the Cathode to ground.

Step 3: Code

After the connect write and upload the following code.


#define trigpin 6 
#define echopin 5

#define R 7

#define G 8

#define B 9

void setup()

{ //serial monitor and pin setup.
Serial.begin(9600); pinMode(trigpin,OUTPUT); //set trigpin as output pinMode(echopin,INPUT);//set echopin as input pinMode(R,OUTPUT);// set R,G and B as outputs pinMode(G,OUTPUT); pinMode(B,OUTPUT); // put your setup code here, to run once:


void loop()

//the trigpin sends out a signal, which bounces off an obstacle and comes back, the //echopin recieves this signal and gives out +5v setting the arduino pin on which it is connected to high. //distance= time*speed, but this distnce is divided by 2 because signal sent out returns //so distance= (the time it takes for the signal to leave and return)/2. //i.e if the time is 6s the distance = (6s/2) = 3m or cm. int duration, distance;//declare distance and duration as integers digitalWrite(trigpin,HIGH);// trigin send out signal _delay_ms(1000);//coninously for 1000ms digitalWrite(trigpin, LOW);// then goes low duration=pulseIn(echopin,HIGH); // duration is the pulseIn to the echopin distance=(duration/2)/29.1; // the 29.1 is used to convert the distnce to cm, the value varies for other units. if(distance > 0 && distance <= 20){//distcance is greater than 0 and less than 20cm digitalWrite(G,LOW);//green led is off digitalWrite(B,LOW);//blue led is off _delay_ms(500);//delay digitalWrite(R,HIGH);//red led is on _delay_ms(500); } else if(distance > 20 && distance <= 80){//distcance is greater than 20 and less than 80cm digitalWrite(R,LOW);//red led is off digitalWrite(G,LOW);//green led is off _delay_ms(500); digitalWrite(B,HIGH);//blue led is on } else if(distance > 80 && distance <= 120 ){//distcance is greater than 80 and less than 120cm digitalWrite(R,LOW);//red led is off digitalWrite(B,LOW);//blue led is off _delay_ms(500); digitalWrite(G,HIGH);//green led is on } Serial.print("cm"); Serial.println(distance);//print values on serial monitor _delay_ms(100); } // put your main code here, to run repeatedly:

Step 4: Test

The floor in my apartment has tiles and you can see the distance from my cupboard and fridge. The LED is GREEN, and changed to BLUE when the distance was cut to half and finally RED when the sensor was really close. I hope you enjoyed this. I am planning to make something similar but I will be using the MQ-2 smoke sensor to change the color of the LEDs. if you have any questions about this project please put them as comments and i will reply. Thank you for your time. Arigato :)