Introduction
Ultrasonic distance sensors are widely used in various applications such as robotics, obstacle detection, and distance measurement. These sensors work by emitting ultrasonic waves and measuring the time it takes for the waves to bounce back from an object. In this tutorial, we will guide you through building a simple ultrasonic distance sensor using TinkerCAD and Arduino. This project will help you understand the basics of working with sensors and measuring distances with precision.
Materials Needed
To build this project in TinkerCAD, you will need the following components:
Arduino Uno
Breadboard
Ultrasonic Sensor (HC-SR04)
LED
Resistor (220Ω for LED)
Jumper Wires
Step 1: Setting Up the Components
Arduino and Breadboard: Start by opening TinkerCAD and creating a new circuit project. Drag and drop an Arduino Uno and a breadboard onto the workspace.
Ultrasonic Sensor (HC-SR04): The HC-SR04 sensor is the core component of this project. It has four pins:
VCC: Connect to the 5V pin on the Arduino.
GND: Connect to GND on the Arduino.
Trig: Connect to digital pin 9 on the Arduino.
Echo: Connect to digital pin 10 on the Arduino.
LED: The LED will indicate when an object is detected within a certain distance.
Connect the anode (longer leg) of the LED to digital pin 13 on the Arduino through a 220Ω resistor.
Connect the cathode (shorter leg) to GND on the breadboard.
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 10 on Arduino
LED:
Anode (long leg) → Digital Pin 13 on Arduino (through 220Ω resistor)
Cathode (short leg) → GND on breadboard
Step 3: Writing the Code
With the circuit set up, let’s write the Arduino code to control the ultrasonic distance sensor and the LED.
const int trigPin = 9;
const int echoPin = 10;
const int ledPin = 13;
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
Serial.begin(9600); // Initialize serial communication
}
void loop() {
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, return the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in cm
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 10 cm, turn on the LED
if (distance < 10) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(500); // Delay for half a second before the next reading
}
Step 4: Simulating the Circuit
After entering the code, click the "Start Simulation" button in TinkerCAD.
The ultrasonic sensor will emit sound waves and measure the time it takes for them to bounce back from an object. The Arduino will calculate the distance based on this time.
When an object is detected within 10 cm of the sensor, the LED will turn on. If the object moves further away, the LED will turn off.
You can monitor the distance readings in the Serial Monitor to see how the sensor responds to different object positions.
Step 5: Understanding the Code
Trig Pin: The trig pin is used to send out a short ultrasonic pulse. This pulse is set HIGH for 10 microseconds and then set LOW.
Echo Pin: The echo pin listens for the pulse to bounce back. The time it takes for this to happen is recorded as duration.
Distance Calculation: The distance is calculated using the formula: distance = duration * 0.034 / 2, where 0.034 cm/µs is the speed of sound in air.
LED Indicator: If the measured distance is less than 10 cm, the LED turns on as an indicator that an object is close to the sensor.
Step 6: Enhancing the System
This basic distance sensor can be expanded with additional features:
Buzzer: Add a buzzer to alert you when an object is too close.
LCD Display: Integrate an LCD screen to display the measured distance in real-time.
Motor Control: Use the distance sensor to control a motor, creating a simple obstacle-avoidance system for a robot.
Conclusion
You have successfully created an Ultrasonic Distance Sensor using TinkerCAD and Arduino. This project demonstrates the principles of ultrasonic distance measurement and how to use sensors with Arduino for practical applications.
By following this guide, you’ve gained valuable experience in working with sensors and Arduino programming. This system can be expanded and modified for various applications, making it a versatile project for learning and experimentation.
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