This post shows a simple interfacing of an Arduino UNO board with HC-SR04 ultrasonic sensor in order to build a distance meter with a range up to 4 meters.
The HC-SR04 ultrasonic sensor module can measure distances form 2 cm to 400 cm (4 m) with an accuracy of 3 mm. The HC-SR04 module includes ultrasonic transmitter, ultrasonic receiver and control circuit.
The HC-SR04 ultrasonic sensor module has 4 pins as shown in the image below where:
VCC : Positive power supply (+5V)
Trig : Trigger input pin
Echo : Echo output pin
GND : Ground (0V)
HC-SR04 ultrasonic sensor timing diagram:
The timing diagram of the HC-SR04 ultrasonic sensor is shown below.
First we have to supply the sensor trigger pin with a pulse of 10µs and the sensor will automatically send 8 cycles burst of ultrasound at 40 kHz and raise its echo pin. The Echo is a distance object that is pulse width and the range in proportion. We can calculate the range through the time interval between sending trigger signal and receiving echo signal. Formula: uS / 58 = centimeters or uS / 148 =inch; or:
the range = high level time * sound velocity / 2 . Sound velocity = 340M/S.
Hardware Required:
- Arduino board
- HC-SR04 ultrasonic sensor module
- 16×2 LCD screen
- 10K variable resistor
- 330 ohm resistor
- Breadboard
- Jumper Wires
The circuit:
Example circuit schematic diagram is shown below.
The Arduino code:
First of all the Arduino sends a pulse of 10 us to the sensor via trigger_pin which is the trigger pin of the sensor, then the Arduino waits until the sensor raises its Echo pin. For this I used the function duration = pulseIn(echo_pin, HIGH). The pulsIn() function waits for the echo_pin to go high, starts timing, then waits for the pin to go low and stops timing. Returns the length of the pulse in microseconds. Gives up and returns 0 if no pulse starts within default timeout which is 1 second.
So, if duration is equal to zero the LCD will print Time Out
and if duration is not zero the distance will be calculated using the function distance = duration / 58.
Now if the distance is greater than 400 cm the LCD will print Out of Range. Otherwise the LCD will print the measured distance in cm.
The full code is below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | // Distance meter using Arduino and HC-SR04 ultrasonic sensor // include LCD library code #include <LiquidCrystal.h> #define trigger_pin 15 // HC-SR04 trigger pin is connected to Arduino A1 #define echo_pin 14 // HC-SR04 echo pin is connected to Arduino A0 // LCD module connection (RS, E, D4, D5, D6, D7) LiquidCrystal lcd(2, 3, 4, 5, 6, 7); void setup() { pinMode(trigger_pin, OUTPUT); pinMode(echo_pin, INPUT); digitalWrite(trigger_pin, LOW); // set up the LCD's number of columns and rows lcd.begin(16, 2); lcd.setCursor(3, 0); lcd.print("Distance:"); } unsigned long duration; unsigned int distance; void loop() { // Send 10us pulse to HC-SR04 Trigger pin digitalWrite(trigger_pin, HIGH); delayMicroseconds(10); digitalWrite(trigger_pin, LOW); // Read pulse comes from HC-SR04 Echo pin duration = pulseIn(echo_pin, HIGH); // Read echo pulse width if(duration == 0) { // Previous function returns 0 if timeout lcd.setCursor(2, 1); lcd.print(" Time Out "); } else { distance = duration / 58; // Calculate the distance in cm if(distance > 400) { // HC-SR04 module can measure up to 400cm lcd.setCursor(2, 1); lcd.print("Out of Range"); } else { lcd.setCursor(2, 1); lcd.print(" cm "); lcd.setCursor(5, 1); lcd.print(distance); } } delay(100); // Wait 100 ms between readings } |
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.