Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Wiring Hardware >> Troubleshooting >> PWM problem in 13?
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1196838328

Message started by eustace on 12/05/07 at 07:05:27

Title: PWM problem in 13?
Post by eustace on 12/05/07 at 07:05:27
I'm a Wiring noob, this is my first post; please excuse me if its a bit long.

I am building a small, tracked robot with a Wiring board for a brain.  At the moment it has 2 motors, for which I would like to use PWM outputs 0-3, a pot on Analog Out 0, and 8 LEDs on pins 24 through 31.

I was having problems getting PWM out while building the bot; I read some posts from August where CSMotion added an initialization  line to the setup() section that allowed output, so I tried that and it seemed to work (I still didn't have really usable hardware at that time so I moved on).

Now I have the bot built and wired and I have a problem.  When my program turns pins 29 thru 31 on, PWM comes on (29 turns on PWM 5, 30 is 4, and 31 is 3).  This despite the fact that there is no initialization line in setup(), and no analogWrite() in loop().

Can someone explain this?  I am appending the code:

// pot motor and leds

int val;                    // used to hold pot value

void setup()
{
 pinMode(24, OUTPUT);            // set the digital pins for LEDs as output
 pinMode(25, OUTPUT);
 pinMode(26, OUTPUT);
 pinMode(27, OUTPUT);
 pinMode(28, OUTPUT);
 pinMode(29, OUTPUT);
 pinMode(30, OUTPUT);
 pinMode(31, OUTPUT);
 Serial.begin(9600);             // sets the serial port to 9600
}

void loop()
{
 int i;
 for(i=24; i<32; i=i+1) {        // loop to cycle through LEDs
   digitalWrite(i, HIGH);        // sets the LED on
   val = analogRead(0);          // read analog input pin 0
   Serial.print(val, DEC);       // prints the value read
   Serial.print(" ");            // prints a space betwen the numbers
   Serial.print(i, DEC);         // prints the LED number
   Serial.print(" ");            // prints a space betwen the numbers  
   delay(500);                   // wait 100ms for next trip thru loop
   digitalWrite(i, LOW);         // turns LED off
 }
}

Title: Re: PWM problem in 13?
Post by Titus on 12/05/07 at 09:27:23
So your problem is that when you run that program, you PWM ports automaticly turn HIGH aswell

That's because the PWM ports are shot circuited to certain I/O pins.

Why not use pins 0 till 7, Those will not interfere with the PWM.

Good luck

Titus

What is the purpose of the analog in?
Are measuring something and just printing it? or are you processing that data?
Where are the motors in you program?

Title: Re: PWM problem in 13?
Post by Vaccumdiode2 on 12/05/07 at 18:19:28
Hello All!

Here are the Pins shared with the PWM port.

Wiring Pin 29 and PWM 1
Wiring Pin 30 and PWM 2
Wiring Pin 31 and PWM 3
Wiring Pin 36 and PWM 4
Wiring Pin 37 and PWM 5
Wiring Pin 38 and PWM 6

I have a excel spreadsheet I made that has all the Pins and what functions they use and share.

I not sure how to post it. I'll E-mail it to Hernando Barragn, He can double check it and post it if he alright with it.

Rich

Title: Re: PWM problem in 13?
Post by Titus on 12/05/07 at 18:56:15
That's a great idea actually. Making a spreadsheet.

I hope these awnsers solve your problem!

Titus

Title: Re: PWM problem in 13?
Post by eustace on 12/06/07 at 05:35:28
You've answered my question. Thanks!  I consulted the schematic today after reading the first post (at work, where I couldn't reply).
The example code was just to exercise my robot's IO devices (currently just 8 LEDs and a pot, besides the motors).

Title: Re: PWM problem in 13?
Post by eustace on 12/06/07 at 06:04:39
...but that doesn't mean I'm out of questions.  Does anyone know why the DC motor example code on the website works (it turns a motor on), but this slightly modified code doesn't?  It should turn the motor off again.  Motor keeps going.  Hmmm.

// Driving a DC Motor

int fwdpin = 0;  // forward is pin PWM 0

void setup()
{
 // nothing for setup
}

