Pages: [1]   Go Down
Author Topic: How to stop PWM  (Read 808 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 3
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just got my first Arduino today (UNO), so my first project is to drive a led panel.
For this I need 9 outputs.

From what I see on the basic instructions, setting pinMode(blah, OUTPUT) should turn it into a digital output, which it does for 0,1,2,4,7,8,12 & 13). The other outputs (the ones marked with PWM) are outputting PWM. Clearly I must be doing something obvious as noone else seems to have this issue, and I couldn't find anything about it (other than how to make them work as PWM, not how to stop them doing it).

I have a program that just does:
Code:
void setup()
{
  for(int i = 0; i < 14; i++)
    pinMode(i, OUTPUT);
}

void loop()
{
  while(1)
  {
    for(int led = 0; led < 14; led++)
    {
      for(int phase = 0; phase < 100; phase++)
      {
        digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
        digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
      }
    }
  }
}

which to my understanding should in turn pulse the digital outputs 100 times for each output. For all the pins that are not PWM, that is what happens. For the PWM pins it just puts a varying PWM pulse for about the time of the other outputs cycle.

What do I need to do to turn the PWM off and use them as a normal digital output please?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27418
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't see anything that turns on PWM there, ie analogWrite.  Very  odd.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

which to my understanding should in turn pulse the digital outputs 100 times for each output. For all the pins that are not PWM, that is what happens. For the PWM pins it just puts a varying PWM pulse for about the time of the other outputs cycle.
How did you establish the part in bold?

What do I need to do to turn the PWM off and use them as a normal digital output please?
Nothing.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34724
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What ever else is the problem
Code:
for(int i = 0; i < 14; i++)
    pinMode(i, OUTPUT);
Do not use pins 0 and 1 for outputs as these handle the serial input / output.
Logged

Offline Offline
Newbie
*
Karma: 3
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

which to my understanding should in turn pulse the digital outputs 100 times for each output. For all the pins that are not PWM, that is what happens. For the PWM pins it just puts a varying PWM pulse for about the time of the other outputs cycle.
How did you establish the part in bold?

With an oscilloscope going through each pin. The non PWM pins look as expected, a series of 100 pulses that were about 4uS each, follow by a long gap, the PWM pins just have 1 full cycle about the same size of the the 100 pulses of the other pins.
Logged

Offline Offline
Newbie
*
Karma: 3
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do not use pins 0 and 1 for outputs as these handle the serial input / output.

ok, why is that? do they not also work as normal ports (they seem to in this case)?


Strangely enough changing to a different program (swapping round the 0-100 and 0-14 loops) gives me the response I was expecting, without PWM issues.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34724
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
do they not also work as normal ports
They do but they are wired to the USB chip as well so you are bashing that up and down and you are driving two outputs together which is never good, and if you wire anything up to it then that can interfere with uploading code.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the PWM pins just have 1 full cycle about the same size of the the 100 pulses of the other pins.

That's not what I would have expected. Since you're set up to test it, could you try putting a small delay after each digitalWrite to lengthen the pulse and see whether the pwm and non-pwm output pins then behave the same?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 3
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Since you're set up to test it, could you try putting a small delay after each digitalWrite to lengthen the pulse and see whether the pwm and non-pwm output pins then behave the same?

if I put a 3uS delay between the on and off, the PWM disappears. If I then upload the original code, it still isn't back. If I turn it off and on again with the original, it is back.
Very weird, but since putting a delay in it stops it happening, and the actual program I want to use has a lot more delays than that, it doesn't affect me much. It is still a puzzle, but not one that is going to cause me an issue, so I will just get on with fiddling with this thing  smiley

Thanks for all your help people.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34724
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you sure it was not aliasing in your scope?
Is it a digital one?
Logged

Offline Offline
Newbie
*
Karma: 3
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes it is a digital scope and I did think of that (I have an analogue one in the cupboard but it has 10 years of junk on it, assuming it still works!) as it is pretty low res (one of the velleman ones), but I noticed the effect before knowing their were PWM on those pins as I got the board yesterday, it was only when I was getting that waveform I noticed it was connected to the pins that had a little dash on them, then noticed the bit that said PWM.

I will see if I can drag the analogue scope out at some point and see if the effect is the same, although I have enough problems with my LED board so maybe it will be later than sooner!
Logged

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are these pins connected to LEDs?  If so, how big is the current limiting resistor that you are using?
Logged

Experience, it's what you get when you were expecting something else.

Offline Offline
Newbie
*
Karma: 3
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, sorry, a little confusion there, the original pins were not connected to anything, the leds I mentioned in my last post were in my next project that I was moving onto, and they were driven separately
Logged

Pages: [1]   Go Up
Jump to: