Bidirectional Speed Calculator using Infrared (IR) Sensors
This project demonstrates how to calculate the speed of a moving object using Arduino and two IR Sensors irrespective of the direction.
Components and supplies
1
IR Sensor
1
Breadboard (generic)
1
128x64 OLED I2C Display
1
Arduino Nano R3
1
LED (generic)
1
Jumper wires (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Code
arduino
1/* Speed Sensor 2 3 This sketch is used to detect the speed of a moving object between two IR 4 Sensors connected to the Arduino. The IR Sensors are placed at a set distance. 5 6 The program calculates the speed using the formula Speed = Distance/Time. 7 The program then prints the speed of the object on the OLED Display. 8 In addition, start time, end time and the speed are displayed on the 9 Serial Monitor. The unit used here is metre per second (m/s). 10 11 This program is made by Shreyas for Electronics Champ YouTube Channel. 12 Please subscribe to this channel. Thank You. 13 14*/ 15 16#include <Wire.h> 17#include <Adafruit_GFX.h> 18#include <Adafruit_SSD1306.h> //Including the libraries 19 20double startTime = 0; //Initialize the start time 21double endTime = 0; //Initialize the end time 22double timeTakenInSeconds = 0; //Difference between start and end time 23double speedOfObject = 0; //Holds the value distance divided by time taken 24const double distance = 1; // Distance between the two sensors is set to 1 metre 25int executed = 0; //Flag to run the code in the loop only once. When set to 1, code in the loop is not executed. 26int sensor1 = 9; //First sensor is connected to pin 9 27int sensor2 = 11; //Second sensor is connected to pin 11 28int led1 = 10; //An LED to indicate that the first sensor has sensed an object 29int led2 = 12; //An LED to indicate that the second sensor has sensed an object 30 31#define SCREEN_WIDTH 128 // OLED display width, in pixels 32#define SCREEN_HEIGHT 64 // OLED display height, in pixels 33 34Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT); 35 36//Prints the logo on OLED Display 37static const uint8_t PROGMEM logo[] = { 38 39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 0x00, 0x00, 0x07, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 47 0x00, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 0x00, 0x01, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 0x00, 0x07, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 50 0x00, 0x1f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 0x00, 0x7f, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 52 0x00, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 53 0x01, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 54 0x03, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 55 0x07, 0xfc, 0x11, 0xf1, 0x0f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 56 0x0f, 0xfc, 0xf1, 0xf1, 0xe7, 0xfe, 0x00, 0x7e, 0x60, 0x00, 0x30, 0x00, 0x00, 0x30, 0x00, 0x00, 57 0x1f, 0xfd, 0xfb, 0xf3, 0xe7, 0xff, 0x00, 0x60, 0x60, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 58 0x1f, 0xfd, 0xff, 0xff, 0xe7, 0xff, 0x00, 0x60, 0x63, 0x87, 0xbb, 0xc7, 0x1f, 0x33, 0xcf, 0x00, 59 0x3f, 0xfd, 0xff, 0xff, 0xe7, 0xff, 0x80, 0x60, 0x66, 0xcd, 0xb3, 0x4d, 0x9b, 0x36, 0xdb, 0x00, 60 0x3f, 0xfd, 0xf9, 0xf3, 0xe7, 0xff, 0x80, 0x7e, 0x64, 0x48, 0x33, 0x08, 0x99, 0x34, 0x18, 0x00, 61 0x7e, 0x40, 0x11, 0xf1, 0x20, 0x1f, 0xc0, 0x60, 0x67, 0xc8, 0x33, 0x08, 0x99, 0x34, 0x1f, 0x00, 62 0x7e, 0x7d, 0xf9, 0xf3, 0xe7, 0xdf, 0xc0, 0x60, 0x64, 0x08, 0x33, 0x08, 0x99, 0x34, 0x01, 0x00, 63 0x7e, 0x7d, 0xfb, 0xff, 0xe7, 0xdf, 0xc0, 0x60, 0x66, 0xcd, 0xb3, 0x0d, 0x99, 0x36, 0xdb, 0x00, 64 0x7e, 0x7d, 0xff, 0xff, 0xe7, 0xdf, 0xc0, 0x7e, 0x63, 0xc7, 0xbb, 0x07, 0x19, 0x33, 0xde, 0x00, 65 0x7e, 0x7d, 0xf9, 0xf3, 0xe7, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 66 0xfe, 0x7c, 0xf1, 0xf1, 0x67, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 67 0xfe, 0x7e, 0x11, 0xf1, 0x0f, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 68 0xfe, 0x7f, 0xfb, 0xf3, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 69 0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 70 0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 71 0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 72 0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 73 0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x3c, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 74 0x7e, 0x7f, 0xff, 0xff, 0x7f, 0xdf, 0xe0, 0x7e, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 75 0x7e, 0x7f, 0x27, 0xf8, 0x9f, 0xdf, 0xc0, 0x62, 0x7c, 0x71, 0xf7, 0x3e, 0x00, 0x00, 0x00, 0x00, 76 0x7e, 0x7e, 0xf1, 0xe1, 0xcf, 0xdf, 0xc0, 0x60, 0x6c, 0x59, 0xbb, 0x36, 0x00, 0x00, 0x00, 0x00, 77 0x7e, 0x7e, 0xf8, 0xe3, 0xef, 0xdf, 0xc0, 0x60, 0x64, 0x09, 0x91, 0x32, 0x00, 0x00, 0x00, 0x00, 78 0x3e, 0x7c, 0xf9, 0xf3, 0xef, 0xdf, 0x80, 0x60, 0x64, 0xf9, 0x91, 0x32, 0x00, 0x00, 0x00, 0x00, 79 0x3f, 0x01, 0xff, 0xff, 0xe0, 0x1f, 0x80, 0x62, 0x64, 0x89, 0x91, 0x32, 0x00, 0x00, 0x00, 0x00, 80 0x1f, 0xfc, 0xf9, 0xf3, 0xef, 0xff, 0x80, 0x7e, 0x64, 0x99, 0x91, 0x36, 0x00, 0x00, 0x00, 0x00, 81 0x1f, 0xfe, 0xf8, 0xe3, 0xef, 0xff, 0x00, 0x3c, 0x64, 0xf9, 0x91, 0x3e, 0x00, 0x00, 0x00, 0x00, 82 0x0f, 0xfe, 0x71, 0xf1, 0xdf, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 83 0x0f, 0xff, 0x01, 0xfc, 0x3f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 84 0x07, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 85 0x03, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 86 0x01, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 87 0x00, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 88 0x00, 0x7f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 89 0x00, 0x1f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 90 0x00, 0x07, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 91 0x00, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 92 0x00, 0x00, 0x7f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 93 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 94 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 95 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 97 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 98 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 99 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 102 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 103 104}; 105 106void setup() { 107 108 Serial.begin(9600); 109 //Sets the pin modes of sensors and LEDs 110 pinMode(sensor1, INPUT); 111 pinMode(sensor2, INPUT); 112 pinMode(led1, OUTPUT); 113 pinMode(led2, OUTPUT); 114 115 //LEDs are turned off 116 digitalWrite(led1, LOW); 117 digitalWrite(led2, LOW); 118 119 //OLED setup (Change the address (0x3C) according to the display) 120 oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); 121 oled.clearDisplay(); 122 oled.drawBitmap(0, 0, logo, 128, 64, 1); 123 oled.display(); 124 delay(1000); 125 oled.clearDisplay(); 126 127} 128 129void loop() { 130 131 if (executed == 0) { //Flag is initialized to 0 to ensure that the loop runs only once 132 133 if (digitalRead(sensor1) == 1) { //If movement is detected by the first sensor 134 135 digitalWrite(led1, HIGH); //Turn on the LED 136 137 if (startTime == 0) { //If startTime is 0 and no time has been logged yet 138 139 startTime = millis(); //Assign Arduino time to startTime 140 141 } 142 143 } 144 145 if (digitalRead(sensor2) == 1) { //If movement is detected by the second sensor 146 147 digitalWrite(led2, HIGH); //Turn on the LED 148 149 if (endTime == 0) { //If endTime is 0 and no time has been logged yet 150 151 endTime = millis(); //Assign Arduino time to endTime 152 153 } 154 155 } 156 157 if ((startTime != 0) && (endTime != 0)) { //Now calculate the speed of the object if both times have been registered 158 159 if (startTime < endTime) { //If the object moves from sensor on pin 9 to sensor on pin 11 160 161 timeTakenInSeconds = (endTime - startTime) / 1000.0; //Convert miliseconds to seconds 162 speedOfObject = distance / timeTakenInSeconds; //Calculate speed in metres per second 163 164 /* Print the values to the serial monitor */ 165 Serial.print("Start Time: "); //Print to the serial monitor 166 Serial.print(startTime); //Print value 167 Serial.println(" milliseconds"); 168 Serial.print("End Time: "); //Print value 169 Serial.print(endTime); //Print value 170 Serial.println(" milliseconds"); 171 Serial.print("Speed of Object = "); 172 Serial.print(speedOfObject); 173 Serial.println(" m/s"); 174 Serial.println(" "); 175 oled.clearDisplay(); 176 oled.setTextSize(2); 177 oled.setCursor(0, 0); 178 oled.setTextColor(WHITE); 179 oled.print("Speed of"); 180 oled.setCursor(0, 19); 181 oled.print("Object is"); 182 oled.setCursor(0, 37); 183 oled.print(speedOfObject); 184 oled.println(" m/s"); 185 oled.display(); 186 executed = 1; //Set the value to 1 to stop the loop from running again 187 reset(); 188 189 } 190 191 else { //If the object moves from sensor on pin 11 to sensor on pin 9 192 193 timeTakenInSeconds = (startTime - endTime) / 1000.0; //The value of startTime is greater than endTime 194 speedOfObject = distance / timeTakenInSeconds; //Calculate speed in metres per second 195 196 /* Print the values to the serial monitor */ 197 Serial.print("Start Time: "); 198 Serial.print(endTime); 199 Serial.println(" milliseconds"); 200 Serial.print("End Time: "); 201 Serial.print(startTime); 202 Serial.println(" milliseconds"); 203 Serial.print("Speed of Object = "); 204 Serial.print(speedOfObject); 205 Serial.println(" m/s"); 206 Serial.println(" "); 207 208 /* Print the values on the OLED Display */ 209 oled.clearDisplay(); 210 oled.setTextSize(2); 211 oled.setCursor(0, 0); 212 oled.setTextColor(WHITE); 213 oled.print("Speed of"); 214 oled.setCursor(0, 19); 215 oled.print("Object is"); 216 oled.setCursor(0, 37); 217 oled.print(speedOfObject); 218 oled.println(" m/s"); 219 oled.display(); 220 executed = 1; //Set the value to 1 to stop the loop from running again 221 reset(); //A function to start the loop from running again 222 223 } 224 } 225 } 226} 227 228void reset() { 229 230 delay(5000); 231 executed = 0; 232 startTime = 0; 233 endTime = 0; //Set the value to 0 to start the loop from running again 234 digitalWrite(led1, LOW); 235 digitalWrite(led2, LOW); //Turns off both the LEDs 236 speedOfObject = 0; //sets the speed to 0 237 238 //Resets the OLED Display 239 oled.clearDisplay(); 240 oled.setTextSize(2); 241 oled.setCursor(0, 0); 242 oled.setTextColor(WHITE); 243 oled.print("Speed of"); 244 oled.setCursor(0, 19); 245 oled.print("Object is"); 246 oled.setCursor(0, 37); 247 oled.print(speedOfObject); 248 oled.println(" m/s"); 249 oled.display(); 250 251}
Downloadable files
Schematic
Schematic

Schematic
Schematic

Comments
Only logged in users can leave comments