# Trinket / Gemma IR Control

## Overview

Trinket and Gemma are perfect for small projects needing to receive some external event, triggering your own defined output. This project uses the Adafruit IR Sensor to first receive IR commands from a remote, then to use those codes in controlling a project of your own.  
  
To learn about IR signals and how they are decoded by a microcontroller, see the [IR Sensor Tutorial](http://learn.adafruit.com/ir-sensor) from which this tutorial is based.  
  
Many larger Arduino projects use the excellent [IRRemote library](http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html "Link: http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html") by Ken Shirriff. This library allows for multiple protocols and is quite flexible. But it relies on hardware specific to larger Arduino processors. This project simplifies the process of obtaining codes and using them to scale to the limits of the Trinket and Gemma boards.

Info: 

http://www.youtube.com/watch?v=Ma2xD7L_SHY&amp;feature=youtu.be

![](https://cdn-learn.adafruit.com/assets/assets/000/011/073/medium800/trinket_IMG_2175.jpg?1379809318)

# Trinket / Gemma IR Control

## Wiring

Below we have three different wiring diagrams. We recommend using the first one ( **Gemma M0 or Trinket M0** ) which requires the least amount of wiring, code while providing maximum debug ability.

1. **Gemma M0** &nbsp; **or Trinket M0** - Minimal wiring with built in console debug ability, CircuitPython only.
2. **Gemma v2 with USB to TTL Serial Cable** - (also works with the Trinket Mini) - with USB to TTL Serial Cable. Arduino only.
3. **Trinket Mini with FTDI Friend** (also works with Gemma v2) - Arduino only.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/052/993/medium800/trinket_Trinket_Gemma_IR_Control_bb.png?1523835918)

# Wiring Diagram #1 (above) - **Gemma M0** &nbsp; **or Trinket M0** :

No need for a serial breakout with this setup. It is built right into the M0 series. This wiring is straight forward. This setup will only work with the CircuitPython example code.

- Gemma M0 D0 \<--\> Piezzo Speaker (+)
- Gemma M0 D2 \<--\> IR Output
- Gemma M0 Vout \<--\> IR (+)
- Gemma M0 GND \<--\> IR (-)
- Gemma M0 GND \<--\> Piezo Speaker (-)

&nbsp;

&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/053/015/medium800/trinket_Trinket_Gemma_IR_Control_-_Arduino_USB_Serial_Debug_bb.png?1523922728)

# Wiring Diagram #2 (above) **- Gemma v2 with [USB to TTL Serial Cable](https://www.adafruit.com/product/954)**

This version allows for a debug console using the older Gemma v2 (ATtiny85 based). This setup is good for detecting IR codes and watching them print to the console. The wiring goes as follows:

- Gemma v2 D0 (RX) \<--\> FTDI Green (TX out of the USB port)
- Gemma v2 D1 (TX) \<--\> FTDI White (RX out of the USB port)
- Gemma v2 D2 \<-\> IR Output
- Gemma v2 3v3 \<--\> FTDI Red (+)
- Gemma v2 GND \<--\> IR (-)
- Gemma v2 GND \<--\> FTDI Black (-)

