Week 7- Journal
Speed Detector Using 2 IR Sensors, Buzzer, LCD & Arduino
Speed Detector Using 2 IR Sensors, Buzzer, LCD & Arduino
Speed sensor
Police officers use portable speed detectors, such as radar guns which rely on speed sensors used by police officers its portable one and you never expect where to see them, I had the experience to see these guns in United Arab of Emirates and they used mainly in places that has no installed Rader to detect the drivers exceeding the speed limit.
Input:
2 IR sensors
Output:
Liquid Crystal LCD screen
Buzzer
Programs:
Tinker CAD
IDE
Others:
GlueGun
Arduino
Jumer wires
Cardboard
Components
IR connected as follow:
Function to detect the speed
VCC: to the +ve on breadbord.
GND:to -ve on Breadboard.
OUT:IR (1) to pin(2),IR(2) to pin (3).
Buzzer:
function to give alarm when the driver cross the speed limit.
+ve to the pin 9 on Arduino.
-ve on the -ve on breadboard.
LCD:
function to show the result of the speed.
SDA ---> pin (A4)
SCL ---> pin (A5)
GND:-ve on breadboard.
VCC:+ on breadboard.
Arduino:
function to code the components
GND: to the -ve on breadboard.
5V:+ve on breadboard.
I used the same connections of the one I used in TinkerCAD
IR connected as follow:
VCC: to the +ve on breadbord.
GND:to -ve on Breadboard.
OUT:IR (1) to pin(2),IR(2) to pin (3).
Buzzer:
+ve to the pin 9 on Arduino.
-ve on the -ve on breadboard.
LCD:
SDA ---> pin (A4)
SCL ---> pin (A5)
GND:-ve on breadboard.
VCC:+ on breadboard.
Arduino:
GND: to the -ve on breadboard.
5V:+ve on breadboard.
then I connect the Arduino, after writing the code on IDE and test (compile) then test my project.
I'll create a simple visual diagram showing how the Arduino speed detector system works. Give me a moment to generate it.
Here is a simple visual diagram illustrating the Arduino speed detector system. Let me know if you need any modifications or further explanations! Now, I'll explain the code step by step.
Libraries and LCD Initialization
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Wire.h is required for I2C communication.
LiquidCrystal_I2C.h allows communication with an LCD screen using the I2C protocol.
The LCD is initialized with the address 0x27 (you may need to update this based on your hardware).
Defining Pins and Constants
#define IR_SENSOR1 2
#define IR_SENSOR2 3
#define BUZZER 9
const float distance = 0.1; // Distance between sensors in meters
const float speedLimit = 5.0; // Speed limit in meters per second
IR sensors are connected to digital pins 2 and 3.
The buzzer is connected to pin 9.
distance is the physical separation between the two sensors (0.1 meters in this case).
speedLimit is set to 5.0 m/s. If the calculated speed exceeds this value, the buzzer will sound.
Variables for Timing and Car Detection
unsigned long startTime;
unsigned long endTime;
bool carDetected = false;
startTime and endTime store the timestamps when the car passes the sensors.
carDetected ensures the system processes one car at a time.
Setup Function
void setup() {
pinMode(IR_SENSOR1, INPUT);
pinMode(IR_SENSOR2, INPUT);
pinMode(BUZZER, OUTPUT);
digitalWrite(BUZZER, LOW);
Wire.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Speed Detector");
delay(2000);
lcd.clear();
}
IR sensors are set as inputs, and the buzzer is set as an output.
Wire.begin(); initializes I2C communication.
lcd.init(); initializes the LCD, and lcd.backlight(); turns on the backlight.
The LCD briefly displays “Speed Detector” before clearing.
Loop Function: Detecting and Calculating Speed
void loop() {
if (digitalRead(IR_SENSOR1) == LOW && !carDetected) {
startTime = millis();
carDetected = true;
}
When a car crosses the first IR sensor (IR_SENSOR1 goes LOW), startTime is recorded.
carDetected prevents multiple detections for the same vehicle.
Detecting Second Sensor and Calculating Speed
if (digitalRead(IR_SENSOR2) == LOW && carDetected) {
endTime = millis();
carDetected = false;
float timeTaken = (endTime - startTime) / 1000.0;
float speed = distance / timeTaken;
When the car crosses IR_SENSOR2, endTime is recorded.
The time taken is calculated in seconds.
Speed is determined using the formula: Speed=DistanceTime Taken\text{Speed} = \frac{\text{Distance}}{\text{Time Taken}}
Displaying Speed and Alerting for Over-speeding
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Speed: ");
lcd.print(speed);
lcd.print(" m/s");
if (speed > speedLimit) {
lcd.setCursor(0, 1);
lcd.print("Over Speeding!");
digitalWrite(BUZZER, HIGH);
delay(2000);
digitalWrite(BUZZER, LOW);
} else {
lcd.setCursor(0, 1);
lcd.print("Safe Speed");
}
delay(2000);
lcd.clear();
}
}
The LCD displays the speed.
If the speed exceeds the limit, a warning message appears, and the buzzer sounds for 2 seconds.
Otherwise, it displays “Safe Speed.”
The LCD clears after displaying results for 2 seconds.
I asked Yassin and he helped me in install the liquidcrystal i2c from the IDE library.
I had challenge in making the LCD working as I switched the pins of SDA and SCL, then Yassin helped me to identify this problem and connect SDA ---> pin (A4) and SCL ---> pin (A5)
I learnt how to combine 2 inputs with 2 outputs which will help in my final project.