arduino range sensor with LEDs and buzzer I Mr.Electrouino

Mr.ElectroUino
1
Arduino ultrasonic sensor with LEDs and buzzer.
 Tutorial  @Mr.ElectroUino

In this blog, we will learn about how to make an Arduino distance sensor with the help of an ultrasonic sensor ( Hc-sr04 ), LEDs, and buzzers. The ultrasonic sensor is used to measure the distance of objects like scale and the LEDs are used to indicate the distance in cm.

When the first led is on that means the distance of an object is 7cm and the max distance detected of this project is 49cm that means the last LEDs will turn on.

Ultrasonic sensors with LEDs and buzzers is sound like a scale to measure the distance of an object. We can easily extend the range of the Hc-sr04 sensor by changing its code.

How its work?

The ultrasonic sensor continues to the transmitter the signal by trigpin and receives the signal by echopin. Hc-sr04 transmit the frequency of 40khz sound and when an object detects then reflect the signal to echopin. The time between the transmission and reception of the signal allows us to calculate the distance to an object.

 ⚙Features:  

  • Voltage (v): +5V DC.
  • Current: 20mA.
  • Distance measure:- 2cm to 400cm📏.
  • Measuring Angle: 30 degree.
  • Sensor Frequency: 40khz.

Usages:
  • Arduino abstacle avoiding robot
  • Distance measure
  • Arduino range sensor
  • Measure water level and many more.

Component requirements:
  1. Arduino Uno 
  2. Ultrasonic sensor Hc-sr04
  3. Buzzer
  4. 7 x LEDs
  5. 7 x 220ohm resistor
  6. Breadboard 
  7. Jumper wire
Pin connection:

Circuit Diagram! Click here.....

Ultrasonic sensor ( Hc-sr04 ) pins  to arduino Uno pins.
Ultrasonic sensor ( Hc-sr04 ) Arduino Uno
VCC 5V
Trig Pin 12
ECHO Pin 13
GND GND

Buzzer pins  to arduino Uno pins.
Buzzer pin Arduino Uno
Positive Terminal ( + ) Pin 9
Negative Terminal ( - ) GND

LEDs pins to arduino uno pins.
Leds Pin Arduino Uno
LED1 ( + ) Pin 8
LED2 ( + ) Pin 7
LED3 ( + ) Pin 6
LED4 ( + ) Pin 5
LED5 ( + ) Pin 4
LED6 ( + ) Pin 3
LED7 ( + ) Pin 2
Negative Terminal ( - ) GND

Warning! Don't forget to connect 220ohm resistor to LEDs



 CODE: 
               /* Code:
  /* Code:
           * by Sayed Mohsin
           * mrelectrouino.blogspot.com
           */
 
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 buzzer =9;
int duration = 0;
int distance = 0;
void setup() 
{
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  pinMode(buzzer, OUTPUT);
  
  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) / 29.1 ;
  Serial.println(distance);
  
  if ( distance <= 7 )          
  {
    digitalWrite(LED1, HIGH);
	tone(buzzer, 400);
  }
  else
  {
    digitalWrite(LED1, LOW);
	noTone(buzzer);
  }
  if ( distance <= 14 )
  {
    digitalWrite(LED2, HIGH);
	tone(buzzer, 400);
  }
  else
  {
    digitalWrite(LED2, LOW);
	noTone(buzzer);
  }
  if ( distance <= 21 )
  {
    digitalWrite(LED3, HIGH);
	tone(buzzer, 400);
  }
  else
  {
    digitalWrite(LED3, LOW);
	noTone(buzzer);
  }
  if ( distance <= 28 )
  {
    digitalWrite(LED4, HIGH);
	tone(buzzer, 400);
  }
  else
  {
    digitalWrite(LED4, LOW);
	noTone(buzzer);
  }
  if ( distance <= 35 )
  {
    digitalWrite(LED5, HIGH);
	tone(buzzer, 400);
	noTone(buzzer);
  }
  else
  {
    digitalWrite(LED5, LOW);
  }
  if ( distance <= 42 )
  {
    digitalWrite(LED6, HIGH);
	tone(buzzer, 400);
  }
  else
  {
    digitalWrite(LED6, LOW);
	noTone(buzzer);
  }
if ( distance <= 49 )
{
    digitalWrite(LED7, HIGH);
        tone(buzzer, 400);
}
else
{
    digitalWrite(LED7,LOW);
     noTone(buzzer);
}
}

 

 
 Code Explanation: 

const int trig = 12;
const int echo = 13;
Start with defining the pin of the ultrasonic sensor to the Arduino Uno board. TrigPin is connected to Arduino Pin 12, and EchoPin is connected to Arduino Pin 13. The Trig and Echo pin are used to transmit and receive the ultrasonic sound wave.

const int LED1 = 8;
            .
            .
const int LED7 = 2;
Next, define the LEDs to Arduino Uno so, LED( 1, 2, 3, 4, 5, 6, 7 ) is connected to Arduino Pin ( 8, 7, 6, 5, 4, 3, 2 ) respectively. LEDs are used to indicate the distance of the object.

int buzzer =9;
Next, define the buzzer pin is connected to Arduino Pin 9. The Buzzer is used to play the sound.

int duration = 0;
int distance = 0;

Now create two variables of types integer: duration and distance. The duration variable is used to hold the time between the transmission and reception of ultrasonic waves. The distance variable is used to store a distance in cm.


In void setup() , we have to set the trigPin as an output and the echoPin as an Input and also set the buzzer and LEDs as an Output.
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(LED1 , OUTPUT);
                         .
                         .
  pinMode(LED7 , OUTPUT);
Serial.begin() is used to initialize the serial communication for showing the results on the serial monitor.


In void loop(), We have to trigger the sensor by setting the trigPin HIGH for 1000 µs. For clearing the signal, We have to trigger the sensor by setting it LOW.
  digitalWrite(trig , HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig , LOW);
 
  duration = pulseIn(echo , HIGH);
For calculation the duration with the help of pulseln() function. Pulseln() function is used to read a pulse on a pin (either HIGH or LOW). when the value is HIGH, Send the signal from LOW to HIGH then start the time, and wait for the signal is reflect an object and received by the echo pin and stops timing. Returns the length of the pulse in microseconds that is duration.

More Info! Pulseln()

After that, you can calculate the distance by using the formula.
distance = (duration/2) / 29.1 ;

You divide by two because it goes out and back so the time would be double that of a one-way travel. The speed of sound is 343 m/s which is converted into 0.0343cm/uS is equal to 1/29.1 cm/uS.
Serial.println(distance);
Print the distance in serial moniter.

  if ( distance <= 7 )          
    digitalWrite(LED1, HIGH);
tone(buzzer, 400);
  else
  digitalWrite(LED1, LOW);
noTone(buzzer);
When the distance is equal to a given distance then turn on the LED1 and play a tone in 400hz.
When the distance is not equal to a given distance then turn off the LED1 and stop a tone.
Notes that the result in cm.

 Watch This Tutorial: 

Comment! If you getting any problem or give a suggestion comment it.


Post a Comment

1Comments

Post a Comment