Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

PROJECT 9 : DISTANCE SENSOR / ULTRASONIC 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!
Grab the following quantities of each part listed to build this circuit:
Page | 1

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 nightlight 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.
PROJECT 9 : DISTANCE SENSOR / ULTRASONIC SENSOR

HOOKUP GUIDE
READY TO START HOOKING EVERYTHING UP? Check out the circuit diagram and
hookup table below to see how everything is connected.

Page | 2
PROJECT 9 : DISTANCE SENSOR / ULTRASONIC SENSOR

Page | 3

SOURCE CODE:
const int trigPin = 11; //connects to the trigger pin on the distance sensor
const int echoPin = 12; //connects to the echo pin on the distance sensor

const int redPin = 3; //pin to control the red LED inside the RGB LED
const int greenPin = 5; //pin to control the green LED inside the RGB LED
const int bluePin = 6; //pin to control the blue LED inside the RGB LED

float distance = 0; //stores the distance measured by the distance sensor

void setup()
{
Serial.begin (9600); //set up a serial connection with the computer

pinMode(trigPin, OUTPUT); //the trigger pin will output pulses of electricity


pinMode(echoPin, INPUT); //the echo pin will measure the duration of pulses coming back from the distance
sensor

//set the RGB LED pins to output


pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

void loop() {
distance = getDistance(); //variable to store the distance measured by the sensor

Serial.print(distance); //print the distance that was measured


Serial.println(" in"); //print units after the distance

if (distance <= 10) { //if the object is close

//make the RGB LED red


PROJECT 9 : DISTANCE SENSOR / ULTRASONIC SENSOR

analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);

} else if (10 < distance && distance < 20) { //if the object is a medium distance

//make the RGB LED yellow Page | 4


analogWrite(redPin, 255);
analogWrite(greenPin, 50);
analogWrite(bluePin, 0);

} else { //if the object is far away

//make the RGB LED green


analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
}

delay(50); //delay 50ms between each reading


}

//------------------FUNCTIONS-------------------------------

//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR


float getDistance()
{
float echoTime; //variable to store the time it takes for a ping to bounce off an object
float calculatedDistance; //variable to store the distance calculated from the echo time

//send out an ultrasonic pulse that's 10ms long


digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the
//pulse to bounce back to the sensor

calculatedDistance = echoTime / 148.0; //calculate the distance of the object that reflected the pulse (half the
bounce time multiplied by the speed of sound)

return calculatedDistance; //send back the distance that was calculated


}

You might also like