You of course don’t need to use exactly the same equipment I’m using, but for full transparency (and Amazon referral links to help cover hosting costs), here’s the bill of materials to replicate my setup exactly.
- 30V 5A Bench Power Supply
- LM2596 Buck Converter
- ZS-H3 DC Motor Control (Spec Sheet)
- Generic 12V Motor
- Assorted Jumper Wires
The Motor Controller
The ZS-H3 is a cheap little dual DC motor driver with impressive stats for its size and cost. It can handle 7.5-40V for motor drive, sustaining 2.5A per channel and peaking at 3.5A per channel. My eventual plan is to use this to drive the impeller and traction motors on my hacked up Maytronics Dolphin. Hopefully the peak current is enough since space is at a premium in the water-tight control box and I haven’t found anything else that can handle 30V and several amps in this small of a form-factor. At any rate, let’s not get bogged down in that project just yet. For today, I just want to show how to connect the motor drive to a Raspberry Pi and do some simple control with python.
With all credit to WiringPi, here’s what the pinout of the Raspberry Pi looks like if you stand it up with the ethernet and USB jacks on the bottom:
|17||0||GPIO. 0||IN||0||11||12||0||IN||GPIO. 1||1||18|
|22||3||GPIO. 3||IN||0||15||16||0||IN||GPIO. 4||4||23|
For this project, I’ve set my bench supply to 12V, which is the motor drive voltage. To power the Pi, I’m using the buck converter to tap 5V off of the main supply. If you’re just playing around on your bench, you can power the Pi any way you like. I’m doing it this way as once I shove this all into the Dolphin, my only power source is 30V.
|Buck Converter||Raspberry Pi Physical Pin|
The ZS-H3 pinout is a little simpler:
|A2||Counter-Clockwise Input for Motor A|
|A1||Clockwise Input for Motor A|
|B2||Counter-Clockwise Input for Motor B|
|B1||Clockwise Input for Motor B|
To drive the motors, just send a PWM signal on the pin for the motor and direction you want to go. The duty cycle of the PWM signal should be proportional to the speed you want the motor to go.
If you light up both the CW and CCW pins of a single motor at the same time, the controller will act as an electric brake.
The ZS-H3 can officially read control PWM signal ranging from 500Hz-30KHz, but it appears to handle anything lower as well. So far as I can tell, the ZS-H3 does not intelligently set a voltage level output to the motor based on the PWM input. Rather, it toggles the full motor output voltage on and off following the PWM signal.
Connecting the ZS-H3 to the Pi:
|ZS-H3||Raspberry Pi Physical Pin|
Once you’re wired up, you can play around with WiringPi’s
gpio on the command line, but dorking around with python is more valuable for my Dolphin project, so that’s the route I went:
#!/usr/bin/python import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) cw = 16 ccw = 11 frequency = 50 direction = cw GPIO.setup(cw, GPIO.OUT) GPIO.setup(ccw, GPIO.OUT) pwm = GPIO.PWM(direction, frequency) pwm.start(10) while True: speed = input("Speed? (0-100) ") if speed == 0: pwm.stop() if direction == cw: direction = ccw else: direction = cw pwm = GPIO.PWM(direction, frequency) time.sleep(1) pwm.start(10) else: pwm.ChangeDutyCycle(speed)
This little script prompts you for a speed (0-100%) and starts Motor B spinning at that speed. If you give a speed of 0, it stops the motor, waits a second to make sure the motor stops and we don’t get any interesting back-current effects, reverses direction, then starts up slowly in the other direction.