Arduino Ultrasonic Distance Measurement Project Tutorial

Hardware List1. Principle of Ultrasonic Distance Measurement2. Using HCSR04 Ultrasonic Sensor3. Using LCD1602 Liquid Crystal Display1. I2C Communication Protocol on Arduino2. Displaying “Hello World!” on LCD16024. Completing Ultrasonic Distance Measurement5. Conclusion

Hardware List

Arduino Ultrasonic Distance Measurement Project Tutorial

1. Principle of Ultrasonic Distance Measurement

Arduino Ultrasonic Distance Measurement Project Tutorial The ultrasonic sensor we use is the HCSR04. It has two “eyes” that function the same way, both capable of emitting and receiving ultrasonic waves, working according to the schematic above. First, the sensor emits ultrasonic waves and starts timing. When the ultrasonic waves hit an obstacle, they are reflected. When the sensor receives the reflected wave, the timing ends, and then the distance is calculated using the formula of distance, speed, and time. The specific working principle is simple to understand and does not affect the subsequent use of the ultrasonic sensor.Arduino Ultrasonic Distance Measurement Project Tutorial Therefore, the roles of the four pins of the HCSR04 ultrasonic sensor are quite clear. Besides connecting VCC to the positive terminal and GND to the negative terminal, there are two pins: “Trig” and “Echo”. The “Trig” pin is used to control the emission of ultrasonic waves, while the “Echo” pin is used to receive the reflected waves. Thus, the pin connected to “Trig” should be set as output, and the pin connected to “Echo” should be set as input. Therefore, in the subsequent wiring, the two pins can be connected to two digital ports, and during port initialization, they can be set as input and output respectively.

2. Using HCSR04 Ultrasonic Sensor

Next, connect the HCSR04 ultrasonic sensor to the Arduino. In addition to connecting VCC and GND to the positive and negative terminals respectively, connect “Trig” to digital port D5 and “Echo” to digital port D4, as shown in the figure below:Arduino Ultrasonic Distance Measurement Project Tutorial

Also, ensure that the installed library files are consistent. Different developers contribute different library files, and the names of the calling methods may differ. Therefore, if the library files are different from those installed in this article, it is likely to cause errors. It is recommended to install the library files consistent with the figure below.Arduino Ultrasonic Distance Measurement Project Tutorial

After connecting the ultrasonic sensor to the Arduino board, continue with the following program:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// This program outputs the distance measured by the SR04 ultrasonic sensor via serial port

#include<HCSR04.h> // Import the SR04 ultrasonic library

HCSR04 ultrasonic(5,4); // Declare an object named ultrasonic (custom name)

// Pin 4 is "Echo", pin 5 is "Trig"

void setup()

{<!-- -->

pinMode(4, INPUT); // Set the pin connected to "Echo" as input

pinMode(5, OUTPUT); // Set the pin connected to "Trig" as output

Serial.begin(9600); // Initialize the serial monitor

}

int distance; // Define a variable named "distance" to store the measured distance value

void loop()

{<!-- -->

distance = ultrasonic.dist(); // Call the "dist()" method in the ultrasonic library to obtain the measured distance value

Serial.print(distance); // Output the distance value without line break

Serial.println(" cm"); // Immediately output the unit and line break

}

Before uploading the program, let’s explain some parts of the code: 1. “HCSR04 ultrasonic(4, 5)” is called instantiation of an object. HCSR04 is the name of the ultrasonic library, followed by a custom name “ultrasonic” (which can be any name), and different instantiation processes require different parameters to be input. For example, after HCSR04, you need to add the port numbers for the pins connected to “Trig” and “Echo”. Thus, in the subsequent program, if you want to call the methods in the HCSR04 library, you only need to use “ultrasonic.methodName()”. 2. “distance = ultrasonic.dist()” calls the method “dist()” in the ultrasonic sensor to get the distance, which is different from the int defined distance. The int defined variable can be named anything, just a variable name, but “Distance()” is a fixed method of the ultrasonic sensor. Next, upload the program to the Arduino board and open the serial monitor to check if the measured values are accurate.Arduino Ultrasonic Distance Measurement Project Tutorial

Additionally, the official measurement range of the HCSR04 ultrasonic sensor is claimed to be 2cm ~ 450cm, so values outside this range may be inaccurate.

3. Using LCD1602 Liquid Crystal Display

Arduino Ultrasonic Distance Measurement Project Tutorial

Arduino Ultrasonic Distance Measurement Project Tutorial

First, observe the front and back of the LCD1602 display (the back is a physical image). The current drive board of the LCD display (the black circuit board) has a total of 6 pins. The two adjacent pins on the left side can be directly connected to turn on the backlight. The right side pins “GND connects to negative terminal” and “VCC connects to positive terminal” are quite familiar. For the “SDA” and “SCL” pins, we will provide a detailed introduction.

1. I2C Communication Protocol on Arduino

