Microcontroller Tutorials

Arduino, Raspberry Pi, PIC, Beaglebone Resources

# Use LM393 IR Module as Motor Speed Sensor

Unlike stepper and servo motors, precision control for DC motors is impossible unless a sort of feedback mechanism is employed. If your application requires a consistent motor speed or a stimulant-dependent speed then you certainly need feedback. This tutorial aims to develop a simple motor speed sensor for a more intelligent DC motor control.

### The LM393 IR Module

For this tutorial, I will use this inexpensive module:

The module has two vertical columns with an IR LED on one column and a phototransistor on the other. Whenever the path between the IR LED and phototransistor is cut, the D0 pin goes high.

Sellers call this module a LM393 speed sensor. The LM393 is in fact a comparator IC and not a sensor, per se. The module schematic is as follows:

Here we see how simple the circuit for the module is. With no obstruction between the IR LED and phototransistor, the voltage between the positive and negative terminals of the comparator is equal. When the phototransistor is blocked, it will draw a higher voltage, making the positive terminal of the comparator more positive than the negative terminal. Thus, a positive voltage, equal to VCC, will be at the D0 terminal.

### Determining Motor Speed

A wheel encoder on the DC motor will help us determine its speed:

With proper placement, the slots on the encoder will block or pass the IR LED. This will create a train of pulses whose frequency is proportional to the speed of the motor.

The wheel encoder pictured has 20 slots. Therefore, counting 20 pulses mean the wheel has traveled one revolution.

If we attach the D0 pin of the module to interrupt 0 of the Arduino, which is digital pin 2, we can count the number of pulses if we have this as the interrupt service routine:

The number of revolutions will then be:

$revolutions = \frac{counter}{20}$

Motor speed is normally in revolutions per minute or RPM. Since we have an idea on determining how many revolutions the motor has travelled, all we need is the check the number of revolutions every minute. We can do that through the help of the millis() function inside the loop():

Here, we calculate the RPM every 1 second and then reset the pulse counter to 0.

### Motor Speed Sensor Project

Using a L298N motor driver, we follow this connection for testing our motor speed sensor:

Arduino LCD L298N Module LM393 Module
8 RS
3 EN
GND RW
7 D4
6 D5
5 D6
4 D7
12 IN1
13 IN2
9 ENA
2 D0
5V 5V VCC
GND GND GND

I used three 18650 LiPo batteries as power source. The L298N module has a 5V regulator so this is where the Arduino, LCD and LM393 module get their power. The DC motor takes on the 11.1 volts from the LiPo batteries.

The speed in RPM is viewable on a 16 x 2 LCD. Here is the full sketch:

This video shows the result of the sketch:

By knowing the speed of the motor, we can now use it as feedback for a PID controller to maintain its speed.

This site uses Akismet to reduce spam. Learn how your comment data is processed.