Ultrasonic sensor is great for all kind of projects that need distance measurements, avoiding obstacles as examples.
The HC-SR04 is inexpensive and easy to use since we will be using a Libraryspecifically designed for these sensor.
(1) x Elegoo Mega 2560 R3
(1) x Ultrasonic sensormodule
(4) x F-M wires (Female to Male DuPontwires)
Ultrasonic sensor module HC-SR04 provides 2cm-400cm non-contact measurement function, the ranging accuracy can reach to 3mm. The modules includes ultrasonic transmitters, receiver and control circuit. The basic principle of work:
Test distance =(high level time × velocity of sound (340m/s) /2
The Timing diagram is shown below. You only need to supply a short 10us pulse to the trigger input to start the ranging, and then the module will send out an 8 cycle burst of ultrasound at 40 kHz and raise its echo.
The Echo pulse width is proportional to the distance of the object, or range.
You can calculate 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 * velocity (340M/S) / 2; we suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.
You can click the blue text link to download the program file to your local device, and double-click the file to open it after the download is complete. Please note: Before opening the file, ensure that you have installed the Arduino IDE development environment and completed the installation of relevant components such as the board support package and driver corresponding to the board. If you have any questions about this operation process, you can refer to the "part 1" chapter of the document for detailed guidance.
Before you can run this, make sure that you have installed the < SR04> library or re-install it, if necessary. Otherwise, your code won't work.
#include "SR04.h"
#define TRIG_PIN 11
#define ECHO_PIN 12SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;
Library and Hardware Configuration:
#include "SR04.h": Includes the SR04 library for HC-SR04 ultrasonic sensor control#define TRIG_PIN 11: Defines the trigger pin (GPIO 11) for the ultrasonic sensor#define ECHO_PIN 12: Defines the echo pin (GPIO 12) for the ultrasonic sensorSR04 sr04 = SR04(ECHO_PIN,TRIG_PIN): Creates an SR04 object with the specified echo and trigger pinslong a: Declares a long integer variable to store the measured distancevoid setup() {
Serial.begin(9600);//Initialization of Serial Port
delay(1000);
}
setup() Function:Initializes the system and serial communication.
Serial.begin(9600): Initializes serial communication at 9600 baud ratedelay(1000): 1-second delay to allow the system to stabilize after startupSerial Communication:
void loop() {
a=sr04.Distance();
Serial.print(a);
Serial.println("cm");//The difference between "Serial.print" and "Serial.println"
//is that "Serial.println" can change lines.
delay(1000);
}
loop() Function Overview:The main loop that continuously measures distance and displays results.
1. Distance Measurement Module:
a=sr04.Distance();
sr04.Distance(): Calls the library function to measure distancea2. Serial Output Module:
Serial.print(a);
Serial.println("cm");//The difference between "Serial.print" and "Serial.println"
//is that "Serial.println" can change lines.
delay(1000);
Serial.print(a): Prints the measured distance valueSerial.println("cm"): Prints "cm" and moves to a new linedelay(1000): 1-second delay between measurementsUltrasonic Measurement Principle:
Distance Calculation:
Serial Monitor Output:
25cm, 12cm, 45cmSerial.println()Temperature Compensation:
Troubleshooting: