+21622886281
Arduino HC-SR04

Using the HC-SR04 ultrasonic sensor with Arduino

Purpose of this tutorial:

In this tutorial, we will learn together how to use an HC-SR04 reference ultrasonic distance sensor with an Arduino board.we will test the accuracy of the measurements in various situations and display the results from the HC-SR04 Ultrasonic Sensor on an LCD i2c Display.

HC-SR04 sensor:

An ultrasonic distance sensor uses the same principle as a laser sensor, but using sound waves (inaudible) instead of a beam of light. They are much cheaper than a laser sensor, but also much less precise. However, unlike infrared sensors, ambient light and the opacity of the surface in front of the sensor do not affect the measurement.

The HC-SR04 sensor is a low cost ultrasonic sensor. This sensor operates with a supply voltage of 5 volts, has a measurement angle of approximately 15° and allows distance measurements between 2 centimeters and 4 meters with an accuracy of 3mm (in theory, in practice That is not exactly correct).

The operating principle of the sensor is entirely based on the speed of sound.

Here is how a measurement takes place:

  1. We send a HIGH pulse of 10µs on the TRIGGER pin of the sensor.

  2. The sensor then sends a series of 8 ultrasonic pulses at 40KHz
  3. Ultrasound travels through the air until it hits an obstacle and returns in the other direction to the sensor.

  4. The sensor detects the echo and closes the measurement run.

The signal on the ECHO pin of the sensor stays HIGH during steps 3 and 4, which allows the duration of the round trip of the ultrasound to be measured and therefore the distance to be determined.

LCD i2c Display: 

An LCD i2c  screen (Liquid Crystal Display) or liquid crystal display in French is a device that displays characters while consuming relatively little electricity, which is why it is found in many electronic projects and on all types of devices.

Necessary components

  • Arduino UNO
  • HC-SR04 ultrasonic sensor
  • LCD display
  • connecting wires
  • Test plate

Mounting

For the HC-SR04 sensor we connect:

  • The 3.3V power supply of the Arduino goes to the VCC pin of the sensor.
  • The GND pin of the Arduino goes to the GND pin of the sensor.
  • The digital Pin1 of the Arduino goes to the TRIGGER pin of the sensor.
  • The digital Pin2 of the Arduino goes to the ECHO pin of the sensor.

For the LCD display we connect:

  • the SDA pin at the Arduino analog pin A4.

  • the SCL pin at the Arduino analog pin A5.

  • the VCC pin at the Arduino 5v pin.

  • the GND pin at the Arduino GND pin

Arduino program

Here is the program that displays the distance in cm that separates the ultrasonic sensor from an obstacle.

Note: you must import the library HCSR04.h for ultrasonic sensor and library LiquidCrystal_I2C.h for the LCD display.

How the code works: 

First, the trigger pin and the echo pin are defined. I call them trigPin and EchoPin. The trigger pin is connected to digital pin 2 and the echo pin to digital pin 3 on the Arduino.

Then you initialize the HC-SR04 sensor with the pins.

In the setup() you initialize 1602 I2C LCD screen.

In the loop() you trigger the sensor and the measured distance is displayed on a 1602 I2C LCD screen.




0 commentaire


Leave a comment

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Scroll to Top