[SOLVED]IRLibrary not working

Hi friends, I downloaded this library and it works to read sansung IR codes, i get the codes.. :
Power E0E040BF HDMI E0E0D12E Source E0E0807F
1 E0E020DF 2 E0E0A05F 3 E0E0609F
4 E0E010EF 5 E0E0906F 6 E0E050AF
7 E0E030CF 8 E0E0B04F 9 E0E0708F

  • E0E0C43B 0 E0E08877 pre-ch E0E0C837

Ok, but when i try repeat the codes, using the example IRrecod, STATUS_PIN blink so fast and i can't see your light and IRLed dont turn on (tested with my cell camera), but when I record a RAW code (Home Theater), STATUS_PIN and IRLed blink normally... If i put my remote control SANSUMG on my camera, i can see the led working, but when i put in IRLed on Arduino, i see nothing... The library is working to write this codes?
Code:

/*

 * Modified by Chris Targett
 * Now includes more protocols
 * Novemeber 2011

 * IRrecord: record and play back IR signals as a minimal 
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * An IR LED must be connected to the output PWM pin 3.
 * A button must be connected to the input BUTTON_PIN; this is the
 * send button.
 * A visible LED can be connected to STATUS_PIN to provide status.
 *
 * The logic is:
 * If the button is pressed, send the IR code.
 * If an IR code is received, record it.
 *
 * Version 0.11 September, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

#include <IRremote.h>

int RECV_PIN = 11;
int BUTTON_PIN = 12;
int STATUS_PIN = 13;

IRrecv irrecv(RECV_PIN);
IRsend irsend;

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(BUTTON_PIN, INPUT);
  pinMode(STATUS_PIN, OUTPUT);
}

// Storage for the recorded code
int codeType = -1; // The type of code
unsigned long codeValue; // The code value if not raw
unsigned int rawCodes[RAWBUF]; // The durations if raw
int codeLen; // The length of the code
int toggle = 0; // The RC5/6 toggle state

// Stores the code for later playback
// Most of this code is just logging
void storeCode(decode_results *results) {
  codeType = results->decode_type;
  int count = results->rawlen;
  if (codeType == UNKNOWN) {
    Serial.println("Received unknown code, saving as raw");
    codeLen = results->rawlen - 1;
    // To store raw codes:
    // Drop first value (gap)
    // Convert from ticks to microseconds
    // Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
    for (int i = 1; i <= codeLen; i++) {
      if (i % 2) {
        // Mark
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
        Serial.print(" m");
      } 
      else {
        // Space
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
        Serial.print(" s");
      }
      Serial.print(rawCodes[i - 1], DEC);
    }
    Serial.println("");
  }
  else {
    if (codeType == NEC) {
      Serial.print("Received NEC: ");
      if (results->value == REPEAT) {
        // Don't record a NEC repeat value as that's useless.
        Serial.println("repeat; ignoring.");
        return;
      }
    } 
    else if (codeType == SONY) {
      Serial.print("Received SONY: ");
    } 
    else if (codeType == RC5) {
      Serial.print("Received RC5: ");
    } 
    else if (codeType == RC6) {
      Serial.print("Received RC6: ");
    } 
	else if (results->decode_type == SAMSUNG) {
      Serial.print("Decoded SAMSUNG: ");
    }
    else if (results->decode_type == JVC) {
	  Serial.print("Decoded JVC: ");
    }
    else if (results->decode_type == PANASONIC) {
	  Serial.print("Decoded Panasonic: ");
    }
    else {
      Serial.print("Unexpected codeType ");
      Serial.print(codeType, DEC);
      Serial.println("");
    }
    Serial.println(results->value, HEX);
    codeValue = results->value;
    codeLen = results->bits;
  }
}

void sendCode(int repeat) {
  if (codeType == NEC) {
    if (repeat) {
      irsend.sendNEC(REPEAT, codeLen);
      Serial.println("Sent NEC repeat");
    } 
    else {
      irsend.sendNEC(codeValue, codeLen);
      Serial.print("Sent NEC ");
      Serial.println(codeValue, HEX);
    }
  } 
  else if (codeType == SONY) {
    irsend.sendSony(codeValue, codeLen);
    Serial.print("Sent Sony ");
    Serial.println(codeValue, HEX);
  } 
  else if (codeType == RC5 || codeType == RC6) {
    if (!repeat) {
      // Flip the toggle bit for a new button press
      toggle = 1 - toggle;
    }
    // Put the toggle bit into the code to send
    codeValue = codeValue & ~(1 << (codeLen - 1));
    codeValue = codeValue | (toggle << (codeLen - 1));
    if (codeType == RC5) {
      Serial.print("Sent RC5 ");
      Serial.println(codeValue, HEX);
      irsend.sendRC5(codeValue, codeLen);
    } 
    else {
      irsend.sendRC6(codeValue, codeLen);
      Serial.print("Sent RC6 ");
      Serial.println(codeValue, HEX);
    }
  } 
  else if (codeType == UNKNOWN /* i.e. raw */) {
    // Assume 38 KHz
    irsend.sendRaw(rawCodes, codeLen, 38);
    Serial.println("Sent raw");
  }
}

