Introduction
Obstacle detection systems are essential in robotics, autonomous vehicles, and various safety applications. These systems help devices recognize and avoid obstacles in their path, ensuring smooth and safe operation. In this tutorial, we’ll guide you through creating a simple obstacle detection system using an ultrasonic sensor and Arduino in TinkerCAD. This project is an excellent introduction to sensor integration and basic automation.
Materials Needed
To build this project in TinkerCAD, you will need the following components:
Arduino Uno
Breadboard
Ultrasonic Sensor (HC-SR04)
Buzzer
LED
Resistor (220Ω for LED)
Jumper Wires
Step 1: Setting Up the Components
Arduino and Breadboard: Open TinkerCAD and create a new circuit project. Start by dragging and dropping an Arduino Uno and a breadboard onto the workspace.
Ultrasonic Sensor (HC-SR04): The HC-SR04 sensor is used to measure the distance between the sensor and an obstacle. It has four pins:
VCC: Power supply (5V)
GND: Ground
Trig: Trigger pin (sends out ultrasonic pulses)
Echo: Echo pin (receives the reflected pulses)
Connect the sensor as follows:
VCC to the 5V pin on the Arduino
GND to GND on the Arduino
Trig to digital pin 9 on the Arduino
Echo to digital pin 8 on the Arduino
LED and Buzzer: These components will indicate when an obstacle is detected.
Connect the longer leg (anode) of the LED to digital pin 12 on the Arduino through a 220Ω resistor. Connect the shorter leg (cathode) to GND.
Connect the positive leg (longer leg) of the buzzer to digital pin 10 on the Arduino and the negative leg to GND.
Step 2: Wiring Diagram
Ensure your wiring looks like this:
Ultrasonic Sensor (HC-SR04):
VCC → 5V on Arduino
GND → GND on Arduino
Trig → Digital Pin 9 on Arduino
Echo → Digital Pin 8 on Arduino
LED:
Anode (long leg) → Digital Pin 12 on Arduino (through a 220Ω resistor)
Cathode (short leg) → GND on Arduino
Buzzer:
Positive Leg → Digital Pin 10 on Arduino
Negative Leg → GND on Arduino
Step 3: Writing the Code
Now that the circuit is set up, let’s write the Arduino code to control the obstacle detection system.
const int trigPin = 9;
const int echoPin = 8;
const int ledPin = 12;
const int buzzerPin = 10;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Trig pin as output
pinMode(echoPin, INPUT); // Echo pin as input
pinMode(ledPin, OUTPUT); // LED pin as output
pinMode(buzzerPin, OUTPUT);// Buzzer pin as output
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
// Send a 10us pulse to trigger the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the time it takes for the echo to return
duration = pulseIn(echoPin, HIGH);
// Calculate the distance (duration/2 because the pulse travels to the obstacle and back)
distance = duration * 0.034 / 2;
// Print the distance to the serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// If the distance is less than 15 cm, activate LED and buzzer
if (distance < 15) {
digitalWrite(ledPin, HIGH); // Turn on the LED
digitalWrite(buzzerPin, HIGH); // Activate the buzzer
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
digitalWrite(buzzerPin, LOW); // Deactivate the buzzer
}
delay(500); // Delay for stability
}
Step 4: Simulating the Circuit
After entering the code, click the "Start Simulation" button in TinkerCAD.
The ultrasonic sensor will detect the distance to an object in front of it. When an obstacle is closer than 15 cm, the LED will light up, and the buzzer will sound, indicating the presence of an obstacle.
You can monitor the distance readings in the Serial Monitor to see how the sensor responds to different distances.
Step 5: Understanding the Code
The ultrasonic sensor sends out a pulse via the Trig pin and waits for it to return to the Echo pin after reflecting off an obstacle. The time taken for this round trip is used to calculate the distance.
The distance is calculated using the formula: distance = duration * 0.034 / 2, where 0.034 cm/µs is the speed of sound in air.
If the detected distance is less than 15 cm, the system considers it an obstacle and activates the LED and buzzer as a warning.
Conclusion
You have successfully created a simple obstacle detection system using TinkerCAD and Arduino. This project demonstrates how ultrasonic sensors can be used in practical applications to detect obstacles and trigger alerts. This is a foundational concept in robotics and automation systems.
You can further expand this project by integrating it with motors to create an autonomous robot that avoids obstacles or by adding more sensors to enhance the detection range and accuracy. The possibilities are endless!
Want us to guide you through your project or make the project for you ?
Create Various Projects
Check out our Free Arduino Projects Playlist - Arduino Projects
Check out our Free Raspberry Pi Projects Playlist - Raspberry Pi Projects
Check out our Free TinkerCAD Projects Playlist - TinkerCAD Projects
Check out our Free IoT Projects Playlist - IoT Projects
Check out our Free Home Automation Projects Playlist - Home Automation Projects
Check out our Free NodeMCu Projects Playlist - NodeMCu Projects
Comments