Tutorial 13 – Using an Ultrasonic Sensor with Arduino
Welcome back to the CraftedTech Engineering Arduino tutorial series! In this tutorial, we’ll teach you how to use an ultrasonic sensor to measure distances with Arduino. This sensor can be used in various projects like obstacle avoidance for robots, distance measuring, and more.
🔌 What You’ll Need
- 1 Arduino Board (e.g., Arduino UNO)
- 1 Ultrasonic Sensor (e.g., HC-SR04)
- Breadboard
- Jumper wires
- USB cable for Arduino
You can order the required components here:
🧠 What Is an Ultrasonic Sensor?
An ultrasonic sensor uses sound waves to measure the distance between the sensor and an object. It sends out a pulse, and when the pulse hits an object, it bounces back to the sensor. The sensor then calculates the time it took for the pulse to return and converts that into a distance reading.
🧾 Step-by-Step Instructions
🔧 Step 1: Wiring the Ultrasonic Sensor
- Connect the VCC pin of the ultrasonic sensor to 5V on the Arduino.
- Connect the GND pin of the ultrasonic sensor to GND on the Arduino.
- Connect the Trig pin to digital pin 9 on the Arduino.
- Connect the Echo pin to digital pin 10 on the Arduino.
💻 Step 2: Arduino Code to Measure Distance
const int trigPin = 9; // Pin connected to Trig
const int echoPin = 10; // Pin connected to Echo
long duration;
int distance;
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(trigPin, OUTPUT); // Set the Trig pin as an output
pinMode(echoPin, INPUT); // Set the Echo pin as an input
}
void loop() {
// Send a 10 microsecond pulse to the Trig pin to initiate measurement
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the time it took for the Echo pin to receive the pulse
duration = pulseIn(echoPin, HIGH);
// Calculate distance in centimeters (time / 2 * speed of sound)
distance = duration * 0.0344 / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500); // Wait for half a second before the next reading
}
🔍 Code Breakdown
- pinMode(): Configures the pins as input or output.
- digitalWrite(): Sends HIGH or LOW signals to pins.
- pulseIn(): Reads the duration of the pulse on the Echo pin.
- distance calculation: Converts the pulse duration into a distance in centimeters.
🧪 Try This!
- Use the distance to control a motor or turn on/off an LED when an object is detected.
- Experiment with placing objects at different distances and observe the readings in the Serial Monitor.
🛠️ Troubleshooting Tips
- Ensure the Echo pin is connected to a digital pin that can read the pulse.
- Double-check the wiring for the VCC and GND connections.
- If the sensor gives unexpected readings, try adjusting the delay time or check for interference in the environment.
🧭 What You Learned in This Tutorial
✔️ How to wire and use an ultrasonic sensor with Arduino
✔️ How to measure distances using sound waves
✔️ How to convert time into distance readings
✔️ How to display distance readings on the Serial Monitor
📌 Next Up: Tutorial 14 – Using a Motor Driver with Arduino
In the next tutorial, we’ll dive into controlling motors with an H-Bridge motor driver. This will allow you to run motors in both directions for projects like robotic vehicles or automated systems.
📣 Don’t forget to follow CraftedTech Engineering for more hands-on tutorials and project ideas!
🔗 Website: craftedtechengineering.com
📘 Facebook: @CraftedTechEngineering
📺 YouTube: @CraftedTechEngineering