CS-424/580A, Lab # 5
2-27-09, Report Due Date 3-6-09

Motor Control with the PIC18F452

In this laboratory you will control a robot's motor with the PIC18F452 
microcontroller on the QuikFlash board. You will also explore the use 
of Pulse Width Modulation (PWM) to control the speed of a motor under 
program control.

Motor control

A DC motor may be turned on or off with a circuit like that shown in 
Figure 1. The NPN transistor used will depend on the current carried 
by the motor. Usually a power transistor such as the TIP140 is used, 
but for the small motors that come with the LEGO Mindstorms Robotic 
Invention System, a pn2222 transistor can be used safely if a 30 Ohm 
current-limiting resistor is wired in series with the motor. (This is 
normally not done, but for the purposes of this experiment, which is 
mainly concerned with CONTROL of the motor, you should include the 
resistor so as not to burn out the transistor.) The basic idea of the 
circuit is that when the output port bit is low, the transistor turns 
off and no collector-emitter current flows through the motor from the 
voltage source to ground. the motor is off. When the port bit is set 
high, that turns on the transistor on, which makes current flow through 
the motor causing it to turn on.

Figure 1. Basic Motor Control Circuit

Varying the speed of operation of the motor

One way to cause a DC motor to turn faster is to increase the voltage 
applied to it. In most inexpensive robotics systems another technique 
called Pulse Width modulation (PWM) is used. Here to change the speed 
of operation of the motor the voltage is not varied. Instead the 
voltage is turned on and off very rapidly. The motor will rotate with 
a speed that is proportional to the percent of time the voltage is on 
(known as the duty cycle).

Experimental procedure -- Part A

In this part of the experiment, you will use bit 4 of Port C (PC4) 
from the QuikFlash/QuikProto boards to turn a DC motor on or off. The 
state (on or off) of a single dip switch attached to bit 0 of Port B 
(PB0) will determine whether the motor is on or off. The dip switch 
should be wired in similar fashion to Lab 3. Wire up the DIP switch 
circuitry on the proto board. Because the QuikFlash power supply is 
not capable of providing the currents required by the motor, the motor 
control circuit discussed above (and shown in Figure 1) should be 
wired up on one of the prototyping boards used in CS-210. The yellow 
variable DC voltage lead provides the voltage to the motor. Use the 
potentiometer control on that proto board to set its voltage to about 
half its maximum value. The signal that goes to the base of the 
transistor will come from the output port on QuikFlash/QuikProto board. 
Be sure to connect the ground leads of the prototyping boards together. 
The pinouts of the pn2222 transistor are given in Figure 2, and the 
cathode markings on the IN4740 shunt diode are given in Figure 2A.


Figure 2. pn2222 transistor pinouts

Figure 2A. IN4740 diode markings

Write a program that will program the PIC18F452 so that bit 4 of 
port C (PC4) is for output and bit 0 of port B is for input. The 
program should then turn off the motor and go into an infinite polling 
loop in which the input bit is examined. If it is on (high), the motor 
should be turned on. If it is off (low), the motor should be turned 
off. When your circuit and program are working correctly show the lab 

Experimental Procedure -- Part B

In this part of the experiment you will wire five DIP switches to the 
low order five bits of port B to control the speed of the motor using 
Pulse Width Modulation. If switch 4 is on, the motor should be turned on 
with a 100% duty cycle; if switch 3 is on, the duty cycle should be 25%; 
if switch 2 is on, it should be 5%; if switch 1 is on, it should be 2%, 
and if switch 0 is on, it should be 1%. If none of the switches is on, 
the motor should be turned off. (You may assume that no more than one 
switch will be on at a time. The control program should once again set 
up bit 4 of Port C for output (turning on or off the motor) and the low 
order four bits of Port B for input (from the DIP switches); it should 
also make sure the motor starts out turned off. The program should then 
go into a polling loop to examine the DIP switches. Depending on their 
state, it should output a square wave of the appropriate duty cycle. 
Figure 3 indicates what that square wave would look like for a 75% duty 

Figure 3. A 75% Duty Cycle Square Wave

Basically one cycle of the square wave is produced by the following 

Turn on motor
Wait for the "on time"
Turn off motor
Wait for the "off time"

The wait can be achieved by a delay loop. You will have to "play around" 
with the numbers of iterations of the "on time" and "off time" loops, but 
a square wave frequency anywhere between 500 and 10000 Hz will probably 
work OK. The important thing is the relative time on and time off. Try 
to set up an oscilloscope so that you can see how the duty cycle of the 
PWM wave changes as you turn off and on the various switches. If you 
don't know how to operate an oscilloscope, have the TA help you. There 
is also a great YouTube video on using an oscilloscope at: 

If your circuit and program are working correctly, you should notice a 
change in speed (sound the motor makes) when you change the DIP switches. 
In order to detect this change you may need to "load" the motor, perhaps 
by putting some LEGO gears or wheels on its axis. Be sure to show the TA 
your working system.