Kikinaak
Posts: 7
Joined: Wed Dec 07, 2016 3:12 pm

Hardware PWM pins on pi zero W?

Sun Oct 13, 2019 3:04 am

I have a simple H bridge motor controller (MX1508 for the curious). 4 PWM inputs to control two motors. I'm trying to run the thing off a pi zero W, and I would like to play around with hardware PWM which documentation says the pi zero W can swap between two pairs of pins.

I am aware of software and DMA options for doing PWM, but those can be assigned to any pin and code is easy to rewrite if I bork something. Soldering, not so much. So my focus for the moment is on getting the wiring right to open up my options. I intend to tinker around and learn with all three methods before I am done.

IF I am understanding the documentation correctly, putting pins 12 and 32 to in1 and in2 on the controller will let me swap hardware PWM0 between them, and pins 33 and 35 to in3 and in4 will let me swap hardware PWM1 between them. This should let me use those two channels to control direction and speed on my motors since only one input per motor needs to be active at once. Can someone confirm I've got this much right before I go melting conductors together?

User avatar
joan
Posts: 14251
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Hardware PWM pins on pi zero W?

Sun Oct 13, 2019 5:31 am

There may be four accessible GPIO, but there are only two channels. GPIO 12/18 are connected to one channel, GPIO 13/19 to the other.

If you want different PWM to two inputs you will need different channels to those inputs, e.g. IN1/IN2 should be GPIO 12/13, IN3/IN4 should be GPIO 18/19. You would then have to switch modes on each pair of GPIO as you swapped motors.

Are you sure you need PWM on two inputs? E.g. the L298N has enable inputs per motor (ENA for motor 1, ENB for motor 2) which is the preferred pin for motor speed control.

User avatar
HermannSW
Posts: 1482
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Hardware PWM pins on pi zero W?

Sun Oct 13, 2019 6:22 am

The mx1508 module seems to have less control pins than the TB6612fng module I use (both modules available for <1$ on aliexpress.com with free shipping):
Image
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://gitlab.freedesktop.org/HermannSW/gst-template
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

User avatar
joan
Posts: 14251
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Hardware PWM pins on pi zero W?

Sun Oct 13, 2019 8:28 am

i misunderstood your post (thinking you only wanted to control one motor at a time),

Yes, use PWM channel 0 on one motor (GPIO 12/18) and PWM channel 1 on the other (GPIO 13/19).

However, as suggested, it might be better to choose a controller with an enable/PWM pin per motor.

Kikinaak
Posts: 7
Joined: Wed Dec 07, 2016 3:12 pm

Re: Hardware PWM pins on pi zero W?

Sun Oct 13, 2019 9:18 am

Thanks Joan. Yes, I'm running a pair of motors in a very tiny little robot.
I actually ordered what claimed to be a pack of L298n off amazon, but what I got was a pack of MX1508, exactly the board pictured by HermannSW. Never heard of this TB6612fng before but its not whats on my desk, so working with what I've got. Possibly a blessing as its a much smaller board than the L298n and fits much better in the aforementioned tiny bot. I'm still new on motor controls, but I'm not seeing the benefit to the extra step of an enable pin.

What I was originally looking at for HW PWM is here
https://www.codecubix.eu/linux/hardware ... y-pi-zero/
The efficiency on cpu cycles and greater precision over software are what attracts me to explore this option.

Do I actually need this benefit over software control? Maybe not, but this whole thing is mostly a learning project to get my feet wet on motor control, so wiring to give myself access to all options to play with seemed the best idea. I was just looking for a more experienced set of eyes on it in case I was missing something with the wiring. Releasing the magic smoke is not the learning experience I am after.

User avatar
HermannSW
Posts: 1482
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Hardware PWM pins on pi zero W?

Sun Oct 13, 2019 12:06 pm

Kikinaak wrote:
Sun Oct 13, 2019 9:18 am
Releasing the magic smoke is not the learning experience I am after.
I once experienced 2cm high flames out of a L293D motor controller ;-)
https://forum.arduino.cc/index.php?topi ... msg2437692
Image

Do I actually need this benefit over software control?
I doubt, since you control the motor voltage for speed, from 0% to 100% only.
That can be done without hardware PWM.

I do need hardware PWM (on GPIO13) for generating flashes at µs precision for my diy highspeed flash.

This script does that:
https://github.com/Hermann-SW/Raspberry ... ls/pwm_ges

I had to start pigpiod with "-s 1" although Joan does not recommend that, but unfortunately the precision is lost with default 5µs refresh:
https://www.raspberrypi.org/forums/view ... 9#p1540885
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://gitlab.freedesktop.org/HermannSW/gst-template
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

User avatar
joan
Posts: 14251
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Hardware PWM pins on pi zero W?

Sun Oct 13, 2019 12:16 pm

I would be quite happy to use software timed PWM for brushed DC motor speed control. Any timing jitter would likely be smoothed by the system's inertia.

I would try to avoid software timing for servo control and LED dimming.

Return to “Automation, sensing and robotics”