Pi width modulation

Using a Raspberry Pi to control a DC motor using PWM and a LDM12200 H-Bridge IC

The hardest part of this project was getting hold of a LMD18200 H-Bridge chip. I had to buy it in Hongkong and it took ages to finally get here. The LMD18200 has a peculiar pin-out and does not use a 0.1 inch distance between pins. Hence it won't directly fit onto a vero board or a standard bread-board. Be careful when fitting the chip. Bending the legs can easily break them off.

The actual circuit diagram is fairly straight forward and follows the recommendation in the data sheet for the chip. Be aware that the LMD18200 does not function at voltages much below 12V.


The h-bridege project circuit diagram
The pcb layout

The h-Bridge projectThe project can be admired on the right. The Raspberry Pi and the motor are resting on a rather ancient book, my interface board is to the top left and the PWM pcb to the right.

I have not made use of the current sensing output or the brake control which the chip provides. The circuit above with the layout on the right seems to work well and I had no problems, once one or two bugs were ironed out.


The BBC BASIC program

I wrote a very simple little program to use this board and was surprised how well it worked. You can study the program below. The heart of the algorithm used looks like this:

  330   SYS"GPIO_WriteData",10,0
  340   FOR J%=0 TO S%:NEXT
  350   SYS"GPIO_WriteData",10,1
  360   FOR J%=0 TO 30000-S%:NEXT

Lines 330 and 340 supply the ON period, lines 350 and 360 the OFF period. The total length of the loop is always 30 000. BBC Basic on the Raspberry Pi is so fast that these simple lines control the motor quite smoothly.

Below is a link to an archive containing the program and the draw file of the pcb layout.

Pi-WM.zip Version 1.0 November 2012

Here is a video of the project working


   10 REM Pi-width-modulation
   20 REM Trfy it, anyway
   30 REM (c) Jochen Lueg
   40 REM http://roevalley.com
   50 REM Limavady, November 2012
   60 REM Version Pi_1.0
   70 
   80 ON ERROR PRINT REPORT$;" at line ";ERL  : END
   90 
  100 
  110 OSCLI"RMensure GPIO 0.40 ERROR  Please install the GPIO module"
  120 PROCsetupGPIO
  130 
  140 ON ERROR PROCerror
  150 MODE 1280,720,32
  160 MOUSE OFF
  170 OFF
  180 PROCport_setup
  190 PROCinit
  191 PRINT'"    Pi Width Modulation"
  192 PRINT''
  200 PRINT' " B  - - - - - -   Back"
  210 PRINT  " F  - - - - - -   Forward"
  220 PRINT  " S  - - - - - -   Stop"
  230 PRINT  " >  - - - - - -   Faster"
  240 PRINT  " <  - - - - - -   Slower"
  241 SYS"GPIO_WriteData",10,1:REPEAT UNTIL INKEY(-104)
  242 PRINT'" Press '>' to start"
  250 REPEAT
  260   IF INKEY(-101)SYS"GPIO_WriteData",9,0
  270   IF INKEY(-68) SYS"GPIO_WriteData",9,1
  280   IF INKEY(-82) SYS"GPIO_WriteData",10,1:REPEAT UNTIL INKEY(-104)
  290   IF INKEY(-104) S%+=10:IF S%>30000 S%=30000
  300   IF INKEY(-103) S%-=10:IF S%<0 S%=0
  310   PRINTTAB(5,15)"Speed: ";S%DIV100;"     "
  320   
  330   SYS"GPIO_WriteData",10,0
  340   FOR J%=0 TO S%:NEXT
  350   SYS"GPIO_WriteData",10,1
  360   FOR J%=0 TO 30000-S%:NEXT
  370   
  380 UNTIL FALSE
  390 SYS"GPIO_WriteData",10,1
  420 *QUIT
  430 END
  510 DEFPROCsetupGPIO
  520 REMSYS"GPIO_EnableI2C",0
  530 SYS"GPIO_ExpAsGPIO",2
  540 ENDPROC
  550 
  560 
  590 DEFPROCport_setup
  591 VDU 28,3,45,40,20
  610 COLOUR 132
  620 CLS
  630 MOUSE OFF
  640 ENDPROC
  650 
  660 
  670 DEFPROCinit
  680 Dir%=9
  690 PWM%=10
  691 S%=0
  700 ENDPROC
  710 
  720 
  730 DEFPROCerror
  740 CLS
  750 PRINT REPORT$;" at line ";ERL
  760 END
  770 ENDROC


Return to the interfacing index

Back to the Limavady home page

Tudor with sign