Updated 24 September 2020
Kits and Gadgets Electronics Inc.
Farwaniya, Kuwait 80003
Kuwait
ph: +965 99571844
kits
DRIVING A MOTOR
It is not advisable to drive any inductive devices such as motors, solenoids or speaker directly from Raspberry Pi GPIOs. It is a common practise of using an interface circuit, buffer or driver for such purpose to handle higher current. Remember, GPIO ports are not designed as an output driver that can handle currents in excess of 16mA since they are also use as an input ports. Always limit the current loading to nominal level or as low as possible to prevent degrading or damaging your RPI.
There are many types of motor drive circuits available on the internet but the one shown below I think is the most practical and economical. The IC L293 Motor Controller is widely utilized for driving small motors. You can connect two motors that can be driven clockwise or counter-clockwise depending on the state of two logic input for each motor. The rotation speed can also be controlled by PWM signal (from GPIO 18) feed on Pin 1 or Pin 9 of IC L293.
Below is a bit complicated program to activate a DC motor. You can write the code using Notepad, Wordpad or MSWord. Just export this text file to RISCOS BBC BASIC preloaded on any Raspberry Pi and save it as BASIC program. This program can control the motor to run clockwise or counter-clockwise, it can increase or decease the speed also. The control is through the standard keyboard connected on RPI USB port. However, I'm using a mini wireless keyboard when I wrote this program. There should be no problem since most keyboards uses the same standard ASCII code though BBC BASIC convert this to different code .
THE PROGRAM
10 REM This program will demonstrate activation of DC motor through a motor drive interface.
20 REM Program for Raspberry Pi Demonstration Test Kit
30 REM Programmed by: Rolly Estomaguio
40 REM Created on: 12 May 2014
50 REM http://www.kits-gadgets.com
60 ON ERROR PRINT REPORT$;" at line ";ERL:END
70 PROCinit
80 PRINT "Press Escape to finish"
90 GOTO 210
100 DEFPROCinit
110 OSCLI"RMEnsure GPIO 0.00 RMLoad GPIO"
120 OSCLI"RMensure GPIO 0.40 ERROR Please install the GPIO module"
130 SYS"GPIO_EnableI2C",0
140 SYS"GPIO_ExpAsGPIO",2
150 SYS"GPIO_GetBoard" TO PiType%
160 DIM A%(16)
170 IF PiType%=12 A%()= 3,4,17,27,22,10,9,11,7,8,25,23,15,14,24
180 REM Other GPIOs not activated: 2,18,28,29,30,31
190 ENDPROC
200 REPEAT
210 L$= GET$: OFF
220 IF L$= "M" OR L$= "m" THEN PROC_motor
230 IF L$= "Q" OR L$= "q" THEN QUIT
240 IF L$= "S" OR L$= "s" THEN PROC_motor_stop: REM STOP THE MOTOR MOMENTARILY
250 IF L$= "M" OR L$= "m" THEN SYS "GPIO_WriteData",18,1
260 GOTO 210
270 SYS "GPIO_WriteData",A%(N%),1
280 DEFPROC_motor
290 SYS "GPIO_WriteData",18,1: REM THIS IS PWM TO CONTROL THE SPEED OF MOTOR
300 S%= 450000
310 FOR D%= 1 TO 30000: NEXT
320 G%= INKEY (1)
330 IF INKEY (-99) THEN PROC_motor_stop
340 IF INKEY (-122) THEN SYS "GPIO_WriteData",15,0: SYS "GPIO_WriteData",14,1: REM CW ROTATION
350 IF INKEY (-26) THEN SYS "GPIO_WriteData",14,0: SYS "GPIO_WriteData",15,1:
370 REM CCW ROTATION
360 IF INKEY (-42) OR INKEY (-98) THEN S%=S% + 450000: REM ARROW DWN TO REDUCE SPEED
370 IF INKEY (-58) OR INKEY (-66) THEN S%=S% - 550000: REM ARROW UP TO INCREASE SPEED
380 SYS "GPIO_WriteData",18,0
390 FOR D%= 1 TO S%: NEXT
400 SYS "GPIO_WriteData",18,0: REM THIS TO DELAY MOTOR CW AND CCW
410 FOR N%=1 TO 100000: NEXT
420 PROC_motor
430 DEFPROC_motor_stop
440 SYS "GPIO_WriteData",18,0
450 SYS "GPIO_WriteData",15,0: SYS "GPIO_WriteData",14,0
460 FOR D%= 1 TO 500000: NEXT
470 ENDPROC
NOTE:
You can modify the program if you hated the GOTO command and the line numbers.
RUNNING THE PROGRAM:
To START the program : Type " M"
To activate the motor : Press cursor keys RIGHT or LEFT
To run motor clockwise : Presss cursor RIGHT
To run motor counter-CW : Press cursor LEFT
To increase speed : Press cursor UP or "A" key
To decrease speed : Press cursor DOWN or "Z" key
To stop the motor : Press SPACEBAR
Copyright 2013 Kits and Gadgets Electronics Inc. All rights reserved.
Kits and Gadgets Electronics Inc.
Farwaniya, Kuwait 80003
Kuwait
ph: +965 99571844
kits