Rpi Pico MicroPythpn Controlling TMC2208 / TMC2209 Stepper Motor Driver Problem
Ask QuestionAsked 11 days agoActive todayViewed 167 times11
I am new to using stepper motors and micropython. I am having trouble getting my stepper motor to turn. I am using a raspberry pi pico and a tmc 2208 driver to control my stepper motor. I can hear the coils being powered on and off, but I get no motion.
Here is my script
from machine import Pin
import utime
direction = Pin(14, Pin.OUT)
step = Pin(15, Pin.OUT)
enable = Pin(13, Pin.OUT)
enable.low()
direction.high()
def stepOne():
step.high()
utime.sleep(0.001)
step.low()
while True:
stepOne()
utime.sleep(0.1)
pi-picoShareEditFollowCloseFlagedited Dec 27 ’21 at 13:24tlfong014,07233 gold badges88 silver badges2222 bronze badgesasked Dec 27 ’21 at 0:18Tristan Verduzco2122 bronze badges
- 2how much time is
utime.sleep(0.001)
? – jsotola Dec 27 ’21 at 0:23 - 1Nice picture but it does not help me much. A schematic showing all connections including power, power source, etc would help a lot. Post a link to the hardware items that has technical information. I will guess and it sounds like you are short on power, voltage and or current. This can be confirmed with more information. – Gil Dec 27 ’21 at 1:32
- (1) First thing first, the TMC2208datasheet: trinamic.com/fileadmin/assets/Products/ICs_Documents/… – tlfong01 Dec 27 ’21 at 4:42
- (2) Next, a quick and dirty summary: 2-phase stepper, 32 uSteps, 36VDC, internal current sensing, hard/soft(uart) config, … – tlfong01 Dec 27 ’21 at 4:50
- 1Need a clear photo showing the connections. – joan Dec 27 ’21 at 10:15
Add a comment | Show 7 more commentsStart a bounty
1 Answer
(1) First thing first, the TMC2208datasheet: trinamic.com/fileadmin/assets/Products/ICs_Documents/… – tlfong01
(2) Next, a quick and dirty summary: 2-phase stepper, 32 uSteps, 36VDC, internal current sensing, hard/soft(uart) config, … –
(3) Get a couple of assembled modules: pt.aliexpress.com/item/… –
(4) I am too lazy to upload pics in Imrur and link them here. So I cheat by using my answer space.
(5) Skim the module user guide.



(6) Show the Pico + TMC2208 Wiring/Schematic.

(7) Compare and contrast the wiring with a similar and already debugged project, say the very popular A4899, to make sure no important wiring is missing.
A4899 Microstepping Motor Driver Troubleshooting – EESE Q&A 2021apr14
(8) Manual testing, using (a) Manaul jumper wiring, (b) manual configurable PWM sig gen.
A4899 Stepping Motor Driver Test

/ to continue, …
(9) Software testing, using Pico.
For software testing and evaluation, I would recommend the updated version TMC2209, with uart interface.
(Ref 21) TMC2209 stepper Motor Driver Module – TaoBao ¥18

(10) TMC2209 Programming Notes

The OP’s original program
Now I am looking at the OP’s program, thinking that perhaps I can repeat running his program in my Pico.
from machine import Pin
import utime
direction = Pin(14, Pin.OUT)
step = Pin(15, Pin.OUT)
enable = Pin(13, Pin.OUT)
enable.low()
direction.high()
def stepOne():
step.high()
utime.sleep(0.001)
step.low()
while True:
stepOne()
utime.sleep(0.1)
My program v0.1
I am adding comments to make my version 1.
# *** TMC2209 Test Program v0.1 tlfong01 2022jan02hkt1126 ***
# *** Import ***
from machine import Pin
import utime
direction = Pin(14, Pin.OUT)
step = Pin(15, Pin.OUT)
enable = Pin(13, Pin.OUT)
enable.low()
direction.high()
def stepOne():
step.high()
utime.sleep(0.001)
step.low()
while True:
stepOne()
utime.sleep(0.1)
/ to continue, …
References
(11) A4988 Datasheet – Allergo
(12) A4988 Stepper Motor Driver Module – AliExpress US$0.6
(13) Fake microstepping in A4899 and LV8729 stepper-drivers? – @GnuReligion, EESE, Asked 2021apr14
(15) What is better for precision? Stepper motor VS DC Motor [Closed] – EESE 2021apr13
(17) Youtube video of A4988 Full Step Performance – tlfong01 2021apr18
(18) RpiMotorLib python library to drive NEMA 17 stepper for A4988 – DiyProjects 2021feb09
(21) TMC2209 stepper Motor Driver Module – TaoBao ¥18
(22) TMC2209 Stepper Motor Driver Module User Guide – Components101
(23) TMC2209 Stepper Motor Driver Datasheet – Trinamic
(24) Pico PWM Primer – TK Hareendran, codrey 2021sep21
(25) TMC2209 v1.2 Manual – BigTreeTech
Appendices
Appendix A – Pico PWM as a clock to TMC2208/2209 Stepper Motor Driver
(Ref 24) Pico PWM Primer – TK Hareendran, codrey 2021sep21


