Using PWM to control voltage output?

@JohnRob: thank you for that, that is exactly the kind of feedback on my circuitry I am sure I really need. I'm going to study my circuit with what you said in mind when I get home from work. But to supply a little more information:

It's a 2N2907 transistor. Chosen because it has an N in the name and that was the example used on the example page.

It's a 100uF capacitor. Chosen based on the advice on that page, and it worked lol.

I'm kinda amazed and relieved y'all haven't savaged me on the placement of the diode and capacitor at all, because it kinda blew my mind. Transistors still blow my mind a bit, too. When I get home I'll figure out if I made the diagram wrong. I'm pretty sure I didn't pay attention to the orientation of the capacitor in the drawing (my mistake), but I believe the transistor is correctly represented (as I have it now).

I'm afraid I do not understand what you mean by "I would move the capacitor (what value are you using) to 5V--Common on the protoplug board." edit: what's the protoplug board? :slight_smile:

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

I think it depends strongly on inductance and current in the motor. Vibration motors are very small and the mentioned app notes suggest cap 10-100pF if using PWM - probably to reduce current spikes from filling the cap when PWM turns on. Or use 100nF only if noise is problem and PWM is not used for drive the motor.

Well. Using an external 5v 2A capable power supply I see no difference--the PWM pins are acting in opposite fashion to each other.

Switching the two circuits between the two PWM pins shows the behavior following the PWM pin. wtf.

Perhaps you should post a diagram of the circuit AS IS, along with the code. Are you sure you are using the same transistor for both?

Ok. Ok. Ok. Sh*t just officially got weird. Turns out it's not the pin per se that matters. What matters is which one gets written to, first! To wit:

With this code, the motor on pin 7 will turn SLOWER with the higher PWM values, and vice versa:

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

}

void loop() {
 // put your main code here, to run repeatedly:

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

Whereas with this code, it is the opposite--the motor on pin 6 goes slower with higher PWM values, and vice versa:

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

}

void loop() {
  // put your main code here, to run repeatedly:

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

@tinman13kup I believe the diagram is correct as it is actually set up, with the exception that the capacitors are backwards...and I can't seem to figure out how to flip them around visually in Fritzing, yet. The transistor is facing the direction shown, for sure--visually.

(yes, the transistors are the same,sorry)

Ok, so this is interesting. Commenting one of those five second delays causes them to act the same (higher is slower btw):

void loop() {
 // put your main code here, to run repeatedly:

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

ALSO, if I remove the bottom two or top two analogWrite's, they also act the same way.

So clearly this is a....man I don't know what this is lol. I'm going to experiment extensively and see if I can pin down the behavior more explicitly.

btw these are the vibration motors in question.

It's a 2N2907 transistor. Chosen because it has an N in the name and that was the example used on the example page.

I'm a little confused here. What does the "N" have to do with selection? This is a pnp transistor, which might explain your results. It is not hooked up the same way as a npn.

HAHAHAHAHA that is funny. I read something somewhere that led me to believe that's how it worked, and now you have just taught me that it does not, in fact, work that way.

Thank you for that tinman13kup. I'll just go ahead and start all over again :slight_smile:

Ah, this may explain JohnRob's explanation above, too. It didn't occur to him I could be so ignorant as to not realize what type of transistor I was using lol.

double check exactly what you have. All I'm finding for that number is a TO-18 package, which is a small metal can. Your fritzing diagram shows a TO-92.

Tom

From posts #24 and #26 I guess you don't understand what the code should do. This

rastoboy:

byte x=10;

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

}

void loop() {
// put your main code here, to run repeatedly:

analogWrite(7 , x );
delay(100);
analogWrite(  6, x );
delay(100);
analogWrite( 7 , 255 );
delay(5000);
analogWrite( 6 , 255 );
delay(5000);
}

should keep the motor on pin 7 off nearly all the time while motor on pin 6 should run for 5s and then stop for 5s. With higher "x" motor 6 should run slower (when running). Is it how it works?

BTW remove the 100uF cap it may do more problems than it possibly solves. You may put it between GND and 5V to help supress noise from PWM.

still pretty weird, the behavior I was seeing, no?

@Smajdalf: sorry I didn't see your post earlier. The weird thing was that on pin 6, 255 was all the way off, while on pin 7, 255 was all the way on. Depending on what order I executed the statements.

Please post a code and PRECICE description what it does. The "wierd" code in #31 should do:

  1. Turn motor 7 on
  2. Wait 100ms
  3. Turn motor 6 on
  4. Wait 100ms
  5. Turn motor 7 off
  6. Wait 5s
  7. Turn motor 6 off
  8. Go to 1)
    It should look this way motor 7 turns on for a moment and nearly at the same time motor 6 turns on and runs for 5s. Then it stops and both are off for another 5s.
    Does it do this or something else?
    And it is not strange higher PWM = slower speed. It is because how the motors are wired. If pin is HIGH it turns transistor off and stops motor.

And it is not strange higher PWM = slower speed. It is because how the motors are wired. If pin is HIGH it turns transistor off and stops motor.

He has the transistor wired as a low side switch, and was under the impression it was a npn transistor, but the number given shows a pnp, but the case is also not right. Without confirmation on exactly what transistors he is using, it's folly to explain why he is getting certain results

Your correct--that's the way they run. It was essentiall random--I was screwing with the motors trying to get them to do stuff. No design or intent to speak of.

The odd thing is that on the first one x=10 is near full speed, but on the second motor x=10 is slow.

@tinman13kup: those results were with the 2N2907 transistor.

The weird(est) thing to me is just the fact that the motors didn't behave the same way--either high PWM is fast or high PWM is slow. Regardless of my incompetence :-). I would think this would be reproducable, at least with the same vibration motors.

ie. I would expect both motors to either being going flat out, or off, for the last 5 seconds, right? But only one of them does. And then, briefly, the other is on full speed and the one going flat out is off.

However, very slight changes to the code, as noted further above, cause this to go away and they both act predictably.

I'm wondering if this is some type of power management issue. I'm using a 5v 2A power supply, which, in my inexperience, seems like it would be enough for just two of these? I don't know how to measure how much current they are drawing.

If you do analogWrite(pin,x) it outputs PWM from the pin equal to x until you change it with another analogWrite command on the same pin. It is not limited for time of following delay. Maybe this is the problem?

tinman13kup:
He has the transistor wired as a low side switch, and was under the impression it was a npn transistor, but the number given shows a pnp, but the case is also not right. Without confirmation on exactly what transistors he is using, it's folly to explain why he is getting certain results

Google (and eBay) shows transistor with this number in TO92 package. So I expect it is this one. By luck the circuit works as well but with more loses and reversed logic.