Updated 24 September 2020

Kits and Gadgets Electronics Inc.
Farwaniya, Kuwait 80003
Kuwait

ph: +965 99571844

kits@kits-gadgets.com

  • Electronic Kits
  • Parts on Sale
  • Parts in StockClick to open the Parts in Stock menu
    • Resistor
    • Potentiometer
    • Capacitor
    • Trimmer Capacitor
    • Transistor
    • Crystal
    • Diode
    • LED
    • Diac
    • Thyristor
    • IC Socket
    • IC - Logic
    • IC - Linear
    • IC - Op-Amp
    • IC - VLSI
    • Relay
    • Motor
    • Lamp
    • Transducer
    • Gear
    • Switch
    • Heatsink
    • Parts Store Inventory
  • Brand New Items
  • Used Items
  • Power Supply
  • Raspberry Pi VaultClick to open the Raspberry Pi Vault menu
    • Demonstration Kit
    • Demonstration Kit - R3
    • The P1 Header Guide
    • BBC BASIC Tutorials
    • Activating the GPIO
    • LED Program
    • Motor Program
    • Solenoid Program
    • Stepper Motor
    • 8 Relay Program
    • GPIO as Input
    • Motors and Sensors Connection Diagram
    • Autonomous Program
    • Infrared Sensor
    • Ultrasonic Sensor
    • Camera Controller
    • PWM Program
    • Robotic Arm
    • STRYDER Robot
    • Demo Kit Program
  • Arduino VaultClick to open the Arduino Vault menu
    • Stepper Motor w/ LED
    • DCMotor
  • NodeMCU ProjectsClick to open the NodeMCU Projects menu
    • Running 8 LEDs
    • 8 LEDs and ULN2803
    • 8 LED Active Modes
    • ON-OFF Switch
    • Switch, Relay and LEDs
    • Motor Drive Interface
    • Connecting to Wi-Fi
    • IR Sensor
  • More GPIOsClick to open the More GPIOs menu
    • Accessing the P5 GPIOs
  • Prototypes
  • Computer Parts
  • Picture GalleryClick to open the Picture Gallery menu
    • Other Prototypes
    • Revived HP Laptop
    • Dismantled Helicopter
    • Dismantled Phone
    • ACER Laptop Repair
    • Revived Laptop
    • Inside of Electric Drill
    • Video Cam Automation
    • My Tours and Travels
  • My Original DesignClick to open the My Original Design menu
    • Handycam Automation
    • Autonomous Robot
  • What's New!Click to open the What's New! menu
    • Article-1
    • Article-2
    • Article-3
    • Article-4
    • Article-5
    • Article-6
    • Article-7
    • Article-8
    • Article-9
    • Article-10
    • Article-11
    • Article-12
    • Article-13
    • Article-14
    • Article-15
    • Article-16
    • Article-17
    • Article-18
    • Article-19
    • Article-20
    • Article-21
    • Article-22
    • Article-23
    • Article-24
    • Article-25
    • Article-26
    • Article-27
    • Article-28
    • Article-29
    • Article-30
    • Article-31
  • Forum
  • Video GalleryClick to open the Video Gallery menu
    • IR Proximity Sensor
    • Robot Vacuum Cleaner
    • Robotic Arm - RPi
    • Raspberry Pi Demo Kit
  • About Us
  • Contact Us

Motor Program

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.

Web Hosting by Yahoo

Kits and Gadgets Electronics Inc.
Farwaniya, Kuwait 80003
Kuwait

ph: +965 99571844

kits@kits-gadgets.com