TMC2209 PWM Pin Assignment
--------------------------
Pico p26/G20 ~ p35/Aref
pin assignment v0.1
tlfong01 2021dec29hkt1526
--------------------------
ylw G20 p26 PWM[A2]
grn G21 p27 PWM[B2]
blk Gnd p28 Gnd
org Run p29 Run
org G22 p30 PWM[A3]
ylw G26 p31 PWM[A5]
grn G27 p32 PWM[B5]
gry Agnd p33 AGnd
blu G28 p34 PWM[A6]
brn Aref p35 Aref
--------------------------

TMC2209 Setup Notes Part 1



Using XY-LPWM3 sig gen to do manual (offline, no Pico yet) TMC2209 testing

Bipolar Stepper Motor Driver for refreshing memory
(Ref) Bipolar Stepper Motors: What is it? (Circuit And Sequence) – Electrical4U 2020dec27
(Ref) How does a Stepper Motor work? – 3,752,110 views Oct 19, 2016, 57k like, Lesics, 4.97M subscribers

Reading summary
(1) The DC current in a winding needs to be reversed to reverse a magnetic pole and allow the motor to function. Typically a H-bridge arrangement is used. The most common H Bridge IC used in most Bipolar stepper interfacing projects is L293D.
(2) A bipolar stepper motor has one winding per stator phase. A two phase bipolar stepper motor will have 4 leads.
(3) There are three different ways in which we can drive the bipolar stepper motor:
(a) Only one of the phase winding is energized at a time. That is, either AB or CD is energized.
(b) Both the phases are activated at the same time. The rotor will align itself between two poles. This arrangement will give higher holding torque than the previous method.
(c) Half stepping, used generally to improve the stepping angle. Here,
step 1, only 1 phase is ON, then in
step 2, 2 phases are ON,
then again, only one phase is ON,
and the sequence continues.
TMC2209 Stepper Motor Driver Driving NEMA17 Motor Notes
Now I am using the following NEMA17 42BYG 3D printer stepper motor (Ref 1)to do the testing.
Ref 2: A4899 Microstepping Driver Testing – 2021apr14

Testing TMC2209 with HK42BYG250-001 NEMA17 Stepper Motor OK
Youtube Video of HK42BYG250-001 NEMA17 Stepper Motor Testing Results

Schematic of TMC2209 Testing: Step 1 – Offline/No Pico, Step 2 On Line/with Pico

Step 1 – Offline/No Pico testing setup

