Arduino is a versatile microcontroller platform that empowers beginners and experienced makers alike to bring their ideas to life through electronics projects. A classic starting point for newcomers is the LED blinking project, where users learn to control an LED's on and off states using Arduino code. Once comfortable with this simple project, they can quickly shift focus to more exciting ventures, like interfacing an ultrasonic sensor such as the HC-SR04.
The HC-SR04 is an ultrasonic sensor that can be used with an Arduino to measure distances. It emits ultrasonic waves and measures the time it takes for the waves to bounce back after hitting an object.
By integrating the sensor with the Arduino, users can create advanced applications like distance measurement, obstacle detection, or even interactive robots that respond to their environment. This seamless transition from LED blinking to ultrasonic sensors showcases Arduino's adaptability and encourages enthusiasts to explore the vast possibilities of electronics and programming.
To implement the circuit for the given Arduino code, you’ll need the following components:
Arduino board (e.g., Arduino Uno)
HC-SR04 Ultrasonic Sensor
Breadboard and jumper wires
Connect the VCC pin of the HC-SR04 to the 5V pin of the Arduino board.
Connect the GND pin of the HC-SR04 to the GND pin of the Arduino board.
Connect the TRIG pin of the HC-SR04 to a digital pin (e.g., pin 7) on the Arduino.
Connect the ECHO pin of the HC-SR04 to another digital pin (e.g., pin 6) on the Arduino.
Here’s a basic example code to measure distances using the HC-SR04 ultrasonic sensor and Arduino:
const int trigPin = 7;const int echoPin = 6;void setup() {pinMode(trigPin, OUTPUT);pinMode(echoPin, INPUT);Serial.begin(9600);}void loop() {long duration, distance;// Clear the trigger pindigitalWrite(trigPin, LOW);delayMicroseconds(2);// Send a 10us pulse to trigger the sensordigitalWrite(trigPin, HIGH);delayMicroseconds(10);digitalWrite(trigPin, LOW);// Measure the duration of the pulse from the ECHO pinduration = pulseIn(echoPin, HIGH);// Calculate the distance based on the speed of sound (343 meters per second)// and the formula: distance = (speed of sound * time) / 2distance = (duration * 343) / 10000;// Print the distance to the serial monitorSerial.print("Distance: ");Serial.print(distance);Serial.println(" cm");// Delay before the next measurementdelay(1000);}
The code begins by defining the trigPin
(the pin connected to the TRIG pin of the HC-SR04) and the echoPin
(connected to the ECHO pin of the HC-SR04). In the setup function, we set these pins as OUTPUT
and INPUT
, respectively, and initialize the Serial communication for displaying the distance.
In the loop function, we perform the following steps:
Set the trigPin
to LOW
to clear any previous trigger pulse.
Send a 10µs trigger pulse to the HC-SR04 by setting the trigPin
to HIGH
and then LOW
after a short delay.
Use the pulseIn
function to measure the pulse duration received on the echoPin
.
Calculate the distance based on the speed of sound (343 meters per second) and the time of flight of the ultrasonic pulse. The formula is distance = (speed of sound * time) / 2. The division by 10000 is to convert the time to centimeters.
Print the calculated distance to the serial monitor.
Add a delay of 1 second before the following distance measurement.
Connect your Arduino board to the computer using the USB cable.
Open the Arduino IDE and copy the code into a new sketch.
Select the correct board and port under the "Tools" menu.
Click on the checkmark icon (✓) to verify the error code. Fix any errors that appear.
Click on the right-arrow icon (➔) to upload the code to your Arduino board.
When you upload this code to your Arduino and open the serial monitor at a baud rate of 9600, you should see the distance measurements in centimeters displayed in the serial monitor. The sensor will continuously measure and display the distance to any object within its range.
To learn about interfacing an LCD with Arduino, click here.