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

Using ENA or IN1/2 PWM for L289N motor speed control

Mon Oct 27, 2014 10:28 pm

You can control DC motor speed by applying a PWM signal to the controller inputs. PWM controls the speed by rapidly switching the motor on and off with different periods for on and off. The number of on/off cycles per second is known as the PWM frequency. The percentage of on time during each cycle is known as the duty cycle.

Some of the popular inexpensive L298N motor driver boards allow for two methods of providing the PWM - either via the enable pin or the input pins.

The advantages/disadvantages of using one method or the other is not clear (at least as far as my googling ability goes).
mot-pic-all.jpg
Overview
mot-pic-all.jpg (58.99 KiB) Viewed 15401 times
Anyhow I set up a test consisting of a Pi, L298N motor driver board, and a DC motor.
mot-pic-det.jpg
Magnets + Hall sensors
mot-pic-det.jpg (58.75 KiB) Viewed 15401 times
A plastic gear wheel was attached to the motor shaft. Two magnets were inserted in the gear wheel. Two were used simply for balance. A pair of digital Hall effect sensors were mounted to align with the magnets. One would have been enough but two allowed misalignments to be easily spotted.

In this arrangement each Hall sensor is pulsed twice per shaft revolution (once each time a magnet passes over the sensor).

The tests consisted of cycling through PWM update frequencies then driving the motor for 3 seconds at 100%, 95%, ..., 5% duty cycles. The number of pulses for each Hall sensor was recorded at the end of each 3 second period. Each test was carried out using enable PWM and then using input PWM. The whole series of tests were repeated 5 times.
mot-50-800.png
PWM 50, 400, 800 Hz
mot-50-800.png (29.75 KiB) Viewed 15401 times
A comparison of RPM achieved at PWM frequencies 50, 400, 800Hz using enable (ENA) or input (IN1) pins.

The chart shows pulses per 3 second interval on the vertical axis and duty cycle percentage on the horizontal axis.

Whether any conclusions can be drawn or not for practical motor usage I'm not so sure. Certainly using the enable pin for PWM performed better for a range of frequencies and duty cycles. But in effect these were extremely light load tests.

Miscellaneous

At peak the motor RPM was around 9000 (10 times the pulse rate, times 20 to go from 3 second to minutes and then divide by two pulses per revolution).

If you do anything similar make sure your magnets are secure. One of mine spun off and hasn't been seen since. I expect it's lodged in my brain like Homer Simpson's crayon.

webm
software, results, plots
Last edited by joan on Tue May 08, 2018 8:37 am, edited 1 time in total.

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Using ENA or IN1/2 PWM for L289N motor speed control

Tue Oct 28, 2014 12:06 am

Nice work, your graph gives the result I'd expect to see.

Enable pin PWM has the disadvantage of using more gpio to control the motor.

Input PWM has the disadvantage of applying brakingduring each off cycle.
Doug.
Building Management Systems Engineer.

sathish_swaminathan
Posts: 1
Joined: Mon Feb 09, 2015 5:59 am

Re: Using ENA or IN1/2 PWM for L289N motor speed control

Mon Feb 09, 2015 6:16 am

Hi,

I am trying to read the input from a single hall effect encoder. Instead of a single pulse, it seems to give a spurious signal for each rotation. My Rpi is not able to read the signals at such a high frequency. However I don't think the speed exceeds 9000 rpm. Can you please tell me how you managed to get the pulses corresponding to 9000 rpm.
I am using GPIO event detect (rising) to callback a function which implements a counter.

Thanks

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

Re: Using ENA or IN1/2 PWM for L289N motor speed control

Mon Feb 09, 2015 8:42 am

sathish_swaminathan wrote: ...

I am trying to read the input from a single hall effect encoder. Instead of a single pulse, it seems to give a spurious signal for each rotation. My Rpi is not able to read the signals at such a high frequency. However I don't think the speed exceeds 9000 rpm. Can you please tell me how you managed to get the pulses corresponding to 9000 rpm.
...
Are you using the software linked to in the first post?

Mark_T
Posts: 149
Joined: Sat Dec 27, 2014 10:54 am

