Go Down

Topic: How to stop PWM (Read 849 times) previous topic - next topic

woodinblack

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: [Select]

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?

CrossRoads

I don't see anything that turns on PWM there, ie analogWrite.  Very  odd.
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.

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

Grumpy_Mike

What ever else is the problem
Code: [Select]

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.

woodinblack



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.

woodinblack


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.

Grumpy_Mike

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.

PeterH


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?
I only provide help via the forum - please do not contact me for private consultancy.

woodinblack


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  :)

Thanks for all your help people.

Grumpy_Mike

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

woodinblack

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!

afremont

Are these pins connected to LEDs?  If so, how big is the current limiting resistor that you are using?
Experience, it's what you get when you were expecting something else.

woodinblack

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

Go Up