Code a TV Remote Using Circuit Playground Express and CircuitPython

by TrIvy in Circuits > Microcontrollers

967 Views, 2 Favorites, 0 Comments

Code a TV Remote Using Circuit Playground Express and CircuitPython

3333-01.jpg

The Circuit Playground Express is a very versatile board, allowing you to code in many languages. Today, we will be using CircuitPython to make a TV remote - from stock board to finished product.

Note that CPX is an acronym for Circuit Playground Express and is used rather heavily in this guide. Make sure you remember that!

EXTRA, EXTRA big note! The Circuit Playground Classic does not support CircuitPython, thus this tutorial is impossible to follow to a T on this board. The Circuit Playground Bluefruit dropped IR communication in favor of Bluetooth, which means this guide will likely not work without external hardware on Bluefruit boards. Only the Circuit Playground Express has all the components needed

on board, so it is made with this board in mind.

Supplies

Circuit Playground Express board, purchasable from the Adafruit website

Data-capable MicroUSB cable
Optional battery pack, in case you can't power your CPX from a nearby USB port

Mu Editor

Any IR-powered TV remote

Prep (Setting Up CircuitPython on Your CPX and Installing Mu)

Firstly, download the latest version of CircuitPython for the CPX board here.
Now, plug in your Circuit Playground and wait for the green lights to show. If they don't show, try hitting the RESET button in the center of the board.
Drag and drop the downloaded uf2 file to your CPLAYBOOT device.

Your board should now reboot and be running CircuitPython!

Download the latest release of IRRemote that matches the CircuitPython version you got from here, extract the file, and copy it into the lib folder of the CIRCUITPY drive.

PROPERLY eject the drive, unplug the Circuit Playground, and plug it back in. This makes sure Windows wrote all of the library file as intended.


Next, let's install Mu. Navigate to the Mu download page, and install it.
Start Mu from your Start menu.
You should see a "Select Mode" box. Click Adafruit CircuitPython and hit OK.


We are done with the prep, and we will now go into understanding Mu.

Basic Understanding of Mu

Screenshot from 2021-06-04 22-08-23.png

First of all, if it is not already loaded, load the "code.py" file from the CIRCUITPY drive on Mu.
Now, on the top, click the "Serial" button. This will allow us to interact directly with the board later, without having to save the code directly to the board. The new section that has shown on the bottom of the window is the Serial prompt. The box above it is where we will copy our main code in.

Let's Copy in Our Code!

Screenshot from 2021-06-04 21-34-09.png

Copy paste all of the text from this link into the code box on the top. The image above is about what it should look like.

Enter the REPL

Screenshot from 2021-06-04 21-36-30.png

Click the smaller, bottom box and press CTRL-C and then hit Enter.
This should ensure you are now in the REPL, and that you can enter Python commands. This box will now be known as the 'REPL' for the rest of the tutorial, so keep it in mind.

Grab Your IR Lights

Unfortunately, as remote IR lights aren't an established standard across different brands or even models of TVs, you'll need to capture your own IR data. Don't worry, it will not be complicated.

In the REPL, enter these commands:
import board
import pulseio

pulses = pulseio.PulseIn(board.IR_RX, maxlen=200, idle_state=True)

import adafruit_irremote

decoder = adafruit_irremote.GenericDecode()

pulses.clear()

pulses.resume()

pulse = decoder.read_pulses(pulses)


Now, once you enter this last command, notice how the REPL pauses and the ">>>" marks go away? This means it is waiting for an IR pulse! Grab your TV remote, point it at the Circuit Playground Express, hit the Power button on the remote, and if everything worked properly... the ">>>" marks should be back! Great, you are now done capturing the IR flashes of the Power button.

Getting the IR Data Into Our Array

Screenshot from 2021-06-04 22-17-49.png

Now, let's print the data it recorded. Simply type the word "pulse" into the REPL. It should spit out a big amount of numbers separated by commas and has two square brackets. Select these, including the square brackets, hit Right Click, then Copy. Look up onto the code area, and look for line 14. It should read "power = array.array('H', )". Put your text cursor directly to the left of the last parentheses. Next, right click your mouse and hit Paste. Now, be absolute sure to save the file! Now that you have done that, properly eject your Circuit Playground from Explorer.

Let's Test It (The Fun Part!)

IMG_4602.JPG

We're almost done! Go to your TV. Power the Circuit Playground Express either from a AAA battery pack or a nearby USB power brick. Now, bring it near your TV's Infrared receiver, face it towards the receiver, and hit the A button on the CPX. Due to the small size of the IR blaster on the CPX, you may need to try different angles to get it just right. But, once you do... Ta-da! Your TV should turn on or off as if you did it from the remote. Congratulations, you've just used the CPX's IR inputs and outputs to imitate your TV remote!