IR remote control for the home – part 2

In the first part, I outlined my plan to build an infrared receiver to act as a hub for IoT devices in the home. Here, we’ll take a closer look at house the receiver works.

Here’s what is required for this initial stage of the project:

  • An ESP32 development board. I’m using a DOIT device, which costs around $14.
  • A breadboard.
  • An 1838T receiver/sensor. At the time of writing, you could pick up 10 of these little blighters for under $1, with free shipping, on eBay.
  • 100Ω resistor.
  • 33 kΩ or 47kΩ resistor.
  • 0.1µF ceramic capacitor.
  • 100µF capacitor (optional, I reckon – I’m not using one yet).
  • Optional: an LED with current limiting resistor (say, 220Ω or 330Ω).

The 1838T has three legs: VCC (works on 3.3V or 5V), GND and OUT (the signal). In my case I’m feeding VCC with 5V via the 100Ω resistor. The OUT line is pulled high to VCC with the 47kΩ resistor. And the 0.1µF capacitor is across the VCC and GND pins.

The OUT pin is connected to pin 25 of the ESP32 dev board. That’s an arbitrary choice – you can choose any pin capable of being an input.

I have the anode of the LED connected to pin 33 and the cathode, via the current limiting resistor, to ground. Note that not all ESP32 pins are capable of acting as outputs (ask me how I know). There’s a handy reference here.

And that’s it for the hardware setup. Pretty basic, huh?

Time to code

Now for some code. I’m working with the Arduino IDE, which isn’t my favourite programming environment. I have, briefly, tried and failed to get the IRremote library working within Atmel Studio 7. I’d still like to do that because it would be handy to be able to use small devices like the ATTINY microcontrollers. But that’s for another day – one when I no longer want to own hair.

So, as mentioned in the previous post, you need Ken Shirriff’s IRremote library – see that post for details of how to amend it to work with the ESP32.

As for my code, links to the GitHub repos are below where you can view the code in full. Bear in mind, though, that this code will change constantly. I won’t go through it in detail, but here are some relevant highlights:

#include <WiFi.h>
#include "wifi-credentials.h"
#include <IRremote.h>

The WiFi.h and IRremote.h includes are pretty self-explanatory. The other include needs a little explaining. Rather than putting my home wifi SSIDs and password in the code for everyone to see, I’ve opted to include these from a separate file – and then not upload that file to GitHub. Instead, I’ve uploaded a file called wifi-credentials-template.h. You should edit this to match your own wifi setup, then rename the file to wifi-credentials.h.

IRrecv irrecv(RECV_PIN);
decode_results results;

These lines create an instance of the IRrecv object, plus an object to hold the results of any signal received.

In the setup() function, we run:

irrecv.enableIRIn();

To start receiving IR signals. Then, in the main loop, we have:

if (irrecv.decode(&results)) {
    /* ... stuff happens here ... */
}

This matches any time a signal has been received. You can then interrogate results.value to find out what the signal was. Most of the code within this if block comes from the video I embedded in the previous post. I’ve amended it slightly. It now displays (via the serial connection) the value of the signal in hex both with and without the ‘0x’ prefix. This is for ease of copy the value to other code or notes. I’ve also opted to display the number of bits in the signal, plus the manufacturer protocol used (this becomes important later if we want to send signals programmatically via, say, an Arduino).

Finally, I chose not to display any value of 0xFFFFFFFF. This is a value used to denote that the same signal has been received more than once. The purpose of this sketch is to allow me which values are being used by various remotes, so I don’t really need the 0xFFFFFFFF thing.

So, with this setup and code, and a serial monitor open, I can now fire various remotes at the sensor and make a note of which buttons send which signals. On the right is some sample output in the Arduino IDE’s serial monitor.

The next step will be setting up the ESP32 to talk MQTT.

The code

The code for this project is in a GitHub repository: ESP32_IR_IoT_hub.

There’s also a simple sketch for decoding the signals from a remote control. This should work with most microcontroller boards – Arduinos, Teensy, ESP etc. The repo is: IR_remote_decode.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.