I am working on a project to control a TV with an arduino. To mimic a remote, I am trying to use a IR LED to send the signal. However, it is not sending anything. I am running this code on an arduino nano. The following is the code I am using:
#include <IRremote.h>
// Define switch pin
const int switchPin = 7;
// Define a variable for the button state
int buttonState = 0;
// Create IR Send Object
IRsend irsend;
void setup()
{
// Set Switch pin as Input
pinMode(switchPin, INPUT);
Serial.begin(9600);
}
void loop() {
// Set button state depending upon switch position
buttonState = digitalRead(switchPin);
// If button is pressed send power code command
if (buttonState == HIGH) {
irsend.sendSony(0x24052816, 20); // TV power code
Serial.print("1");
}
// Add a small delay before repeating
delay(200);
}
(Note this is not my code, I am modifying it based on a website I found).
I have also made a IR receiver program using a arduino uno, which is working and shows when I have pressed a button on my TV remote. I've used that program to try and see if my LED is transmitting anything, but it is not showing anything in the serial monitor. If it is helpful, here is the code I used for the receiver:
#include <IRremote.h>
// Define sensor pin
const int RECV_PIN = 4;
// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup(){
// Serial Monitor @ 9600 baud
Serial.begin(9600);
// Enable the IR Receiver
irrecv.enableIRIn();
}
void loop(){
if (irrecv.decode(&results)){
// Print Code in HEX
Serial.println(results.value, HEX);
irrecv.resume();
}
}
I have tried using both a 100 ohm and a 220 ohm resistor with my LED. I know the wiring is not an issue, as it works with ordinary LEDs. I have tried to change the IR LED, but they have all been having the same issue. Also, I have checked that the diode is ok with a multimeter, which is returning around 0.8 volts.Does anybody have any ideas what might be wrong with my code or the circuit itself? If you have any other ideas on how I could make the arduino control my TV, I would also be interested.
Thanks in advance, Tim
There are a couple of things here. The second parameter of irsend.sendSony()
is the number of bits supplied in the first. In your case you have: irsend.sendSony(0x24052816, 20)
- and 0x24052816 is 32 bits.
The second issue is that when sending to Sony you need to repeat the send call three times with a 40ms gap. This will send the Power on/off code to a Sony.
In the code below I have used the Nano's internal LED as an indicator of when the button (on pin 7) is pressed. It uses an internal pull-up resistor.
#include <IRremote.h>
int LED = 13;
IRsend irsend;
void setup() {
pinMode(LED, OUTPUT);
pinMode(7,INPUT_PULLUP);
}
void loop() {
if (digitalRead(7) == LOW){
digitalWrite(LED, HIGH);
for (int i = 0; i < 3; i++) {
irsend.sendSony(0xa90, 12); // 12 bit wide Sony TV power code
delay(40);
}
}
else digitalWrite(LED, LOW);
delay(100);
}
In the first instance, I would replace the IRLED with an ordinary LED. This will flicker when you press the button. Once it is flickering OK, replace it with the IRLED.
The reason that I am suggesting the LED test is that I have found that there are issues with later versions of IRremote. The code above works fine on IRremote v1.0.0, but not on later versions, which change the way you call sendSony() - so you will probably need to downgrade via the Library Manager.
But with that proviso the code above works fine, and as well as switching my Sony TV on and off, I also verified the code sent on an Arduino IR receiver.