Pages: [1]   Go Down
Author Topic: Arduino Uno and PWM on 11 & 3  (Read 8149 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16523
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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();
 
}
}
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16523
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 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:
/*
 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);                           
  }
}


Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6373
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16523
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6908
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged


Pages: [1]   Go Up
Jump to: