hIOTron
Created July 27, 2020 © LGPL

Analog Speedometer Using Arduino and IR Sensor

The project describes an analog speedometer where we have used IR Sensor to calculate speed.

Analog Speedometer Using Arduino and IR Sensor

Things used in this project

Story

Read more

Code

Run a program

Arduino
#include<LiquidCrystal.h>
LiquidCrystal lcd(A5,A4,A3,A2,A1,A0);
#include <Stepper.h>
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
volatile byte REV; 
unsigned long int rpm,RPM;
unsigned long st=0; 
unsigned long time; 
int ledPin = 13;  
int led = 0,RPMlen , prevRPM; 
int flag = 0;     

int flag1=1;
#define bladesInFan 2
float radius=4.7;   // inch
int preSteps=0;
float stepAngle= 360.0/(float)stepsPerRevolution;
float minSpeed=0;
float maxSpeed=280.0; 
float minSteps=0;
float maxSteps=maxSpeed/stepAngle;

void setup() 
{
  myStepper.setSpeed(60);
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  lcd.begin(16,2);
  lcd.print("Speedometer");
  delay(2000);
  attachInterrupt(0, RPMCount, RISING); 
}

void loop() 
{
    readRPM();
    radius=((radius * 2.54)/100.0);  // convering in meter
    int Speed= ((float)RPM * 60.0 * (2.0 * 3.14 * radius)/1000.0);
    // RPM in 60 minute, diameter of tyre (2pi r) r is radius, 1000 to convert in km
    int Steps=map(Speed, minSpeed,maxSpeed,minSteps,maxSteps);
   
   if(flag1)
   {
    Serial.print(Speed);
    Serial.println("Kmh");
     lcd.setCursor(0,0);
     lcd.print("RPM: ");
     lcd.print(RPM);
     lcd.print("           ");
     lcd.setCursor(0,1);
     lcd.print("Speed: ");
     lcd.print(Speed);
     lcd.print(" Km/h       ");
    flag1=0;
   }
    int currSteps=Steps;
    int steps= currSteps-preSteps;
    preSteps=currSteps;
    myStepper.step(steps);
}

int readRPM()
{ 
  if(REV >= 10 or millis()>=st+1000)                  //  IT WILL UPDATE AFETR EVERY 10 READINGS or 1 second in idle
  {           
     if(flag==0)                 
       flag=1;                      
     rpm = (60/2)*(1000/(millis() - time))*REV/bladesInFan;  
     time = millis();                            
     REV = 0;
     int x= rpm;                 
     while(x!=0)
     {
       x = x/10;
       RPMlen++;
     }       
     Serial.println(rpm,DEC);
     RPM=rpm;
     delay(500);
     st=millis();
     flag1=1;
   }
}

void RPMCount()                        
 {
   REV++;                              
   if (led == LOW)
   {
     led = HIGH;                         
   } 
   else
   {
     led = LOW;
   }
   digitalWrite(ledPin, led);
 }

Credits

hIOTron

hIOTron

78 projects • 2 followers
hIOTron is an internet of things based company that offers an IoT Platform, products, IoT Solutions, and IoT Training.

Comments