using multiple pins as PWM pin with D32 pro

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
miririe
Posts: 7
Joined: Fri Nov 23, 2018 4:36 pm

using multiple pins as PWM pin with D32 pro

Post by miririe » Thu Apr 04, 2019 1:20 pm

Hey everyone,
I am trying to run two stepper motors with an ESP 32 (D32 pro). It has micropython 1.10 on it.
First I tried the two motors seperately, so I initialized motor1 with a pin for the steps (pin 19) using machine.PWM and one for the direction (pin 27). The motor runs perfectly. The same I did for motor2, just used pin 18 (step) and 12 (direction).

Then I tried to use both motors, each with their own PWM pin and I really do not understand, what happened:
When giving some frequency to one of the step pins, directly both motors started to turn with that frequency. This always happens, when initializing both PWM pins (of course they have different names!)

Is there anything which I have to take care when using machine.PWM and want to use several pins? Can I use several pins with PWM and give them different frequencies?

I would be greatful for help!

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: using multiple pins as PWM pin with D32 pro

Post by mattyt » Thu Apr 04, 2019 9:41 pm

Try initialializing the PWM pins with different freq values (pass freq to PWM() init). Will explain later!

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: using multiple pins as PWM pin with D32 pro

Post by OutoftheBOTS_ » Thu Apr 04, 2019 9:51 pm

Not sure which port of Micro-Python your using??

The low level of PWM timers work like this: they are counters, you set how high the count up to the ARR(the FREQ) and you set how high the count gets to before switching the pin the CCR (the duty width). Most timers have many channels i.e they will have 1 ARR but can have many CCR this means the 1 timer can generate many PWMs of same FREQ but with different duty widths. If you need 2 PWM with different FREQ then you must use 2 different timers not 2 different channles of the same timer :)

There isn't muc docs for ESP32 offical port atm but there is some docs fro ESP32 Lobo port and on Lobo port it support 4 timers with 8 channels on each timer see https://github.com/loboris/MicroPython_ ... o/wiki/pwm

I have some experience with using stepper motors in micro-python see my cube solving robot https://www.youtube.com/watch?v=sD4bG8hPYLQ&t=194s

Although as you can see I managed to make the stepper motors work well in Micropython including smooth ramp up and ramp down with accurate speed and rotation distance ti isn't very easy or overly viable in MP compared to doing it in C.

Using PWM means that you won't get a ramp up or ramp down or be able to control number of steps (distance traveled)

Usually stepper motor control is done by timers with hardware interrupts were instead of the timer generating a PWM it fires an interrupt and in the interrupt it pulses the step pin and adds up the count and adjust the ARR (FREQ) for the ramp-up/ramp-down timing. This isn't possible to do in MP because the interrupt calls a python function which has a latency of about 10ms which is slower than the step FREQ needed for stepper motors

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: using multiple pins as PWM pin with D32 pro

Post by mattyt » Fri Apr 05, 2019 11:37 pm

Sorry miririe my initial post was terse and misleading.

To answer your question, I believe the answer is no, you can't use different PWM frequencies with MicroPython today. You can use different duties however.

MicroPython's PWM is built on the Espressif ESP-IDF LED Control. Summarising the system:
  • Timers provide a frequency. There are four timers.
  • Channels connect a timer to one or more pins. They allow you to set the duty. There are eight channels.
MicroPython will allocate a new channel for each PWM object but, currently, only one timer is used. So only one frequency.

Note that there's also a relationship between the base frequency chosen and the resolution - or number of steps - that duty can be set to. Higher frequencies have less resolution. The timer manages that constraint.

To make the PWM system more useful - to allow multiple PWM frequencies - we need to allow multiple timers to be configured. I'll take a look in to how best this could be implemented (PWM in the Loboris port introduces a timer ID but that introduces incompatibilities with other ports).

OutoftheBOTS points generally still stand, PWM isn't great for stepper control though it's useful to get them spinning! The RMT module is actually more interesting to me for the kind of motor control he refers to - it allows accurate 'replay' of a sequence of bits. We don't yet have RMT support in MicroPython but it has the potential to improve stepper control significantly...

miririe
Posts: 7
Joined: Fri Nov 23, 2018 4:36 pm

Re: using multiple pins as PWM pin with D32 pro

Post by miririe » Tue Apr 09, 2019 10:12 pm

Hey,
thanks a lot for your answers! Of course I was not happy about my mistake in thinking it could be easy to use different pwm frequencies... I am completely new to all this, so I hope it is okay, if I ask some more (maybe a bit basic) questions.

OutOfTheBots, are you using different speeds for your different motors in your robots? And if yes, how are you achieving it?

I did not try this untill now, but can I use the LoBo micropython on the esp as well?

For my setup I just need to get the motors spinning, but I want to controll its rotational speed. So no need for position controll but different frequencies would be great... For accelerating and decelerating the motor I simply used a loop increased the pwm frequency in certain steps and time intervalls.

Best, Miriam

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: using multiple pins as PWM pin with D32 pro

Post by OutoftheBOTS_ » Wed Apr 10, 2019 7:05 am

My stepper motor control is a bit more complex as I use the methods used by CNC firmware to achieve smooth acceleration and precise step counts.

From Matty post above it looks like main stream Micro-python doesn't allow for different freq of PWM just different duty widths on the same freq as it only uses 1 timer.

I usually use Lobo port for ESP32 projects as it has a number of features not yet in main stream MP for ESP32. Lobo port for ESP32 can use all 4 timers so it allows to you have up to 4 different freq of PWM.

It is a little unusual to be using stepper motors when your not concerned about precise position control. Usually if you just need a reasonable constant speed then this is quite often achieved by using a DC motor with an encoder and feed back loop as DC motors are so much more efficient than stepper motors.

Do you want to tell me a little more about your project?

miririe
Posts: 7
Joined: Fri Nov 23, 2018 4:36 pm

Re: using multiple pins as PWM pin with D32 pro

Post by miririe » Wed Apr 10, 2019 3:49 pm

Hello,
I got micropython by LoBo running on my ESP and now I can controll 4 motors. Better than just controlling the speed of one motor. Will see whether I can manage working with this...
Thanks for your hints!

Post Reply