IR Proximity Motor Control using PIC18F4520

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.

IR Proximity Motor Control
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.

7805 +5v Regulator
PIC 18F4520
TIP42 Power BJT
IR Proximity Sensor
+3v Motor
20 MHz Crystal
3x 10uF Capacitors
Green LED
2x 100Ω Resistor
330Ω Resistor Network
10kΩ Resistor
Jumper Wire
+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.

PIC 18F4520
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.

20 MHz
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.

Breadboard and Jumper Wire
We’ll use a breadboard for building the circuit since everything is low frequency. Standard jumper wire will be used to connect the circuit together.

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.

IR Proximity Motor Control schematic

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.


For more detail: IR Proximity Motor Control using PIC18F4520

Current Project / Post can also be found using:

  • projects pic proximity sensor
  • 18f4520 pdf
  • Usocket using pic18f4520
  • interfacing proximity sensor using pic microcontroller

Leave a Comment

= 4 + 1

Read previous post:
A Tiny Intelligent Buzzer
The Annoy – A Tiny Intelligent Buzzer using PIC10F202

A while back I made a blog post about a PIC10F200 Project: The Annoy. In this project they combined the...

Scroll to top