0% found this document useful (0 votes)
5 views6 pages

Speedometer Instructions

The document provides instructions for building a speedometer using an Arduino Uno and two ultrasonic sensors. It outlines the steps for detecting an object's velocity based on the time it takes to pass between the two sensors, along with the necessary Arduino code for implementation. The process includes setting up the sensors, recording times, calculating velocity, and resetting for the next detection.

Uploaded by

Dostee Hemin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Speedometer Instructions

The document provides instructions for building a speedometer using an Arduino Uno and two ultrasonic sensors. It outlines the steps for detecting an object's velocity based on the time it takes to pass between the two sensors, along with the necessary Arduino code for implementation. The process includes setting up the sensors, recording times, calculating velocity, and resetting for the next detection.

Uploaded by

Dostee Hemin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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;

// The second sensor's pins


int secondTriggerPin = 7;
int secondEchoPin = 6;

// The distance (in meters) between the two sensors.


// This is just an example value; make sure to set this to the actual distance between your two sensors.
float distanceBetweenSensors = 2;

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);

// Set the pins of the first sensor


pinMode(firstTriggerPin, OUTPUT);
pinMode(firstEchoPin, INPUT);

// Set the pins of the second sensor


pinMode(secondTriggerPin, OUTPUT);
pinMode(secondEchoPin, INPUT);

// Print the status of the device


Serial.println("Waiting for the object to pass the first sensor...");
}

// 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();

// Print the value of the initial time


Serial.print("First sensor has detected something! Initial time = ");
Serial.print(timeInitial/1000);
Serial.println("s");
// Print the status of the device
Serial.println("Waiting for the object to pass the second sensor...");

// 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();

// Print the value of the initial time


Serial.print("Second sensor has detected something! Final time = ");
Serial.print(timeFinal/1000);
Serial.println("s");

// Calculate the velocity. Velocity = Distance / Time


float velocity = distanceBetweenSensors / ((timeFinal - timeInitial) * 0.001);

// Print the calculated value of the velocity


Serial.print("Velocity = ");
Serial.print(velocity);
Serial.println(" m/s");

// Print the status of the device


Serial.println("Resetting the program");

// Reset the program


waitingForFirstSensor = true;

// Print the program is waiting


Serial.println("Waiting for the object to pass the first sensor...");
}
}
}

// 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;
}

You might also like