This project uses two IRF 630 MOS-FET transistors for pulse width modulation and two relays to change the direction of the two motors. . The circuit diagram is below. The two IRF 630 MOS-FETs are connected to the GPIO 15 and 18 pins of the Pi. GPIO 24 and 23 are used to switch the relays. As the RISC OS version of the Raspberry Pi does not have a PWM output (as yet), the signals at GPIO 15 and GPIO 18 need to be pulsed by the control program.
A suggested pcb layout is below right. It need hardly be stated that the relays and the motor have the same voltage rating, in this case between 6 and 8V. The four diodes control the back emf of the relays and the motor. They should be type 1N4148 or similar signal diodes.
The entire project is on the right. The two motors are hidden by the two big red wheels,k the Pi is to the left of the motors and my 5 in 16 out interface is on the left. The dual pwm circuit board is at the front of the picture..
The program
The program works surprisingly well and allows the speed of both motors to be regulated very smoothly. The heart of the algorithm is below.
550 DEFPROCpulse(P1%,P2%)
560 LOCAL N%,M%,J%
570 IF Go1%=0 P1%=99
580 IF Go2%=0 P2%=99
590 FOR J%=1 TO 100
600 IF J%<P1% N%=1 ELSE N%=0
610 IF J%<P2% M%=1 ELSE M%=0
620 SYS&58F81,23,N%
630 SYS&58F81,24,M%
640 NEXT
650 ENDPROC
For more detail: Dual DC motor control using pwm with the Raspberry Pi Schematic