Speedometer Instructions
Speedometer Instructions
Materials Needed:
- Arduino Uno x1
- Ultrasonic Sensor x2
- Female-to-Male Wires
Electronics Diagram:
Project Plan:
Step 1: The device waits for an object to pass in front of the first sensor.
Step 2: The first sensor detects an object pass in front of it. It records the current time (ti) of
the program for future reference.
Step 3: The device waits for the object to pass the second sensor.
Step 4: The second sensor detects the object pass in front of it. It records the current time (tf)
of the program for future reference.
Step 5: The device calculates the velocity of the object according to the distance between the
sensors and the difference in time between the two detections. It then writes the value of the
velocity to the console so that the user can see the results.
𝑑
𝑣=
𝑡𝑓 − 𝑡𝑖
Step 6: The device resets and is now ready to detect another object. The process starts again
from Step 1.
Arduino Program:
To operate the following piece of code, download the latest version of Arduino from their
website. Connect the Arduino Uno to your computer so you can upload your program. Setup the
application and establish a connection between your computer and the Arduino Uno (there are
many online video tutorials that explain how to do this).
Then, copy and paste the following code into the Arduino editor. Make sure to adjust the
distance value to the actual distance between your two sensors (in meters). Once everything is
finished, click the run button. If all goes well, your code should be uploaded to the Arduino Uno.
In order to see your results, your Arduino Uno must be connected to your computer. To
see the status of the device, open up the serial monitor. It should say that it is waiting for an
object to pass the first sensor. By using the device, the serial monitor should display different
messages based on which state it is in. After the device has detected an object pass both sensors,
the serial monitor should display the calculated velocity. After this, the program goes back to
step 1 and waits for a new object.
// The first sensor's pins
int firstTriggerPin = 12;
int firstEchoPin = 11;
boolean waitingForFirstSensor; // Boolean that's true if we're waiting for the first sensor to detect something
unsigned long timeInitial; // The initial time of detetction (in milliseconds)
unsigned long timeFinal; // The final time of detetction (in milliseconds)
float range = 0.5; // The range of the sensors (in meters)
// Function that is called at the start of the program once and only once
void setup() {
// Initialize the serial monitor
Serial.begin(9600);
// Function that is called after the program is set up and repeats forever until the user terminates the program
void loop() {
// If we are waiting for the first sensor to detect something...
if (waitingForFirstSensor) {
// If the first sensor has detected an object...
if(sensorHasDetected(firstTriggerPin, firstEchoPin)) {
// Record the current time
timeInitial = millis();
// Update the program so that we are no longer waiting for the first sensor
waitingForFirstSensor = false;
}
}
// If we are waiting for the second sensor to detect something...
else {
// If the second sensor has detected an object...
if(sensorHasDetected(secondTriggerPin, secondEchoPin)) {
// Record the current time
timeFinal = millis();
// Function that returns whether or not the given sensor detects an object pass in front of it
boolean sensorHasDetected(int triggerPin, int echoPin) {
// Pulse the trigger pin so that an ultrasonic wave can be emitted from the sensor
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// Calculate the time it took for the pulse to come back to the sensor
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance (in meters) that the pulse travelled until it bounced off of a surface.
long distance = ((duration / 2) * 0.0344) / 100;
// Return 'true' if the distance traveled is less than the accepted range, and 'false' otherwise
return distance < range;
}