Infrared proximity sensors are very fun to play with. In the past we have used them to make Digital Theremins and Control Robots, always using its proximity sensing (how far away an object is from the sensor) as the main feature. Today, we’re going on a quest to use an infrared proximity sensor, to control the speed of a DC motor.
In this article, we will go step-by-step through the process of understanding, designing and building a system that uses an infrared proximity sensor for input, correlates that input to how far away an object is from the sensor and then drives a motor and some LEDs at distinct speeds depending upon the proximity of the object.
Purpose & Overview Of This Project
The goal of this project and article is to explain how to use an infrared proximity sensor to drive a motor. The system should be able to drive the motor at 8 different speeds (1 = slowest, 8 = fastest), likewise a representative LED bar will be added to give a second visual speed indicator. Up to 8 LEDs will be controlled to represent the 8 different levels of speed.
To make this system we will use a sharp ir distance sensor (10cm-80cm) for detecting how far away the object is, a PIC 18F4520 microcontroller to interpret the input and drive the output, a 10 LED Bar for giving a visual indication of what speed we’re at, and a TIP42 + DC motor for the actual motor and power transistor to drive the motor.
TIP42 Power BJT
IR Proximity Sensor
20 MHz Crystal
3x 10uF Capacitors
2x 100Ω Resistor
330Ω Resistor Network
+9v Battery Connector
Parts List Details
Luckily this project is half hardware intensive and half software intensive, so there aren’t too-too many parts. Below I’ll describe the most important parts in more detail.
This microcontroller will be used for understanding the input (an analog voltage) using its built-in Analog to Digital converter and it will also be used to drive the motor output and the LED bar output.
A 20 MHz crystal will be used to run the microcontroller at a 20 MHz clock rate (5 MHz instruction rate).
Sharp IR Distance Sensor
This sensor is the center-piece of this article. It outputs a specific analog voltage depending upon how far away an object is from the sensor.
TIP42 Power BJT
To provide enough current to the motor we need to use a power transistor. A PWM signal from the PIC will tell the power transistor when to turn the motor on and when to turn the motor off. The PWM’s duty cycle will determine the speed the motor turns.
The Sharp IR Distance Sensor
There’s two parts to the theory of this project that we need to cover before looking at the schematic. The first part is how the IR distance sensor works and the second part of the theory section will be looking at how the motor is controlled.
The video above demonstrates in a crude manner what the output of the IR distance sensor does when connected directly to a red LED. The LED gets brighter when the piece of paper nears the sensor because the voltage output increases. The opposite happens when the piece of paper is moved backward away from the sensor.
The Sensor’s Output
Let’s take a look at the datasheet’s theoretical output vs. distance. The graph below shows what voltage output from the sensor you should expect when a white piece of paper is placed in front of the sensor.
As you can see right away, the output voltage is not linear which makes things a little annoying as we won’t be able to have a straight-forward correlation between voltage and distance. Sharp has made a few attempts at building an algebraic equation that you can use, check the app notes on their website for more info on those.
We won’t be dynamically evaluating the sensor output in this project and converting it to distance, instead we’ll use set voltages hard-coded in the software with some if statements. That means we’ll need to pick 9 voltage levels at certain distances. Below you can see the 9 we chose and the ‘windows’ that those points form.
The specific distances are chosen as follows:
- Window #1 6cm-8cm
- Window #2 8cm-10cm
- Window #3 10cm-12cm
- Window #4 12cm-14cm
- Window #5 14cm-16cm
- Window #6 16cm-18cm
- Window #7 18cm-20cm
- Window #8 20cm-22cm
We call them ‘windows’ because each window has two sides defined by the distance. For example if the sensor’s output voltage is +2.4v, then take a look at the graph above and you will see that we are inbetween 6cm and 8cm, thus we’re inside of Window #1’s range. Our software will create these ‘windows’ using IF statements.
Read more: IR Proximity Motor Control using PIC18F4520