Using PWM to control voltage output?

Hello,

I’m a novice, and I’m reading this page on how to drive vibration motors with Arduino:

https://www.precisionmicrodrives.com/tech-blog/2016/05/16/how-drive-vibration-motor-arduino-and-genuino

Long story short, the writer suggests that we can drive a 3v motor with the PWM by simply using, say, a value of 128 to analogWrite().

Intuitively, this seemed to make sense, until I thought about it. My understanding is that PWM always outputs 5v (or close to it) but just at different duty cycles.

So…I’m confused. Any input would be greatly appreciated!

Best,
Lee

Motors are inductive which means the current doesn't keep up with rapid changes in voltage, it
smooths them out.

So a square wave voltage of 1kHz across your motor will produce a much more steady current in
the winding, but only some fraction of the current that would flow at the full 5V or whatever the supply
is.

The action of the motor cares only about the current in the winding, that's what generates forces.

Whether 1kHz is rapid enough depends on how much inductance in the motor though - small motors
may need higher frequencies to work well, and any motor may need ultrasonic frequencies to get rid
of the acoustic whining that audible frequencies give (which can be annoying).

That is really interesting, thanks! So...how can you ensure not sending too much juice?

So it's not a nutso thing to do I guess :slight_smile:

One thing that bugs me...a lot of times the motor spins faster when the PWM value I output is lower. Which does not make sense to me.

Depends on how you've hooked it up.
E.g., an LED with cathode to I/O would be brighter with lower analogWrite values.

rastoboy:
That is really interesting, thanks! So...how can you ensure not sending too much juice?

Set the maximum duty cycle to something that seems right?

So it's not a nutso thing to do I guess :slight_smile:

One thing that bugs me...a lot of times the motor spins faster when the PWM value I output is lower. Which does not make sense to me.

That's odd indeed - however I note the circuit you linked to lacks a free-wheel diode across the motor so
you have possibly damaged your MOSFET already. Switching inductive loads always needs some form of protection
from inductive voltage spikes, which for motor drivers is usually free-wheel diodes. With a single switch
circuit the diode goes across the motor. With an H-bridge every switch has a free-wheel diode (fortunately
MOSFETs automatically have a body diode as part of their structure, so MOSFET H-bridges are already
protected).

So why does the motor spin slower or an LED get dimmer as the analogWrite() value goes up? This makes no sense to me.

rastoboy:
So why does the motor spin slower or an LED get dimmer as the analogWrite() value goes up? This makes no sense to me.

Because a HIGH pin doesn't always mean ON. This is fundamental and you need to understand this before going on with your life.

