- changed status to open
How to implement ledcWrite in MicroBlocks to control motor speed?
When writing a driver for a TB6612 motor driver board, I need a PWM signal to be introduced to control the motor speed. In the arduino example, we use ledcWrite to control the motor speed.
The example code is as follows:
//ledcSetup(pwmChannel, pwmFrequency, pwmResolution);
ledcSetup(0, 5000, 8);
ledcAttachPin(25, 0);
ledcWrite(0, dutyCycle);
//dutyCycle from 0 to 255 to control the mortor speed
I try to use set-pin block, set pin25 value from 1 to 1023. But only when the set value is greater than 750 does the motor start, and at a very fast speed! This is not the same as what is happened in the arduino code.
Comments (7)
-
repo owner -
repo owner Interesting!
MicroBlocks also uses ledc to implement PWM. The main difference is the frequency. MicroBlocks is using 100 Hz while the Arduino code is using 5000 Hz.
I'm guessing that the 100 Hz PWM signal is interacting badly with the armature of the DC motor. Is there anything unusual about the motor you are controlling?
As an experiment, you might try changing your Arduino code to use 100 Hz:
//ledcSetup(pwmChannel, pwmFrequency, pwmResolution); ledcSetup(0, 100, 8);
Does that give similar behavior? That is, does the motor not start until you get up to about 75% of the PWM range (i.e. about 192) and then start fast?
If it does, then the frequency is probably the issue and I will look into increasing the MicroBlocks PWM frequency. (I want to keep the same 0-1023 range though, since that is a standard across all MicroBlocks boards.)
-
reporter No, changing Arduino code to use 100 Hz didn’t cause the issue with MicroBlocks . The motor still started from 70 in slow speed.
-
repo owner Oh, now I see the issue! It is because you are using pin 25.
ESP32 pins 25 and 26 are DAC (digital-analog converter) pins. In MicroBlocks, when you you set one of those pins to a number between 0 and 1023 you will get a steady analog voltage between 0 and 3.3 volts proportional to the number you set. MicroBlocks does not output a PWM signal those pins.
Try using a different ESP32 output pin. I'm guessing you'll see the expected motor behavior.
-
repo owner I'm glad you included the Arduino code so I could see that you were using pin 25!!
-
reporter cool, will change the pin and try again.
Thanks John. -
repo owner - changed status to resolved
Marking this resolved, but feel free to leave additional comments.
- Log in to comment