Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Programming Questions & Help >> Programs >> odd problem with PWM
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1182652753

Message started by roy_pardi on 06/24/07 at 02:39:13

Title: odd problem with PWM
Post by roy_pardi on 06/24/07 at 02:39:13
Hi,

Trying to set up a simple test file controlling a DC motor using an H-bridge (SN754410 - functionally eq. to the L293D).

I think I have it all wired correctly- the weird thing is I can *either* use PWM to vary speed OR use digitalWrite to reverse direction but I can't do both. In the code below the PWM doesn't work *unless* I comment out the pinMode declarations and the digitalWrite calls. Then PWM works fine.

I realize it's hard to comment without seeing how things are wired - I have it set up exactly like here: http://tigoe.net/pcomp/labs/lab-motors.shtml - including a pull-down resistor on the enable pin.

Is there anything obvious that I am missing? I've tried every variation I can think of.

Thanks!

--Roy


Code:
int motorpin = 0;  // L293D Pin En1 connected to pin PWM 0
int value=0;
void setup(){

 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
}

void loop()
{
 digitalWrite(8, HIGH);
 digitalWrite(9, LOW);

 for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
 {
   analogWrite(motorpin, value);           // sets the value (range from 0 to 255)
   delay(30);                            // waits for 30 milli seconds to see the dimming effect
 }
 digitalWrite(8, LOW);
 digitalWrite(9, HIGH);
 for(value = 255; value >=0; value-=5)   // fade out (from max to min)
 {
   analogWrite(motorpin, value);
   delay(30);
 }  
}

Title: nevermind....Re: odd problem with PWM
Post by roy_pardi on 06/24/07 at 16:52:32
I was using Wiring v10. Upgrading to v12 resolved the problem.

arrgh! A whole night of frustration  :P

Title: Re: odd problem with PWM
Post by roy_pardi on 06/24/07 at 17:40:06
ok - there is still something odd going on with the board here. Using v12 of Wiring my analogWrite() values don't take effect if they are less than 255 *unless* I issue an   analogWrite(motorpin, 255) first (code below).

Is this an expected behavior? I have an LED on the PWM pin so I can see when it's HIGH even if the pulse rate is not enough to start the motor.

Is this a board firmware bug or just FOL?

Oh - is it normal for a small 5v motor to whine when it is being PWM < 255?

thanks!

--Roy

//test to switch motor direction every 2 seconds + PWM at 125

Code:
int motorpin = 0;  // L293D Pin En1 connected to pin PWM 0
int value=0;
void setup(){

 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);

 digitalWrite(8, HIGH);
 digitalWrite(9, LOW);
 
}


void loop()
{

 analogWrite(motorpin, 255); ///<-----------needed?
 analogWrite(motorpin, 125);

 digitalWrite(8, HIGH);
 digitalWrite(9, LOW);

 delay(2000);

 analogWrite(motorpin, 255); ///<-----------needed?
 analogWrite(motorpin, 125);
 
 digitalWrite(8, LOW);
 digitalWrite(9, HIGH);  

 delay(2000);

}

Title: Re: odd problem with PWM
Post by barragan on 06/25/07 at 11:41:44
hi Roy, don't et very well what's the problem you're experimenting, could you please post a diagram of your connections as well? Some motors doesn't stop right the way this might be confusing sometimes, perhaps it is easier to debug using leds, then replace them for the motors. Let me know.

Title: Re: odd problem with PWM
Post by roy_pardi on 06/25/07 at 14:25:32
Hi,

This is how I have it wired (with addition of pulldown resistor on enable pin).

It works but in order for analogWrite to work, I have to call it twice:

analogWrite(pin, 255)
analogWrite(pin, desiredSpeed)

I am wondering if this is expected behavior, a board issue or my bad

--Roy


Title: Re: odd problem with PWM
Post by barragan on 06/25/07 at 14:42:30
what type of motor are you using? I'll try your code with a motor here, also with leds.

Title: Re: odd problem with PWM
Post by roy_pardi on 06/25/07 at 21:40:56
It's a 5.9 v DC motor that I pulled out of an old CD player.

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.