Step 2 – Online/Pico Micropython Software Testing
So the time has come to start Pico MicroPython Programming. The first step is to assign Pico GP pins for controlling the TMC2209 Step motor controller.
We will need to use
(1) Pico uart0 to talk to the TMC2209 Tx, Rx,
(2) 6 GP pins (GP 20, 21, 22, 26, 27, 28) to talk to TMC2209 and the Motor rotary encoder. (Note – This is temporary pin assignment. Later we will use IO expanders such a MCP23x17 to create more GPIO pins (for multiple motor controllers etc, to avoid runing out of Pcio GP pins sooner or later)

TMC2209 Test Program V0.8 – Blink Control Pins
Now I have written a little microPython program to blink all the TMC2209 Control Pins.
# *** tmc2209test08.py tlfong01 2022jan02hkt2116 ***
programName = 'tmc2209test07.py'
# ========= ========= ========= ========= ========= ========= ========= =========
# Contents
# 1. System Config
# 2. Import Modules
# 3. System Utility Functions/Constants
# 4. Pico
# 4.1 Pico GPConfig (GPIO, PWM, UART)
# 4.2 Pico GP Functions
# 5. TMC2209
# 5.1 TMC2209 Config
# 5.2 TMC2209 Functions
# ========= ========= ========= ========= ========= ========= ========= =========
# 1. System Config
# Thonny 3.3.13, Windows 10 (64-bit), Python 3.7.9, Tk 8.6.9, MicropPython
# (Raspberry Pi Pico), USB Port (COM3)
# ========= ========= ========= ========= ========= ========= ========= =========
# 2. Import Modules
from machine import Pin, PWM, UART
import utime
# ========= ========= ========= ========= ========= ========= ========= =========
# 3. System Functions/Constants
# Repeat Times
oneTime = 1
twice = 2
fourTimes = 4
# Seconds Constants
oneMilliSecond = 0.001
oneTenthSecond = 0.1
quarterSecond = 0.25
halfSecond = 0.5
oneSecond = 1
# Calander Time
monthNameDict = \
{'Month' : {'1': 'Jan', '11': 'nov', '12': 'dec',},
}
timeNow = utime.localtime()
timeNowShort = str(timeNow[0]) + monthNameDict['Month'][str(timeNow[1])] + str(timeNow[2]) + 'hkt' + str(timeNow[3]) + ':' + str(timeNow[4])
def printProgramNameTimeNow():
print('Program Name =', programName, 'Time Now =', timeNowShort)
print('')
return
# Time Functions ***
def pause(pauseTime):
utime.sleep(pauseTime)
return
# ========= ========= ========= ========= ========= ========= ========= =========
# 3. Pico
# *** Pico Config ***
# *** Pico GP Output Pin Assignment ***
gpOutPin20 = Pin(20, Pin.OUT)
gpOutPin21 = Pin(21, Pin.OUT)
gpOutPin22 = Pin(22, Pin.OUT)
gpOutPin26 = Pin(26, Pin.OUT)
gpOutPin27 = Pin(27, Pin.OUT)
# *** Pico GP PWM Pin Assignment ***
gpPwmPin28 = PWM(Pin(28))
gpPwmPin28.freq(1000) # 1kHz
gpPwmPin28.duty_u16(int(65535/2)) #dutyCycle 50%
# *** Pico UART Config ***
uart0 = UART(0, baudrate = 9600, tx=Pin(0), rx=Pin(1))
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Pico GP Out/PWM Pin Functions ***
def setGpOutPinHigh(gpOutPin):
gpOutPin.high()
return
def setGpOutPinLow(gpOutPin):
gpOutPin.low()
return
def toggleGpOutPin(gpOutPin, highSeconds, lowSeconds, toggleCount):
for count in range(toggleCount):
print(' count =', count)
setGpOutPinHigh(gpOutPin)
pause(highSeconds)
setGpOutPinLow(gpOutPin)
pause(lowSeconds)
return
def toggleGpOutPinList(gpOutPinList, highSeconds, lowSeconds, toggleCount):
for gpOutPin in gpOutPinList:
toggleGpOutPin(gpOutPin, highSeconds, lowSeconds, toggleCount)
return
def testToggleGpOutPin(gpOutPin):
#print('Begin testToggleGpOutPin(), ...')
toggleGpOutPin(gpOutPin20, quarterSecond, quarterSecond, fourTimes)
#print('End testToggleGpOutPin().')
return
def testToggleGpOutPinList(gpOutPinList):
print('Begin testToggleGpOutPinList(), ...')
toggleGpOutPinList(gpOutPinList, quarterSecond, quarterSecond, twice)
print('End testToggleGpOutPinList().')
return
# *** Sample Test ***
#testToggleGpOutPin(gpOutPin20)
#testToggleGpOutPinList([gpOutPin20, gpOutPin21, gpOutPin26])
# ========= ========= ========= ========= ========= ========= ========= =========
# 4. TMC2209 Config/Functions
tmc2209EnblPin = gpOutPin20
tmc2209DirPin = gpOutPin21
tmc2209StepPin = gpOutPin22
tmc2209Ms1Pin = gpOutPin26
tmc2209Ms2Pin = gpOutPin27
tmc2209PwmPin = gpPwmPin28
def testBlinkTmc2209ControlPins():
testToggleGpOutPinList([tmc2209EnblPin, tmc2209DirPin, tmc2209StepPin, tmc2209Ms1Pin, tmc2209Ms2Pin])
return
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Main ***
printProgramNameTimeNow()
#testToggleGpOutPin(gpOutPin20)
#testToggleGpOutPinList([gpOutPin20, gpOutPin21, gpOutPin26])
#testToggleGpOutPinList([tmc2209EnblPin, tmc2209DirPin, tmc2209StepPin, tmc2209Ms1Pin, tmc2209Ms2Pin])
testBlinkTmc2209ControlPins()
# *** End ***
'''
Sample Output
>>> %Run -c $EDITOR_CONTENT
Program Name = tmc2209test07.py Time Now = 2022Jan2hkt21:18
Begin testToggleGpOutPinList(), ...
count = 0
count = 1
count = 0
count = 1
count = 0
count = 1
count = 0
count = 1
count = 0
count = 1
End testToggleGpOutPinList().
>>>
End of Sample Output
'''
Testing Pico PWM 1 kHz 50% dutyCycle perofrmance
I replace the XY-LPWM3 1kHz control signal by the Pico PWM signal and found that the motor turns slightly jerking. I also found that motor turns fro PWM signal range in 500Hz to kKHz. Too low frequency motor jerks, too high frequency, eg, at 10kHz, motor trembles and does not move.

Testing TMC2209 Control Pins OK
I have debugged the functions to set High/Low the TMC2209 control pins.
# *** tmc2209test12.py tlfong01 2022jan04hkt2138 ***
programName = 'tmc2209test12.py'
# ========= ========= ========= ========= ========= ========= ========= =========
# Contents
# 1. System Config
# 2. Import Modules
# 3. System Utility Functions/Constants
# 4. Pico
# 4.1 Pico GPConfig (GPIO, PWM, UART)
# 4.2 Pico GP Functions
# 5. TMC2209
# 5.1 TMC2209 Config
# 5.2 TMC2209 Functions
# ========= ========= ========= ========= ========= ========= ========= =========
# 1. System Config
# Thonny 3.3.13, Windows 10 (64-bit), Python 3.7.9, Tk 8.6.9, MicropPython
# (Raspberry Pi Pico), USB Port (COM3)
# ========= ========= ========= ========= ========= ========= ========= =========
# 2. Import Modules
from machine import Pin, PWM, UART
import utime
# ========= ========= ========= ========= ========= ========= ========= =========
# 3. System Functions/Constants
# Repeat Times
oneTime = 1
twice = 2
fourTimes = 4
tenTimes = 10
tenMillionTimes = 10000000
# Seconds Constants
halfMilliSecond = 0.0005
oneMilliSecond = 0.001
oneTenthSecond = 0.1
quarterSecond = 0.25
halfSecond = 0.5
oneSecond = 1
# Calander Time
monthNameDict = \
{'Month' : {'1': 'Jan', '11': 'nov', '12': 'dec',},
}
timeNow = utime.localtime()
timeNowShort = str(timeNow[0]) + monthNameDict['Month'][str(timeNow[1])] + str(timeNow[2]) + 'hkt' + str(timeNow[3]) + ':' + str(timeNow[4])
def printProgramNameTimeNow():
print('Program Name =', programName, 'Time Now =', timeNowShort)
print('')
return
# Time Functions ***
def pause(pauseTime):
utime.sleep(pauseTime)
return
# ========= ========= ========= ========= ========= ========= ========= =========
# 3. Pico
# *** Pico Config ***
# *** Pico GP Output Pin Assignment ***
gpOutPin20 = Pin(20, Pin.OUT)
gpOutPin21 = Pin(21, Pin.OUT)
gpOutPin22 = Pin(22, Pin.OUT)
gpOutPin26 = Pin(26, Pin.OUT)
gpOutPin27 = Pin(27, Pin.OUT)
# *** Pico GP PWM Pin Assignment ***
#gpPwmPin28 = PWM(Pin(28))
#gpPwmPin28.freq(1000) # 10kHz jerking, 1~5kHz slight jerking
#gpPwmPin28.duty_u16(int(65535/2)) #dutyCycle 50%
gpOutPin28 = Pin(28, Pin.OUT)
gpOutPin28.low()
# *** Pico UART Config ***
uart0 = UART(0, baudrate = 9600, tx=Pin(0), rx=Pin(1))
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Pico GP Out/PWM Pin Functions ***
def setGpOutPinHigh(gpOutPin):
gpOutPin.high()
return
def setGpOutPinLow(gpOutPin):
gpOutPin.low()
return
def toggleGpOutPin(gpOutPin, highSeconds, lowSeconds, toggleCount):
for count in range(toggleCount):
#print(' count =', count)
setGpOutPinHigh(gpOutPin)
pause(highSeconds)
setGpOutPinLow(gpOutPin)
pause(lowSeconds)
return
def toggleGpOutPinList(gpOutPinList, highSeconds, lowSeconds, toggleCount):
for gpOutPin in gpOutPinList:
toggleGpOutPin(gpOutPin, highSeconds, lowSeconds, toggleCount)
return
def testToggleGpOutPinV01(gpOutPin):
#print('Begin testToggleGpOutPin(), ...')
toggleGpOutPin(gpOutPin20, oneSecond, oneSecond, tenTimes)
#print('End testToggleGpOutPin().')
return
def testToggleGpOutPinV02(gpOutPin):
#print('Begin testToggleGpOutPin(), ...')
toggleGpOutPin(gpOutPin21, oneMilliSecond, oneMilliSecond, tenMillionTimes)
#toggleGpOutPin(gpOutPin21, halfSecond, halfSecond, tenMillionTimes)
#print('End testToggleGpOutPin().')
return
def testToggleGpOutPinV03(gpOutPin):
print('Begin testToggleGpOutPin(), ...')
toggleGpOutPin(gpOutPin20, halfSecond, halfSecond, tenMillionTimes)
print('End testToggleGpOutPin().')
return
def testToggleGpOutPinList(gpOutPinList):
print(' Begin testToggleGpOutPinList(), ...')
toggleGpOutPinList(gpOutPinList, quarterSecond, quarterSecond, twice)
print(' End testToggleGpOutPinList().')
return
# *** Sample Test ***
#testToggleGpOutPinV01(gpOutPin20)
#testToggleGpOutPinV02(gpOutPin21)
#testToggleGpOutPinV03(gpOutPin21)
#testToggleGpOutPinList([gpOutPin20, gpOutPin21, gpOutPin26])
# ========= ========= ========= ========= ========= ========= ========= =========
# 4. TMC2209 Config/Functions
tmc2209EnblPin = gpOutPin20
tmc2209DirPin = gpOutPin21
tmc2209StepPin = gpOutPin22
tmc2209Ms1Pin = gpOutPin26
tmc2209Ms2Pin = gpOutPin27
#tmc2209PwmPin = gpPwmPin28
def testBlinkTmc2209ControlPins():
print(' Begin testBlinkTmx2209ControlPins(), ...')
testToggleGpOutPinList([tmc2209EnblPin, tmc2209DirPin, tmc2209StepPin, tmc2209Ms1Pin, tmc2209Ms2Pin])
print(' End testBlinkTmx2209ControlPins(), ...')
return
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Old Main Test Programs ***
#testToggleGpOutPinV03(gpOutPin20)
#testToggleGpOutPinList([gpOutPin20, gpOutPin21, gpOutPin26])
#testToggleGpOutPinList([tmc2209EnblPin, tmc2209DirPin, tmc2209StepPin, tmc2209Ms1Pin, tmc2209Ms2Pin])
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Main ***
printProgramNameTimeNow()
testBlinkTmc2209ControlPins()
# *** End ***
'''
Sample Output
>>> %Run -c $EDITOR_CONTENT
Program Name = tmc2209test11.py Time Now = 2022Jan4hkt21:42
Begin testBlinkTmx2209ControlPins(), ...
Begin testToggleGpOutPinList(), ...
End testToggleGpOutPinList().
End testBlinkTmx2209ControlPins(), ...
>>>
'''
Next step is to write functions to control the motor start, stop, step, direction, clock, microstep setting etc. The last step is to use UART to control the TMC2209 driver. I searched the SO/SE forums and found useful references, but sadly on Arduino based libraries. So I need to write/DIY Pico MicroPython TMC2209 library on my own.
I found the following references useful:
(5) Upgrading RAMPS 1.4 With TMC2130 Stepper Drivers – Core3D in Workshop3D Printing, 71,591 views
(6) TMC2209 stepper motor module datasheet – GitHub
(7) TMC2209 stepper-motor-driver-for-3d-printer-board module Product sheet – BiQu
(8) TMC2209 Stepper Motor Driver ICdatasheet – Trinamic
(9) AccelStepper library for Arduino – AirSpayce
(10) Generate stepper-motor speed profiles in real time – David Austin
Shortening my TLDR Answer
My answer is getting TLDR and editing it s very this TLDR answer very slow. So I am thinking of using GitHub, GitFile or Penzu to speed up my answer editing job. Stay tuned,. 🙂
tmc2209test14.py complete program listing with sample output
tmc2209test14.py sample run YouTube video

Now I can move motor fast or slow, CCW or CW, any numbers of steps, with this function:
def testTmc2209V02():
print('Begin testTmc2209V02(), ...')
initTmc2209V02() # init motor
tmcStartMotor(enblPin) # start motor
tmcSendStepsPause(stepPin, oneMilliSecond, oneThousandTimes, halfSecond) # move CCW fast, 1000 steps, pause half second
tmcSendStepsPause(stepPin, oneMilliSecond, fiveHundredTimes, halfSecond) # move CCW fast, 500 steps, pause half second
tmcSendStepsPause(stepPin, oneMilliSecond, oneThousandTimes, halfSecond) # move CCW fast, 1000 steps, pause half second
tmcCwMotor(dirPin) # change direction to CW
tmcSendStepsPause(stepPin, twoMilliSeconds, oneThousandTimes, halfSecond) # move CW slow, 1000 steps, pause half second
tmcOneSixteenthStep(ms1Pin, ms2Pin) # change to 1/16 microstep
tmcSendStepsPause(stepPin, twoMilliSeconds, oneThousandTimes, halfSecond) # move CW slow, half step, 1000 steps, pause half second
tmcStopMotor(enblPin)
print('End testTmc2209TestV02().')
return
tmc2209test19 – The complete program listing

Configuring and Testing TMC2209 using UART interface
Now that I found it OK to use the couple of control lines to test the TMC2209, next step is to replace the messy control line by UART interface. The first thing to do is to setup and test the uart0 and uart1 for loopback, to make sure uart hardware and software setup are OK. The debugged test program is listed below.
tmctestv21.py (uart0, uart1 loopback with sample output)
Next step is to read/write tmc2209 control/status registers.

/ to continue, …ShareEditDeleteFlagedited 6 mins agoanswered Dec 27 ’21 at 5:13tlfong014,07233 gold badges88 silver badges2222 bronze badges
- 1Thanks for the detailed help! – Tristan Verduzco Dec 31 ’21 at 2:48
- Thanks a lot for your kindness and support, I am hoping to keep up my newbie friendly problem based learning (PBL) on smart stepper motor, steadily though slowly. Stay tuned. 🙂 Cheers. – tlfong01 Dec 31 ’21 at 4:11
- 1Really awesome answer! – guidoism Dec 31 ’21 at 17:59
- @guidoism, Thanks you for your nice words. Cheers. – tlfong01 Jan 1 at 7:16
Add a commentAdd Another Answer
Not the answer you’re looking for? Browse other questions tagged pi-pico or ask your own question.
Categories: Uncategorized