Re: Using ENA or IN1/2 PWM for L289N motor speed control

Thu Feb 12, 2015 8:35 pm

Pulsing EN gives fast-decay mode, pulsing IN gives slow-decay mode.

With such low PWM frequency neither mode will have linear response, but
the L298 does slow-decay mode actively meaning it will be able to brake.

Raise the PWM to perhaps 16kHz and you'll find the slow-decay mode
(which is really then synchronous rectification) will be more linear. Small
motors usually need ultrasonic PWM, not 400Hz (more reasonable for
a hefty great motor with lots of inductance)

You can (with care and providing enough dead-time at cross over) also
drive INA/INB in antiphase, which is synchronous rectification across the
whole bridge, where 50% PWM means stationary and PWM can drive
the motor either way. Its important in this mode to use a high enough PWM freq
to keep the ripple current under control. Typically used for servo-systems
(its the same as class-D audio amplification).

Mats Van Gestel
Posts: 3
Joined: Sun Jun 21, 2015 10:19 am

Re: Using ENA or IN1/2 PWM for L289N motor speed control

Sun Jun 21, 2015 10:59 am

Hello!

Interesting job you did here, quite impressive!

I am attempting something similar, namely measuring the amount of times a bug is able to rotate a flight mill... also using magnets and hall-sensors (This is a University-project).
I am getting a reasonable result using the "catflap example" given on this page: https://leanpub.com/RPiMRE/read#leanpub ... ut-sensors

problem is: this uploads data on the internet, but the location where the final test will be done will not have internet available... so my question to you: do you have a way of getting your data straight to a USB device or the microCard used in the Rpi so I don't need a connection to the internet to conduct my tests?

thanks for reading, kind regards,
Mats

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

Re: Using ENA or IN1/2 PWM for L289N motor speed control

Sun Jun 21, 2015 11:39 am

Mats Van Gestel wrote: ...
problem is: this uploads data on the internet, but the location where the final test will be done will not have internet available... so my question to you: do you have a way of getting your data straight to a USB device or the microCard used in the Rpi so I don't need a connection to the internet to conduct my tests?

thanks for reading, kind regards,
Mats
Your other post has an answer.

Onkarb68
Posts: 2
Joined: Mon Jan 23, 2017 1:23 pm

Re: Using ENA or IN1/2 PWM for L289N motor speed control

Tue Jan 24, 2017 2:07 pm

Can you please tell exact name of hall effect sensor?


pygmalion
Posts: 22
Joined: Sat Jul 18, 2015 4:44 pm

Re: Using ENA or IN1/2 PWM for L289N motor speed control

Sat Apr 21, 2018 4:26 pm

I see your software uses callback function, which, I presume, counts edges on a GPIO input behind the scenes (while the python program does other stuff). Is that correct?

What is the highest edge frequency that callback is able to detect reliably? I suppose that depends on RPi version too, I have RPi3.

Regards, Marko

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

Re: Using ENA or IN1/2 PWM for L289N motor speed control

Sat Apr 21, 2018 4:54 pm

On the Pi3 probably about 90k edge transitions per second sustained. That would be shared among all GPIO. The peaks could be far higher if the inputs are bursty. The limitation is probably Python itself. If you need more I'd try C.

pygmalion
Posts: 22
Joined: Sat Jul 18, 2015 4:44 pm

Re: Using ENA or IN1/2 PWM for L289N motor speed control

Sat Apr 21, 2018 5:04 pm

I need 1kHz, which is an order of magnitude more than 150Hz you have in this article, and two orders of magnitude less than the limit you specified. I'll give it a shot.

But I wonder... Isn't pigpio (that is doing counting) written in C? In that case what do you gain by using C?

By the way, which magnets did you use? Any magnet will do?

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

Re: Using ENA or IN1/2 PWM for L289N motor speed control

Sat Apr 21, 2018 5:35 pm

The raw data is captured in C, but each GPIO level change is sent in batches via a TCP (socket) interface to the Python script. Python must read each change and then call the appropriate callback function. Those 20 to 30 lines of Python logic take much more time to execute than their C equivalent.

The magnets were button style neodymium grade N35.

Return to “Automation, sensing and robotics”