0% found this document useful (0 votes)
4 views3 pages

Vehical Speed Detection Code

This document contains code for a vehicle speed detection system using two IR sensors and an LCD display. It measures the time taken for a vehicle to pass between the sensors to calculate speed in centimeters per second. The results are displayed on the LCD and printed to the Serial Monitor.

Uploaded by

07Sakshi Bongale
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)
4 views3 pages

Vehical Speed Detection Code

This document contains code for a vehicle speed detection system using two IR sensors and an LCD display. It measures the time taken for a vehicle to pass between the sensors to calculate speed in centimeters per second. The results are displayed on the LCD and printed to the Serial Monitor.

Uploaded by

07Sakshi Bongale
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/ 3

Vehical speed detection code:

const int sensor1Pin = 2; // IR Sensor 1 connected to digital pin 2


const int sensor2Pin = 3; // IR Sensor 2 connected to digital pin 3

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

unsigned long timeDiff;


float distanceBetweenSensors = 20.0; // in cm
float speedOfLight = 343.0; // Speed of sound in m/s
(approximation)

boolean measure = false;

void setup() {
Serial.begin(9600);
pinMode(sensor1Pin, INPUT_PULLUP);
pinMode(sensor2Pin, INPUT_PULLUP);

lcd.init();
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" Vehical Speed ");
lcd.setCursor(0, 1);
lcd.print(" Measure System ");
}

void loop() {
if (digitalRead(sensor1Pin) == LOW) {

measure = true;
}
if (measure)
{
unsigned long time1 = micros(); // Record the time when Sensor
1 is triggered

while (digitalRead(sensor2Pin) == HIGH) {} // Wait for Sensor 2 to


be triggered

unsigned long time2 = micros(); // Record the time when Sensor


2 is triggered
timeDiff = time2 - time1; // Calculate the time difference in
microseconds

// Calculate the speed of the vehicle


float timeInSeconds = timeDiff / 1000000.0; // Convert time
difference to seconds
float speed = distanceBetweenSensors / timeInSeconds; //
Calculate speed in meters per second
// speed = speed / 100; // Convert meters per second to
kilometers per hour

Serial.print("Time: ");
Serial.print(timeInSeconds);
Serial.print(", Speed: ");
Serial.print(speed);
Serial.println(" cm/s");

lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Vehical Speed ");
lcd.setCursor(3, 1);
lcd.print(speed);
lcd.print("cm/s");
measure = false;
delay(1000);
}

You might also like