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👀:-
,
nice bro
ReplyDeletebkotyu0[in[0i9n n-j-9j
Delete😀
DeleteThere are two codes provided . Do we have to make 2 code files or write in a single file .. can you help me out please .
ReplyDeleteUse any of them ... If u want to measure distance only use 1st code
DeleteIf u want to measure distance as well as glowing led use 2nd code
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.
ReplyDeleteBoth will work fine
DeleteUse any of them
👏👏👏
ReplyDeleteI tried the same thing .....
ReplyDeleteDistance is shown correctly ....
But leds are not glowing
same
Deletedid u find the solution? i stucked with same issue
DeleteHi, 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.
DeleteMany 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 🍻
ReplyDeleteGreat project
ReplyDeleteGreat and informative Content thanks for sharing with us. Keep it up.Setup Echo Input
ReplyDeleteHi team i keep getting this error all the time i run this code all the time any leads?
ReplyDeleteArduino: 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.
Hi really loved the video
ReplyDeletei 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
Hello
ReplyDeleteI have a problem in first code.Im not able to get my distance as its showing 0 in serial monitor..what I should do