# Gemma LightTouch

## Red light, green light, blue light!

CircuitPython is Python that runs on microcontrollers, including your Gemma board! To learn more about CircuitPython, check out the [Welcome to CircuitPython](../../../../welcome-to-circuitpython) and [CircuitPython Essentials](../../../../circuitpython-essentials/) guides. This guide uses CircuitPython and Gemma.

This project is a simple one that requires touch interaction from you! There are three touch pads on your Gemma, and it just so happens that there are three colors in the built-in RGB LED! So, we've written up a program using CircuitPython that allows you to control the red level from the first pad, the green level from the second pad, and the blue level from the third to make any color in the rainbow!

https://youtu.be/wbSnZCqVuvo

RGB LED colors are set using a combination of&nbsp; **r** ed, **g** reen, and **b** lue, in the form of an ( **R** ,&nbsp; **G** , **B** ) tuple. Each member of the tuple is set to a number between 0 and 255 that determines the amount of each color present. Red, green and blue in different combinations can create all the colors in the rainbow! So, for example, to set the LED to red, the tuple would be (255, 0, 0), which has the maximum level of red, and no green or blue. Green would be (0, 255, 0), etc. For the colors between, you set a combination, such as cyan which is (0, 255, 255), with equal amounts of green and blue. If you increase all values to the same level, you get white!

Let's take a look at the code!

# Code Walkthrough
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Gemma/Gemma_LightTouch/red_green_blue/code.py

First we import our libraries: `time`, `touchio`, `adafruit_dotstar`, and `board`.

Next, we create the LED and touch objects.

[DotStar LEDs](../../../../adafruit-dotstar-leds/overview) use SPI, but the DotStar on the Gemma has its own unique pin assignments. So we assign `led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)` to tell it which pins to use. The `1` tells the code that we are using a single LED.

If you look at your board, you'll see three pads that have A0, A1 and A2 next to them. Those are the touch pads on your Gemma. We have three touch pads, so we create three touch objects. To create a touch object, you need to provide the pin you plan to use. We start with `touch_A0 = touchio.TouchIn(board.A0)` and then use the same concept for `touch_A1` and `touch_A2`.

```auto
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
touch_A0 = touchio.TouchIn(board.A0)
touch_A1 = touchio.TouchIn(board.A1)
touch_A2 = touchio.TouchIn(board.A2)
```

Since we're going to be using r, g, b color values, we need to initialise the variables for later use. So we set `r = g = b = 0`.

We create our main loop with `while True:`.

Within the loop, we have three `if` statements. Each are the same concept, using a touch pad to change a color value.

We start with `if touch_A0.value:`, which says, "if this touch pad is touched, do the following:". We are going to use touch pad A0 to change the red color level. So, we have `r = (r + 1) % 256`. This says every time you touch the pad, increase the red value by 1 up to a maximum value of 255, and loop once the maximum is reached. This cycles from 0 to 255 and then back to 0. However, you don't need to touch the pad 256 times to get through the cycle! The touch pads respond if you touch and hold, so you can place your finger on A0 and leave it, and it will go through the entire cycle.

```auto
    if touch_A0.value:
        r = (r + 1) % 256
```

Then we do the same for A1 and A2, where A1 affects the green value and A2 affects the blue value.

```auto
    if touch_A1.value:
        g = (g + 1) % 256
    if touch_A2.value:
        b = (b + 1) % 256
```

Then, we set `led[0] = (r, g, b)`. This set the LED to the color that matches the current values that you've set by touching the different pads! Note that we have 1 LED, however we've set `led[0]`. This is because Python starts counting at 0. So our first LED is number 0 according to the code.

Then we print the color values to the serial console with `print((r, g, b))`. If you are connected to the serial console, you'll see the values change live as you touch the pads!

Last, we include a ` time.sleep(0.01)` to include a tiny delay. Otherwise the colors can change so quickly you'll pass right by the color you were trying to set! You can change this if you'd like to make the values change more quickly.

You don't have to change each value separately! You can touch any number of pads at the same time to affect multiple color values at the same time.

That's all there is to it! Simple code with a little math, and you've got a way to use the touch pads on your Gemma to show off any color you'd like!

- [Next Page](https://learn.adafruit.com/gemma-lighttouch/fancy-an-interactive-light.md)

## 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)

## Related Guides

- [Porting an Arduino library to CircuitPython: VL6180X Distance Sensor](https://learn.adafruit.com/porting-an-arduino-library-to-circuitpython-vl6180x-distance-sensor.md)
- [Your Very First Circuit Sculpture](https://learn.adafruit.com/first-simple-circuit-sculpture.md)
- [Glowing Fascinator Hat with Gemma M0 and MakeCode](https://learn.adafruit.com/glowing-fascinator-hat-gemma-m0-makecode.md)
- [CircuitPython Basics: Analog Inputs & Outputs](https://learn.adafruit.com/circuitpython-basics-analog-inputs-and-outputs.md)
- [Infinity Mirror Valentine's Candy Box](https://learn.adafruit.com/infinity-mirror-candy-box.md)
- [3D Printed NeoPixel Ring Hair Dress](https://learn.adafruit.com/neopixel-ring-hair-dress.md)
- [CircuitPython-Powered 3-minute Nightlight](https://learn.adafruit.com/circuitpython-powered-gemma-nightlight.md)
- [Using Servos With CircuitPython and Arduino](https://learn.adafruit.com/using-servos-with-circuitpython.md)
- [Logan’s Run Hand Jewel LED](https://learn.adafruit.com/led-in-you-hand-logans-run-life-clock.md)
- [Disco Band Camp Jacket](https://learn.adafruit.com/disco-band-camp-jacket.md)
- [NeoPixel Tiara](https://learn.adafruit.com/neopixel-tiara.md)
- [Jewel Hair Stick](https://learn.adafruit.com/jewel-hair-stick.md)
- [Flashing LED Strand with MakeCode](https://learn.adafruit.com/flashing-led-strand-with-makecode.md)
- [Gemma Color Touch Pendant Necklace](https://learn.adafruit.com/gemma-color-touch-pendant-necklace.md)
- [GEMMA M0 Case](https://learn.adafruit.com/gemma-m0-case.md)