void loop() {
 analogWrite(fwdpin, 127);  // turn ON the Motor at half speed
 delay(1000);
 analogWrite(fwdpin, 0);  // turn OFF the Motor
 delay(1000);
}


Title: Re: PWM problem in 13?
Post by eustace on 12/06/07 at 07:28:15
Last post before bed.

I wanted to eliminate the motor control pcb as a possible culprit, so I put my LED bank on the PWM pins and ran this code:

// Driving DC Motors

int xfwdpin = 0;
int xrevpin = 1;
int yfwdpin = 2;
int yrevpin = 3;
int zfwdpin = 4;
int zrevpin = 5;

void setup()
{
 // nothing for setup
}

void loop()
{
 analogWrite(xfwdpin, 255);
 analogWrite(xrevpin, 255);
 delay(1000);
 analogWrite(xfwdpin, 0);
 analogWrite(xrevpin, 0);
 delay(1000);
 analogWrite(yfwdpin, 255);
 analogWrite(yrevpin, 255);
 delay(1000);
 analogWrite(yfwdpin, 0);
 analogWrite(yrevpin, 0);
 delay(1000);
 analogWrite(zfwdpin, 255);
 analogWrite(zrevpin, 255);
 delay(1000);
 analogWrite(zfwdpin, 0);
 analogWrite(zrevpin, 0);
 delay(4000);
}

The expected outcome: pairs of LEDS turning on, then off, down the bank.
The actual outcome: PWM0 will turn on, but not off.  PWM1 and PWM3 behave as expected.  PWM2 and PWM4 never light up.
So maybe I just have bad outputs.  But I make this change to the int assignments:

int xfwdpin = 1;
int xrevpin = 0;
int yfwdpin = 3;
int yrevpin = 2;
int zfwdpin = 5;
int zrevpin = 4;

...and now it's PWM1 that won't shut off, PWM0 and PWM2 that behave as expected, and PWM3 and PWM5 that won't light.  The problem didn't follow the hardware outputs, it followed the order in which they were activated by the code.

Any suggestions?

Title: Re: PWM problem in 13?
Post by Titus on 12/06/07 at 08:15:09
That's is very odd?

Are you sure that you didn't short circuit any wires?
Perhaps you can post a picture of you LED bank(im interessted)

@Barragan, all PWM port can be used at one time, can they?

I'll try my own PWM ports.

TItus

Title: Re: PWM problem in 13?
Post by Vaccumdiode2 on 12/06/07 at 16:12:31
Hello everyone!
         If you turn on the port anything connect will turn on.  you can not attach a motor pwm 1 and a LED to pin 29.   If you you make Pin 29 High The motor and the LED will recieve that signal.   If you look at the Board Schematic you see they are connected.  They are connected to the same pin on the processor chip.  That is why I made the spreadsheet.  I e-mailed it to Hernando yesterday.  I hope he got it.  
         If you remove all power from the wiring board and using an ohm meter you can see that pwm 1 and pin 29 have zero resistance.  Therefore they are the same connection.  
         I hope this helps.  I know that some of the wiring board users are new to electronics.  But most of the fun is in the learning process.  I have been playing around with the stuff for many years and I'm still learning.  

Rich

Title: Re: PWM problem in 13?
Post by Vaccumdiode2 on 12/06/07 at 18:11:28
Hello Again.
           The PWM pins?   Are they 0 through 5  or 1 through 6?   On the schematic they are listed as 1 through 6 and on the label on the board they are list as 0 through 5.   So when we use them in the software we should use 0 through 5?

 Eustace,
             are you using  MOSFETs to run your motors?  If you are what type and what type of motors are you using.  It could be that the MOSFETs are not turn off do to the current your drawing through them.  Not sure this is the answer but it may be something to look into.

Rich

Title: Re: PWM problem in 13?
Post by eustace on 12/07/07 at 02:59:02
Titus - I made the LED bank to help troubleshoot the board.  Since I consider it test equipment, I check it when I use it.  It helps in this case that I can just shift it two places and see if the pattern repeats (the bank has 8 LEDs, the PWM header has 6 pins).  I made it by soldering the anodes of the LEDs to the connector pin of an 8-position header socket, soldering a resistor to the other lead, and connecting all the resistor leads to a ground wire which connects to ground on the 4 pin power header.  I would send you a pic but a) my camera makes lousy closeups of small things and b) I haven't figured out how to post pics yet!  Have you tried your PWM outputs yet?

