IR Speed Sensor - Arduino Project

by IEEE BITS Pilani in Circuits > Arduino

1553 Views, 2 Favorites, 0 Comments

IR Speed Sensor - Arduino Project

IR Speed Gun | Arduino Project for Beginners | IEEE BITS PILANI

A speed gun is a simple application device used to find out speeding cars in the real world. A small-scale prototype version based on IR sensors is presented in this project. Using buzzers, it will also alert the user if the speed is beyond a certain threshold.

The concept is rudimentary: The IR sensors capture the time difference at which they capture the moving object and find the velocity using the formula v=s/t.


This project is for beginners who want to explore what can be done using an Arduino. It helps integrate the basic functions of an input device: IR sensor and output devices: LCD and Buzzer.

Supplies

  • Arduino UNO Board
  • 2 x IR Sensors
  • I2C LCD
  • Buzzer
  • Jumper Wires
  • Breadboard

Circuit Diagram

Schematic.png

IR Sensors :

Sensor 1 Output Pin -> D2

Sensor 1 GND Pin -> GND

Sensor 1 VCC Pin -> 5V

Sensor 2 Output Pin -> D3

Sensor 2 GND Pin -> GND

Sensor 2 VCC Pin -> 5V

LCD (With I2C) :

SDA Pin -> A4 Pin

SCL Pin -> A5 Pin

VCC Pin -> 5V

GND Pin -> GND

Buzzer :

Positive Pin -> D5

Negative Pin -> GND

Code

After making the connections, open up the Arduino IDE:

Download the following libraries if doing on your local machine:

Copy-paste the code. You can understand this by reading the comments and see how the algorithm is working.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>


#define sensorPin1 2 
#define sensorPin2 3 
#define buzzerPin 5 


const float dist = 0.07;
bool sensor1State = false;
bool sensor2State = false;
unsigned long interval = 0;
unsigned long startTime = 0;
double speed = 0;
const int timeout =  3000;


LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
  pinMode(sensorPin1, INPUT);
  pinMode(sensorPin2, INPUT);
  pinMode(buzzerPin, OUTPUT);


  Serial.begin(9600);


  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Speed (kmph):");


}


void loop() {
  // put your main code here, to run repeatedly:
  sensor1State = digitalRead(sensorPin1);
  digitalWrite(buzzerPin, LOW);
  startTime = 0;
  if(!sensor1State){
    startTime = millis();
    while (digitalRead(sensorPin2)){
      if(millis()-startTime > timeout){
        lcd.setCursor(0, 1);
        lcd.print("Timeout");
        return;
      }
      delay(3);
    }
    interval = millis() - startTime;
    if(interval > 3){
      Serial.print("Speed in kmph = ");
      speed = ((dist)/(interval/1000.0)) * (18/5.0);
      Serial.println(speed);
      lcd.setCursor(0, 1);
      lcd.print(speed);
      lcd.print("       ");
      if(speed > 1){
        digitalWrite(buzzerPin, HIGH);        
      }


    }
    delay(1000);
  }
  


//Serial.println(digitalRead(sensorPin1));  
}

Explanation

  1. The code starts by including the required libraries for the project. The Wire.h library is used for communication with the I2C LCD and the LiquidCrystal_I2C.h library allows for easy interfacing with the I2C LCD.
  2. The pin numbers for the sensors and buzzer are defined using #define. Then, variables are declared for the state, capturing time from the hardware clock as well as the distance between the sensors, the speed of the vehicle and the speed limit.
  3. The LiquidCrystal_I2C object is created with the I2C address of the LCD (0x27), the number of columns (20) and the number of rows (4). The setup() function is then called, where the pins are initialised as input or output pins, and the LCD is initialised.
  4. In the loop() function, the state of sensor 1 and sensor 2 is checked using digitalRead(). If a sensor is triggered (i.e. the state is HIGH), the corresponding time is noted. The millis function gives the time on the clock.
  5. Once both sensors have been triggered, the time taken by the vehicle is calculated and multiplied by 0.001 (to convert to seconds). The speed of the vehicle is then calculated by dividing the distance between the sensors by the time taken, and multiplying by 3.6 (to convert to km/h).
  6. The speed is then printed to the LCD using lcd.setCursor() and lcd.print(). Finally, if the speed exceeds the speed limit, the buzzer is turned on using digitalWrite(). Otherwise, the buzzer is turned off.
  7. Along with this, A timeout is added to ensure that if the object doesn’t pass through the other sensor within the threshold limit, the state of sensor is reset.


Overall, this project is a simple yet effective way to measure the speed of an object using two IR sensors and an I2C LCD.