int lastButtonState;

void loop() {
  // If button pressed, send the code.
  int buttonState = digitalRead(BUTTON_PIN);
  if (lastButtonState == HIGH && buttonState == LOW) {
    Serial.println("Released");
    irrecv.enableIRIn(); // Re-enable receiver
  }

  if (buttonState) {
    Serial.println("Pressed, sending");
    digitalWrite(STATUS_PIN, HIGH);
    sendCode(lastButtonState == buttonState);
    digitalWrite(STATUS_PIN, LOW);
    delay(50); // Wait a bit between retransmissions
  } 
  else if (irrecv.decode(&results)) {
    digitalWrite(STATUS_PIN, HIGH);
    storeCode(&results);
    irrecv.resume(); // resume receiver
    digitalWrite(STATUS_PIN, LOW);
  }
  lastButtonState = buttonState;
}

First of all, the status pin change is fast because you have no significant delay between setting it HIGH and then LOW.
Try to add a delay of 200ms or more before the ' digitalWrite(STATUS_PIN, LOW);' - just for debugging.

Secondly, if you are driving the IR LED directly from the Arduino pins, the signal will be weak and harder to see in a digital camera.
If this is the case try to send the IR signal 10 or more times in a loop, maybe then you will be able to see it.
Also, try to bring your IR LED with 30cm of the TV as the range may be very short.

Otherwise, please describe your circuit for driving the LED.

AnalysIR:
First of all, the status pin change is fast because you have no significant delay between setting it HIGH and then LOW.
Try to add a delay of 200ms or more before the ' digitalWrite(STATUS_PIN, LOW);' - just for debugging.

Secondly, if you are driving the IR LED directly from the Arduino pins, the signal will be weak and harder to see in a digital camera.
If this is the case try to send the IR signal 10 or more times in a loop, maybe then you will be able to see it.
Also, try to bring your IR LED with 30cm of the TV as the range may be very short.

Otherwise, please describe your circuit for driving the LED.

Thanks... But dont work. I tried put a loop of 15 to send the code... and i cant see IRLed in on state..
I'm using a resistor of 120 Ohms with led on pin 3.. http://arcfn.com/images/ir-schematic.png
If i use my Remote Control of Home Theater, it works and i can see IRLed of arduino and control blinking...

Received unknown code, saving as raw
 m2700 s4550 m650 s450 m650 s500 m650 s500 m650 s1500 m650 s500 m600 s500 m600 s550 m550 s600 m550 s600 m500 s600 m550 s600 m500 s650 m500 s1650 m500 s1650 m500 s1700 m450 s1700 m450 s700 m450 s1700 m450 s1750 m400 s1750 m450 s700 m400 s700 m450 s700 m450 s700 m450 s650 m450 s1750 m450 s1700 m450 s1700 m450 s700 m450 s700 m450 s700 m400 s700 m450
Pressed, sending
Sent raw
Sent raw
...
Sent raw
Released

But, when i use Remote Control of Sansumg TV, it dont work... and i can see only IRLed of control blinking, arduino dont blink... (i tried put a loop of 15...).

Decoded SAMSUNG: E0E0E01F
Decoded SAMSUNG: E0E0E01F
Decoded SAMSUNG: E0E0E01F
Pressed, sending
Released
Pressed, sending
Released

I do a delay between STATUS_LED on and off, (delay(200)) and ok.. But, i think, when i use Sansumg code, it blink so so so so fast... or no... I dont have a osciloscope to see..

Add dump:

