# Arduino LED dimmer with IR remote control

I want to make an LED dimmer with IR remote control using Arduino, by pressing each number in the remote control give different luminescence for example number 1 give 10% duty cycle and number 9 100% duty cycle. I tested many codes but all them don't work, i have following code, can you help me why it doesn't work?

#include <IRremote.h>

int RECV_PIN = 11; // the pin where you connect the output pin of TSOP4838
int led = 3;
int brightness = 0;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
Serial.begin(9600);   // you can comment this line
pinMode(led, OUTPUT);

}

void loop() {
if (irrecv.decode(&results)) {
unsigned int value = results.value;

Serial.println(value); // you can comment this line
analogWrite(led, brightness);
if(value == 6630){
brightness=25;
}

else if(value == 2550){
brightness = 255;
}
irrecv.resume();

}

}

• Have you confirmed that you're receiving the IR value correctly? – DigitalNinja Oct 3 '16 at 19:03
• I apologise, but I don't understand your question. Are you saying that the Arduino+electronics+program does not get any signal from the IR remote control transmitter (nothing, zip, nada, nowt)? If that is the case, are you sure the IR remote control transmitter is working? Have you a device which does detect a signal from the IR remote control transmitter and uses the signal to do something? Ideally it will do something different when each of 1 to 9 is pressed. – gbulmer Oct 3 '16 at 19:06
• it work good just when i put "digitalWrite(led, HIGH);" rather than "brightness= " but i want to dimmer not only ON OFF the LED. it received the signal very good but i want "brightness=" to run the LED by different duty cycle. – M.A.K Oct 3 '16 at 19:38
• If digitalwrite() does output to a single digital I/O pin, the pin can only be Off/Low or ON/High - it can't output a variable voltage to make the LED bright or dim. – Peter Bennett Oct 3 '16 at 20:26
• Yes i know, i just use digitalwrite() for testing if the components working or no, i must use "brightness=" command to make it dimmer but it don't work :( that prove that my coding is right just "brightness=" command are used incorrect, maybe i must define the range of duty cycle 0 to 255 in the programming code. – M.A.K Oct 3 '16 at 20:35

First prove that the IR remote control is working.

You should be able to see the IR LED light using the camera on a smart phone or a digital camera.

If you have a device that the IR remote control works with, use that to test it.

Then simplify the program.
Try something which shows it is receiving an IR signal.

#include <IRremote.h>

const int RECV_PIN = 11; // the pin where you connect the output pin of TSOP4838
const int led = 3;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
Serial.begin(9600);   // you can comment this line
pinMode(led, OUTPUT);
}

void loop() {
if (irrecv.decode(&results)) {
digitalWrite(led, HIGH);
Serial.println(results.value, DEC);
irrecv.resume(); // Receive the next value
} else {
digitalWrite(led, LOW);
}
}


If this works, there is evidence that the IR remote is working. So a likely problem is the codes sent for each of '1' to '9' is different from the codes the program is trying to match. For example, if(value == 6630) is looking for a code of 6630, but if the IR remote control doesn't send that when one of its '1' to '9' buttons is pressed, then a button will not be recognised.

To find out which codes are being sent by the IR remote control, open the Arduino IDE's serial monitor, and see which codes are printed when you press each of the '1' to '9' buttons. Press them one at a time, and make a note of the values, then repeat several times to ensure they are reasonably reliable.

Once you have these codes, update the program, to match, and it should work. For example change if(value == 6630){ brightness=25; } to have the code for IR remote control button '1'.

The program in the question uses brightness=25 to brightness=255.

Use a smaller range initially, as very low levels of brightness might be hard to see.

I suggest starting at brightness=100 for '1' to brightness=255 for '9' initially, and then extend that down if 100 is too bright.

Human eye sensitivity isn't linear, but logarithmic, so getting good brightness levels might be quite difficult.

You might have a slightly easier to manage program if you could put your codes and values in a table. I would normally use an array of structs, but it might be easier to understand the code with two simple arrays:

int button_codes[10] = {
0, // unused
6630, // '1'
// other codes
2550, // '9'
};

unsigned char brightness[10] = {
0, // unused
100, //'1'
// other matching brightness values
255,
};

// ...

void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, DEC);
for (int i=0; i<0; i+=1) {
if (results.value == button_codes[i]) {  // find a matching code
analogWrite(led, brightness[i]);    // set LED to corresponding value
}
}
irrecv.resume(); // Receive the next value
}
}


This has the small benefit that all of the brightness values are in one place. Also, it might be useful to use the brightness table to allow you to use, say, up and down buttons to increase and decrease the LEDs brightness.

If this test program doesn't work, and the lED doesn't light when an IR key is pressed, then the obvious options to eliminate are:

• IR remote control isn't working properly
• IR remote control is working but it's modulation frequency is sufficiently different from the IR receiver that it is being ignored (not very likely)
• The wiring is broken
• The program is broken
• it work good just when i put "digitalWrite(led, HIGH);" rather than "brightness= " but i want to dimmer not only ON OFF the LED. it received the signal very good but i want "brightness=" to run the LED by different duty cycle. – M.A.K Oct 3 '16 at 19:41
• Ah, good! So you are sure you have evidence that the IR remote control is working, and the LED goes off when you release buttons on the IR remote control? If that works, then open the Arduino IDE's serial monitor, and see which (hex) codes are printed when you press each of the '1' to '9' buttons. You will need to put those codes into your program, prefixed with '0x'. I've changed my program so the line Serial.println(results.value, HEX); is Serial.println(results.value, DEC); to make it easier to update your program, these numbers are ordinary decimal , so no '0x'. – gbulmer Oct 3 '16 at 19:49
• Okay, I've updated my answer. I think the problem may be the codes your program expects from the '1' to '9' buttons aren't the codes the IR remote is sending. – gbulmer Oct 3 '16 at 20:00
• NO, that code is correct, if not so why when i put "digitalWrite(led, HIGH);" rather than "brightness= " it works good! I think i used "brightness= " command incorrect maybe i must define the range of duty cycle 0 to 255 in the programming code – M.A.K Oct 3 '16 at 20:10
• Okay, if the code is correct, then brightness 25 might be too low. I think it would help to change the program so that it is easier to debug it, and to manipulate the brightness levels. If you understand how to use arrays, then I have added a program which gathers all of the brightness values into one table, making it easier to find them and adjust the values. – gbulmer Oct 3 '16 at 22:27