Control Lights with TV Remotes

Our TV remotes control most of our entertainment like the TV, DVD player and Kodi box. We thought that it would be useful if the same TV remotes could also turn on powered devices like the lights so that we could get to the kitchen or bathroom while we’re watching a movie.

We setup our project to work with 2 different TV remotes, and we selected 2 push buttons on the remotes that we not being used in our home entertainment arrangement.

For this project we used 2 small Arduino programs. The first program we used to find TV remote IR codes and the second program used our codes to control the light switch.

The equipment we used:

Power Switch

The PowerTail II switch is an isolated DC-actuated power cord (NO or NC) for controlling power to 120VAC appliances with a 2 wire input.

ir_switch_powertail

An Arduino Power Relay module or Shield is much cheaper than a Power Switch and the code for this project will be identical. We used the Power Tail Switch because we have one and we didn’t want to cut up our power cords.

The Setup

The Setup is very straightforward (see code for pin outs), one data pin for the IR module and one digital output for the relay or Power Tail Switch.

ir_switch_nano

Finding TV Remote IR Codes

The simple code below is used to catch IR codes:

 /*
 
 IR TEST PROGRAM

PINOUTS:

lEFT = DATA PIN
MIDDLE = gnd
RIGHT = 3.3 volts

*/

#include 

int RECV_PIN = 11;  //11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Setup Complete");
}

void loop()
{
  if (irrecv.decode(&results))
    {
     Serial.println(results.value, HEX);
     irrecv.resume(); // Receive the next value
     delay(500);
     
    }
}

Final Code

We used 2 remotes, a newer Samsung, and an old Philips. On the Samsung we used the A and B buttons to turn the lights on and off. On the Philips we used the sharp and natural buttons.

Below is our final code:

 /*
 
TV Remote Control of Switches and Powered Devices
PINOUTS:

lEFT = DATA PIN
MIDDLE = gnd
RIGHT = 3.3 volts

*/

#include <IRremote.h>

int RECV_PIN = 11;  //11;

IRrecv irrecv(RECV_PIN);

decode_results results;

int swtpin = 10;

void setup()
{
  Serial.begin(9600);
  pinMode(swtpin, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Setup Complete");
}

void loop()
{
  if (irrecv.decode(&results))
    {
      switch (results.value) {
     case 0x94CC5A5E: 
       Serial.println("A pushed - light on");
       digitalWrite(swtpin, HIGH);
       break;     
     case 0x23B1EF88:
       Serial.println("B pushed - light off");
       digitalWrite(swtpin, LOW);
       break;    
         case 0x16892E01: 
       Serial.println("natural pushed - light on");
       digitalWrite(swtpin, HIGH);
       break;     
     case 0xDAD07464:
       Serial.println("sharp pushed - light off");
       digitalWrite(swtpin, LOW);
       break;   
     default:
       Serial.println(results.value, HEX);
      }
      irrecv.resume();
     delay(500);
     
    }
}

NFC (Near Field Communications)

If you would like to add some security to your Arduino projects, NFC or Near Field Communications is a low cost solution. We purchased a NFC module and card for under $5. This module is also supported by Raspberry Pi Python libraries.

To get things started you will need to load that Arduino MFRC522 library. You can do this by calling up the Arduino Library Manager, then search for 522.

mfrc522_lib

The default circuit wiring should be:

Uno_RFID_wiring

NFC tags can contain information besides just their tag ID number.

We modified one of the library examples, to include a check based on some “good” tag IDs. The code is below:

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          10         // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

#define NUMCARDS 2
int gooduid[NUMCARDS][10] = {
  {0xD5, 0xF6, 0xA6, 0xA5, 0x0, 0x0, 0x0, 0x0, 0x0},
  {0x13, 0x2F, 0x4E, 0xE5, 0x0, 0x0, 0x0, 0x0, 0x0}
  };

void setup() {
	Serial.begin(9600);		// Initialize serial communications with the PC

	SPI.begin();			// Init SPI bus
	mfrc522.PCD_Init();		// Init MFRC522
	mfrc522.PCD_DumpVersionToSerial();	// Show details of PCD - MFRC522 Card Reader details
	Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}

void loop() {
	// Look for new cards
	if ( ! mfrc522.PICC_IsNewCardPresent()) {
		return;
	}

	// Select one of the cards
	if ( ! mfrc522.PICC_ReadCardSerial()) {
		return;
	}

	// Dump debug info about the card; PICC_HaltA() is automatically called
	mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

  // Reset the valid card checks
  bool anyok = false;
  bool cardok[NUMCARDS];
  for (int i ;  i < NUMCARDS; i++ ) {cardok[i] = false; }
  // Check the card ID   
  for (int j=0; j< NUMCARDS; j++) { 
    for (int i=0; i<mfrc522.uid.size; i++) {
      if (mfrc522.uid.uidByte[i] == gooduid[j][i]) {
        cardok[j] = true;
      } else {
        cardok[j] = false;
        break;
      }
    }
  }
  // Check status of card check
  for (int i=0 ;  i < NUMCARDS; i++ ) {
    if (cardok[i] == true) {
      anyok = true;
      break;
    }
  }
// Print the card check status  
  if (anyok == true) { 
    Serial.println ("Good Card -- do some action");
  } else {
    Serial.println ("Invalid Card");
  }
  
}

When the Arduino monitor is running the output should look something like:

nfc_output

Once we found our “good” tag Id we added a reference to it in our code:

int gooduid[NUMCARDS][10] = { {0xD5, 0xF6, 0xA6, 0xA5, 0x0, 0x0, 0x0, 0x0, 0x0},
{0x13, 0x2F, 0x4E, 0xE5, 0x0, 0x0, 0x0, 0x0, 0x0} };

Once the basic code and setup is working there are a lot of potential projects to work on.  Below is a example where we used a PowerSwitch Tail II with an NFC tag to control a power connection.

nfc_2_power

Wake Up !!! Wake Up !!!

The goal of the “Wake me up” programs is to set a wake time that will turn on the lights and say something like: “Wake Up Wake Up”.  The logic was done in Node-Red and it was quite straightforward. The hardware used was:

The PowerSwitch Tail II ($26) is a power cord that is enabled/disabled with I/O pins. The PowerSwitch pins connect to GPIO18 and GND on the Pi. A desk light is plugged into the PowerSwitch Tail and speakers are connected to audio jack on the Pi.

pi2switch

With the PowerSwitch Tail II there are a lot of devices that could be controlled. For us, we simply connected it to a desk lamp. The setup shown below was later moved into a bedroom and place on a bedside table.

wakeup_hwd

For this project two Node-Red libraries were used; a scheduler library and a text to speech library. They are installed by:

 sudo apt-get install festival
cd $HOME/.node-red
npm install node-red-contrib-say
npm install node-red-contrib-simple-weekly-scheduler 

After the packages are loaded restart the Pi, and then start up Node-Red. On the Node-Red configuration Web page, drop a scheduler, and wire a Pi GPIO, and a Say node to the scheduler.

3nodes

Double-click the scheduler node and set the wake up times. The start/end payloads are numeric and 1/0. Below is the scheduler configuration.

schedule

For the Pi GPIO node, set the GPIO to Pin 12/GPIO18.

powerinfo

Next for the Say node, enter the text you want spoken .

wakeup

For the final circuit, inject nodes can be included for testing. An “on” inject sends a 1, and an “off” inject sends 0. At the wake up time the schedule node will send a payload that will trigger the speaking of the wake up text and the desk light will be turned on for 15 minutes.

final_wakeup