3772833823
Decoded SAMSUNG: E0E0E01F (32 bits)
Raw (68): 29614 4700 -4300 750 -1500 750 -1500 750 -1500 750 -400 750 -350 750 -400 750 -350 750 -400 750 -1450 800 -1500 750 -1450 800 -350 750 -350 800 -350 750 -350 750 -400 750 -1500 750 -1500 750 -1500 750 -350 750 -400 750 -350 750 -400 750 -350 750 -400 750 -400 650 -450 650 -1600 650 -1600 650 -1600 600 -1650 600 -1650 600 
3772795063
Decoded SAMSUNG: E0E048B7 (32 bits)
Raw (68): -5062 4750 -4300 750 -1500 750 -1500 750 -1500 750 -350 750 -400 750 -350 750 -400 750 -350 750 -1500 750 -1500 750 -1500 750 -350 800 -350 750 -350 800 -350 750 -350 800 -350 750 -1500 750 -350 750 -400 750 -1500 750 -350 750 -400 750 -350 750 -1500 750 -400 650 -1600 650 -1600 650 -500 600 -1600 650 -1600 650 -1600 650 
3772795063
Decoded SAMSUNG: E0E048B7 (32 bits)
Raw (68): -14314 4800 -4250 750 -1500 750 -1450 800 -1450 800 -350 750 -350 750 -400 750 -400 700 -400 650 -1600 650 -1600 650 -1600 650 -500 600 -500 650 -500 600 -500 600 -550 600 -500 600 -1650 600 -550 550 -550 550 -1700 550 -600 550 -550 550 -600 550 -1700 550 -550 550 -1700 550 -1700 550 -600 550 -1650 600 -1650 600 -1650 550 
492363578
Unknown encoding: 1D58DF3A (32 bits)
Raw (68): -8728 2800 -4400 800 -350 750 -400 750 -350 800 -1350 800 -350 750 -400 750 -350 800 -350 750 -400 750 -350 800 -350 750 -400 750 -1400 750 -1400 750 -1400 800 -1400 750 -1400 750 -400 750 -1400 750 -1400 800 -350 750 -400 750 -400 750 -350 800 -1400 750 -350 750 -1450 650 -1500 650 -500 650 -500 650 -500 600 -500 650 
955479388
Unknown encoding: 38F3755C (32 bits)
Raw (68): 32672 2800 -4450 700 -450 650 -450 650 -500 650 -1500 650 -500 650 -500 600 -500 650 -500 600 -550 600 -500 600 -550 600 -550 550 -1600 550 -1600 600 -1600 550 -1600 550 -600 550 -550 600 -550 550 -1600 600 -1600 550 -550 600 -550 550 -600 550 -600 550 -550 600 -550 550 -1600 600 -1600 550 -550 600 -550 550 -600 550

Whats happened?

How do you know if the status pin is on? If you are using a multimeter, then increase the delay to 2 seconds for testing only, as a DMM may miss a pulse of 200ms.

You will only get a very weak IR signal from the IR led when driving directly form the Arduino. If you add a transistor etc, you will get much better range.
It is easier to see a weak IR signal with a camera if the room is darker.

How have you verified that the IR LED is connected correctly. (Where is the longer lead connected to? GND or resistor?)
Is it possibile your IR LED is connected the wrong way around?

Did you try sending another protocol, just to test if there is something wrong with the Samsung code?

If all else fails, take a few steps back and get a simple demo example working with IRremote, this will confirm if your wiring & components are OK. Then add extra features and continue testing until you get to where you are now.

How do you know if the status pin is on? If you are using a multimeter, then increase the delay to 2 seconds for testing only, as a DMM may miss a pulse of 200ms.

When i receive raw codes, status blink more slow and i can see blinking..

You will only get a very weak IR signal from the IR led when driving directly form the Arduino. If you add a transistor etc, you will get much better range.
It is easier to see a weak IR signal with a camera if the room is darker.

I am in my room, and it is very darker, but i cant see IRLed blinking.. Only with "RAW" code!

How have you verified that the IR LED is connected correctly. (Where is the longer lead connected to? GND or resistor?)
Is it possibile your IR LED is connected the wrong way around?

I test it, and work's.. I can see led on when i put +5V signal on resistor... Anode in resistor and on pin 3 and cathode to GND.

Did you try sending another protocol, just to test if there is something wrong with the Samsung code?

I can sed RAW codes to my Home Theater and work's..
I have a SMART TV SANSUMG and a BDPlayer SANSUMG. TV decodes and type is SANSUMG, but when i send command, i can't see IRLed blinkin with my camera... BDPlayer decodes and type is RAW, but when i send command, i CAN SEE IRLed blinking but BDPlayer do nothing.. and Home Theater decodes and type is RAW and I CAN SEE IRLed blinking and Home do the command.

If all else fails, take a few steps back and get a simple demo example working with IRremote, this will confirm if your wiring & components are OK. Then add extra features and continue testing until you get to where you are now.

I wanna know how it work's, but i think to use other library... maybe one wich work's with my devices... Can I use your library to do this? Sorry for my english and thank's a lot for your help.

