IR infrared speed sensor with Arduino | How does work IR speed sensor

Hello, welcome back. In this tutorial, we will learn what is the IR Infrared speed Sensor and how it works with Arduino. This sensor is designed based on IR rays. We can mainly use this sensor for measuring the speed of motors and pulse detection. This is a very small sensor and can be purchased from the market at a very affordable price. We can also use this sensor to create projects such as speedometers and speed control systems.

How does this IR infrared speed sensor work?

In this sensor, we can see two columns and one column includes an IR diode. The other column includes a phototransistor. When powering on the sensor a connection is made across these two columns. That is, the IR rays emitted by the IR diode are captured by the phototransistor. Then, a path is created through this. Also, the path is disconnected when an object is carried between the two-column. In that case, the sensor gives us a signal. That is, we can get it as a digital or analog value. Digital values can also be obtained from the LM393 comparator IC in this sensor module.

The PIN structure of this sensor

OK, let’s learn how to work this sensor with the Arduino. The required components are given below.

Step 1

Firstly, identify these components.

Step 2

Secondly, connect these components. To do this, use the circuit diagram below.

Step 3

OK, now let’s creates the program for this project. It is as follows.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x27, 16, 2);

const byte PulsesPerRevolution = 2;
const unsigned long ZeroTimeout = 100000;
const byte numReadings = 2;

volatile unsigned long LastTimeWeMeasured;
volatile unsigned long PeriodBetweenPulses = ZeroTimeout + 1000;
volatile unsigned long PeriodAverage = ZeroTimeout + 1000;
unsigned long FrequencyRaw;
unsigned long FrequencyReal;
unsigned long RPM;
unsigned int PulseCounter = 1;
unsigned long PeriodSum;

unsigned long LastTimeCycleMeasure = LastTimeWeMeasured;
unsigned long CurrentMicros = micros();
unsigned int AmountOfReadings = 1;
unsigned int ZeroDebouncingExtra;
unsigned long readings[numReadings];
unsigned long readIndex;  
unsigned long total; 
unsigned long average;
void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  attachInterrupt(digitalPinToInterrupt(2), Pulse_Event, RISING);
  delay(1000);

}
void loop() {
  LastTimeCycleMeasure = LastTimeWeMeasured;
  CurrentMicros = micros();
  if (CurrentMicros < LastTimeCycleMeasure) {
    LastTimeCycleMeasure = CurrentMicros;
  }
  FrequencyRaw = 10000000000 / PeriodAverage;
  if (PeriodBetweenPulses > ZeroTimeout - ZeroDebouncingExtra || CurrentMicros - LastTimeCycleMeasure > ZeroTimeout - ZeroDebouncingExtra) {
    FrequencyRaw = 0;  // Set frequency as 0.
    ZeroDebouncingExtra = 2000;
  } else {
    ZeroDebouncingExtra = 0;
  }
  FrequencyReal = FrequencyRaw / 10000;

  RPM = FrequencyRaw / PulsesPerRevolution * 60;
  RPM = RPM / 10000;
  total = total - readings[readIndex];
  readings[readIndex] = RPM;
  total = total + readings[readIndex];
  readIndex = readIndex + 1;

  if (readIndex >= numReadings) {
    readIndex = 0;
  }
  average = total / numReadings;



  Serial.print("Period: ");
  Serial.print(PeriodBetweenPulses);
  Serial.print("\tReadings: ");
  Serial.print(AmountOfReadings);
  Serial.print("\tFrequency: ");
  Serial.print(FrequencyReal);
  Serial.print("\tRPM: ");
  Serial.print(RPM);
  Serial.print("\tTachometer: ");
  Serial.println(average);
  lcd.setCursor(0, 0);
  lcd.print("RPM : ");
  lcd.print(RPM);
  lcd.print("   ");

}

void Pulse_Event() {
  PeriodBetweenPulses = micros() - LastTimeWeMeasured;
  LastTimeWeMeasured = micros();
  if (PulseCounter >= AmountOfReadings)  {
    PeriodAverage = PeriodSum / AmountOfReadings;
    PulseCounter = 1;
    PeriodSum = PeriodBetweenPulses;

    int RemapedAmountOfReadings = map(PeriodBetweenPulses, 40000, 5000, 1, 10);
    RemapedAmountOfReadings = constrain(RemapedAmountOfReadings, 1, 10);
    AmountOfReadings = RemapedAmountOfReadings;
  } else {
    PulseCounter++;
    PeriodSum = PeriodSum + PeriodBetweenPulses;
  }
}

Step 4

Now, select board and port. Afterward, upload this program.

OK, enjoy this tutorial. The full video guide is given below. So, we will meet in the next tutorial.

IR infrared speed sensor with Arduino | How does work IR speed sensor

Similar Posts

4 Comments

  1. hello!!
    firstly, thank you very much for tis nice work. It is really helpful.

    I would like to kindly ask you if you can comment the lines in the code ?

    Thank you in advance
    Regards!!

Leave a Reply

Your email address will not be published.