Create a PWM library that allows control of PWM frequency

Issue #206 resolved
Simon Walters created an issue

I’m working on a motor controller board (4tronix DriveBit) which uses pins12/13 for an H-bridge motor controller

I used top set of blocks to prove all working digitally and that worked fine

I switched to using PWM blocks to control speed - that all worked fine :)

But when I tried top blocks again - no response :(

Can you either make them reset back to plain digital mode when used or provide a block (in advanced mode maybe?) to reset a pin back to plain on/off mode

Comments (9)

  1. John Maloney repo owner

    Unfortunately, the Arudino framework that we use does not provide a way to turn off PWM once it has been started. I wish it did! It seems as though they expect users simply restart their Arduino program in order to stop PWM.

    A workaround is to press the reset button on the board. The will do a hardware reset and the IDE should reconnect to the board in a few seconds (except in the browser, where you need to manually reconnect).

    For your motor control application, you can treat 0 as off. That will allow you to set one pin of the H bridge to 0 and control speed by setting the other pin to a value between 0 and 1023. To reverse the motor, swap the roles of the two pins.

    One more option is to write your own PWM loop using “set digital pin” blocks and the “wait _ microseconds” block. That way, you never start the hardware PWM at all.

    Hope that helps!

  2. Simon Walters reporter

    “A workaround is to press the reset button on the board. The will do a hardware reset and the IDE should reconnect to the board in a few seconds (except in the browser, where you need to manually reconnect).”

    🙂 I hadn’t thought of that - I was closing and restarting Microblocks 🙂

    “One more option is to write your own PWM loop using “set digital pin” blocks and the “wait _ microseconds” block. That way, you never start the hardware PWM at all.”

    I was wanting to do that as the PWM frequency is too high to get decent torque at low motor speeds but I wanted to easily swap between methods when testing

    c’est la vie 🙂

  3. John Maloney repo owner

    I don’t have much experience with DC motors. It makes sense that a lower PWM frequency might result in more torque at low motor speeds. Let me know if that works and, if so, what a good PWM frequency is. I’m guessing that you could go as low as 10 or 20 Hz and still get a reasonably constant motor speed.

  4. Simon Walters reporter

    I use quite agressive values (which results in jerkiness at very low motor speeds - but does mean wheels do turn 🙂 My algo from ScratchGPIO on Pi is max(5,abs(value/2)) where max value is +/- 100 - so frequency goes from 1 to 50 Hz but minimum of 5Hz. My mate Gareth who run 4tronix and makes the DriveBit controller I’m trying out uses this in his makecode extension

    if (speed < 200)

    ‌ pins.analogSetPeriod(AnalogPin.P12, 60000);

    ‌ else if (speed < 300)

    ‌ pins.analogSetPeriod(AnalogPin.P12, 40000);

    ‌ else

    ‌ pins.analogSetPeriod(AnalogPin.P12, 30000);

    which I think translates to 16->33Hz

    Of course, if you use these sort of values to dim LEDs then you get a lot of flickering so I use different values for them.

    If you could provide access to changing the PWM freq/period that would get me around my issue

  5. John Maloney repo owner

    One way to provide this feature would be as a PWM library implemented in MicroBlocks itself. For low PWM frequencies (16->33Hz), that should be totally feasible and it has the additional benefit that it would not require a reset to get the output pins out of PWM mode. Will change this to a feature request.

  6. John Maloney repo owner

    Another trick I've seen used for DC motors at low speeds is to start with a short, full-power burst, then switch to the desired speed. Once the motor is moving it doesn't take as much power to keep it moving.

  7. John Maloney repo owner

    Won't fix. One can implement low-speed PWM in MicroBlocks itself or use other techniques to work with DC motors at low speeds.

  8. Log in to comment