PWM activating multiple motors at once

Hi all,

I’ve tried searching for a solution to my problem but was unable to find one.

I am currently working with Zerynth and a DOIT ESP32 DevKit V1. I have included my code below. I am trying to activate only one motor at a time using PWM, setting the duty to 0 when a motor should not be activated. The problem is I took this straight from a working project, and now this section of the PWM-related code isn’t working.

For example, when I activate Motor 1, I can confirm via print statements that only the duty for one motor is being set at a time, and all others are 0. When I set the duty for Motor 1, this is the case. However, if I then set the duty for Motor 2, Motor 1 continues working AND Motor 2 is activated, even though the duty for Motor 1 should be set to 0. When I set all motors to 0, they turn off in unison as expected. However, when I try to activate Motor 2 again, Motor 2 and Motor 1 turn on together again. It seems they are somehow tied together even though I can confirm that this portion of the code works perfectly when tested in a different environment. I’m wondering if there’s some issue with the PWM module that I’m not aware of? Any help would be much appreciated.

import pwm
...

forward_pin = D18.PWM
left_pin = D19.PWM
back_pin = D22.PWM
right_pin = D23.PWM

pinMode(forward_pin, OUTPUT)
pinMode(left_pin, OUTPUT)
pinMode(back_pin, OUTPUT)
pinMode(right_pin, OUTPUT)

forward = 0
left = 0
back = 0
right = 0

# MICROS so every sec is 1000000 of micros
frequency = 100
period = 1000000//frequency

...

def set_PWM(direction, range):
    # Range comes in 0, 1, 2, or 3
    if range == 0: # Set duty to 0
        duty = 0
    else: # Set duty to period/3, period/2, or 1
        duty = period//range

    if direction == 1: # Motor 1
        forward = duty
        left = 0
        back = 0
        right = 0
    elif direction == 2: # Motor 2
        forward = 0
        left = duty
        back = 0
        right = 0
    elif direction == 3: # Motor 3
        forward = 0
        left = 0
        back = duty
        right = 0
    elif direction == 4: # Motor 4
        forward = 0
        left = 0
        back = 0
        right = duty
    else: # Set all to 0
        forward = 0
        left = 0
        back = 0
        right = 0
    
    print('\nForward ')
    print(forward)
    print('Left')
    print(left)
    print('Back')
    print(back)
    print('Right')
    print(right)

    pwm.write(forward_pin, period, forward, MICROS)
    pwm.write(left_pin, period, left, MICROS)
    pwm.write(back_pin, period, back, MICROS)
    pwm.write(right_pin, period, right, MICROS)

Hi @Grant_Stankaitis
I suggest you start from identifying the difference between the working code that you took this algorithm from and this code.
But for instance, could you switch the pins? if you switch the pins of the motors, does the same behavior occur on the same pins or the same motors?

Hey @karimhamdy1
Thanks for helping out. I did a thorough comparison of this code and the working code that I took it from, and they are functionally identical. The only difference is I had to switch out MicroPython’s PWM module for Zerynth’s PWM module. This is what makes me think it is related to Zerynth’s PWM module. Is it possible to import the MicroPython ‘machine’ module to test this?

I did try to switch the pins of the motors and it still exhibits the same behavior of activating multiple motors at once. I switched the pins both connecting to the ESP32 and switched the pins in the code. Either way, it still turns on the motors sequentially as if the previous motor was not deactivated.

I was unable to resolve the problem after continued testing. I returned back to uPyCraft IDE with MicroPython and the PWM was activating properly when sending the values using the Nordic UART service via BLE. I’m bummed that I wasn’t able to use Zerynth for this. Still not sure what the issue is, but hopefully in the future this will get worked out.

Hi @Grant_Stankaitis
I am sorry that you had some problems with the PWM implementation.
I think there might be some differences between Micropython PWM and Zerynth PWM.
Porting codes from one platform to another is tricky.
We’ll test the PWM on the ESP32 and bug fixes will be rolling in the coming updates.