# Infrared Receive and Transmit with Circuit Playground Express

## Overview

The Circuit Playground Express is an amazing little board with tons of sensors and things built in, including an **infrared transmitter** and an **infrared receiver**. This guide will show you how to use CircuitPython to send simple messages between two Circuit Playground Expresses using infrared!

This guide expects that you have **two Circuit Playground Expresses** , as we will be using IR to communicate between them. You can get wireless communications without antennas, pairing or passwords.

![](https://cdn-learn.adafruit.com/assets/assets/000/058/039/medium800thumb/light_IR-Kattni.jpg?1532480832)

Infrared (IR) is invisible to the naked eye which makes it great for wireless communication. IR communication is _line-of-sight_&nbsp;(the sensor must be pointed towards the receiver) and has about a 10-20 meter range (optimally). It's good for sending short amounts of data. IR remotes use infrared for communicating with their targets, for example your television. For more information about IR, check out [this article](https://en.wikipedia.org/wiki/Infrared).

The IR transmitter and receiver on the Circuit Playground Express can be found near the center of the board.

&nbsp;

The transmitter is labeled TX and is on the left side of the reset button, to the right of button A. The receiver is labeled RX and is on the right side of the reset button, to the left of button B.

![light_circuitpython_CPX_IR_TX_RX.jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/858/medium640/light_circuitpython_CPX_IR_TX_RX.jpg?1532480921)

# Infrared Receive and Transmit with Circuit Playground Express

## IR Test with Remote

The first thing we're going to do is test IR receive with a NEC remote. NEC is a electronics manufacturer, one of several, that defined their own IR coding scheme which has also been used by other folks in products. You can try any remotes you have sitting around the house (although they might use an encoding other than NEC). We have [this handy little one](https://www.adafruit.com/product/389) available in the store which we're going to use for our test.

### Mini Remote Control

[Mini Remote Control](https://www.adafruit.com/product/389)
This little remote control would be handy for controlling a robot or other project from across the room. It has 21 buttons and a layout we thought was handy: directional buttons and number entry buttons. The remote uses the NEC encoding type and sends data codes 0 thru 26 (it skips #3, #7,...

Out of Stock
[Buy Now](https://www.adafruit.com/product/389)
[Related Guides to the Product](https://learn.adafruit.com/products/389/guides)
![Mini Remote Control with 21 buttons](https://cdn-shop.adafruit.com/640x480/389-03.jpg)

Copy the following code to your **code.py** :

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Circuit_Playground_Express_and_IR/CPX_IR_Remote_Recieve/code.py

We create the `pulsein` object to listen for infrared signals on the IR receiver. Then we create the `decoder` object to take the pulses and turn them into numbers.

Then we take the `decoder` object and attempt to convert the received pulses into numbers. There's two errors we check for and tell the code to continue running if they're encountered.

One possible decoding error is an unusually short code, which is probably an NEC repeat signal. If you hold down a remote button, the remote control may 'save effort and time' by&nbsp; sending a short code that means "keep doing that". For example, holding down the volume button to quickly increase or decrease the volume on a TV. We don't handle those repeat codes in this project, we're only looking for unique button presses

The second possible decoding error is when it fails to decode, which can mean the signal got distorted or you're not using a NEC remote.

Then we print the code we receive from the remote. If we receive the codes from the first three buttons on the remote, we print which button was pressed.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/997/medium800/circuitpython_CPXIRRemoteReceive.png?1532447538)

If you're using your own remote, you can check the serial console to find the codes you're receiving. They'll be printed after `NEC Infrared code received:`. Then, you can change `code.py` to reflect the specific button codes for your remote.

# Infrared Receive and Transmit with Circuit Playground Express

## IR from CPX to CPX

Your Circuit Playground Express can both transmit and receive IR signals! There is an example where each button on the CPX sends a different signal. We've emulated the volume up and volume down buttons on the Adafruit NEC remote since the receive code is already looking for those signals. So, the program will be sending four bytes of data with each button press for the other CPX to receive and decode.

Why send four bytes instead of one? IR communication is messy, and often gets mixed up with flickering lights in the room, or maybe something blocking the photons. If you send only one byte, you run the risk of a signal being mis-interpreted as some other signal. By requiring and checking for four digits, your chance of getting a mistaken message is less likely. Why not more than four? If the message is too long, it would take a long time to send, and use more power. Four bytes are defined for the NEC standard - it's a nice trade-off between too-short and too-long.

**You'll need two Circuit Playground Expresses for this example.**

You should still have the code from [the previous example](https://learn.adafruit.com/circuit-playground-express-and-ir/ir-test-with-remote) on the first CPX.

Copy the following code into your `code.py` **on the second CPX** :

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Circuit_Playground_Express_and_IR/CPX_IR_CPX_transmit/code.py

We create a `pulseio` output to send infrared signals from the IR transmitter at 38KHz. Then we create an `encoder` that will take the numbers we're sending and turn them into NEC IR pulses.

The arguments we pass into the&nbsp;`adafruit_irremote.GenericTransmit` line is what lets the irremote library know how to _encode_ the 4 bytes into NEC remote data. There's a 9ms pulse high followed by a 4.5ms pulse low to let the receiver know data is coming. Then 560us+560us pulse pairs for a '0' value and 560us+1700us pulse pair for a '1' value.

`header=[9000, 4500], one=[560, 1700], zero=[560, 1700], trail=0`

Inside our loop, we check to see if each button is pressed. When a button is pressed, we `print` a message to the serial console. Then, we turn on the red LED, send our 4 byte data signal, and turn the red LED off. Then we wait 0.2 seconds to give the receiver a chance to receive the full message.

Connect **both** Circuit Playground Expresses to the serial console to see the associated serial output. We've overlapped the windows side-by-side to show the serial output from both boards next to each other. You can see exactly what happens on the left when you press a button on the transmitting board, and on the right after the signal is received by the receiving board.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/998/medium800/circuitpython_CPXIRTxRxOutput.png?1532448588)

If you'd like to emulate your own remote, simply change the IR codes in the `code.py` above to match the changes you made in the last example. It works as long as you're transmitting the same code that the receiving board is expecting!

# Infrared Receive and Transmit with Circuit Playground Express

## Using IR as an Input

We've shown how to send and receive IR signals from Circuit Playground Express to Circuit Playground Express. You can use those signals as an input to trigger events on the receiving CPX. So, let's do something fun with it. It's time to light it up and make some noise!

**You'll need two Circuit Playground Expresses for this example.**

Copy the following code to **code.py on the CPX currently receiving signals**. The transmission `code.py` will remain the same from the previous page

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Circuit_Playground_Express_and_IR/CPX_IR_as_Input/code.py

The beginning of this code is the same as the test with the remote. We create the `pulsesio` and `decoder` objects, wait to receive the signals, and attempt to decode them. Then we print the IR code received.

We check for the button presses like before as well. However, instead of simply `print()ing` to the serial output, we've added something more fun!

- If we receive the IR code now associated with button A, we `print` `Button A signal`, and turn all the NeoPixel LEDs purple!
- If we receive the IR code now associated with button B, we `print` `Button B signal`, turn all of the LEDs off, and play a 262Hz tone!

We've put the two windows together again to show the associated `print` statements from each board. The transmitting board output is on the left and the receiving output is on the right.

![](https://cdn-learn.adafruit.com/assets/assets/000/058/000/medium800/circuitpython_CPXIRasInputOutput.png?1532450498)

![](https://cdn-learn.adafruit.com/assets/assets/000/058/038/medium800thumb/light_IR-Kattni.jpg?1532480489)

You can do all kinds of things by adding code to these `if` blocks, such as, move a servo, play a wave file, change LED animations, or print a special message to the serial output. The possibilities are endless. Pick something and give it a try!


## Featured Products

### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### Mini Remote Control

[Mini Remote Control](https://www.adafruit.com/product/389)
This little remote control would be handy for controlling a robot or other project from across the room. It has 21 buttons and a layout we thought was handy: directional buttons and number entry buttons. The remote uses the NEC encoding type and sends data codes 0 thru 26 (it skips #3, #7,...

Out of Stock
[Buy Now](https://www.adafruit.com/product/389)
[Related Guides to the Product](https://learn.adafruit.com/products/389/guides)

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Sparky the Blue Smoke Monster Automaton](https://learn.adafruit.com/sparky-automaton.md)
- [Professor Bubbleton’s Breathing Head in a Jar](https://learn.adafruit.com/professor-bubbleton-s-breathing-head-in-a-jar.md)
- [Circuit Playground Express: Piano de Limones](https://learn.adafruit.com/circuit-playground-express-piano-de-limones.md)
- [CircuitPython Hardware: PCA9685 PWM & Servo Driver](https://learn.adafruit.com/micropython-hardware-pca9685-pwm-and-servo-driver.md)
- [Simon Game Clone with Circuit Playground Express and CircuitPython](https://learn.adafruit.com/simon-game-clone-with-circuitplayground-express-and-circuitpython.md)
- [CircuitPython Hardware: SSD1306 OLED Display](https://learn.adafruit.com/micropython-hardware-ssd1306-oled-display.md)
- [Make It Glow - Your First NeoPixel Project](https://learn.adafruit.com/make-it-glow-your-first-neopixel-project.md)
- [Simple and Beautiful NeoPixel Holiday Lights](https://learn.adafruit.com/simple-beautiful-color-changing-light-strand.md)
- [How to Choose a Microcontroller](https://learn.adafruit.com/how-to-choose-a-microcontroller.md)
- [Magical Cardboard Craft Obsidian Sword](https://learn.adafruit.com/cardboard-obsidian-sword.md)
- [MicroBlocks Circuit Playground Express Ornament](https://learn.adafruit.com/microblocks-circuitplayground-express-ornament.md)
- [Circuit Playground Bike Light](https://learn.adafruit.com/circuit-playground-bike-light.md)
- [Make It a Mouse](https://learn.adafruit.com/make-it-a-mouse.md)
- [Game Clock with Circuit Playground & MakeCode](https://learn.adafruit.com/game-clock-with-circuit-playground-makecode.md)
- [Getting Started With Steven Universe](https://learn.adafruit.com/getting-started-with-steven-universe.md)
