How to Use Ultrasonic Sensor with Arduino?
1. Introduction
The HC-SR04 ultrasonic sensor is a very affordable distance sensor, mainly used for obstacle avoidance in various robotic projects. It is also used for water level sensing and even as a parking sensor. We treat the ultrasonic sensors as bat’s eyes. In the dark, bats can still identify objects in front of them and directions through ultrasound.
2.Components Required
|
|
|
|
---|---|---|---|
Keyestudio Plus Mainboard*1 |
Ultrasonic Sensor*1 |
220Ω Resistor*4 |
Red LED*4 |
|
|
|
|
M-F Dupont Wires |
USB Cable*1 |
Breadboard*1 |
Jumper Wires |
3. Component Knowledge
HC-SR04 ultrasonic sensor: Like bats, sonar is used to determine the distance to an object. It provides accurate non-contact range detection, high-precision and stable readings. Its operation is not affected by sunlight or black materials, just like a precision camera (acoustically softer materials like cloth are difficult to detect). It has an ultrasonic transmitter and receiver.
In front of the ultrasonic sensor are two metal cylinders, these are the converters. The converters convert the mechanical energy into an electrical signal. In the ultrasonic sensor, there are transmitting converters and receiving converters. The transmitting converter converts the electric signal into an ultrasonic pulse, and the receiving converter converts the reflected ultrasonic pulse back to an electric signal. If you look at the back of the ultrasonic sensor, you will see an IC behind the transmitting converter, which controls the transmitting converter.
There is also an IC behind the receiving converter, which is a quad operational amplifier that amplifies the signal generated by the receiving converter into a signal large enough to be transmitted to the Arduino.
Sequence diagrams:
The figure shows the sequence diagram of the HC-SR04. To start the measurement, the Trig of SR04 must receive at least 10us high pulse (5V), which will activate the sensor to emit 8 cycles of 40kHz ultrasonic pulses, and wait for the reflected ultrasonic pulses. When the sensor detects ultrasound from the receiver, it sets the Echo pin to high (5V) and delays it by one cycle (width), proportional to the distance. To get the distance, measure the width of the Echo pin.
Time = Echo pulse width, its unit is “us” (microseconds)
Distance in centimeters = time / 58
Distance in inches = time / 148
4.Read the Distance Value
We will start with a simple ultrasonic distance measurement and output the measured distance on the serial monitor.
The HC-SR04 ultrasonic sensor has four pins, they are Vcc, Trig, Echo and GND. The Vcc pin provides the power source for generating ultrasonic pulses and is connected to Vcc (+5V). The GND pin is grounded. The Trig pin is where the Arduino sends a signal to start the ultrasonic pulse. The Echo pin is where the ultrasonic sensor sends information about the duration of the ultrasonic pulse to the Plus control board. Wiring as shown below.
/*
Read_the_ultrasonic_distance
http//www.keyestudio.com
*/
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) / 28.5 ;
Serial.print(distance);
Serial.println("cm");
}
Upload the code to the Plus Mainboard, connect the wires and power on first. Then open the serial monitor, set the baud rate to 9600. When an object is placed in front of the ultrasonic sensor (near or far), it will detect the distance of the object and the value will be displayed on the monitor.
5.Circuit Diagram and Wiring Diagram
Next, we will make a simple ultrasonic ranger using an ultrasonic sensor and 4 LED lights. Connect the wires as shown below.
6.Code
/*
Ultrasonic_Ranger
http//www.keyestudio.com
*/
const int trig = 12;
const int echo = 13;
const int LED1 = 11;
const int LED2 = 10;
const int LED3 = 9;
const int LED4 = 8;
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);
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);
}
}
7. Result
Upload the code to the PLUS Mainboard. After connecting the wires and powering on, the ultrasonic module can detect the distance of obstacles ahead. In addition, when we move our hands in front of the ultrasonic sensor, the corresponding LED will light up.