Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumProgramming Questions & HelpPrograms › odd problem with PWM

Page Index Toggle Pages: 1
odd problem with PWM (Read 6234 times)
06/24/07 at 02:39:13

roy_pardi   Offline
YaBB Newbies
Boston USA

Posts: 49
*
 
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: [url]http://tigoe.net/pcomp/labs/lab-motors.shtml[/url] - 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);
  } 
}
[/code]
 
IP Logged
 
Reply #1 - 06/24/07 at 16:52:32

roy_pardi   Offline
YaBB Newbies
Boston USA

Posts: 49
*
 
I was using Wiring v10. Upgrading to v12 resolved the problem.

arrgh! A whole night of frustration  Tongue
 
IP Logged
 
Reply #2 - 06/24/07 at 17:40:06

roy_pardi   Offline
YaBB Newbies
Boston USA

Posts: 49
*
 
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);

}[/code]
 
IP Logged
 
Reply #3 - 06/25/07 at 11:41:44

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
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.
 
IP Logged
 
Reply #4 - 06/25/07 at 14:25:32

roy_pardi   Offline
YaBB Newbies
Boston USA

Posts: 49
*
 
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

...
 
IP Logged
 
Reply #5 - 06/25/07 at 14:42:30

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
what type of motor are you using? I'll try your code with a motor here, also with leds.
 
IP Logged
 
Reply #6 - 06/25/07 at 21:40:56

roy_pardi   Offline
YaBB Newbies
Boston USA

Posts: 49
*
 
It's a 5.9 v DC motor that I pulled out of an old CD player.
 
IP Logged
 
Page Index Toggle Pages: 1