0% found this document useful (0 votes)
23 views

Ultrasonic Distance Sensor Arduino Tinkercad

The document describes how to use an ultrasonic distance sensor with an Arduino board to measure distances. It explains how to connect the sensor and LEDs to a breadboard along with the Arduino. The document then provides code explanations to read values from the sensor and light different numbers of LEDs depending on the measured distance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Ultrasonic Distance Sensor Arduino Tinkercad

The document describes how to use an ultrasonic distance sensor with an Arduino board to measure distances. It explains how to connect the sensor and LEDs to a breadboard along with the Arduino. The document then provides code explanations to read values from the sensor and light different numbers of LEDs depending on the measured distance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

instructables

Ultrasonic Distance Sensor in Arduino With Tinkercad

by circuits

Ultrasonic Distance Sensor in Arduino With Tinkercad: Page 1


Let's measure distances with an ultrasonic
rangefinder (distance sensor) and Arduino's digital
input. We'll connect up a circuit using a breadboard Find this circuit on Tinkercad
and use some simple Arduino code to control a single
LED. Explore the sample circuit embedded here by starting
the simulation and clicking on the proximity sensor.
You may have already learned to read a pushbutton This will activate a highlighted area in front of the
and PIR motion sensor with Arduino's digital input, sensor with a circle "object" inside. You may need to
and we'll build on those skills in this lesson. resize the view if the circle is off screen.

Ultrasonic rangefinders use sound waves to bounce Click and drag the "object" circle closer and further
off objects in front of them, much like bats using away, noticing the changing distance values on
echolocation to sense their environment. The screen. More LEDs will light up the closer you get to
proximity sensor sends out a signal and measures the sensor.
how long it takes to return. The Arduino program
receives this information and calculates the distance
https://www.tinkercad.com/embed/j0k4YgzXoDF
between the sensor and object.

In this lesson, you'll build this simulated circuit


yourself along side the sample. To optionally build the
physical circuit, gather up your Arduino Uno board,
USB cable, solderless breadboard, three LEDs,
resistors (any value from 100-1K), ultrasonic
rangefinder, and breadboard wires.

You can follow along virtually using Tinkercad


Circuits. You can even view this lesson from within
Tinkercad (free login required)! Explore the sample
circuit and build your own right next to it. Tinkercad
Circuits is a free browser-based program that lets you
build and simulate circuits. It's perfect for learning,
teaching, and prototyping.

Ultrasonic Distance Sensor in Arduino With Tinkercad: Page 2


Step 1: Build the LED Circuit

Just as you’ve learned from the introductory lessons, start by wiring up your Arduino and breadboard with power
and ground next to the example circuit, then add the the three red LEDs to the breadboard, as shown. These will
be the "bar graph" lights for visually indicating the sensor's distance measurement.

Drag an Arduino Uno and breadboard from the components panel to the workplane, next to the existing circuit.

Connect the 5 volt and ground pins on the Arduino to the power (+) and ground (-) rails on the breadboard with
wires. You can change the wire colors if you want to! Either use the inspector dropdown or the number keys on
your keyboard.

Drag three LEDs on the breadboard in row E, spaced 2 breadboard sockets apart. You can change the LED color
using the inspector that pops up when you click on each one.

Use a 220 Ohm resistor to connect each LED's cathode (left leg) to the ground rail (black) of the breadboard. You
can change a resistor's value by highlighting it and using the dropdown menu.

Connect the LED anodes (right legs) to digital pins 4, 3, and 2 on the Arduino. The LED anode (+) is the terminal
that current flows into. This will connect to the digital output pins on the Arduino. The cathode (-) is the terminal
that current flows from. This will connect to the ground rail.

Ultrasonic Distance Sensor in Arduino With Tinkercad: Page 3


Step 2: Add Proximity Sensor

Proximity sensors come in multiple flavors. Here in dropdown menu).


