Data Sheet
60 : circuit 3b
NEW COMPONENTS
ULTRASONIC DISTANCE SENSOR:
Distance sensors work by sending pulses of
light or sound out from a transmitter, then
timing how long it takes for the signals to
bounce off an object and return to a receiver
(just like sonar). Some sensors use infrared
light, some use lasers, and some, like the
HC-SR04 included in your kit, use ultrasonic
sound (sound so high-pitched that you can’t
hear it).
NEW CONCEPTS
DATASHEETS: When working with
electronics, datasheets are your best
friend. Datasheets contain all the relevant
information needed for a part. In this
circuit, we are calculating distance based
on the time it takes sound waves to be
transmitted, bounce off an object and then
be received. But, how can we tell distance
from that information? The answer lies in
the datasheet for the distance sensor. In
it, you can find the equation the program
needs to interpret the distance. View the
datasheet at http://sfe.io/HCSR04.
ELSE IF STATEMENTS: In the night-
light circuit, you used an if/else statement
to run one set of code when a logic
statement was true, and another when
it was false. What if you wanted to have
more than two options? Else if statements
let you run as many logical tests as you
want in one statement. For example, in the
code for this circuit, there is an if statement
that flows like this:
1. If the distance is less than 10, make
the RGB LED red.
2. Else if the distance is more than
10 but less than 20, make the RGB LED
yellow.
3. Else make the RGB LED green.
To have four or five colors for different
distances, add more else if statements.
Else if statements are different from
nested if statements in that only one of
the statements above can be true, whereas
multiple nested if statements could be true.
Circuit 3B:
Distance
Sensor
Distance sensors are amazing tools with
all kinds of uses. They can sense the
presence of an object, they can be used
in experiments to calculate speed and
acceleration, and they can be used in
robotics to avoid obstacles. This circuit will
walk you through the basics of using an
ultrasonic distance sensor, which measures
distance using sound waves!
T
VCC
Trig
Echo
GND
TJP16527BH2
1652
R
YOU
NEED
T
VCC
Trig
Echo
GND
TJP16527BH2
1652
R
10k
100k
330
RGB LED 3 330Ω RESISTORS DISTANCE SENSOR 10 JUMPER WIRES