Arduino Ultrasonic Distance Measurement Project Tutorial The I2C bus is a simple, bidirectional, two-wire synchronous serial bus developed by Philips. It only requires two wires to transmit information between devices connected to the bus. Each different hardware has a different I2C address, so we can understand the working principle of I2C communication as follows: SCL (clock line) is used to determine whether to transmit data at the current moment, just like a traffic light to prevent data transmission confusion; SDA (data line) is used to transmit data to hardware with different addresses within a given time, and it supports bidirectional transmission. Currently, we only need to understand the I2C address of different hardware to easily use I2C communication.

2. Displaying “Hello World!” on LCD1602

Arduino Ultrasonic Distance Measurement Project Tutorial There are dedicated I2C pins next to the reset button on the Arduino expansion board, which can be directly connected. Of course, you can also connect in the order “SDA – A4” and “SCL – A5”. After connecting, observe the screen. Before writing the program, the first row of the LCD display should show 16 small squares. If not, use a cross screwdriver to turn the blue knob on the back of the display until the small squares are clearly visible. This is why the LCD1602 display is marked “1602”—this display can show 16 columns and 2 rows of characters (in English). The LCD display used in this chapter uses I2C communication, so the library files used must correspond, as shown in the figure below:Arduino Ultrasonic Distance Measurement Project Tutorial

Next, complete the following program:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// This program outputs "Hello World!" using LCD1602

#include <Wire.h> // Import I2C communication library

#include <LiquidCrystal_I2C.h> // Import LCD display library

LiquidCrystal_I2C lcd(0x27,16,2); // Instantiate an object named "lcd" with three parameters

// I2C communication address, number of columns, number of rows

void setup()

{<!-- -->

lcd.init(); // Initialize the display

lcd.backlight(); // Turn on the display backlight

lcd.setCursor(0,0); // Set cursor position (column, row)

lcd.print("Hello, world!"); // Display characters

lcd.setCursor(0,1); // Start displaying in the first column, second row

lcd.print("Successful!"); // Display characters

}

void loop()

{<!-- -->

}

Upload the above program and check if two lines of characters are displayed: “Hello, world!” and “Successful!”. Next, let’s explain some parts of the code: 1. “#include <Wire.h>” imports the I2C communication library. 2. “#include <LiquidCrystal_I2C.h>” imports the library for the LCD display used in this program based on the I2C communication protocol. 3. “LiquidCrystal_I2C lcd(0x27,16,2)” instantiates an object named “lcd” and specifies the I2C address of the LCD as “0x27”, as well as the number of columns and rows displayed on the screen. Other code functions have been explained in the program comments, so they will not be repeated.

4. Completing Ultrasonic Distance Measurement

Arduino Ultrasonic Distance Measurement Project Tutorial

Connect the “SR04 ultrasonic sensor” and “LCD1602 display” to the Arduino board as shown in the figure above; connect “Trig” to digital port D5 and “Echo” to digital port D4, and the LCD display pins are connected to the IIC area; then complete the following program:

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

// This program displays the distance measured by the ultrasonic sensor on LCD

// It displays normally within 2cm-450cm, and outputs an error message if out of range

#include<HCSR04.h> // Import the SR04 ultrasonic library

#include<Wire.h> // Import the I2C communication library

#include<LiquidCrystal_I2C.h> // Import the LCD display library

LiquidCrystal_I2C lcd(0x27,16,2); // Declare an object using the LCD library

HCSR04 ultrasonic(5,4); // Declare an object named ultrasonic (custom name)

// Pin 4 is "Echo", pin 5 is "Trig"

void setup()

{<!-- -->

lcd.init(); // Initialize the display

lcd.backlight(); // Turn on the LCD display backlight

pinMode(4,INPUT); // Echo pin

pinMode(5,OUTPUT); // Trig pin

}

int distance; // Define a variable to store the values measured by the ultrasonic sensor

void loop()

{<!-- -->

distance = ultrasonic.dist(); // Assign the value measured by the ultrasonic sensor to the variable

if(distance >= 2 && distance <= 450) // Check if the measured value is within the measurement range

{<!-- -->

lcd.setCursor(0,0); // Set the LCD cursor position

lcd.print(distance); // Display the distance value on the LCD

lcd.print(" cm"); // Display the unit "cm"

delay(200); // Delay 200 milliseconds

lcd.clear(); // Clear the LCD display for the next value

}

else // If out of range

{<!-- -->

lcd.print("Wrong!"); // Output "Wrong!" message

delay(200);

lcd.clear();

}

}

The above program is a comprehensive application of the ultrasonic sensor and LCD display, and it uses the “if statement”. The functions of the code blocks have been marked in the code area, so they will not be explained in detail. Upload the above code and test whether the ultrasonic distance measurement function can be implemented.

5. Conclusion

This chapter used the SR04 ultrasonic sensor combined with the LCD liquid crystal display to achieve the function of ultrasonic distance measurement. Additionally, both the ultrasonic sensor and the display need to call the corresponding library files for control. It is important to use the correct library files, as the library files for various hardware come from different developers, and the names of methods in different libraries may differ. Of course, theoretically, different library files can control the hardware, so it is best to use the same library files as those in this article. After completing the ultrasonic distance measurement function in this chapter, you may want to think about the reverse radar principle used in cars, which can also be implemented on the Arduino platform. More hardware usage methods and combinations into complete functional tutorials will be presented in the future.

Leave a Comment

×