Motor control using PWM with the K8055 and K8061 boards
This project turned out to be easier than expected. The circuit
diagram is below. An IRF 630 MOS-FET is connected to the PWM1 output of
the Velleman board. The waveform at this output turns the transistor on
and off as it rises and falls. As the average ON and OFF time changes,
so does the speed of the motor.
A relay is used to control the direction of the motor. This is
connected to the number 8 output of the K8055 USB board. The circuit
diagram and a suggested PCB layout can be admired below. |
![]() | ![]() |
![]() The programK8055
To produce a pulse modulated waveform on the K8055, one simply
writes to the D to A converter. This produced a voltage at the DAC
output and the corresponding waveform at the PWM output. Note that
writing 255 produces a minimum and 0 a maximum waveform, so don't be
surprised when your motor starts turning the minute the board is
connected. |
10 20 REM K8055_SinglePWM 30 REM Control one dc motor using pulse width modulation with the Velleman K8055 USB interface 40 REM This version uses a reed switch for speed feedback 50 REM Version 2.0 60 REM Jochen Lueg 70 REM http://roevalley.com 80 REM April 2012 90 100 PROCK8055_init 110 SYS K8055_CloseDevice%,0 120 SYS K8055_OpenDevice%, 0 130 140 ON ERROR PROCerror 150 PROCinit 160 PROCwindow(650,300,"Motor control with feedback") 170 *FONT Lucida Console,14,2 180 OFF 190 SYS K8055_OutputAnalogChannel%,1,255 200 PRINT 210 PRINT " Left - Stop - Right . . . . Z - X - C" 220 PRINT " Faster - slower . . . . < - >" 230 PRINT " Speed count On - Off . . . . . F - G" 240 PRINT 250 PRINT 260 PRINT " Leave the program . . . . . . Q" 270 TIME=0 280 SYS K8055_ResetCounter%,1 290 REPEAT 300 Key$=INKEY$(0) 310 REPEAT UNTIL INKEY(0)=-1 320 IF (Key$="z" OR Key$="Z") AND (Direction$="Right" OR State$="Stopped") PROCleft 330 IF (Key$="c" OR Key$="C") AND (Direction$="Left " OR State$="Stopped") PROCright 340 IF (Key$="x" OR Key$="X") AND State$="Running" PROCstop 350 IF (Key$="f" OR Key$="F") THEN SpeedCount$="Start" 360 IF Key$="g" OR Key$="G" THEN SpeedCount$="Stop" : PRINTTAB(6,12)" " 370 IF Key$="," OR Key$="<" THEN 380 RunningSpeed%+=1 390 IF RunningSpeed%>255 RunningSpeed%=255 400 SYS K8055_OutputAnalogChannel%,1,RunningSpeed% 410 StartSpeed%=RunningSpeed% 420 ENDIF 430 IF Key$="." OR Key$=">" THEN 440 RunningSpeed%-=1 450 IF RunningSpeed%<0 RunningSpeed%=0 460 SYS K8055_OutputAnalogChannel%,1,RunningSpeed% 470 StartSpeed%=RunningSpeed% 480 REPEAT UNTIL INKEY(0)=-1 490 ENDIF 500 PROCfeedback(RunningSpeed%) 510 IF SpeedCount$="Start" AND TIME>=500 THEN 520 SYS K8055_ReadCounter%,1 TO SpeedCount% 530 PRINTTAB(6,12);SpeedCount%;" rev/5sec = ";SpeedCount%*12;" rev/min " 540 TIME=0 : SYS K8055_ResetCounter%,1 550 ENDIF 560 UNTIL Key$="q" OR Key$="Q" 570 PROCstop 580 SYS K8055_ClearAllDigital% 590 SYS K8055_OutputAnalogChannel%,1,255 600 SYS K8055_CloseDevice% 610 SYS "FreeLibrary",K8055_Board% 620 *QUIT 630 END 640 650 660 DEFPROCfeedback(Speed%) 670 LOCAL S% 680 S%=(255-Speed%)/2.55 690 PRINTTAB(6,10)State$;" "Direction$;" PWM: ";S%;"% " 700 ENDPROC 710 720 730 DEFPROCspeed_feedback 740 REM Alternative detyection. Not used at the moment. 750 SYS K8055_ReadDigitalChannel%,5 TO A% 760 IF Flag%=0 AND A%=1 SpeedCount%+=1:Flag%=1 770 IF Flag%=1 AND A%=0 Flag%=0 780 IF TIME>=500 PRINTTAB(6,12);SpeedCount%;" rev/5sec = ";SpeedCount%*12;" rev/min ":TIME=0:SpeedCount%=0 790 ENDPROC 800 810 820 DEFPROCright 830 PROCstop 840 SYS K8055_SetDigitalChannel%,8 850 PROCstart 860 Direction$="Right" 870 ENDPROC 880 890 900 DEFPROCleft 910 PROCstop 920 SYS K8055_ClearDigitalChannel%,8 930 PROCstart 940 Direction$="Left " 950 ENDPROC 960 970 980 DEFPROCstart 990 FOR J%= 255 TO StartSpeed% STEP-4 1000 SYS K8055_OutputAnalogChannel%,1,J% 1010 PROCfeedback(J%) 1020 NEXT 1030 RunningSpeed%=StartSpeed% 1040 State$="Running" 1050 ENDPROC 1060 1070 1080 DEFPROCstop 1090 FOR J%= RunningSpeed% TO 255 STEP 4 1100 SYS K8055_OutputAnalogChannel%,1,J% 1110 PROCfeedback(J%) 1120 NEXT 1130 SYS K8055_ClearDigitalChannel%,8 1140 State$="Stopped" 1150 RunningSpeed%=255 1160 ENDPROC 1170 1180 1190 DEFPROCinit 1200 RunningSpeed%=255 1210 StartSpeed%=100 1220 Direction$="Left " 1230 State$="Stopped" 1240 SpeedCount%=0 1250 Flag%=0 1260 SpeedCount$="Stop" 1270 ENDPROC 1280 1290 1300 DEFPROCwindow(WindowWidth%,WindowHeight%,WindowTitle$) 1310 MODE 30 1320 SYS "SetWindowPos",@hwnd%,0,0,0,WindowWidth%,WindowHeight%,6 1330 COLOUR 128 1340 CLS 1350 COLOUR 15 1360 VDU 26 1370 SYS "SetWindowText",@hwnd%,WindowTitle$ 1380 ENDPROC 1390 1400 1410 DEFPROCerror 1420 PRINT REPORT$;" at line ";ERL : 1430 SYS K8055_ClearAllDigital%,1 1440 SYS K8055_ClearAllAnalog%,1 1450 SYS K8055_CloseDevice%,1 1460 SYS "FreeLibrary",K8055_Board% 1470 END 1480 ENDPROC 1490 1500 1510 DEFPROCK8055_init 1520 REM These are all the system calls in the order found in the manual 1530 SYS"LoadLibrary","K8055D.dll" TO K8055_Board% 1540 SYS"GetProcAddress",K8055_Board%,"OpenDevice" TO K8055_OpenDevice% 1550 SYS"GetProcAddress",K8055_Board%,"CloseDevice" TO K8055_CloseDevice% 1560 SYS"GetProcAddress",K8055_Board%,"ReadAnalogChannel" TO K8055_ReadAnalogChannel% 1570 SYS"GetProcAddress",K8055_Board%,"ReadAllAnalog" TO K8055_ReadAllAnalog% 1580 SYS"GetProcAddress",K8055_Board%,"OutputAnalogChannel" TO K8055_OutputAnalogChannel% 1590 SYS"GetProcAddress",K8055_Board%,"OutputAllAnalog" TO K8055_OutputAllAnalog% 1600 SYS"GetProcAddress",K8055_Board%,"ClearAnalogChannel" TO K8055_ClearAnalogChannel% 1610 SYS"GetProcAddress",K8055_Board%,"ClearAllAnalog" TO K8055_ClearAllAnalog% 1620 SYS"GetProcAddress",K8055_Board%,"SetAnalogChannel" TO K8055_SetAnalogChannel% 1630 SYS"GetProcAddress",K8055_Board%,"SetAllAnalog" TO K8055_SetAllAnalog% 1640 SYS"GetProcAddress",K8055_Board%,"WriteAllDigital" TO K8055_WriteAllDigital% 1650 SYS"GetProcAddress",K8055_Board%,"ClearDigitalChannel" TO K8055_ClearDigitalChannel% 1660 SYS"GetProcAddress",K8055_Board%,"ClearAllDigital" TO K8055_ClearAllDigital% 1670 SYS"GetProcAddress",K8055_Board%,"SetDigitalChannel" TO K8055_SetDigitalChannel% 1680 SYS"GetProcAddress",K8055_Board%,"SetAllDigital" TO K8055_SetAllDigital% 1690 SYS"GetProcAddress",K8055_Board%,"ReadDigitalChannel" TO K8055_ReadDigitalChannel% 1700 SYS"GetProcAddress",K8055_Board%,"ReadAllDigital" TO K8055_ReadAllDigital% 1710 SYS"GetProcAddress",K8055_Board%,"ResetCounter" TO K8055_ResetCounter% 1720 SYS"GetProcAddress",K8055_Board%,"ReadCounter" TO K8055_ReadCounter% 1730 SYS"GetProcAddress",K8055_Board%,"SedtCounterDebouceTime" TO K8055_SetCounterDebounceTime% 1740 ENDPROC |
Return
to the interfacing index Back to the Limavady home page ![]() |