MD13S Motor Driver - Pico¶
The Cytron MD13S is a driver board for brushed motors which can drive up to 13 Amps motor current from a 6-30VDC supply.
For electrical information, please see Cytron MD13S Motor Driver.
MD13S Demo¶
1# md13s_demo.py
2#
3# Demonstrate operation of one high-power DC motor using:
4#
5# Cytron Maker Pi Pico with Raspberry Pi Pico installed
6# Cytron MD13S motor driver on Grove 6 connector
7#
8# The MD13S connected via Grove 6 uses the following I/O:
9# GPIO26 -> PWM
10# GPIO26 -> DIR
11#
12
13################################################################################
14# print a banner as reminder of what code is loaded
15print("Starting md13s_demo script.")
16
17# load standard Python modules
18import time
19
20# load CircuitPython hardware modules
21import board
22import digitalio
23
24# load the local md13s module
25import md13s
26
27#--------------------------------------------------------------------------------
28# Create an object to represent the motor driver.
29print("Creating driver objects.")
30driver = md13s.MD13S(PWM=board.GP26, DIR=board.GP27)
31
32#--------------------------------------------------------------------------------
33# Begin the main processing loop.
34
35print("Starting main script.")
36
37# initial pause
38time.sleep(2.0)
39
40while True:
41 print("Starting motor test.")
42 driver.write(1.0)
43 time.sleep(2.0)
44
45 driver.write(0.0)
46 time.sleep(2.0)
47
48 driver.write(-1.0)
49 time.sleep(2.0)
50
51 driver.write(0.0)
52 time.sleep(2.0)
53
54 driver.write(0.5)
55 time.sleep(2.0)
56
57 driver.write(-0.5)
58 time.sleep(2.0)
59
60 print("Ramp test.")
61
62 for i in range(10):
63 driver.write(i*0.1)
64 time.sleep(0.5)
65
66 driver.write(0.0)
67 time.sleep(2.0)
68
69 for i in range(10):
70 driver.write(-i*0.1)
71 time.sleep(0.5)
72
73 driver.write(0.0)
74 time.sleep(2.0)
MD13S Module¶
1# md13s.py - driver module for the Cytron MD13S motor driver.
2
3
4# load CircuitPython hardware modules
5import board
6import pwmio
7import digitalio
8
9#--------------------------------------------------------------------------------
10# Class to represent a single H-bridge driver.
11
12class MD13S:
13 def __init__(self, PWM=board.GP0, DIR=board.GP1, pwm_rate=20000):
14
15 self.pwm_pin = pwmio.PWMOut(PWM, duty_cycle=0, frequency=pwm_rate)
16 self.dir_pin = digitalio.DigitalInOut(DIR)
17 self.dir_pin.direction = digitalio.Direction.OUTPUT
18 return
19
20 def write(self, rate, verbose=True):
21 """Set the speed and direction of the motor drive.
22
23 :param rate: modulation value between -1.0 and 1.0, full reverse to full forward."""
24
25 # convert the rate into a 16-bit fixed point integer
26 pwm = min(max(int(2**16 * abs(rate)), 0), 65535)
27
28 self.pwm_pin.duty_cycle = pwm
29
30 # this defines 'forward' as a CCW rotation as viewed from the shaft end
31 # (i.e., shaft right-hand rule), if the red motor lead is attached to MA
32 # and the black motor lead to MB.
33 self.dir_pin.value = (rate < 0)
34
35 if verbose:
36 print("Setting motor driver:", rate)
37
38 return