Vacuumdiode2 - The pins are numbered 0-5 on the board, and the example code on the site always starts with analogWrite(0, something), so that's what I've been going by:)

Everyone - some good news!  I just ran this code:

// Driving two DC Motors

int xfwdpin = 0;
int xrevpin = 1;
int yfwdpin = 2;
int yrevpin = 3;
int zfwdpin = 4;
int zrevpin = 5;

void setup()
{
 // nothing for setup
}

void loop()
{
 analogWrite(xfwdpin, 255);
 analogWrite(xrevpin, 255);
 analogWrite(yfwdpin, 255);
 analogWrite(yrevpin, 255);
 analogWrite(zfwdpin, 255);
 analogWrite(zrevpin, 255);
 delay(1000);
 analogWrite(xfwdpin, 0);
 analogWrite(xrevpin, 0);
 analogWrite(yfwdpin, 0);
 analogWrite(yrevpin, 0);
 analogWrite(zfwdpin, 0);
 analogWrite(zrevpin, 0);
 delay(1000);
}

Now all six diodes light up, then PWM1 thru PWM5 start blinking, but PWM0 stays lit.  I needed 4 PWM outputs, it looks like I have 5!  

Title: Re: PWM problem in 13?
Post by eustace on 12/07/07 at 03:23:33
I celebrated too soon.   But first - Vacuumdiode2, you asked about MOSFETS and I forgot to answer.  After some experiments with L293Ds, and trying to build my own H-bridge out of a MAX620 and whatever MOSFETS I could find, I broke down and bought a Solarbotics motor driver board.  Which I disconnected once I began having problems (I've been using LEDs to troubleshoot).

I just ran this code:

// Driving two DC Motors

int sacrifice = 0;
int xfwdpin = 1;
int xrevpin = 2;
int yfwdpin = 3;
int yrevpin = 4;


void setup()
{
 // nothing for setup
}

void loop()
{
 analogWrite(sacrifice, 255);
 analogWrite(xfwdpin, 255);
 analogWrite(yrevpin, 255);
 delay(1000);
 analogWrite(xfwdpin, 0);
 analogWrite(yrevpin, 0);
 delay(1000);
}

...and I set the board up with the PWM pins 1 thru 4 going to my motor controller, and PWM 0 to an LED.  When I run the code not only does the LED go on and stay on (of course, I'm not telling it to turn off) but so does PWM 1.  Aaaargh!

So I added this line "analogWrite(sacrifice, 255);", just after the first delay.  I MEANT to add analogWrite(sacrifice, 0), and at first I thought I had, because when I ran the code the motors both started and stopped, and only the LED (PWM 0) stayed on.

So what does this mean?  Will I be able to write code for the other 4 PWM outputs if I throw in an occasional analogWrite(sacrifice, 255)?  This is just wrong.  I'm thinking of getting an Arduino, just so I can finish my bot.  Sheeesh.

Title: Re: PWM problem in 13?
Post by Titus on 12/07/07 at 09:24:12
I think arduino will not solve your problem.
Try cutting all power and start measuring if some PWM pin's are shot circuited.

Ill try my PWM pins this weekend.

Titus

Title: Re: PWM problem in 13?
Post by Vaccumdiode2 on 12/07/07 at 16:16:49
Hello,
           Did you Try put a scope on the PWM pins.  The LED on PWM 0 may be Pulsing at very High rate, looking like it is on all the time.  Try puting a delay between your writes and see if it helps.  I had found a kit that has a dual H-Bridge QUASAR CODE 3166 - Bidirectional DC Motor Speed Controller.  I pulled the POT off and had to make a Digital to analog converter to get a true analog out put.  I found that using different delay times I got different voltage changes.  Try puting 2 or 3 delays that add up to the total delay your looking for.  For some reason the voltage did not want to drop to zero volts and bounce between two voltage seemly never hitting zero.  I had to use both an analog meter and a digital meter.  Hope this gives some more insite.

Rich

Title: Re: PWM problem in 13?
Post by eustace on 12/08/07 at 09:50:19
Titus - I really look forward to your PWM test result - this might tell me whether I have a bad board or not.

Rich - There already is a delay in the code, this is why the LEDs blink.  I haven't scoped the output yet, mostly because I've been concentrating on finishing the hardware (I haven't actually turned it on at all tonight, but I did build, install, and wire the four bumpers!  It's almost a robot!).  The question I need to settle is whether my board is bad, or alternatively, whether the PWM firmware problems mentioned in the forums from Wiring 11 & 12 still haven't been fixed.  When Titus tries my code on his board I will know this.  The problem I have is that the different ports behave differently from each other; some can turn on and off using code like the examples from the website, but others can't.  The pattern of can/can't doesn't stay fixed to the particular hardware ports, so I must surmise that either my firmware is corrupt or Wiring 13 still has a PWM problem.
And I hope you weren't suggesting that I generate "PWM"-like intervals using code!  I believe that one of the posters I was referring to did that, but like him, I don't consider that a solution.  I need PWM that runs without that kind of caretaking, or I'll never be able to develop fast enough code to implement my ideas.

Once again, thanks for your help, both of you.

Title: Re: PWM problem in 13?
Post by Titus on 12/08/07 at 12:30:22
I THINK I FOUND IT!!!

When you use PWM, you turn it on like this

analogWrite(1, 1000);
analogWrite(0, 1000);


In this case, only led 1 turns on...

I thought it was the processor that coudn't handle it. so i change it to this:

analogWrite(1, 1000);
delay(1);
analogWrite(0, 1000);


Guess what, both leds turned on!!


void setup()  
{  
 // nothing for setup  
}

void loop()  
{  
 analogWrite(1, 1000);
 delay(1);
 analogWrite(0, 1000);
 delay(200);
 analogWrite(1, 0);
 delay(1);
 analogWrite(0, 0);
 delay(200);
}  


DAMN, i tried this code:


void setup()  
{  
 
}

void loop()  
{  
 analogWrite(0, 255);
 delay(1);
 analogWrite(1, 255);
 delay(1);
 analogWrite(2, 255);
 delay(1);
 analogWrite(3, 255);
 delay(1);
 analogWrite(4, 255);
 delay(1);
 analogWrite(5, 255);
 delay(1);
 delay(1000);
 analogWrite(0, 0);
 delay(1);
 analogWrite(1, 0);
 delay(1);
 analogWrite(2, 0);
 delay(1);
 analogWrite(3, 0);
 delay(1);
 analogWrite(4, 0);
 delay(1);
 analogWrite(5, 0);
 delay(1);
 delay(1000);
}  



And it did not run like it should...
Led 0 stays on...
(actually, I ran that code with just 3 leds, on pin 0,1,3.

when i try the same code on the leds 1,2,4, it works aswell, just like 2,3,5)

It seems that only pin 0 stays on.)

It seems to me, that there is a little bug in the software, instead of the hardware,

Why would my first program work, and my second not.
But then again, why would only pin 0 stay on..?

I hope this helps.

Anyway, if you sacrefice PWM pin 0, you it will work. i think.

@Barragan, could you help us out?

Titus

@Eustace Don't give up!, we can do it!

Title: Re: PWM problem in 13?
Post by eustace on 12/08/07 at 17:06:59
Thanks, Titus!  It's a relief to find that I'm not seeing a problem unique to my particular board, that others are affected, too.  Hopefully we can get a fix into the next revision (although considering the persistence of PWM bugs this may be optimistic).  I posted a comment in the Bugs section of the forum.  I'm afraid that if Barragan looked in on this thread he may have stopped when he saw my confusion over the cross-connected pins.  After all, you guys straightened me out on that pretty well without his help.
Anyway, I can keep troubleshooting my bot, just by sacrificing one PWM.  But I'm worried that as my code becomes more complex that will prove to be a poor workaround!

Title: Re: PWM problem in 13?
Post by Kars on 12/08/07 at 19:05:17
Hi,

Maybe you could try the value '1' for your pwm command instead of '0'
So it would be like:

analogWrite(ledpin, 1);

Title: Re: PWM problem in 13?
Post by barragan on 12/09/07 at 19:36:05
Please try this example on each individual PWM output pins 0-5 and let me know on the results. http://wiring.org.co/learning/examples/led_fade.html

Title: Re: PWM problem in 13?
Post by Titus on 12/09/07 at 21:00:13
I tried that example code for all the PWM pin's from 0 to 5

Although i tried it without the 200 ohm resistor, all the PWM ports worked

This is not the problem barragan,

It is something with the software, when analogwriting 2 PWM ports.

Hope this helps.

Titus

Title: Re: PWM problem in 13?
Post by barragan on 12/11/07 at 06:26:24
could you post the minimum code possible that replicates the problem?

Title: Re: PWM problem in 13?
Post by eustace on 12/11/07 at 15:49:08
I'll try (I tried twice last night but Firefox kept crashing when I tried to type in the reply window?!?).
The shortest piece of code that shows there is a problem would be this (a modification to your DC motor example code):

// Driving a DC Motor
// by BARRAGAN <http://barraganstudio.com>

int motorpin = 0;  // L293D Pin En1 connected to pin PWM 0

void setup()
{
 // nothing for setup
}

void loop() {
 analogWrite(motorpin, 125);  // turn ON the Motor at medium speed
 delay(1000);
 analogWrite(motorpin, 0);  // turn the Motor OFF
 delay(1000)    
}

If I run this, the PWM output never shuts off.

By the way, I posted some code examples to the Bugs forum, did you try those?

Title: Re: PWM problem in 13?
Post by barragan on 12/11/07 at 16:24:38
ok, Ill try this, but i recall the motor I used before to do the test needed some time to fully stop, 1 second wasn't enough for it to stop completely.

Title: Re: PWM problem in 13?
Post by Titus on 12/11/07 at 16:52:56
Perhaps 0 isn't off.

I know someone, who had to use 127 as the 0 position....

@barragan, that won't explane why a led will not turn off, a led reacts way faster then a DC motor...

Title: Re: PWM problem in 13?
Post by eustace on 12/11/07 at 17:40:15
Titus - Regarding motors - "off" can just mean a PWM value low enough that the motor can't run at that voltage level, but that's not what I need.  I need OFF, no voltage - if I can't turn the PWM ouput pin all the way off, I have problems with bidirectional motor control (motor controller circuits can be explosively unhappy with both a forward and a reverse signal, even if one is very low).

Barragan - As for the speed with which a motor reacts - once I realized I had a problem I stopped running my examples with the motor controller hooked up, and started running them with a bank of LEDs on those pins instead (so I didn't have to worry about blowing up my motor controller by accidently sending both FWD and REV signals).  The LEDs turn on and off instantly, so motor momentum doesn't really play a part in the troubleshooting I've been doing.  If you are using motors to run the code example I sent, just bump the delay(1000) up to delay(10000).  The result should be the same, assuming you can duplicate my problem.


Title: Re: PWM problem in 13?
Post by roypardi on 12/22/07 at 18:19:49
Hi - late to this discussion but I've had similar problems using PWM to control a DC motor through an h-bridge (posted @ this in another section of the forum).

What I found was that in order to have an analogWrite() command take effect on a PWM pin, I had to make the call twice -

analogWrite(pinNum, 0)
analogWrite(pinNum, valueDesired)

Didn't fill me with great confidence but it seemed to work. Perhaps this has already been discovered/discussed. (this was with v13 btw)

--Roy

Title: Re: PWM problem in 13?
Post by Titus on 12/23/07 at 10:01:18
that could be a solution.
then the " analogWrite(pinNum, 0); " command would be the "sacrifice pin"

Im gonna try this with the programs that bug.

by the way, this is actually a wiring 0012 bug:
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1185988954/2#2

Thanks!

Titus


EDIT!

Yep, something strange is going on:

void setup()  
{  
 
}

void loop()  
{  
 analogWrite(0, 0);
 analogWrite(0, 255);
 analogWrite(1, 255);
 analogWrite(2, 255);
 delay(1000);
 analogWrite(0, 0);
 analogWrite(0, 0);
 analogWrite(1, 0);
 analogWrite(2, 0);
 delay(1000);
}  


Works fine.
same as this program:

void setup()  
{  
 
}

void loop()  
{  
 analogWrite(0, 0);
 analogWrite(0, 255);
 analogWrite(1, 0);
 analogWrite(1, 255);
 analogWrite(2, 0);
 analogWrite(2, 255);
 delay(1000);
 analogWrite(0, 0);
 analogWrite(0, 0);
 analogWrite(1, 0);
 analogWrite(1, 0);
 analogWrite(2, 0);
 analogWrite(2, 0);
 delay(1000);
}  


I think, Barragan:
Could you modify the analogWrite command as following:

when you use the analogWrite command,  it writes a value(from 0 to 255) to the pin.
in stead of doing that,
it should first write 0 to that pin, and than the value named in the program.

I hope this workes, it seems to work for me.
actually it is the same, but in stead of doing it in a program like this:
 analogWrite(0, 0);
 analogWrite(0, 255);

you only type:
 analogWrite(0, 255);
and it actually sets that pin to 0 first.

Not the best way to do this, but a temporary way.


Title: Re: PWM problem in 13?
Post by barragan on 12/23/07 at 18:50:48
Definitely there is a bug there and seems to be related to PWM pin 0 specifically, it will be out of there for 0014 thanks you all for the help. Ill post more as soon as I have more information on the fix. For those experts remember the source code for the PWM is here: http://wiring.uniandes.edu.co/source/trunk/wiring/core/WTimer.c?revision=394&view=markup

Title: Re: PWM problem in 13?
Post by barragan on 12/24/07 at 00:34:19
hi guys, ok got it out, nasty bug, now seems to be working fine, I tried the code you post and now the led blinks on PWM output 0. Id also like to introduce here another theme which is the resolution of the PWM, right now it is 8 bit resolution, which means you could use values between 0 and 255, for a long time Ive been thinking of making it 10 bit resolution to equals the analogRead command which will means values between 0-1023, Id like to hear your thoughts on this.
Additionally Ive been thinking of enabling the use of the real pin numbers using analogWrite, right now the pins are separated as a port called PWM on the board with pins numbered from 0 to 5, this to make things easier and comprehensible for novices but those pins are just pins 37, 36, 35, 31, 30 and 29 of the digital I/O pins in that order,
but, Wiring mini si coming soon, perhaps in late January and it doesnt have (because of space) a special PWM section, so PWM is expected to be available on those digital I/O pins directly. (Any thoughts on this?)
Thank you all for the help.
(btw I have updated the svn repository with the fix)

Title: Re: PWM problem in 13?
Post by Titus on 12/24/07 at 11:22:10
-The problem
How did you fix it? what was wrong?

-PWM, a 10 bits resolution
Since I'm used to use values from 0-1023 as PWM output, i think it is a great idea!

-Just the digital I/O pin's as the PWM, in stead of a seperated PWM port
That would not envolve the owner's of the latest wiring board, would it.
In case it would, it would only affact the software, you won't be able to analogWrite(//value from 0 to 5);? or you can add a
"select your wiring board version"menu, and let the software/compiler automaticly change the 0-5 to the matching digital port
In case of a new Wiring board version, i like what arduino did, when numbering the digital I/O pin's they put a little P  
(I thought) under the I/O pins matching the PWM pins


-Wiring mini
COOL STUFF!, we want more information about the mini!

-wiring 0014
software version update? or just a libery update?


yeah, 40 posts!

Title: Re: PWM problem in 13?
Post by barragan on 12/25/07 at 09:19:23
Fixing the problem involved changing of a data type for the analogWrite command, it was int and it should be uint16_t for the value parameter causing the problem.

Yeah I also think it makes sense to do the change to 10 bits, it will make easier for people to use analog Read/Write

Regarding the PWM port, it will be there for the normal Wiring board, the analogWrite command will work as usual with pins 0-5 but also for the 37, 36, 35, 31, 30 and 29 pins from the digital section, both numberings will coexist with the propper explanation, so no need for a select board menu, it should be the same for the Wiring and the mini version. Ill post more info on the Mini very soon, it will be cool :)

0014 will include several updates, bug fixes and addition of the SoftwareSerial and Stepper libraries. I hope to get it there in then next few days, finally got a good time spot to work on it with no interruption :)

Thanks for the feedback!

Title: Re: PWM problem in 13?
Post by eustace on 12/29/07 at 21:14:04
Thanks, that sounds great!

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