I skimmed Ref 9 and found it surprisingly complicated, because of its very lower level API and macros. An example is that it have macros to configure the deadtime of switching two half H-bridges, and also inputting motor speed by Hall sensors, and syncing two PWM operators for fault.
But I found two reasons that I should be use esp32’s mcpwm: (1) They are too lowlevel, so may be good one is developing DC/BLDC motor drivers using two barebone half H-bridges, or power MSOFET current switches.
(2) The whole mcpwm is designed for BLDC/DC motors, nothing is considered with stepper motor in mind. In short, esp32 is “useless” for stepper motors. (3) And if we are using stepper motor drivers such as tmc2130/2160/2208/2209/2225, all the esp32 mspcm tricks are actually handled in hardware/firmware, so should be more time/compute efficient.
Conclusion, I will forget esp32 mcpwm and go direct use tmc2225 as planned.
Though I am not using mcpwm, I still need to create some PWM signals, perhaps using a library of something. So I am skimming Ref 8 to steal or borrow some ideas.
1 hour later…
tlfong01
tlfong01
6932
Sun 21:45
So after my disappointment to mcpwm, I went reading Ref 8 from Electronics Hub. I had a pleasant surprise: I learnt that, beside mcpwm, there is another pwm for LED control, called ledC. The tutorial summarize the following interesting points and APIs.
A couple of interesting points about LED PWM Controller in ESP32.
16 independent PWM Channels, divided into group of two with 8 channels per group.
Programmable resolution between 1-bit and 16-bits.
Frequency of the PWM wave depends on the resolution of PWM.
Automatically increases / decreases duty cycle without processor intervention.
A list of all the LEDC APIs exposed by the driver. These functions are written for Arduino IDE port of ESP32.
ledcSetup(channel, frequency, resolution_bits)
ledcAttachPin(pin, channel)
ledcWrite(channel, dutycycle)
ledcRead(channel)
ledcWriteTone(channel, frequency)
ledcWriteNote(channel, note, octave)
ledcReadFreq(channel)
ledcDetachPin(pin)
My quick and dirty conclusion is that this ledC is very good to create step pulses for the tmc2225 step motor controller.
Talk is cheap. Tomorrow I will try to show how to use esp32 ledC wpm controller to control tmc2225 to drive a step motor.
Bed time. See you tomorrow.
17 hours later…
tlfong01
tlfong01
6932
Mon 15:11
So I just picked up my tmc2225 toys this morning.
user image
4 hours later…
tlfong01
tlfong01
6932
Mon 19:00
Now I am setting up Rpi Pico + PCF8574 IOx hardware to test tmc2225.
user image
user image
1 hour later…
tlfong01
tlfong01
6932
Mon 20:01
Now the tmc2225 module pinout
user image
2 hours later…
tlfong01
tlfong01
6932
Mon 21:55
The setting up and testing was smooth, except one troubleshooting trick.
tmc2225 test 2022feb1401►
16 hours later…
tlfong01
tlfong01
6932
Tue 13:31
The troubleshooting trick I referred above is how to start the motor moving, IF you find the motor started trembling, got stuck, and could not move by hand. This is stepper motor newbie FAQ. The trick I use is to manually scan/sweep the PWM frequency, using the mks testing tool or xy-lpwm. It took me a couple of seconds to find that the initial freq setting of 5kHz is not appropriate for my motor and microstepping frequency resolution. I found a good frequency to start is around 3kHz.
I guess I need also to check out if 3kHz is OK for my stepping motor, or a range of motors. So I decided to use a standard 3D printer as a bench mark or case study, so newbies can use a similar setup like mine, if their motor trembles and get stuck. My benchmarking or calibrating motor is the following:
(removed)
user image
I am thinking of also using another popular hobbyist toy stepper motor 28byj18 as an example.
1 hour later…
tlfong01
tlfong01
6932
Tue 14:53
17HS8401 NEMA17 42 Stepper Motor 48mm 42BYGH 1.7A 4-lead for 3D printer CNC (80deg Max) – Usongshine AliExpress US$6 (26/5pcs)
aliexpress.com/item/…
(removed)
user image
Now the spec says no load start freq – 1900 pps/Hz, and Holding torque = 4k8 NM. And I should set motor current to 1.5A/phase.
tlfong01
tlfong01
Tue 15:13
@kyrpav I am testing 17H8401, 1.5A 480NM, max temp 80 deg. If your motor is 17H4401, then you should not set 1.5A, or torque not strong enough and motor too hot.
kyrpav
kyrpav
943
Tue 15:38
from the two pins that are uart int tcm2225 which one is tx which one is rx? and what is the vm pin
can you also tell me where to find all these blue breadboards and the distantiators that you have?
20 hours later…
tlfong01
tlfong01
6932
Wed 11:14
@kyrpav I got most of the prototyping boards from TaoBao, some blue, some green, some are 5 hole joint, some 3 joint. I guess you can also find them in AliEx. References below:
TaoBao 洞洞板,實驗板 (Hole Hole Board, Prototyping Board)
dzyj.world.taobao.com/…
(see full text)
tlfong01
tlfong01
6932
Wed 11:25
I usually 5 joint boards like (1) below:
(1) 5连孔单面喷锡板2.54/5.08MM间距 10*15CM万能板实验板全玻纤蓝油 – ¥3.80
item.taobao.com/…
(2) PCB电路板双面喷锡绿油玻纤FR4实验板万能板 28 37 46 57 915 item.taobao.com/… (3) below is also my favourite: (3) 2连孔单面喷锡板2.54MM间距 79CM万能板实验板 全玻纤蓝油 ¥1.90
item.taobao.com/…
2 hours later…
tlfong01
tlfong01
6932
Wed 13:44
@kyrpav Ah, yes, the tmc2208/2209/2225 uart pins are confusing. You may like to skim my answer to the RpiSE forum’s 2208 question to get a rough idea of the tmc’s strange uart interface:
Rpi Pico MicroPythpn Controlling TMC2208 / TMC2209 / TMC2225 Stepper Motor Driver Problem – 2021dec27 Viewed 533 times
raspberrypi.stackexchange.com/…
3 hours later…
tlfong01
tlfong01
6932
Wed 17:00
Now I have setup the second tmc2225 for basic testing, using XY-LPWM sig gen as step pulse input, and both tmc2225 in standalone mode, no uart control.
The motor spec says min pulse rate is 1k7 pps. But I found that if I set micro stepping to 1/4 resolution, I can lower the freq to as low as 20pps, both motor still move slowly. The max pps is around 4kHz, which the motor starts trembling violently, and not moving at all.
So the operating range is about 20 pps to 3k pps.
Next is to test the Vref, Diag pins, internal current sense resistance, and finally uart config and control.
tmc2225 test 2022feb1601►
tlfong01
tlfong01
Wed 17:31
@kyrpav What do you mean by “distantiators”? Do you mean the DuPont pinstripes and connectors?
tlfong01
tlfong01
6932
Wed 17:47
These breadboard jumpers or connectors:
(1) item.taobao.com/…
(2) dzyj.world.taobao.com/…
You can usually get these cheapy TaoBao stuff from AliEx, but not vice versa. 🙂
tlfong01
tlfong01
6932
Wed 18:23
Now I am measuring the tmc2225 Vref and Diag pins’ output voltages:
tmc2225 Datasheet Section 2.2 Signal Descriptions TMC2225
VREF 12 AI Analog reference voltage for current scaling or reference current for
use of internal [current] sense resistors (optional mode)
DIAG 18 DO Diagnostic output. Hi level upon driver error. Reset by ENN=high.
I found Vref == 08.2V (It should be factory setting, and I could not find an appropriate Philips screw driver to adjust the trim pot.
I found Diag pin’s voltage 0V, which means “no error”.
2 hours later…
tlfong01
tlfong01
6932
Wed 20:09
Now I am using xy-lpwm to input 2kHz step pulses to the motor controller, and display the motor encoder pulse output waveform.
user image
user image
user image
Now I always dodgy calculations. 🙂
Calculation
- XY-LPWM sig gen frequency = 2 kHz = 2000 step pulses per second
- Stepper Motor = 20 encoder pulses/revolution
- Stepper Motor Speed = (2000 step pulses/sec) / (20 encoder pulses/revolution) = 100 rps
user image 16 hours later…
tlfong01
tlfong01
6932
yst 12:09
Now I am setting up Pico’s uart0 and uart1 to config the two tmc2225 drivers. The first thing I always do is to do uart loopback, to make sure that the uart port and setup and initialized correctly. The test function and sample outputs are listed below.
*** UART Test Functions ***
printProgramName()
testUartLoopBack(uartPortNum = 0, writeBytes = ‘uart0 Hello World’)
testUartLoopBack(uartPortNum = 1, writeBytes = ‘uart1 Hello World’)
*** Sample output ***
”’
%Run -c $EDITOR_CONTENT
*** Program Name = tmc2225test60.py Programmer = tlfong01 Date = 2022feb17 ***
Begin testUartLoopBack(), ...
uartPortNum = 0
writeBytes = uart0 Hello World
readBytes = b'uart0 Hello World'
End testUartLoopBack().
(see full text)
And the complete listing of the tmc2225 test program with the sample output is list below:
tmc2225test60.py complete listing and sample output penzu.com/p/2a15c22d
tlfong01
tlfong01
6932
yst 12:27
tmc2225 uart config/diagnostics testing setup:
user image
Now I am checking the wiring of the tmc2225 module, making sure the module has 1k resistor already connecting the mdoule’s TxD and RxD pins.
user image
2 hours later…
tlfong01
tlfong01
6932
yst 14:13
Now You need to skim the following product sheet to understand why tmc2225 uart is so confusing.
BigTreeTech TMC2225 UART Stepper Motor – USS$8.85
fr.aliexpress.com/item/…
Pay particular attention to the section with 3 figures, as shown below:
user image
tlfong01
tlfong01
6932
yst 14:29
What confuses everybody is the productsheet’s naming of the two UART protocols as
UART Mode and Tx/Rx Mode
“UART Mode” is used when the MPU is write only, using one wire, the Tx wire to talk or broadcast all (can be as many as 4, 8, or 16, say) the tmc2225 modules at the same time, configuring all modules alike.
If the mpu wants to config different modules with different config, or wants to read the status (diagnostics) of an individual module, address by the m1, m1 hardware pins for up to 4 modules (or more than 4 with external address decoding hardware), using both Tx and Rx wires (with 1k resistor connected TxD to Rxd) Jumper R3 at the lower left corner of the bottom side of the PCB), as shown in Fig 4.1 above, or the photo below.
(removed)
user image
tlfong01
tlfong01
yst 14:58
You might like to read this TB6600 Q&A about PID: electronics.stackexchange.com/…
2 hours later…
tlfong01
tlfong01
6932
yst 16:30
So I have soldered a 1k resistor across the R3 jumper at the bottom of the tmc2225 board. It took me about an hour to solder two AWG30 wire wrapping wire to the jumper and soldered the 1k resistor.
user image
Next step is the use the Rpi Pico MicroPython UART port to talk to the tmc2225board.
2 hours later…
tlfong01
tlfong01
6932
yst 18:28
But I need to first use Pico to move mother, then perhaps then use pico uart to change m1, m1, microstepping resolution, to see if motor speed changes. I also forgot to mention that I don’t need to use any Pico PWM pin to replace xy-lpwm sig gen, I can just toggle/blink a GPIO pin as step pulse, to move the motor. The following code is an example. One thing I am not sure is that if I can use very low step rate, such as 10pps, as the OP’s demo code.
Rpi Pico MicroPythpn Controlling TMC2208 / TMC2209 / TMC2225 Stepper Motor Driver Problem – Asked 2021dec27, Viewed 553 times
https://raspberrypi.stackexchange.com/questions/135191/rpi-pico-micropythpn-controlling-tmc2208-tmc2209-tmc2225-stepper-motor-drive
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)
(see full text)
13 hours later…
kyrpav
kyrpav
7:50
@tlfong01 i found the spacers
2 hours later…
tlfong01
tlfong01
9:34
@kyrpav Ah, you mean the “distantiators”!
6 hours later…
tlfong01
tlfong01
6932
15:11
Now I have started using Rpi Pico MicroPython to do the tmc2225 testing. First thing is to use Pico PWM pin to create 1kHz 50% duty cycle to replace the hardware PWM sig gen xy-lpwm3. The PWM signal creation is a simple as three lines of code:
gpPwmPin28 = PWM(Pin(28))
gpPwmPin28.freq(oneKHz)
gpPwmPin28.duty_u16(int(65535/2)) #dutyCycle 50%
(removed)
(removed)
The complete listing of the program is as below (the pwm signal creation is at lines 134~136)
tmc2225test68.py = penzu.com/p/de67d7f3
I used the scope to display the Rpi Pico PWM signal and found it OK:
user image
I replaced the xy-lpwm signal by the pico pwm and found both tmc2225 controlled stepping motor moves smoothly and quietly.
tlfong01
tlfong01
15:37
Another question on PID: electronics.stackexchange.com/…. Can you let me know if you are using a similar cct to to do PID of motor torque, instead of motor position?
6 hours later…
tlfong01
tlfong01
6932
21:16
Now I have written a micropython function to use GP pin#22 to toggle High/Low every millisecond, thus creating a step/pulse rate of 1,000 pulses per second (pps). I used this step pulse to input to the tmc2225 driver to verify can also smoothly move the motor, same as the xy-pwm sig gen or pcio pwm pin in earlier experiments.
The scope screen capture and complete listing of the micropython code is geven below.
user image
tmc2225test70.py tlfong01 2022feb18hkt2109 = penzu.com/p/8a33dd91
*** 1 kHz (1k step pps) function definition ***
def toggleGpOutPin1kHz(gpOutPinNum):
print(‘Begin toggleGpOutPin1kHz’)
toggleGpOutPin(gpOutPinNum, highTime = halfMilliSecond, lowTime = halfMilliSecond, repeatTimes = once, totalToggleTimes = tenMillionTimes)
print(‘End toggleGpOutPin1kHz’)
return
*** Sample Test ***
toggleGpOutPin1kHz(gpOutPinNum = 22)
Categories: Uncategorized