Go Down

Topic: Arduino Uno and PWM on 11 & 3 (Read 10136 times) previous topic - next topic

Sparkinman

I just realized that I don't have PWM functions on pins 11 and 3 (they work High and Low just fine). I'm using Arduino 0021, am I doing anything wrong?

retrolefty

We would have to see the code you use to determine those two pins are not working with analogWrite(). They should if you are using a mega328 based board. http://arduino.cc/en/Reference/AnalogWrite


Sparkinman

Here is my code, it's basically just turning a few LED's on and off and dimming them from an IR remote.  The other pins work fine (and if I move my led from 11 or 3 to say 6 it works as well).
Code: [Select]

#include <IRremote.h>
int RECV_PIN = 12;
int codevalue;
int ledpower;
byte ledpin[]={3,10,9};
int ledselect;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  irrecv.enableIRIn();
for (int x=0; x<3; x++) {
pinMode(ledpin[x], OUTPUT); }


}
void loop(){

  if (irrecv.decode(&results)){
  codevalue=results.value;
    switch(codevalue) {
   
   case -30601:
     if (digitalRead(ledpin[0])==HIGH) {
    digitalWrite(ledpin[0], LOW);
        ledpower=0;
     }
     else {
       digitalWrite(ledpin[0], HIGH);
       ledpower=255;
     }
     ledselect=0;
     break;
 
  case 18615:
    if (digitalRead(ledpin[1])==HIGH) {
    digitalWrite(ledpin[1], LOW);
        ledpower=0;
     }
     else {
       digitalWrite(ledpin[1], HIGH);
       ledpower=255;
     }
     ledselect=1;
     break;
 
  case -14281:
    if (digitalRead(ledpin[2])==HIGH) {
    digitalWrite(ledpin[2], LOW);
    ledpower=0;
     }
     else {
       digitalWrite(ledpin[2], HIGH);
       ledpower=255;
     }
     ledselect=2;
     break;
 
  case 4335:
     for (int x=0; x<3; x++) {
digitalWrite(ledpin[x], LOW); }
    break;
   
   case -16321:
   if (ledpower!=0 && ledpower!=15) {
    ledpower=ledpower-20;
    analogWrite (ledpin[ledselect],ledpower);
    }
    if (ledpower==15) {
      ledpower=0;
      digitalWrite(ledpin[ledselect],LOW);
    }
   
    break;
   
    case 16575:
  if (ledpower!=0 && ledpower!=255) {
    ledpower=ledpower+20;
    analogWrite (ledpin[ledselect],ledpower);
    }
    if (ledpower==255) {
      ledpower=255;
      digitalWrite(ledpin[ledselect],HIGH);
    }
   
    break;
    }
   
    irrecv.resume();
 
}
}

retrolefty

I suspect that the <IRremote.h> library may be utilizing some of the timers that the PWM command uses to control pins 3 & 11.
Why don't you just load the example program Fading from the Arduino IDE file menu. Then modify the
int ledPin = 9;    // LED connected to digital pin 9  line to use pin 3 and then pin 11 and see if the pins work properly. Be sure to use series current limiting resistor with an LED if that is how you are testing for PWM outputs.

Lefty

Code: [Select]
/*
Fading

This example shows how to fade an LED using the analogWrite() function.

The circuit:
* LED attached from digital pin 9 to ground.

Created 1 Nov 2008
By David A. Mellis
Modified 17 June 2009
By Tom Igoe

http://arduino.cc/en/Tutorial/Fading

This example code is in the public domain.

*/


int ledPin = 9;    // LED connected to digital pin 9

void setup()  {
  // nothing happens in setup
}

void loop()  {
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(30);                           
  }

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(30);                           
  }
}




westfw

This was one of the things fixed in between 0021 and 0022, IIRC.
(Hmm.  Maybe that was 9 and 10...  http://code.google.com/p/arduino/issues/detail?id=364 )

Sparkinman

Okay, that works, so obviously not a problem with the board..wish I would have tried that first!
Guess it's either my code, or the library....I did upgrade to 22... same problem!

retrolefty

Quote
Guess it's either my code, or the library....I did upgrade to 22... same problem!


Most likely the library. There are only so many internal hardware timers avalible and one can't use all possible functions in the same sketch at all times.

Lefty


liudr

I also discovered, through my own stupidity, that tone breaks PWM on pins 3 and 11. I should have read every aspect about tone() before using it but it's so attempting that I grab something and use it without knowing its limitations. So if you do this, it won't work, the pin won't turn on:

tone(3,550);
analogWrite(3,255);
delay(250);
noTone(3);

I had to use digitalWrite(3,HIGH);

Go Up