![](https://cdn-learn.adafruit.com/assets/assets/000/013/251/medium800/trinket_CorrectedIRSensorDiagram.jpg?1388268897)

# Wiring Diagram #3 (above) **&nbsp;- Trinket Mini with FTDI Friend**

The diagram is shown below. The IR data pin is connected to the Trinket GPIO #2 (Gemma Pin D2) and is connected to power and ground. To read codes, you will need to connect Trinket GPIO #0 / Gemma D0 to a serial to USB board such as the [FTDI Friend](http://www.adafruit.com/products/284) receive RX pin (cross connect).   
  
To demonstrate how the Trinket or Gemma may process IR commands into an action of your choice, a piezo speaker is connected to Trinket Pin GPIO #1 (Gemma Pin D1) to output a tone when a certain IR code is received.   
  
Going further, you can use an IR code to change NeoPixels, a servo, a solenoid, or any other output.

- If using an FTDI Friend: The green **#0** wire should connect to the **RX** pin and the black **GND** wire connects to the **GND** pin

- If using a USB console cable: The green **#0** wire connects to the **White Wire on the console cable** and the black **GND** wire to the **Black** ground wire on the console cable.

# Trinket / Gemma IR Control

## Arduino Code

Info: 

# IR Reader
The sketch for reading the IR codes for a new remote is below. We will be using Wiring Diagram#the The earlier Gemma v2 and Trinket Mini do not have a built-in hardware serial so we will be using [Wiring Diagram #2](../../../../trinket-gemma-ir-remote-control/wiring) Gemma v2 with the [USB to TTL Serial Cable](https://www.adafruit.com/product/954). Be sure the Arduino serial console (Putty or screen also work) to&nbsp;9600 baud, use 8 bits, 1 stop bit, no parity.

Plug in Gemma v2 to your computer with a USB cable and load up the following:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Gemma/Trinket_Gemma_IR_Control/IR_reader/IR_reader.ino

Now that we have a way capture IR data, you can write your own program which uses these codes to do great things.

# IR Remote Speaker
The demonstration uses a piezo speaker to make different beep tones depending based on four captured codes. This code is based on [Wiring Diagram #3 - Trinket 5v with FTDI Friend](../../../../trinket-gemma-ir-remote-control/wiring).&nbsp;

Danger: 

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Gemma/Trinket_Gemma_IR_Control/Trinket_Gemma_IR_Control/Trinket_Gemma_IR_Control.ino

# Trinket / Gemma IR Control

## CircuitPython Code

![](https://cdn-learn.adafruit.com/assets/assets/000/053/040/medium800/trinket_gemma-cp.jpg?1523996768)

 **GEMMA&nbsp;M0** &nbsp;boards can&nbsp;run&nbsp; **CircuitPython** &nbsp;— a different approach to programming compared to Arduino sketches. In fact,&nbsp; **CircuitPython comes&nbsp;factory pre-loaded on GEMMA&nbsp;M0**. If you’ve overwritten it with an Arduino sketch, or just want to learn the basics of setting up and using CircuitPython, this is explained in the&nbsp;[**Adafruit GEMMA&nbsp;M0 guide**](../../../../adafruit-gemma-m0/circuitpython-setup-1).

Info: 

Below is CircuitPython code that works&nbsp;similarly (though not exactly the same) as the Arduino sketch shown on a prior page. To use this, plug the GEMMA M0 into USB…it should show up on your computer as a small&nbsp; **flash drive** …then edit the file “ **main.py** ” with your text editor of choice. Select and copy the code below and paste it into that file,&nbsp; **entirely replacing its contents** &nbsp;(don’t mix it in with lingering bits of old code). When you save the file, the code should&nbsp; **start running almost immediately** &nbsp;(if not, see notes at the bottom of this page).

**If GEMMA M0 doesn’t show up as a&nbsp;drive, follow the GEMMA M0 guide link above to prepare the board for CircuitPython.**

# IR Reader
In this example we will read IR codes sent by the [Adafruit Mini Remote Control](https://www.adafruit.com/product/389) and output them to the python console in list form so they can be pasted into our next speaker example. This example is based on [Wiring Diagram #1 - Gemma M0 or Trinket M0.&nbsp;](../../../../trinket-gemma-ir-remote-control/wiring)A step-by-step explanation of using IR codes with CircuitPython can be found in the [IR Sensor tutorial](../../../../ir-sensor/circuitpython).&nbsp;

This examples requires that an additional library be copied onto the Gemma M0 or Trinket M0 (under the 'lib' folder):

1. adafruit\_irremote - for decoding the IR signals

The libraries can be obtained from the link below.

&nbsp;

[Adafruit CircuitPython Bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases)
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Gemma/Trinket_Gemma_IR_Control/IR_reader/code.py

# IR Remote Speaker
This demonstration uses a piezo speaker to make different beep tones depending based on the four captured codes we acquired in the IR Reader example above. This code is based on [Wiring Diagram #1 - Gemma M0 or Trinket M0](../../../../trinket-gemma-ir-remote-control/wiring). There is a 20% 'fuzziness' factor used in matching the IR pulse codes.

This example requires that two additional libraries be copied onto the Gemma M0 or Trinket M0 (under the 'lib' folder):

1. adafruit\_irremote - for decoding the IR signals
2. simpleio - used to send tones to the piezo speaker

Both libraries can be obtained here:&nbsp;

[Adafruit CircuitPython Bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases)
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Gemma/Trinket_Gemma_IR_Control/Trinket_Gemma_IR_Control/code.py

# Trinket / Gemma IR Control

## Use and Going Further

## Video
Here's a short clip demonstrating the project  
http://www.youtube.com/watch?v=Ma2xD7L_SHY&amp;feature=youtu.be

## Going Further
The IR sensor only requires one digital input pin. On a Trinket, this leaves you 4 pins for other functions, on the Gemma, two pins. This gives you maximum flexibility for using an IR signal to trigger other devices like different Neopixel effects. See the [Adafruit NeoPixel Überguide](http://learn.adafruit.com/adafruit-neopixel-uberguide) on how to connect Neopixels.  
  
An IR sensor makes an inexpensive, compact (and nearly magical) input device to trigger actions in any project.  
  
  

## Featured Products

### Adafruit GEMMA M0 - Miniature wearable electronic platform

[Adafruit GEMMA M0 - Miniature wearable electronic platform](https://www.adafruit.com/product/3501)
The **Adafruit Gemma M0** is a super small microcontroller board, with just enough built-in to create many simple projects. It may look small and cute: round, about the size of a quarter, with friendly alligator-clip sew pads. But do not be fooled! The Gemma M0 is incredibly...

In Stock
[Buy Now](https://www.adafruit.com/product/3501)
[Related Guides to the Product](https://learn.adafruit.com/products/3501/guides)
### Adafruit Trinket M0 - for use with CircuitPython & Arduino IDE

[Adafruit Trinket M0 - for use with CircuitPython & Arduino IDE](https://www.adafruit.com/product/3500)
The&nbsp;Adafruit Trinket M0 may be small, but do not be fooled by its size! It's a tiny microcontroller board, built around the Atmel ATSAMD21, a little chip with _a lot_ of power. We wanted to design a microcontroller board that was small enough to fit into any project, and low...

In Stock
[Buy Now](https://www.adafruit.com/product/3500)
[Related Guides to the Product](https://learn.adafruit.com/products/3500/guides)
### Adafruit Trinket - Mini Microcontroller - 5V Logic

[Adafruit Trinket - Mini Microcontroller - 5V Logic](https://www.adafruit.com/product/1501)
 **Deprecation Warning: The Trinket bit-bang USB technique it uses doesn't work as well as it did in 2014, many modern computers won't work well. So while we still carry the Trinket so that people can maintain some older projects, we no longer recommend it.** <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1501)
[Related Guides to the Product](https://learn.adafruit.com/products/1501/guides)
### Adafruit Trinket - Mini Microcontroller - 3.3V Logic

[Adafruit Trinket - Mini Microcontroller - 3.3V Logic](https://www.adafruit.com/product/1500)
 **Deprecation Warning: The Trinket bit-bang USB technique it uses doesn't work as well as it did in 2014, many modern computers won't work well. So while we still carry the Trinket so that people can maintain some older projects, we no longer recommend it.** <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1500)
[Related Guides to the Product](https://learn.adafruit.com/products/1500/guides)
### IR (Infrared) Receiver Sensor

[IR (Infrared) Receiver Sensor](https://www.adafruit.com/product/157)
IR sensor tuned to 38KHz, perfect for receiving commands from a TV remote control. Runs at 3V to 5V so it's great for any microcontroller.  
  
To use, connect pin 3 (all the way to the right) to 5V power, pin 2 (middle) to ground and listen on pin 1. It doesn't do any decoding...

In Stock
[Buy Now](https://www.adafruit.com/product/157)
[Related Guides to the Product](https://learn.adafruit.com/products/157/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)
### Piezo Buzzer

[Piezo Buzzer](https://www.adafruit.com/product/160)
Piezo buzzers are used for making beeps, tones and alerts. This one is petite but loud! Drive it with 3-30V peak-to-peak square wave. To use, connect one pin to ground (either one) and the other pin to a square wave out from a timer or microcontroller. For the loudest tones, stay around 4 KHz,...

In Stock
[Buy Now](https://www.adafruit.com/product/160)
[Related Guides to the Product](https://learn.adafruit.com/products/160/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

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

## Related Guides

- [Adafruit Trinket M0](https://learn.adafruit.com/adafruit-trinket-m0-circuitpython-arduino.md)
- [3D Printed LED Fire Horns](https://learn.adafruit.com/3d-printed-led-fire-horns.md)
- [Trinket Bluetooth Alarm System](https://learn.adafruit.com/trinket-bluetooth-alarm-system.md)
- [Timelapse Spy Camera](https://learn.adafruit.com/timelapse-spy-camera.md)
- [Hanukkah MakeCode Menorah Sweater](https://learn.adafruit.com/hanukkah-menorah-sweater.md)
- [Hidden Ink UV Message Reader](https://learn.adafruit.com/hidden-ink-uv-message-reader.md)
- [Lie Ren's Stormflower Gun Blade](https://learn.adafruit.com/stormflower-gun-blade.md)
- [Clockwork Goggles](https://learn.adafruit.com/gemma-m0-clockwork-goggles.md)
- [Camera LED Ring Light](https://learn.adafruit.com/camera-ring-led-light.md)
- [Building the Assistive Technology Ultimate Remote](https://learn.adafruit.com/building-the-assistive-technology-ultimate-remote.md)
- [Trinket / Gemma Mini-Theremin](https://learn.adafruit.com/trinket-gemma-mini-theramin-music-maker.md)
- [Using Piezo Buzzers with CircuitPython & Arduino](https://learn.adafruit.com/using-piezo-buzzers-with-circuitpython-arduino.md)
- [Gemma LightTouch](https://learn.adafruit.com/gemma-lighttouch.md)
- [Trinket “Question Block” Sound Jewelry](https://learn.adafruit.com/trinket-question-block-sound-jewelry.md)
- [CircuitPython-Powered 3-minute Nightlight](https://learn.adafruit.com/circuitpython-powered-gemma-nightlight.md)
- [NeoPixel 60 Ring Wall Clock](https://learn.adafruit.com/neopixel-60-ring-clock.md)
