Go Down

Topic: Does IR library disable PWM on Pin3? (Read 1 time) previous topic - next topic

JimboZA

Jun 23, 2012, 05:54 pm Last Edit: Jun 23, 2012, 05:59 pm by JimboZA Reason: 1
Hi all,

(Edit: posted it here because the IR sensor is a sensor.... might not be the best part of the forum)

Got a strange one here...

Running the IR library of Ken Shirriff... the sketches work a treat, to show what key on my remote is being pressed.

So then I thought I'd make a key actually do something, and decided to use the volume keys to control an LED's brightness. Added a few lines to Ken's code (see below) to do an analog write to pin 3 with a value which is increased or decreased when the volume up or down key is pressed.

The LED on pin 3 turned out to be either on or off though, and nothing in between. When I upped or downed the volume the LED was off unless I pressed the key to get up to the max of 255. I changed my code to use PWM on 5, moved the LED, and Lo! my LED dims and brightens as expected.

Thinking my pin 3 might be faulty and have lost its PWMability, I wrote a simple sketch to send arbitrary values between 0 and 255 to pin 3 and the LED brightness changes as expected.

So, I'm wondering if something in the IR library has disabled PWMing on pin 3?

Here's the code, but bear in mind with led_pin set to 5 it works as expected; with led_pin = 3, the led is on or off. (Edit: The serial print shows the correct brightness between 10 and 255, and that we're going up or down)

Code: [Select]
/*
* IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
* An IR detector/demodulator must be connected to the input RECV_PIN.
* Version 0.1 July, 2009
* Copyright 2009 Ken Shirriff
* http://arcfn.com
*/

/* well now it controls the brightness of an LED
  LED is PWM'd on pin5
  *******   PWM on pin3 seems disabled when using IR?
  uses the volume key... up to brighten, down to dim
  under rc5, the keys have two toggled values
  down:    411 or c11 hex; 1041 or 3089 dec
  up:      410 or c10 hex; 1040 or 3088 dec
*/
             

#include <IRremote.h>

int RECV_PIN = 11;
int led_pin = 5;   // seems PWM not work on 3 with IR library
int led_bright = 130;  //half way to start
int led_step = 10;     // and change in pwm steps of 10

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
 Serial.begin(9600);
 irrecv.enableIRIn(); // Start the receiver
 pinMode(led_pin, OUTPUT);
 analogWrite(led_pin, led_bright);
}

void loop() {
 if (irrecv.decode(&results)) {
   Serial.println(results.value, HEX);
   irrecv.resume(); // Receive the next value
   
   //now check if it's correct key and act...
   //down
   if (results.value == 1041 || results.value == 3089) {
     led_bright = led_bright - led_step;
     if (led_bright < 10) {
       led_bright = 10;
       }
     Serial.print("Going down to ");
     Serial.println(led_bright);
     analogWrite(led_pin, led_bright);
   }
   
   //up
   if (results.value == 1040 || results.value == 3088) {
     led_bright = led_bright + led_step;
     if (led_bright > 255) {
       led_bright = 255;
     }
     Serial.print("Going up to ");
     Serial.println(led_bright);
     analogWrite(led_pin, led_bright);
   }
 }
}

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

JimboZA

PS it works on PWM pins 6, 9 and 10 too.

And just in case I was going mad, I re-tested PWM on pin 3 with a "non-IR" sketch as below, and it works. So the pin isn't stuffed.


Code: [Select]
//

void setup()
{
  pinMode(3, OUTPUT);
}

void loop()
{
analogWrite(3, 10);
delay(1000);
analogWrite(3, 100);
delay(1000);
analogWrite(3, 200);
delay(1000);
analogWrite(3, 100);
delay(1000);
analogWrite(3, 10);
delay(1000);
}

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

JimboZA

Ah I think I might have my own answer....

Reading Ken's blog, I see that to send IR he uses PWM on pin3. I wasn't using the send facility of the IR library, but I'm guessing that the facility to send IR has somehow interfered with normal PWM on pin 3?

If that's true, and I don't need to send IR, can I do something to disable that interference and get the pin back? Pins are few!

(Was in a local Arduino supplier this morning and the guy before me was buying a Mega... I think my next one will be one of those- all those pins!)

Jim
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

Go Up