one thing I noticed is that...you must re-enable 'irrecv' after every 'irsend' .

e.g. irrecv.resume(); // resume receiver

..because rx is automatically disabled when sending to avoid loops.

So try putting 'irrecv.resume(); // resume receiver' ...
after after sending IR = after 'sendCode' or at the end of that function.

If that doesnt work...remove all the code for protocols you are not using and add lots of Serial.println statements to help debugging.

120 ohms to a led (especially IR led with lowest threshold V) from a 5V pin is not enough, the life will be shortened.

IR receiver doesn't need bright-bright IR to work, even reflected off walls. 220 ohm is the 'standard' resistor used for leds and even that makes a very bright light. The more resistance you can get away with the less current drawn which if you use batteries makes a real difference.

Try using a red led and 220 ohm resistor for testing. Red leds put out a lot of IR, look at one with your camera and see if it shows as red or white.

Good idea about the red LED - I think the OP has a color LED on the status pin for a similar effect.

The OP also said the circuit is working when he sends RAW timings with another script, so it appears the circuit is OK.

Your point about the resistor, is valid. However, it assumes the Arduino will deliver the required current via the pin, which it may not?

So if he is getting 20 or 40 mA from the Pin it averages out to about 25% of that or 5-10 mA. (allowing 50% modulation duty cycle & 50:50 mark/space timings)

Normal colour leds are often 'rated' up to 20mA, but some IR LEDs can be driven at 100mA, 200mA and can be rated up to 1A for short burst (I believe).

In my experience, driving an IR LED from an Arduino pin, achieves short range, which I have presumed was because the pin delivers a low-ish current. Consequently, I have preferred to use a transistor circuit with IR LEDs in anything other than prototyping.

However, the lifespan of a LED is probably increased at a lower current rather than the maximum rating.(which is a max and not a target)

You don't want to pull more than 20 mA from an I/O pin (or 200 mA total from Arduino) but 20 mA really is plenty for an ordinary led in most situations. 10 mA is plenty for all that matters.

The resistor is there as much or more for protecting the pin as the led but IIRC you can burn an ordinary led from a pin quicker than vice-versa. Who would -want- to find out different? Try a 1k resistor with a red led just to see what that looks like.

For a final product, use a resistor (220 ohm) and a 1k or higher trim pot in series. See what you can get away with and if background light makes a difference.

Just how the receivers determine background IR level, or if they do, seems interesting to me.

So try putting 'irrecv.resume(); // resume receiver' ...
after after sending IR = after 'sendCode' or at the end of that function.

Dont work... ='(

120 ohms to a led (especially IR led with lowest threshold V) from a 5V pin is not enough, the life will be shortened.

IR receiver doesn't need bright-bright IR to work, even reflected off walls. 220 ohm is the 'standard' resistor used for leds and even that makes a very bright light. The more resistance you can get away with the less current drawn which if you use batteries makes a real difference.

I was using a 220 ohm resistor, but i replace for 120 to increase the bright, but now i returned to 220 ohm... Thanks...

Try using a red led and 220 ohm resistor for testing. Red leds put out a lot of IR, look at one with your camera and see if it shows as red or white.

Sorry, i dont understand... Test with a normal RED Led? If yes, i tried and with Samsung codes dont work, only with RAW format.. and IRLed in my camera shows white light and RED Led show me a red light in camera...

In my experience, driving an IR LED from an Arduino pin, achieves short range, which I have presumed was because the pin delivers a low-ish current. Consequently, I have preferred to use a transistor circuit with IR LEDs in anything other than prototyping.

However, the lifespan of a LED is probably increased at a lower current rather than the maximum rating.(which is a max and not a target)

Tomorrow I will try with a transistor, but i think the problem is the protocol, I will try send RAW code over Samsung..

The resistor is there as much or more for protecting the pin as the led but IIRC you can burn an ordinary led from a pin quicker than vice-versa. Who would -want- to find out different? Try a 1k resistor with a red led just to see what that looks like.

Thanks..

wizarcl:

Try using a red led and 220 ohm resistor for testing. Red leds put out a lot of IR, look at one with your camera and see if it shows as red or white.

Sorry, i dont understand... Test with a normal RED Led? If yes, i tried and with Samsung codes dont work, only with RAW format.. and IRLed in my camera shows white light and RED Led show me a red light in camera...

I've found that ordinary cheap red leds put out a lot of IR, but those showed up white on cheap CCD cameras even with the built-in IR filter.

Do the Samsung codes work with any led you have? I expect that RAW shows you that the receiver is getting signal but maybe Samsung codes use shorter ON times?

