A dual dc motor controller for the Raspberry Pi

Circuit diagram

 

 For two motors to be turned on and off individually as well as change direction one requires four of the Raspberry Pi's PIO control lines. The relays I picked for this project  happened to be in my junk box. They can switch up to 10A. As there seem to be as many different relays as there are fingerprints, you will have to change my PCB layout to suit your own requirements.


The circuit is straight forward. The motors have their own supply shown at the top right. Relay RL1 and RL3 turn the motors on or off, RL2 and RL4 change the direction of their motor. The four relay coils are connected to  GPIO pins 21, 22, 10 and 9 on my 8 in 8 out interface. Notice that the 0V line and the positive line also have to be connected to 0V and Vin (Clamp)  on the interface board.

For the motor M1 to turn in one direction, the relay RL1 has to be activated. If you want to change the direction of that motor, relays RL1 and RL2 have to be both turned on. To stop the motor, relay RL1 has to be turned off. The same applies to motor M2 but with the relays RL3 and RL4 instead . Notice that with this particular circuit the motors and the relays have to have the same voltage. It is of course an easy matter to separate the power supply so that there is one voltage for the relay coils and a different voltage for the motors.

The PCB


On the right is a suggested printed circuit board layout. Remember what I said above about the different pin-out for relays - it is unlikely that your own stuff looks the same as mine. You'll need to adapt the layout to your needs. Remember also that you need two double pole double throw relays as well as two double pole single throw ones.

The zip file below also contains the draw file of the pcb layout on the right.


The 2 motor controller with a Raspberry Pi

On the left is a photograph of the complete system.

The relay board is connected to the Raspberry Pi board via my Pi interface board. The connector strip on the right is for the two motors and the power supply.


Controlled by the the small program below, the set-up performed very well.






Below is the archive containing a simple BBC BASIC program to control the motors. 


Dual_DC_Motor__Pi.zip


  Here is the simple control program. Motor 1 is controlled with the A S D  motor 2 with Z X C. Q quits the program.



    10 REM Dual_DC_Motor_Pi
   20 REM Control two dc motors with the Raspberry Pi
   30 REM Version 1       .1
   40 REM
   50 REM Jochen Lueg
   60 REM http://roevalley.com
   70 REM January 2013
   80
   90 ON ERROR PROCERROR
  100 PROCsetupGPIO
  110 OFF
  120 OSCLI"RMEnsure GPIO 0.40 ERROR HELP can't find the GPIO module"
  130
  140 REM GPIO 24 is for M1 on-off, GPIO 23 for M1 left-right
  150 REM GPIO 18 is for M2 on-off, GPIO  15 for M2 left-right
  160
  170 PRINT
  180 PRINT "  Motor 1:  Left - Stop - Right  . . . . .  A  S  D"
  190 PRINT "  Motor 2:  Left - Stop - Right  . . . . .  Z  X  C"
  200 PRINT
  210 PRINT "  Leave the program  . . . . . . . . . . .  Q"
  220
  230 Motor1$="Stop" :Motor2$="Stop"
  240
  250 REPEAT
  260   Key$=INKEY$(0)
  270   IF Key$="a" OR Key$="A"  PROCmotor1_left(Motor1$)
  280   IF Key$="d" OR Key$="D"  PROCmotor1_right(Motor1$)
  290   IF Key$="s" OR Key$="S"  PROCmotor1_stop(Motor1$)
  300   IF Key$="z" OR Key$="Z"  PROCmotor2_left(Motor2$)
  310   IF Key$="c" OR Key$="C"  PROCmotor2_right(Motor2$)
  320   IF Key$="x" OR Key$="X"  PROCmotor2_stop(Motor2$)
  330   PRINTTAB(10,10);"Motor 1 ";Motor1$;"   Motor 2 ";Motor2$;"           "
  340 UNTIL Key$="q" OR Key$="Q"
  350 SYS "GPIO_WriteData",24,0
  360 SYS "GPIO_WriteData",23,0
  370 SYS "GPIO_WriteData",18,0
  380 SYS "GPIO_WriteData",15,0
  381
  390 STOP
  400
  410
  420 DEFPROCmotor1_left(RETURN m$)
  430 SYS "GPIO_WriteData",24,1
  440 SYS "GPIO_WriteData",23,1
  450 m$="left"
  460 ENDPROC
  470
  480
  490 DEFPROCmotor1_right(RETURN m$)
  500 SYS"GPIO_WriteData",24,1
  510 SYS"GPIO_WriteData",23,0
  520 m$="right"
  530 ENDPROC
  540
  550
  560 DEFPROCmotor1_stop(RETURN m$)
  570 SYS"GPIO_WriteData",24,0
  580 SYS"GPIO_WriteData",23,0
  590 m$="stop"
  600 ENDPROC
  610
  620 DEFPROCmotor2_left(RETURN m$)
  630
  640 SYS"GPIO_WriteData",18,1
  650 SYS"GPIO_WriteData",15,1
  660 m$="left"
  670 ENDPROC
  680
  690
  700 DEFPROCmotor2_right(RETURN m$)
  710 SYS"GPIO_WriteData",18,1
  720 SYS"GPIO_WriteData",15,0
  730 m$="right"
  740 ENDPROC
  750
  760
  770 DEFPROCmotor2_stop(RETURN m$)
  780 SYS"GPIO_WriteData",18,0
  790 SYS"GPIO_WriteData",15,0
  800 m$="stop"
  810 ENDPROC
  820
  830
  840 DEFPROCerror
  850 PRINT REPORT$;" at line ";ERL
  860 END
  870 ENDPROC
  880
  881
  890 DEFPROCsetupGPIO
  900 OSCLI"RMEnsure GPIO 0.00 RMLoad GPIO"
  910 OSCLI"RMensure GPIO 0.40 ERROR Please install the GPIO module"
  920
  930 SYS"GPIO_EnableI2C",0
  940 SYS"GPIO_ExpAsGPIO",2
  950
  960 SYS"GPIO_GetBoard" TO PiType%
  970 DIM Out%(3)
  980
  990
 1000 REM ISSUE 2 Interface board 5x16
 1010 Out%()=24,23,18,15
 1020
 1030 REM Set up the otput lines
 1040 FOR J%=0 TO 3
 1050   SYS"GPIO_WriteMode",Out%(J%),1
 1060 NEXT
 1070
 1080 ENDPROC
 1090

 


Back to the interface section 
 

Back to the Roe Valley index page
  Return sign