Arduino Ultrasonic sensor hc-sr04 Measure distance with LEDs

Mr.ElectroUino
18



The Ultrasonic sensor is very popular and used to measure the distance to an object. It comes complete with ultrasonic transmitter and receiver modules. Hc-sr04 transmit high-frequency sound and when an object detects then reflect the signal to echo.


⚙Features:  
Distance measure:- 2cm to 400cm📏.
Power Supply: +5V DC.
Measuring Angle: 30 degree.
uses as: Obstacle avoiding robot, distance measure, measure water level and many more.




Requirement:
1. Arduino Uno 
2. Ultrasonic sensor Hc-sr04
3. 7 x LEDs
4. 7 x 220ohm resistor
5. Breadboard 
6. jumper wire 


Coding:
const int trig = 12;
const int echo = 13;

int duration = 0;
int distance = 0;

void setup() 
{
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
    
  Serial.begin(9600);
}

void loop()
{
  digitalWrite(trig , HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig , LOW);

  duration = pulseIn(echo , HIGH);
  distance = (duration/2) / 29.1 ;
  Serial.println(distance);

}





Coding:
const int trig = 12;
const int echo = 13;

const int LED1 = 8;
const int LED2 = 7;
const int LED3 = 6;
const int LED4 = 5;
const int LED5 = 4;
const int LED6 = 3;
const int LED7 = 2;

int duration = 0;
int distance = 0;

void setup() 
{
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  
  pinMode(LED1 , OUTPUT);
  pinMode(LED2 , OUTPUT);
  pinMode(LED3 , OUTPUT);
  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);
  }
}

Watch This👀:-

,

Post a Comment

18Comments

  1. There are two codes provided . Do we have to make 2 code files or write in a single file .. can you help me out please .

    ReplyDelete
    Replies
    1. Use any of them ... If u want to measure distance only use 1st code
      If u want to measure distance as well as glowing led use 2nd code

      Delete
  2. Thanks a lot for nice share. I want to try this. Plz tell me this is Arduino uno or arduino r3. Actually I was ordering online but found mone than 1 types of arduino.

    ReplyDelete
  3. I tried the same thing .....
    Distance is shown correctly ....
    But leds are not glowing

    ReplyDelete
    Replies
    1. did u find the solution? i stucked with same issue

      Delete
    2. Hi, this is because the code has 12 as trig and 13 as echo, and it's wrong. Watch the video, there you can see that the correct is, 12 as echo and 13 as trig.

      Delete
  4. Many mnay thanks to you for taking the time to share this! This is exactly what I needed for a future project and I am just starting out with the arduino. Can't wait to incorporate this into my project!! Wohoo thx man! 👍Cheers 🍻

    ReplyDelete
  5. Great and informative Content thanks for sharing with us. Keep it up.Setup Echo Input

    ReplyDelete
  6. Hi team i keep getting this error all the time i run this code all the time any leads?

    Arduino: 1.8.13 (Windows 7), Board: "Arduino Uno"

    Sketch uses 3234 bytes (10%) of program storage space. Maximum is 32256 bytes.

    Global variables use 190 bytes (9%) of dynamic memory, leaving 1858 bytes for local variables. Maximum is 2048 bytes.

    An error occurred while uploading the sketch

    avrdude: ser_open(): can't open device "\\.\COM3": Access is denied.

    ReplyDelete
  7. Hi really loved the video
    i am just having trouble being able to get the LEDs to work with it
    its all set up right and the code is right but its not working

    ReplyDelete
  8. Hello
    I have a problem in first code.Im not able to get my distance as its showing 0 in serial monitor..what I should do

    ReplyDelete
Post a Comment