# Make It Sense

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/057/833/medium800thumb/makecode_P10000009.jpg?1532113536)

Sensing the world around us is central to what we do and what we'd like electronics projects to do for us. Be it the weather or detecting specific things like light, sound, etc., we always want to know.&nbsp;

The Circuit Playground Express board is specifically designed to have the sensors you might wish to use built-in.

This guide will help you use the Circuit Playground Express sensors quickly, with code examples, so you can experience how the sensors work and how you might wish to use the sensors in your own project.

![](https://cdn-learn.adafruit.com/assets/assets/000/058/136/medium800thumb/makecode_ezgif.com-resize.jpg?1532653711)

## Parts List
For using IR transmit and receive with MakeCode, you will need two Circuit Playground Express boards.

### 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)
![A Black woman's manicured hand holds a round microcontroller with lit up LEDs.](https://cdn-shop.adafruit.com/640x480/3333-05.jpg)

### 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,...

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

# Make It Sense

## Setting Up Your Programming Environment

If you are using Microsoft MakeCode or CircuitPython, you'll need to do a couple of basic things to set things up.

# For MakeCode

In Makecode, the editor is located at [https://makecode.adafruit.com/](https://makecode.adafruit.com/ "MakeCode Website")

![](https://cdn-learn.adafruit.com/assets/assets/000/057/723/medium800/makecode-small.png?1531949994)

# For CircuitPython

See [this guide page](https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-quickstart)&nbsp;from Introducing Adafruit Circuit Playground Express on setting up your CircuitPython environment.

Adafruit suggests using the Mu editor to edit your code and have an interactive REPL in&nbsp; &nbsp; &nbsp; CircuitPython.&nbsp;[You can learn about Mu and installation in this tutorial](https://learn.adafruit.com/welcome-to-circuitpython/installing-mu-editor "Mu tutorial").

![](https://cdn-learn.adafruit.com/assets/assets/000/057/724/medium800/makecode_blinka-small.png?1531950012)

# Make It Sense

## Using the Onboard Switches

Switches are the most common sensor around us. The Circuit Playground Express has three switches: two pushbuttons and one slide switch.

There are two large&nbsp; **A** &nbsp;and&nbsp; **B** &nbsp;buttons, connected to digital&nbsp;#4&nbsp;(Left) and&nbsp;#5&nbsp;(Right) each.&nbsp;These are unconnected when not pressed, and connected to 3.3V when pressed, so they read HIGH. Set the pins&nbsp;#4&nbsp;and&nbsp;#5&nbsp;to use an internal pull-down resistor when reading these pins so they will read LOW when not pressed.

![makecode_circuit_playground_pushbutton_(1).jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/728/medium640/makecode_circuit_playground_pushbutton_%281%29.jpg?1531951609)

There is a single slide switch near the center bottom of the Circuit Playground Express. It is connected to digital&nbsp;#7. The switch is unconnected when slid to the left and connected to ground when slid to the right. Set pin #7 to use an internal pull-up resistor so that the switch will read&nbsp;HIGH when slid to the left and&nbsp;LOW when slid to the right.

&nbsp;

This is not an on-off switch, but you can use code to have this switch control how you want your project to behave.

![makecode_circuit_playground_slide.jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/729/medium640/makecode_circuit_playground_slide.jpg?1531951640)

Note that you need to use an internal&nbsp;_pull-up&nbsp;_for&nbsp;the slide&nbsp;switch, but an internal&nbsp;_pull-down_&nbsp;for the pushbuttons.

The following pages will show you how to quickly use these switches first in MakeCode then in CircuitPython.

# Make It Sense

## MakeCode

Makecode use of the switches could not be simpler. There are several ways to read the switches and use that action to trigger other actions in code.

First, each button has its own block in the&nbsp; **INPUT** &nbsp;blok group to do an action if a switch movement is detected:

![](https://cdn-learn.adafruit.com/assets/assets/000/057/730/medium800/makecode-switches.PNG?1531951928)

The `on button` blocks are the same, you just select `button A` for the left button, `button B` for the right button. The `on switch` moved block is used twice also, to detect a `left` or `right` movement of the switch.

For the push buttons you can also read them in a conditional, such as an `if..then` block similar to the one below.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/731/medium800/makecode-switches-conditional.PNG?1531952243)

Here is a program that lights NeoPixels depending on the switch activations:

- Pushbutton A makes NeoPixel 0 Green
- Pushbutton B makes NeoPixel 9 Green
- Slide switch left makes NeoPixel 4 Blue, right makes NeoPixel 5 Blue

![](https://cdn-learn.adafruit.com/assets/assets/000/057/732/medium800/makecode-switches-example.PNG?1531952518)

This program can be downloaded below.

[MakeCode for the Button Example](https://makecode.com/_Xui7wrWxxgeu)
# Make It Sense

## CircuitPython

## Using the Circuit Playground Express (CPX) Library

The `adafruit.circuitplayground` library is designed to get you working quickly with easy to use code. It takes up a bit more memory than lower-level calls (also available in the next section). If you are using Crickit with the Circuit Playground Express and are having memory issues, try the code below, otherwise use the following code to get started.

Here is code to read Buttons A, B, and the slide switch.

```
from adafruit_circuitplayground.express import cpx
 
if cpx.button_a:
    # do something
    pass
    
if cpx.button_b:
    # do something else
    pass
    
if cpx.switch:
    # do more like silence a sound, etc.
    pass
```

Here is an example which lights certain NeoPixels when the switches are triggered:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Make_It_Sense/cpx-lib-buttons/code.py

Press Button A and NeoPixel 0 lights up, press B and NeoPixel 9 lights up. If the slide switch is left, NeoPixel 4 is blue, if the switch is to the right, NeoPixel 5 is blue.

## Using Lower-Level Library Calls to Save Memory

The following code is very lightweight for reading the switches. It is perfect for when you want to add additional code such as Crickit or other library modules which may require a lot of memory.

Reading keys uses the following libraries:

- `digitalio`&nbsp;- we use&nbsp;`DigitalInOut, Pull, Direction`
- `board`&nbsp;- we use board definitions for the switches

The bare code to set up all three switches is as follows. You can comment out or delete any button that you don't plan to use.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Make_It_Sense/cpx-buttons/code.py

Here is sample code using the buttons to light the same LEDs as in the MakeCode button example. This requires also adding the `neopixel` library.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Make_It_Sense/buttons-neopixels/code.py

# Make It Sense

## Using the Light Sensor

The light sensor on the Circuit Playground Express is in the upper left corner above push button A marked with an eye and A8.

![makecode_circuit_playground_lightsensor.jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/737/medium640/makecode_circuit_playground_lightsensor.jpg?1532013688)

The light sensor returns a relative value, not a calibrated reading. The light sensor is very good at reading values and having values compared, such that a determination is made if the environment is darker (less light detected) or lighter (more light detected).

The next pages will show you how to use the light sensor both in MakeCode and CircuitPython.

# Make It Sense

## MakeCode

Using MakeCode to read the light sensor is just as easy as previously reading the switches.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/751/medium800/makecode_light.png?1532021981)

[Download this MakeCode Example](https://makecode.com/_VWX034d3Xeby)
The code above is a bit longer to ensure it can is easy to understand. A variable called `light` gets the value of the sensor from the **INPUT** group `light level` block value. The value that the `light level` block returns is `0` to `255` (which is different than in CircuitPython).

The code will use the NeoPixel LEDs as the indicator. There are ten LEDs, numbered 0 to 9. The `map` block is used to take the value of `light` and change it from 0-255 to 0-9. Then that pixel is displayed red.&nbsp;

The code will&nbsp;`pause` 0.1 seconds (100 milliseconds), then&nbsp;`clear` the NeoPixel and start again, `forever`.

The map in the example uses 245 instead of 255. My flashlight could not get the 10th NeoPixel to light under bright light so I lowered the value. Feel free to raise or lower the values to get the light range you want.

## Using Light as a Trigger

There are additional blocks in MakeCode to have code run based on whether the light is below a dark threshold or above a light threshold. Use the `set .. light threshold to` block from the **INPUT** group at the start of your code for both light and dark.

With those set you can use the `on light bright` and `on light dark` to perform any action you want when the light gets bright or dark.

The example code below sets a NeoPixel green when `dark`, red when `light`. NeoPixel 6 is furthest away from the light sensor so readings would not be affected by the indicator.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/752/medium800/makecode_threshold.png?1532022681)

[Download this MakeCode Example](https://makecode.com/_iEFWv3UmRePa)
You can set the thresholds to values that work in your project.&nbsp;

# Make It Sense

## CircuitPython

## Using the Circuit Playground Express (CPX) Library

The value of the light sensor is available as `cpx.light`. You can use this, for example, to plot light levels over time or trigger an alarm if the value gets high (like when an intruder turns on the lights).

The example below uses the plot capability of the Mu editor to plot light readings. The time.sleep function waits a tenth of a second between values.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Make_It_Sense/cpx-lib-light/code.py

When you run this program with the Mu editor, open the REPL via the Serial button to see it print out values for the light. The numbers will get higher with bright light (about 7500 next to white on a monitor), and go lower you put a finger on the sensor (below a thousand covering the board with your hands).

In the code above, the numbers are printed as Python _tuples_, grouped fixed values, so the values are printed in parenthesis with a comma. Why print a tuple verses just the number?&nbsp;

Click the Mu **Plotter** button. The plotter pulls out next to the REPL/Serial window and you get a graph of the light value over time. Go ahead and try covering the sensor and putting it up to light to see the changes.

![](https://cdn-learn.adafruit.com/assets/assets/000/058/067/medium800/makecode_plot-light-cpx.png?1532540580)

Warning: 

## Using Lower-Level Library Calls With the Light Sensor

To read the light sensor, you will need three libraries:

- `analogio` - to access the analog values from the sensor
- `board` - to use the pin name of the light sensor
- `time` - to read values in specific intervals

The `analogio` function `AnalogIn` takes the board's light sensor pin and creates an object that will return the value of the sensor when we need it.

In the simple example, the light sensor value is read forever (in a `while True:` loop) and it prints out the sensor's value every tenth of a second (10 times a second).

Warning: 

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Make_It_Sense/light-plot/code.py

![](https://cdn-learn.adafruit.com/assets/assets/000/057/745/medium800/makecode_light-screen.png?1532017580)

# Make It Sense

## Using the Sound Sensor

The light sensor on the Circuit Playground Express is in the lower right corner below push button B marked with an ear.

![makecode_circuit_playground_mic.jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/738/medium640/makecode_circuit_playground_mic.jpg?1532013732)

This microphone is a bit different than some used in Arduino projects.&nbsp;Instead of an analog microphone, which requires an external op-amp and level management, the Circuit Playground Express uses a PDM microphone. This is a digital mic which is a lot smaller and less expensive. You will need to use&nbsp; MakeCode/CircuitPython/Arduino support libraries to read the audio, you cannot read it like an analog voltage.

The following pages show how to easily use the microphone in your own project.

# Make It Sense

## MakeCode

The following code reads the `sound level`, `map`s it from a range of 0 - 255 down to 0 - 9 so we can use the NeoPixel LEDs to see the readings. It waits a tenth of a second (100 milliseconds), `clear`s the LEDs, and takes another reading.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/834/medium800/makecode_sound.png?1532114599)

[Download this MakeCode Example](https://makecode.com/_dV3HdYHEYiEP)
This is nearly identical to the light sensor program.&nbsp;

And again in this example the value 255 is not in there, it's been lowered to `150`. I could not whistle high enough to register a 255 so it was lowered to get a bigger range of values. You can change this to your own desired value.

## Using Sound as a Trigger

There are additional blocks in MakeCode to run code based on whether the sound is above a set threshold. Use the `on loud sound` block from the **INPUT** group at the start of your code to indicate whether it has detected a loud sound.

The example sets NeoPixel 9 green on a loud sound. Pushing button A clears the light to start again. When the code is run, the green LED may light, press button A to begin, MakeCode may have a bug when starting the sound detector.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/836/medium800/makecode_loud_sound.png?1532114664)

[Download this MakeCode Example](https://makecode.com/_JEWKUe7ckAHY)
# Make It Sense

## CircuitPython

Using the sound sensor in CircuitPython is a bit more involved, because the sensor provides the actual sound data so we have to do math to turn it into loudness.

We're going to use CircuitPython, Mu and the sound sensor to plot sound levels. We'll run this code on Circuit Playground Express and use Mu to plot the data in the Serial window with Mu.

Save the following as&nbsp; **code.py** &nbsp;on your Circuit Playground Express board, using the Mu editor:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Sensor_Plotting_With_Mu_CircuitPython/audio/code.py

This code imports the&nbsp;`audiobusio`,&nbsp;`time`,&nbsp;`board`,&nbsp;`array`&nbsp;and&nbsp;`math`&nbsp;libraries. There are also two helper functions. The first one uses math to return a mean, or average. It is used in the second helper. The second felper function uses math to return a&nbsp;[normalised rms average](https://en.wikipedia.org/wiki/Root_mean_square). These functions are then used to take multiple sound samples really quickly and average them to get a more accurate reading.

Next the microphone object and the samples variable are set. Then initialization of the mic object so it's ready for use.

The mic object starts taking sound samples. The normalised rms is used to find the average of a given set of samples, and that is the&nbsp;`magnitude`. Last,&nbsp;`print`&nbsp;the&nbsp;`magnitude`&nbsp;to the serial console.

Press the Serial button to see the values being printed out. Press the Plotter icon to have Mu plot those values also. Note that the Mu plotter looks for&nbsp; **tuple** &nbsp;values to print. Tuples in Python come in parentheses () with comma separators. If you have two values, a tuple would look like&nbsp;`(1.0, 3.14)`&nbsp;Since we have only one value, we need to have it print out like&nbsp;`(1.0,)`&nbsp;note the parentheses&nbsp;_around_&nbsp;the number, and the&nbsp;_comma_&nbsp;after the number. Thus the extra parentheses and comma in&nbsp;`print(((magnitude),))`.

Once you have everything setup and running, try speaking towards the Circuit Playground Express, and watch the plotter immediately react! Move further away from the board to cause smaller changes in the plotter line. Move closer to the board to see bigger spikes!

It's a really easy way to test your microphone and see how it reads sound changes on the Circuit Playground Express!

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

## Advanced - an Analog Sound Meter

Some Makers look to build a classic sound meter - the rainbow meter that measures sound with sample normalization and logarithmic scaling.&nbsp;

If this is the type of code you're looking to implement, [see this guide for the CircuitPython code](https://learn.adafruit.com/adafruit-circuit-playground-express/playground-sound-meter "VU Meter Guide").

# Make It Sense

## Using the Temperature Sensor

The temperature sensor on the Circuit Playground Express is in the lower right corner below push button B marked with an ear.

![makecode_circuit_playground_temp.jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/739/medium640/makecode_circuit_playground_temp.jpg?1532013819)

The sensor is analog, and like most other Circuit Playground sensors, it is easy to read in both MakeCode and CircuitPython. See the following pages to get started with this sensor.

# Make It Sense

## MakeCode

The value of the temperature sensor can be read in degrees, either Fahrenheit or Celsius via the **INPUT** &nbsp;group&nbsp;`temperature in` block. The example is coded for Fahrenheit.&nbsp;

Rather than use map for this project, an `if..then..else` block is used. Lower than 89 degrees, it lights all green. Between 89 and 98 degrees, there is one yellow dot corresponding to the temperature minus 89 degrees. Above 98 degrees, it displays all red.

At a cool room temperature all NeoPixels should be green. Pressing a finger on the sensor should warm it up to a flashing yellow depending on the temperature value. Most likely a finger will not get the sensor above 98 degrees, but taking it out on a hot day will.

Feel free to adjust the values to change the ranges.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/790/medium800/makecode_temp1.png?1532032458)

[Download this MakeCode Example](https://makecode.com/_cudiwYJaaMLr)
# Make It Sense

## CircuitPython

## Using the Circuit Playground Express (CPX) Library

The temperature may be easily obtained in Celsius via the `adafruit_circuitplayground` value `cpx.temperature`.

You can get the value in Fahrenheit as&nbsp;&nbsp;`temperature_f = cpx.temperature * 1.8 + 32`

Here is a sample program printing out the temperature every quarter of a second (0.25 seconds)

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Make_It_Sense/cpx-lib-temp/code.py

Try placing your finger over the sensor (you'll see the thermometer icon on the board) and watch the readings change. As the temperature is in Celsius, it will not make a huge change over time unless you have a fever.

![](https://cdn-learn.adafruit.com/assets/assets/000/058/134/medium800/makecode_cpx-temp.png?1532650408)

## Using Lower-Level Library Calls
To read the Circuit Playground Express temperature sensor, Adafruit has a CircuitPython library named `adafruit_thermistor`. There are some values that need to be passed to the function which characterizes the specific thermistor on the board.

The following short example shows printing out the temperature in both scales.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_Temperature/code.py

![](https://cdn-learn.adafruit.com/assets/assets/000/057/791/medium800/makecode_circuit_playground_cpxTemperature.png?1532034442)

If you put one of the values (`temp_c` or `temp_f`) in a tuple like `(temp_f, )` then you can use the Mu plotter like was done in the [Light Sensor](https://learn.adafruit.com/make-it-sense/circuitpython-2 "Light Sensor CircuitPython Code") to plot temperature over time.

# Make It Sense

## Using Infrared Transmit and Receive

The infrared transmit and receive sensors are located on either side of the RESET button and accelerometer at the center of the board marked TX (the IR LED transmitter) and RX (the IR receiver).

![makecode_circuit_playground_ir.jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/740/medium640/makecode_circuit_playground_ir.jpg?1532013914)

For MakeCode, there is a limitation to only sending a number via IR. There is more capability in CircuitPython. It is best to check both code example pages to get a feel what each language can do.

The MakeCode example assume there are two Circuit Playground Express boards.

The CircuitPython example uses one Circuit Playground Express and an [Adafruit mini IR remote control](https://www.adafruit.com/product/389).

# Make It Sense

## MakeCode

## Sending an Indication to Another Circuit Playground Express

In MakeCode, the infrared communications blocks are in the **NETWORK** block group.

You can send a number of your choice over infrared with the `infrared send number` block. On the receive side, a trigger block, `on infrared receive` runs code blocks when the board detects an infrared signal. The variable `num` has the numeric value received.

You can compare the numbers sent and received to determine if that is the signal you were looking for or not. With a simple one-on-one conversation, the number can be anything. In a whole classroom of Circuit Playground Express boards, you may only want to trigger when you get a special number received by the instructor or your friend.

## An Example

The example below has the Circuit Playground Express lights all set to green in the `forever` loop.&nbsp;

If button A is pressed, the number 9 (an arbitrary number) is sent via IR.

If a Circuit Playground Express receives any number, it will run two blocks: `set all pixels to red` and `wait` for 1,000,000 microseconds (1 second).&nbsp;

Normally the lights would immediately turn green again due to the `forever` loop. But the `wait` doesn't let that happen (it _blocks_&nbsp;other code from running until it finishes waiting) so you will see the red lights indicating a detection for the `wait` period. Then the NeoPixels will turn back green. Another receive starts the red display for an additional second.

Load the code below on two or more Circuit Playground Expresses.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/792/medium800/makecode_ir.png?1532035910)

[Download this MakeCode Example](https://makecode.com/_h4y7Fe3Yv7d5)
## Using This Code

Download the same MakeCode to two or more Circuit Playground Express boards. They should indicate all green when they don't detect anything.

Aim the two boards at each other. Start off at about 4 inches / 10 centimeters away from each other.

Press the A pushbutton on one board. If the send and receive are roughly aligned on each board, the LEDs on the receiving board will turn red for 1 second then turn green again. Press the A button on the second Express and the first boards' LEDs should turn red.

This can be used a number of ways. Games, sending sensor values, etc. A fun project is [Circuit Playground Express Laser Tag](https://learn.adafruit.com/circuit-playground-express-laser-tag?view=all "Circuit Playground Express Laser Tag").

# Make It Sense

## CircuitPython

## Receiving Infrared Remote Codes

Unlike MakeCode, CircuitPython has some strengths and challenges. Communications between two boards is a challenge but decoding the commands from a remote control, something MakeCode cannot do, can be done in CircuitPython.

This example uses the [Adafruit mini remote control](https://www.adafruit.com/product/389 "Adafruit mini remote control"). Different remotes send differing remote commands with possibly different encodings (the television industry has fractured the remote encoding methodologies, let's point at them). The Adafruit remote sends codes defined by the TV maker NEC. The codes it sends are known and will be in our program.

If you have another remote, you can learn how to read the codes sent [using this guide](https://learn.adafruit.com/ir-sensor/reading-ir-commands "Reading IR Commands").

## Libraries

Adafruit has developed CircuitPython libraries to read IR signals and to translate them to remote codes. The `pulseio` library reads pulses in from any source pin. The `adafruit_irremote` library does the decoding.&nbsp;

The example will use the `neopixel` library again to light the NeoPixels on the Circuit Playground Express from 0 to 9 depending on the number pressed on the remote. IR codes are represented by a Python list of 20 numbers, called a _mapping,_ where each remote button is in a certain place in a list and we look in that list to find which button was pressed by where the number is in the list.

## Code

Here is the code for reading the remote. The example is considerably longer than previous examples in this guide. This is due to comments on the keycodes and the decoding and error checking during the "get key press" process.&nbsp;

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Make_It_Sense/cpx-ir-decode-list/code.py

## Use

Aim the remote at the Circuit Playground Express. Press any of the number keys and the NeoPixel corresponding to that number will light up. Pressing Enter, Back, or any other button will blank the lights to start again.

For a more involved project using these concepts, see the following Adafruit guides:

- [Infrared Receive and Transmit with Circuit Playground Express](https://learn.adafruit.com/infrared-ir-receive-transmit-circuit-playground-express-circuit-python)
- [Remote Control Tree Ornament with Circuit Playground Express](https://learn.adafruit.com/remote-control-tree-ornament-with-circuit-playground-express?view=all)&nbsp;
- [Hacking Ikea Lamps with Circuit Playground Express](https://learn.adafruit.com/hacking-ikea-lamps-with-circuit-playground-express "Hacking Ikea Lamps with Circuit Playground Express")

# Make It Sense

## Using Capacitive Touch

![](https://cdn-learn.adafruit.com/assets/assets/000/058/459/medium800/makecode_C07F007.jpg?1533064526)

Capacitive Touch provides you with 7 additional switch inputs that you may have not known are available.

How do they work? The microcontroller has built-in circuitry to measure the capacitance of each of the pads A1 to A7. They are calibrated when the microcontroller is reset. Any change in capacitance is measured and we can read the output to decide if one of the pads is being touched (which changes the capacitance).

Warning: 

Both Microsoft MakeCode and CircuitPython have built-in support for capacitive touch. See the following pages for examples.

# Make It Sense

## MakeCode

MakeCode has a couple of ways to read the capacitive touch inputs.

The first example uses the **INPUT** code group `is pressed` conditional block. It starts as `button A` is pressed but you can change this to any of the buttons or cap touch pins. The program below uses a big `if` statement block to check each input to see if it is touched. If it is, then it lights the NeoPixel closest to that pad green using blocks from the **LIGHT** block group. If no pad is touched, the green dots are cleared.

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

[MakeCode for the first Capacitive Touch Example](https://makecode.com/_c39WAvEj9dHX)
You can change the sensitivity of the touchpads individually. This code sets the sensitivity in an `on start` block prior to the `forever` loop running.

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

[MakeCode for the second Capacitive Touch Example](https://makecode.com/_46w78sWba5T0)
The final example shows using 4 capacitive touch pads. The `on button` event blocks from the **INPUT** group change a NeoPixel color when touched. This has the benefit of not having to poll for touches in a loop. If button A is pressed, it clears the NeoPixels that have been lit by touches.

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

[MakeCode for the third Capacitive Touch Example](https://makecode.com/_U1sbovAzx00b)
# Make It Sense

## CircuitPython

Similar to reading the standard buttons, the capacitive touch buttons can be checked to see if they have been touched to perform some action.

Using the `adafruit_circuitplayground.express` library, there are 7 touch buttons: `cpx.touch_A1`&nbsp;through `cpx.touch_A7`. The sensitivity of the touchpads is one value for all pads set via&nbsp;`cpx.adjust_touch_threshold`. Setting the threshold is not required if the default sensitivity is ok.

Below is an example of turning on NeoPixels green if pads A4 through A7 are touched. Pressing Button A clears the lights.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Make_It_Sense/cpx-cap-touch/code.py

## Using Lower-Level Library Calls to Save Memory

The following code is very lightweight for reading the capacitive touch pads. It is perfect for when you want to add additional code such as Crickit or other library modules which may require a lot of memory.

Reading keys uses the following libraries:

- `touchio`&nbsp;- contains the touch functions
- `board`&nbsp;- we use board definitions for the switches
- `time` - contains the `sleep` function to pause between readings

The code to set up four cap touch switches and print their values is as follows:&nbsp;

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Sensor_Plotting_With_Mu_CircuitPython/touch/code.py

The documentation for touchio can be found [here](https://circuitpython.readthedocs.io/en/3.x/shared-bindings/touchio/ __init__.html "Touchio - ReadtheDocs"). There is not a current setting for sensitivity.


## 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,...

In Stock
[Buy Now](https://www.adafruit.com/product/389)
[Related Guides to the Product](https://learn.adafruit.com/products/389/guides)
### Circuit Playground Express - Base Kit

[Circuit Playground Express - Base Kit](https://www.adafruit.com/product/3517)
It's the **Circuit Playground Express Base Kit!** &nbsp;It provides&nbsp;the few things you'll need to get started with the new [Circuit Playground Express](https://www.adafruit.com/product/3333).&nbsp;This version of Circuit Playground is super powered, and will...

In Stock
[Buy Now](https://www.adafruit.com/product/3517)
[Related Guides to the Product](https://learn.adafruit.com/products/3517/guides)
### Circuit Playground Express Advanced Pack

[Circuit Playground Express Advanced Pack](https://www.adafruit.com/product/2769)
 **Circuit Playground Express** &nbsp;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 program....

In Stock
[Buy Now](https://www.adafruit.com/product/2769)
[Related Guides to the Product](https://learn.adafruit.com/products/2769/guides)
### Code.org Circuit Playground Express Educators' Pack

[Code.org Circuit Playground Express Educators' Pack](https://www.adafruit.com/product/3399)
For many years, instructors and teachers have asked us to come up with a better way to teach programming and electronics. We have worked on Circuit Playground Express for over a year to come up with a board that is powerful, beautiful, fun, and perfect for teaching

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

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Circuit Playground Express Laser Tag](https://learn.adafruit.com/circuit-playground-express-laser-tag.md)
- [Circuit Playground Express Sugar Glider](https://learn.adafruit.com/cpx-sugar-glider.md)
- [Getting Started With Steven Universe](https://learn.adafruit.com/getting-started-with-steven-universe.md)
- [AdaBox 008](https://learn.adafruit.com/adabox008.md)
- [Flapping Halloween Vampire Bat](https://learn.adafruit.com/flapping-halloween-vampire-bat.md)
- [Trash-Built Robotic Fish](https://learn.adafruit.com/trash-robo-fish.md)
- [CPX Glowing Disembodied Hand](https://learn.adafruit.com/cpx-glowing-disembodied-hand.md)
- [Crawling Baby Sea Turtle Robot](https://learn.adafruit.com/baby-turtle-makecode.md)
- [Make It a Mouse](https://learn.adafruit.com/make-it-a-mouse.md)
- [Adabot Operation Game](https://learn.adafruit.com/adabot-operation-game.md)
- [Circuit Playground Quick Draw](https://learn.adafruit.com/circuit-playground-quick-draw.md)
- [Circuit Playground Express Serial Communications](https://learn.adafruit.com/circuit-playground-express-serial-communications.md)
- [CircuitPython Hardware: ILI9341 TFT & FeatherWing](https://learn.adafruit.com/micropython-hardware-ili9341-tft-and-featherwing.md)
- [Techno-Tiki RGB LED Torch](https://learn.adafruit.com/techno-tiki-rgb-led-torch.md)
- [Using Circuit Playground Express, MakeCode and CircuitPython on a Chromebook](https://learn.adafruit.com/using-circuit-playground-express-makecode-circuitpython-on-a-chromebook.md)