I have a cheap IR receiver (cost me 60 cents) but got no luck using a library/code made for another unit so I put it aside. I should get back to that, it is wireless for less and I do want to make a universal 'learning' remote. That RAW mode is probably the ticket I want.

GoForSmoke:
Do the Samsung codes work with any led you have?

No. With Samsung codes I can't see any led working.. IR Led or other not work.

I expect that RAW shows you that the receiver is getting signal but maybe Samsung codes use shorter ON times?

I dont know, because, I think, the original code (remote control) isn't so fast, because I can see IR Led blinking with my camera, but the code provides of Arduino, maybe so fast and it dont work... :~

I have a cheap IR receiver (cost me 60 cents) but got no luck using a library/code made for another unit so I put it aside. I should get back to that, it is wireless for less and I do want to make a universal 'learning' remote. That RAW mode is probably the ticket I want.

I think the better way is using RAW code...

All that you see when you see any light from the led (see IR with camera) are likely the zero bits but maybe the ones. The led flickers at 38 KHz as part of the IR standard. You can't begin to see that fast but if the led is on enough of the time you will see light.

Can you get the Arduino to turn a Samsung device on using RAW code? It seems that you have trouble not with reading codes but sending them. Does your IR led work?

So try putting 'irrecv.resume(); // resume receiver' ...
after after sending IR = after 'sendCode' or at the end of that function.

Sorry, I should have said

"...you need to use the irrecv.enableIRIn() after every send before you can receive again"

so try to put
irrecv.enableIRIn()
after after 'sendCode' or at the end of the 'sendCode' function

May I ask on which pin did you put your IR Led?

Found the error. Added:

 else if (codeType == SAMSUNG) {
    irsend.sendSamsung(codeValue, codeLen);
    Serial.print("Sent Samsung ");
    Serial.println(codeValue, HEX);
  }

Autor of library dont include in example a method to send Samsung types... I saw inside IRremote.h wath method send to Samsung and add in sketch. Now it work's but I needed change 220 ohm resistor to 100 ohm, because with 220 ohm, the bright of IR Led isn't enought to Samsung receiver.
Next step is add a transistor to active IR Led...
Thank you so much guys...

Great - seems so obvious now that you solved it yourself :slight_smile:

You might try 2 IR leds or find one with a tighter focus or brighter element. The remotes I have use 3mm leds and the IR leds I have are 5mm... but are they brighter?

Still, at 100 ohms the led you have should last a good while since it's flickering only short periods.

I've seen a 555 project that pulsed 9V through a red led with low resistance to get really bright fake laser light (it used a tube collimator for focus) and that was supposed to last long enough for fun. A 50,000 to 80,000 hour led is that many continuous hours. Even at half life, when you run an average 50% duty cycle it should last at least as long. When it's only pulsing for a few millis here and there, even if the life is greatly reduced it will still be a good while before it quits. The pin is the real concern and using a transistor protects that.

AnalysisIR, there's cheap 5 mW red laser modules out, supposed to be good 1000m. A bag of 10 is $4.60, shipped! The constant current circuit is built in, they run fine on 4.5V and 5V. Has your shop done anything with lasers? I've wondered, but I don't want to burn a receiver up, if a laser and IR receiver might make a cheap long range modem.

GoForSmoke:
AnalysisIR, there's cheap 5 mW red laser modules out, supposed to be good 1000m. A bag of 10 is $4.60, shipped! The constant current circuit is built in, they run fine on 4.5V and 5V. Has your shop done anything with lasers? I've wondered, but I don't want to burn a receiver up, if a laser and IR receiver might make a cheap long range modem.

They sound interesting - link?

Haven't played much with lasers, but we did up a demo laser based game for this years Dublin Maker Faire & were taken aback with how popular it was. Based on what I read regarding long range IR, its all about the optics - so having a tightly fouced beam with a good lens is very important. I guess its even more important for Laser.
I have never tested it, but IR receivers might not perform as well with Laser if the wavelenght is much off the centre (need to check the wavelength of red lasers vs IR). Also, at ranges of 1km aligning the transmitter might be a challenge! - but for short distances good laser + matching receiver should work (in the shade & darkness) as long as the beam isnt interrupted/blocked.

The miles tag forum has lots of interesting info for long range IR & I think they do some stuff with lasers also. ( worth a read)

Start a new topic & lets know how you get on, if you decide to try them out.

I should have thought more. The lasers probably need a different sensor, they're not bandwidth devices. Can't be, not as coherent light.

Anyway, FWIW here's that link. I have bought 2 bags and sent a few off.