Electricity works on voltage potential. Current flows from higher voltage to lower voltage. Put 5V on both ends of an LED, nothing flows. Put 0V on both ends, nothing. You have one end at 5V and one end at 0V (and resistor in series). LED is on.
You can either turn it off by making the 5V end go to 0V (which is how you're seeing it) OR by making the 0V end go to 5V. In the latter case, you are writing a HIGH or 255 to the cathode (-) end of the LED to turn it OFF.

@INTP: first off, you have blown my mind and i am sure you are right that I need to grasp this.

Like most things involving electronics, I find that I think I get something, and then something else comes along to blow it all away in my mind and I have to start over again.

For example, just now I hooked a second, identical motor to a second pin, and on this one it’s spinning faster with the higher values.

How do I control this situation? lol. btw I’ve put in a diode and capacitor in parallel for each motor now to try to achieve better practice.

My code is thus (This is on a Mega 2560):

void setup() {
 // put your setup code here, to run once:

}

void loop() {

         analogWrite( 6 , 10 );
delay(100);
          analogWrite( 7 , 10 );
delay(100);
          analogWrite( 6 , 255 );
delay(5000);   
           analogWrite( 7 , 255 );
delay(5000);            

}

Currently (as it were), I’m only actually hooking up one motor at a time because I’m still running off USB power.

pin 7 PWM?
Are you running these motors straight off the I/O pins?

A thousand pardons--it's a Mega 2560. I've edited my last post to make that clear.

My understanding is that those are both PWM capable. Oh I wonder if they run at the same frequency?

Hmm I tried moving it to pin 3 which is apparently on a different timer, same result--higher number equals faster.

I'm not sure of the answer to your question---sorry I'm super new. I think the answer is "no" because power is not being supplied to the motor from the I/O pin?

rastoboy:
So why does the motor spin slower or an LED get dimmer as the analogWrite() value goes up? This makes no sense to me.

What exact circuit are you using?

Lets look at concrete example of what INTP brought up. Fortunately, there's two perfect examples of this at the linked post.

These two circuits are opposites of each other. Without going into too much detail, the left circuit is turned on when the input is HIGH, and the right circuit is turned off when the input is HIGH. Since the duty cycle you give to analogWrite is the percentage of time the signal is HIGH, whether a high duty cycle represents more on time or off time depends entirely on the circuit you connect it to.

I am attempting the one on the left, with the N type transistor. Unfortunately, I have not yet learned how to read wiring diagrams competently. I have a feeling you’re not going to like this, I’ve attached a fritzing diagram of what I’ve done (repeated twice, with the other motor on pin 7).

Sometimes there’s no other way to learn than to embarrass yourself :slight_smile:

Quick question: should I be surprised to be getting different results?

The answer to that question would help determine what I need to be learning, here, I think. If you wouldn't expect it, maybe I need to examine my wiring more closely to see where I screwed up. If not, then I need to understand what I need to do to control the motors reliably.

Quick question: should I be surprised to be getting different results?

Yes and no. It's a loaded question. Without test equipment to actually get exact differences, it's hard to say if there is a problem with one of the "made by the lowest bidder" parts, or just natural differences with the same cheap parts.

This is where you should ask yourself, "if I'm going to get into this stuff, what test gear should I start looking for?"

2 things to mention - 1; be mindful of hooking high current items to the 5V/3V header. Just like the I/O pins, there is limits on how much current it can source. Motors might pull a small amount of current while free-running, but that number can grow substantially as a load is applied until it hits stall, which might roast the regulator. Learn how to throw in a second supply that can source a few amps. It's pretty easy, and you probably have a few laying around already (walwarts)

Second, reserve edits to prior posts to correcting spelling. Technical edits make those who post to the original look foolish. Everyone makes mistakes, it's not a big deal.

rastoboy:
Intuitively, this seemed to make sense, until I thought about it. My understanding is that PWM always outputs 5v (or close to it) but just at different duty cycles.

As mythbuster's often say .... 'well, there's your problem'. The issue is wording.

From the perspective of 'DC' or direct current condition ..... outputting 5 V will mean 5 V appears on the output pin at all times. If the output is outputting 5V at all times, then it is 'DC'. Not PWM.

PWM involves outputting something (eg. a voltage) having a fixed value for some duration of time, and then changing the value to a different fixed value for another duration of time. Those two durations of time always add up to be a constant value of time. "Ideal" PWM signal is two-level.

When you apply a PWM source to a purely resistive circuit or component (note...purely resistive), then voltages and currents will also be two-level. But when it comes to applying PWM source to circuits that aren't purely resistive, like motor coils, then voltages across the motor terminals and current going into the motor won't be just 2-level (- noting that connecting wires between components involves resistance and some inductance etc as well). When this happens....... people might conveniently consider signal levels from a "time-averaged" point of view. But also got to keep in mind - that choice of time-durations for PWM cycle can affect the behaviour or performance. Eg...... if durations are relatively large.... like the 'ON time' of your PWM is 100 seconds, and your system is able to reach your PWM levels relatively quickly (like within a few milliseconds)..... then the PWM signal might as well be 'DC' during the ON-time. So PWM duration, PWM frequency should be considered.

@Southpark: thanks for that, that kind makes sense :slight_smile:

@tinman13kup: I have certainly seen using an external power supply help with servos--definitely makes sense to try that. I'm gonna give it a shot tomorrow, as atm my power supply is rather complicatedly attached to another project :slight_smile:

One thing though...I've been experimenting, and it definitely appears that the PWM pin is the factor. I switched the two pins to the opposite set of circuitry, and the effect followed the pins--pin 7 (or pin 3) cause the motor to spin faster with higher values, and pin 6--even with the other circuitry--did the opposite.

Why is this? I had about decided I had made a mistake in replicating the circuit, but that appears not to be the case.

Why does one pin seem to work one way, and another the opposite?

There was some recent discussion about this somewhere ----- about needing to look at the PWM details of particular arduino pins that have been designed to output PWM. The PWM frequency of certain pins were purposely designed to be different from the PWM frequency of other pins. The arduino documentation for a particular arduino board will shed light on the PWM frequency assigned to particular dedicated PWM pins.

I'm thinking that every PWM tutorial with arduinos should begin with something like ... 'there are dedicated PWM pins, and beware that the PWM frequencies of some pins may be different from others, which is associated to features'.

The analogwrite function has details like "The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz. Pins 3 and 11 on the Leonardo also run at 980 Hz."

I think that.... it can be beneficial to confirm that a particular pin is outputting the required signal..... by using an oscilloscope, or using a suitable frequency measuring device. On the other hand....if the system is behaving in the way you want already, then maybe no need to check.

Well, I'll tell ya. I could have sworn I'd seen this behavior when I'd gone through the SIK tutorials, and so I hooked up experiment #6, which uses a light sensor to dim an LED using PWM.

And the damn thing worked exactly the same on every PWM pin on the Uno, and on six or seven PWM pins I tried on the Mega.

So obviously, my memory or understanding is faulty. I'm quite enthused now about trying an external power source as tinman13kup forcefully suggested :slight_smile: There clearly appears to be some effect due to the fact it's a motor hooked up.

I had contemplated the different frequencies of the different PWM pins being an issue, but couldn't find a correlation. But from what I could find, pins 6 and 7 are on the same timer. Which, now that I think about it, may not mean they run at the same frequency? Hmmmm. It's surprisingly hard to find information on the default frequencies for the mega PWM pins.

I'll let you know what I find!

If you don't mind I have a couple of thoughts.

  1. I didn't see what the Mosfet is but if you are using something like a 2N7000, the connections you show are not correct.

The Mosfet drain lead should go to the motor.
The Mosfet gate lead to the arduino output
The Mosfet source lead to ground (more correctly to common).

The diagram shows the drain and source reversed.

I disagree with a capacitor across the motor. Reason: the Mosfet turns ON and OFF very quickly. Capacitors are like shock absorbers to voltage, they don't like to move/change fast. So the Mosfet tries to change the voltage fast, the capacitor tries to keep the voltage the same. The result is a significant transient current draw from the power supply.

I would move the capacitor (what value are you using) to 5V--Common on the protoplug board.

On the drawing, I suspect you just picked any capacitor from the program library. If it is really an electrolytic like the diagram suggests, it is way to large to be across the motor and it is in backward (the stripe is usually negative)

A little insight to PWM motor control. Unlike the capacitor, the motor which has inductance which wants the current not to change (at least not quickly).

During the ON cycle, Current from the power supply --> motor --> FET --> common. During this time the current ramps up at a rate controlled by motor characteristics.

During the OFF cycle, the current from the motor goes through the diode and decays at a similar rate.

The average current is what controls the speed of your motor. The more ON time and less OFF time the higher the average current will be.

If looked at on and oscilloscope, the current would look like a lopsided triangle wave.

Hope I didn't make things worse.

Jon
Jon

JohnRob:
I disagree with a capacitor across the motor. Reason: the Mosfet turns ON and OFF very quickly. Capacitors are like shock absorbers to voltage, they don’t like to move/change fast. So the Mosfet tries to change the voltage fast, the capacitor tries to keep the voltage the same. The result is a significant transient current draw from the power supply.

Shock absorption is literally the point of the capacitor. Brushed DC motors can create a lot of sparking that can cause electrical noise. When I look inside my cheap drill while it’s running, the point where the brush and commutator touch glows a steady blue from all the sparking. Capacitors are meant to stop that.

An electrolytic is the wrong kind of capacitor though. Pololu recommends 1-3 0.1 uF ceramics for proper noise suppression.