How to connect and use a Speed Sensor with an Arduino
Whether you’re building a small robot or using any kind of motors in your projects, it might be useful to know the speed at which your are driving them.
In this tutorial we will see how to connect and use an infrared speed sensor based on the LM393 chip.
The speed sensor uses a disc with holes (encoder disc) to block the infrared beam, thus by counting the number of times the sensors goes from Low to High we can calculate the number of revolution for a given time period.
For our tutorial we will count the number of time the speed sensor goes from Low to High in a second and then divide that number by 20 (number of holes in the encoder disc) to get the number of revolution per second.
The speed sensor uses only 1 pin that goes from Low to High to detect holes in the encoder disc.
Since we will be using Interrupt zero to read the speed sensor, we need to connect it to Pin 2 (interrupt 0 pin) on the UNO.
Were are also using the L9110 motor driver to control the speed and direction of the geared DC motor.
Pin 2 is connected to the trigger pin of the speed sensor.
5V and GND is connected to the UNO
We connect the potentiometer to 3.3V and GND and use the Analog Pin 1 (A1) to read the value.
Pin 6 and Pin 9 are connected to B-1A and B-1B of the L9110
Power and GND of the L9110 are connected to an external 5V 2A power supply since DC motors can require more current than the UNO can provide.
We will use interrupts and the TimerOne library in our code.
The TimerOne will be used to wait 1 second before displaying the speed of our motor in the serial monitor window.
Interrupts are used so that we can control the speed and direction of our motor at the same time that we read the trigger pin of the speed sensor and to display information in the serial monitor.
So every time the speed sensor pin goes High, interrupt zero will be called since it’s connected to Pin 2 of the UNO (which is the interrupt 0 pin), thus increasing the counter variable by 1.
Then when Timer1 reaches 1 second it will call another interrupt to display the results, and reset everything back to zero to start over again.
As you can see in the code, we are using the counter value after 1 second elapsed and dividing it by 20 since our encoder disc has 20 holes. Thus giving us the number of revolution per second.
As always, please check out the tutorial video to have more information.
Just copy the above Sketch code you want to use above in your Arduino IDE software to program your Arduino.
You can download the latest version of the TimerOne library here : TimerOne Library download
Once downloaded, just extract the content of the zip files inside your “arduino/libraries” folder.