Tinkercad Circuits, you can choose between a three-
pin sensor or a four-pin sensor. In general, ultrasonic Place the sensor on the breadboard to the left of the
rangefinders have one pin that connects to ground, LEDs in row E, as shown in the figure.
another that connects to 5 volts, a third for sending a
signal, and a fourth for receiving a signal. The 'send' Wire up the sensor so the 5V pin connects to the 5V
and 'receive' pins are combined into one pin on the voltage rail, the GND pin connects to the ground rail,
three-pin flavor. the SIG or TRIG pin to Arduino pin 7, and, if using the
four-pin flavor, the ECHO pin connects to Arduino pin
In the circuits editor, find the ultrasonic rangefinder in 6.
the components drawer. To find the four-pin sensor,
view "All" in the components panel (using the

Ultrasonic Distance Sensor in Arduino With Tinkercad: Page 4


Step 3: Code With Blocks

Let's use the code blocks editor to listen to the state arithmetic block to read "set inches to (cm / 2.54)".
of the sensor, then make decisions about which LEDs
to light up based on the sensor's value. Add some serial monitoring blocks to print out the
sensor distance in centimeters and inches.
Click the "Code" button to open the code editor. The
grey Notation blocks are comments for making note Click the Control category and drag out an if then
of what you intend for your code to do, but this text block, then navigate to Math and drag a comparator
isn't required or executed as part of the program. block onto the if block.

Click on the Variables category in the code editor. In the Variables category, grab the cm variable and
Create a new variable called distanceThreshold and the distanceThreshold variable and drag them into the
use a "set" block to set it to 350 (centimeters). comparator block, adjusting the dropdown so it reads
"if cm > distanceThreshold then".
To store the sensor value, create a variable named
"cm". Add three digital output blocks inside the if statement
to set pins 2, 3, and 4 LOW.
Drag out a "set" block and adjust the dropdown to our
new variable cm. Duplicate this if statement four times and add
arithmetic blocks and and/or blocks to create five total
In the Input category, drag out a "read ultrasonic state detection if statements. The first state is "the
distance sensor on" block, and place it inside the set distance is farther than our threshold" so no LEDs
block. light up. When the distance is closer than or equal to
the distanceThreshold and greater than
Adjust the dropdown menus inside the input block to distanceThreshold-100, light up only pin 2's LED.
set the trigger pin to 7, the echo pin to "same as When the temperature is between distanceThreshold-
trigger" and units to cm. 100 and distanceThreshold-250, light up two LEDs.
And so on to account for all the desired states.
Optionally create a new variable for converting
centimeters to inches with a set block and an

Ultrasonic Distance Sensor in Arduino With Tinkercad: Page 5


Step 4: Ultrasonic Rangefinder Arduino Code Explained

When the code editor is open, you can click the dropdown menu on the left and select "Blocks + Text" to reveal
the Arduino code generated by the code blocks. Follow along as we explore the code in more detail.

int distanceThreshold = 0;
int cm = 0;
int inches = 0;

Before the setup() , we create variables to store the target distance threshold, as well as the sensor value in
centimeters (cm) and inches. They're called int because they are integers, or any whole number.

Ultrasonic Distance Sensor in Arduino With Tinkercad: Page 6


long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}

The next section is a special bit of code for reading the ultrasonic distance sensor. It's called a function. So far you
are familiar with setup() and loop(), but in this sketch, the function readUltrasonicDistance() is used to describe the sensor
code and keep it separate from the main body of the program. The function definition starts with what type of data
the function will return, or send back to the main program. In this case the function returns a long , which is a
decimal point number with many digits. Next is the name of the function, which is up to you. Then in parentheses
are the arguments the function takes. int triggerPin, int echoPin are the variable declarations for your sensor's connection
pins. The pin numbers will be specified when you call the function in the main program loop() . Inside the function,
these local variables are used to reference the information you passed to it from the main loop (or from another
function). The function itself sends a signal through the triggerPin and reports back the time it takes to get the
signal back over echoPin.

void setup()
{
Serial.begin(9600);

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}

Inside the setup, pins are configured using the pinMode() function. The serial monitor connection is established with
Serial.begin . Pins 2, 3, and 4 are configured as outputs to control the LEDs.

void loop()
{
// set threshold distance to activate LEDs
distanceThreshold = 350;
// measure the ping time in cm
cm = 0.01723 * readUltrasonicDistance(7, 6);

In the main loop, distanceThreshold is set to its target 350cm.

// convert to inches by dividing by 2.54


inches = (cm / 2.54);
Serial.print(cm);
Serial.print("cm, ");
Serial.print(inches);
Serial.println("in");

To convert centimeters to inches, divide by 2.54. Printing to the serial monitor helps you observe the distance
change more granularly than the LED states show alone.

Ultrasonic Distance Sensor in Arduino With Tinkercad: Page 7


if (cm > distanceThreshold) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if (cm <= distanceThreshold && cm > distanceThreshold - 100) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if (cm <= distanceThreshold - 100 && cm > distanceThreshold - 250) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}
if (cm <= distanceThreshold - 250 && cm > distanceThreshold - 350) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
if (cm <= distanceThreshold - 350) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(100); // Wait for 100 millisecond(s)
}

The loop's six if statements evaluate for different ranges of distance between 0 and 350cm, lighting up more LEDs
the closer the object.

If you want to see a more obvious change in bar graph lights, you can change the distanceThreshold variable
and/or the range that you are looking at by changing the arguments in the if() statements. This is called calibration.

Step 5: Build a Physical Circuit (Optional)

If you build a physical version of this circuit, you can try it out with the Arduino software's serial monitor
(magnifying glass button in the upper right of the sketch window), activating the sensor with your hand, body,
notebook, etc.

If using a physical board, put something in front of the sensor and observe the distance reading using the serial
monitor, and set distanceThreshold to that value.

Adjust your different distance threshold "buckets" to a range suitable to your initial value, for instance if your hand
was 60cm away, your ranges might be 60-40, 40-20, and 20-0.

Upload your code again, and try moving in front of the sensor. As the distance shortens, you should see the LEDs
turn on one by one.

Ultrasonic Distance Sensor in Arduino With Tinkercad: Page 8


Step 6: Next, Try...

Congratulations! You have learned to detect distance target="_blank">temperature sensor. Or add motors
using an ultrasonic sensor. You also learned about to create a robot with obstacle detection!
standalone functions in this lesson, and used and the
serial monitor to track changes inside your Arduino. You can also learn more electronics skills with the
You could expand this project by making it a free Instructables classes on Arduino, Basic
proximity alarm by adding a piezo buzzer that turns Electronics, LEDs & Lighting, 3D Printing, and more.
on when all three LEDs are lit up (closest distance).
Consider swapping the distance sensor for a

Ultrasonic Distance Sensor in Arduino With Tinkercad: Page 9

You might also like