M a r k   C r o s b i e

You are in: STAMP PWM Motor control  Home  Resume  Linux  LEGO  Electronics  Movies  Mac OS X 

Overview Simple buzzer LCD control Buzzer and LCD PWM motor control Stepper motor control
Contact Mark at mark@mastincrosbie.com

STAMP PWM Motor control

Usually you want to control the speed of revolution of a DC motor. Varying the voltage supplied to the motor is one way to accomplish this, but it is difficult to achieve with digital circuitry. Pulse Width Modulation (PWM) simulates a variable voltage by rapidly sending short pulses of power to the motor (hence the name). The frequency and duration of each pulse will dictate the motor's speed.

At low speeds you may notice that the motor does not move smoothly. This is an unfortunate side effect of the square-wave of power being sent to the motor.

Wiring Details

The STAMP does not have the current capability to drive a motor directly. Instead it must drive the motor via a power transistor. I has a TIP41 power transistor handy, and wired the output from the STAMP on pin 7 to the base of the transistor.

Source Code

You can download the source code.
' pwm.bs2
' Drive the motor at a varying speed depending on the pot input
' Display the speed setting on the LCD
' Mark Crosbie 1998  mark@mastincrosbie.com
' Assumes:
'	pin5:	Bump button 1 (active high)
'	pin6:	RC input for pot
'	pin7:	PWM output to TIP41 base
'	pin8:	Serial output to serin of LCD
'	pin9:	Set input to latching relay
'	pin10:	Reset input to latching relay

'''''''''''''''''' Useful LCD constants ''''''''''''''''''''

LCDCLS	con	1
LCDCR		con	192

N24N	con	16780

'''''''''''''''''' Current PIN assignments '''''''''''''''''

LCD	con	8	' LCD serial output
MOTOR	con	7	' Motor driver line
POT	con	6	' RC pot input
SET	con	9	' SET input to latching relay
RST	con	10	' RST input to latching relay
BUMP	con	15	' Bump button (active high)

'''''''''''''''''' Local Variables '''''''''''''''''''''''''

result	var	word	' for RC timing
wrk	var	byte	' button workspace
direction var	bit	' direction of travel
			' 0 = forward, 1 = backward

'''''''''''''''''' Initialise lines and vars '''''''''''''''

output LCD		' set the LCD pin to output
output Motor		' set the PWM pin to output

serout LCD,N24N,[LCDPREFIX, LCDCLS]	' clear the LCD

low Motor		' turn off the motor

direction = 0		' start out going forward
wrk = 0			' clear button workspace

''''''''''''''''''''''''' Main Loop '''''''''''''''''''''''

	' do an RC timing charge on a capacitor on pin Pot
	' from this value, we deduce the motor speed control
	high POT		' charge the capacitor
	pause 1			' wait for it to charge
	rctime POT,1,result	' do the RC timing

	result = result // 255	' scale result to 0-255 range

	' display the result on the LCD
	' This can be commented out for final version
	serout LCD, N24N, [dec ? result] ' display the result
	serout LCD, N24N, ["   "] ' display some blanking text


	gosub PrintDirection

	serout LCD,N24N, [LCDPREFIX, LCDHOME] ' home the cursor

	' Pulse the Motor pin to drive the transistor
	pwm Motor, result, 100

	' Has anyone pressed the bump button
	button BUMP, 1, 255, 250, wrk, 0, noPress

	' change button state
	direction = direction ^ 1


	goto loop

' Assumes direction is stored in variable named direction
' Prints string on LCD with NO trailing characters
	' display the direction string
	if direction = 0 then Print_Forward

	serout LCD, N24N, ["Reverse"]

	Print_Forward:	serout LCD, N24N, ["Forward"]

PIC Microcontroller Project Book
Lot's of great PIC project ideas!

Programming and Customizing the Pic Microcontroller

If you are learning to program microcontrollers then Myke's book is good start.
The Art of Electronics
A classic in the field. Teaches you the art and science of linear and digital electronic design. If you want to learn why your circuit is not working, read this book and you'll know why.
Mobile Robots: Inspiration to Implementation
A very readable introduction to the art of robotic design and implementation from the best practioners in the field: the MIT Artificial Intelligence Lab!
Microcontroller Cookbook
A set of cookbook style designs for the 8051 and PIC microcontrollers. A handy reference to have if you need a quick solution to a problem.

© 2002-2004 Mark Crosbie   shareright © 2002 Phlash