Rpi Pico MicroPythpn Controlling TMC2208 / TMC2209 Stepper Motor Driver Problem
Ask QuestionAsked 6 days agoActive todayViewed 110 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-picoShareEditFollowClose 2Flagedited Dec 27 ’21 at 13:24tlfong014,05433 gold badges88 silver badges2222 bronze badgesasked Dec 27 ’21 at 0:18Tristan Verduzco2122 bronze badges New contributor
- 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
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

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
'''
/ to continue, …ShareEditDeleteFlagedited 33 secs agoanswered Dec 27 ’21 at 5:13tlfong014,05433 gold badges88 silver badges2222 bronze badges
- 1Thanks for the detailed help! – Tristan Verduzco 2 days ago
- 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 2 days ago
- 1Really awesome answer! – guidoism yesterday
- @guidoism, Thanks you for your nice words. Cheers. – tlfong01 yesterday
Categories: Uncategorized