# Sensors in MakeCode

## Overview

The Adafruit Circuit Playground Express comes packed with built-in sensors. This guide will show you how to leverage them in Microsoft MakeCode. If you are new to MakeCode, make sure to [read the MakeCode primer](https://learn.adafruit.com/makecode).

## Events vs live data

You can use a sensor through **events** or by reading the **live data**. 

* **Events** allow to register code that runs when a particular pattern is detected. For example, the [``on shake``](https://makecode.adafruit.com/reference/input/on-gesture) event runs code when a shaking gesture is detected by the accelerometer. You can mix and match events from different sensors in the same program but each event type can only be registered once.
* **Live data** gets a live (or slightly filtered) reading of the sensor data. For example, [``acceleration``](https://makecode.adafruit.com/reference/input/acceleration) gets an immediate reading from the accelerometer.

The block code below show the 2 style of programming with sensors. Through events (`on shake` left) or in the traditional loop style with live data (`acceleration` right).

## Reference

Looking for the complete block reference, start at https://makecode.adafruit.com/reference/input .

![](https://cdn-learn.adafruit.com/assets/assets/000/042/385/medium800/circuit_playground_circuitplayground-screenshot_%282%29.png?1496827362)

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

# Sensors in MakeCode

## Buttons

![](https://cdn-learn.adafruit.com/assets/assets/000/042/387/medium800/circuit_playground_buttons.jpg?1496860731)

## Events

The [``input.onEvent``](https://makecode.adafruit.com/reference/input/button/on-event) block allows to respond to clicks, double clicks and other classic button events. It can be mounted on button ``A``, ``B`` or both ``A+B`` together. Internally, MakeCode takes care of handling the pin state, debouncing, timing and other fun stuff.

## Live data

You can read the live button status using [``input.isPressed``](https://makecode.adafruit.com/reference/input/button/is-pressed). In some situations, you might miss clicks because your program was busy while the user was pressing. In such case, you can also use [``input.wasPressed``](https://makecode.adafruit.com/reference/input/button/was-pressed) which keeps track if the button was pressed between successive calls.

## Example

The ``click`` event on button ``A`` is used to [``play a sound``](https://makecode.adafruit.com/reference/music/play-sound). The state of button ``B`` is checked in a [``forever``](https://makecode.adafruit.com/reference/loops/forever) loop to switch between red and blue on the neopixels. 

https://makecode.com/_62sTjyfFwDzx

# Sensors in MakeCode

## Accelerometer

![](https://cdn-learn.adafruit.com/assets/assets/000/042/388/medium800/circuit_playground_acceleromter.jpg?1496860794)

## Events

The [``input.onGesture``](https://makecode.adafruit.com/reference/input/on-gesture) allows to run code on a number of pre-defined gestures such as **shake**, **freefall** or various orientation events.

## Live data

The [``input.acceleration``](https://makecode.adafruit.com/reference/input/acceleration) returns the immediate acceleration for a given direction **in milli-g**, e.g. 1/1000 of a **g**. The measure includes earth gravity (1000mg) You can query ``X``, ``Y``, ``Z`` or the ``strength``.

If you look closely at the center of the Circuit Playground, you will see the accelerometer axis printed on the board.

Assuming the board is at rest on a table,
* the ``X`` axis is aligned horizontally from left to right. If you tilt left, ``X`` is negative, tilt right ``X`` is positive.
* the ``Y`` axis is aligned vertically from bottom to top. If you tilt forward, ``Y`` is positive, tilt backward ``Y`` is negative.
* the ``Z`` axis is perpendicular to the board and pointing down. At rest, ``Z`` is aligned with earth gravity.

## Example

The example below [plays a sound](https://makecode.adafruit.com/reference/music/play-sound) when the Circuit Playground is shaken. In a [forever loop](https://makecode.adafruit.com/reference/loops/forever), it display the accelerometer reading using [``graph``](https://makecode.adafruit.com/reference/light/graph).

https://makecode.com/_VYm0we7aY7Kz

# Sensors in MakeCode

## Light sensor

![](https://cdn-learn.adafruit.com/assets/assets/000/042/389/medium800/circuit_playground_lightsensor.jpg?1496860839)

## Events

The [``on light condition changed``](https://makecode.adafruit.com/reference/input/on-light-condition-changed) event allows to run code when the light goes dark or bright. For example, you can use the ``on light bright`` event to detect a sudden flash of light.

## Live data

The [``light level``](https://makecode.adafruit.com/reference/input/light-level) returns the current light intensity reading between ``0`` (no light) and ``255`` (max light).

## Example

In this example, we add a ``on light bright`` event to run the [``sparkle``](https://makecode.adafruit.com/reference/light/show-animation) animation when a flash is detected. Using a [``forever``](https://makecode.adafruit.com/reference/loops/forever) loop, we use the light intensity to control the pitch of the [tones played on the speaker](https://makecode.adafruit.com/reference/music/ring-tone).

https://makecode.com/_1s5ckca2THf4

# Sensors in MakeCode

## Microphone

![](https://cdn-learn.adafruit.com/assets/assets/000/042/390/medium800/circuit_playground_microphone.jpg?1496860882)

## Events

The [``on loud sound``](https://makecode.adafruit.com/reference/input/on-loud-sound) event detects a peak in the sound level, for example when someone claps. You can change the loud threshold using ``set loud sound threshold``. 

## Live Data

The [``sound level``](https://makecode.adafruit.com/reference/input/sound-level) block returns the current sound intensity from ``0`` (silent) to ``255`` (very loud).

## Example

The example plays a power up sound when a loud sound is detected. In a [``forever``](https://makecode.adafruit.com/reference/loops/forever) loop, it continuously charts the sound level using the [``chart``](https://makecode.adafruit.com/reference/light/chart) block.


https://makecode.com/_HJxJm8KT2R32

Info: 

# Sensors in MakeCode

## Capacitive Touch

![](https://cdn-learn.adafruit.com/assets/assets/000/042/392/medium800/circuit_playground_captouch.jpg?1496864923)

Capacitive pins can be used as buttons similarly to buttons ``A`` and ``B``. You can use pins ``A1``, ``A2``, ``A3``, ``A4``, ``A5``, ``A6`` and ``A7``.

## Events

The [``input.onEvent``](https://makecode.adafruit.com/reference/input/button/on-event) block allows to respond to clicks, double clicks and other classic button events. It can be mounted on any of the capacitive pins. You'd typically use croc-clips to create circuits connected to those pins.

## Live data

You can read the live button status using [``input.isPressed``](https://makecode.adafruit.com/reference/input/button/is-pressed) or [``input.wasPressed``](https://makecode.adafruit.com/reference/input/button/was-pressed).

## Example

* the ``click`` event on pin ``A1`` is used to [``play a sound``](https://makecode.adafruit.com/reference/music/play-sound)
* the state of button ``A7`` is checked in a [``forever``](https://makecode.adafruit.com/reference/loops/forever) loop to switch between red and blue on the neopixels. 

https://makecode.com/_5Tb6K6ReW6aE

Info: 

https://youtu.be/TtiQb0UAIbQ

# Sensors in MakeCode

## Switch button

![](https://cdn-learn.adafruit.com/assets/assets/000/042/391/medium800/circuit_playground_switch.jpg?1496860924)

The switch button is special kind of button with two positios: left or right. It is very useful to create on/off state in programs.

## Event

The ``on switch moved`` event runs code when the switch is moved left or right.

## Live data

The ``switch right`` block indicates if the switch is on the right position. 

## Example

The example below plays different animations when the switch is positioned left or right.

https://makecode.com/_gz41edAeyef2

# Sensors in MakeCode

## Thermometer

![](https://cdn-learn.adafruit.com/assets/assets/000/042/393/medium800/circuit_playground_temp.jpg?1496864962)

The onboard thermometer provides an easy to track the ambient temperature. The MakeCode blocks support Celcius or Fahrenheit degrees.

## Events

The [``on temperature hot/cold``](https://makecode.adafruit.com/reference/input/on-temperature-condition-changed) event allows to trigger code when hot or cold conditions are detected.

## Live data

The [``temperature``](https://makecode.adafruit.com/reference/input/temperature) block returns the current temperature in Celcius or Fahrenheit degrees.

## Example

The example turns the LEDs to red when the temperature goes above 15. It also graphs the current temperature using the LEDs from 0 to 50C.

https://makecode.com/_MrJ75TCfpCM6

# Sensors in MakeCode

## Infrared Transceiver

![](https://cdn-learn.adafruit.com/assets/assets/000/042/839/medium800/sensors_infrared.jpg?1497936169)

The onboard Infrared transmitter and receiver diodes allow communicate small chunks of data between Circuid Playgrounds.

## Sending data

The ``infrared send number`` send a number packet over IR. It may or may not be received.

## Events

The ``on infrared received`` event triggers when a number has been received.

## Example

The example shows how a rgb color can be sent over IR and used to turn on the neopixel remotely. Button A sends blue, button B sends red.

https://makecode.com/_hrKeVz2VfP5q

### 

Long story short: it's pretty exciting stuff. [Read the deep dive on the MakeCode blog](https://makecode.com/blog/timing-adventures-in-infrared).


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

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Cyberpunk Santa Eye](https://learn.adafruit.com/cyberpunk-santa-eye.md)
- [Circuit Playground Seashell Pendant](https://learn.adafruit.com/circuit-playground-seashell-pendant.md)
- [Stroboscopic Zoetrope](https://learn.adafruit.com/strobe-zoetrope.md)
- [Spinning Logo](https://learn.adafruit.com/spinning-logo.md)
- [CircuitPython Powered AT Hand-Raiser](https://learn.adafruit.com/at-hand-raiser.md)
- [CircuitPython Essentials](https://learn.adafruit.com/circuitpython-essentials.md)
- [UART Communication Between Two CircuitPython Boards](https://learn.adafruit.com/uart-communication-between-two-circuitpython-boards.md)
- [Interactive Map with Circuit Playground Express](https://learn.adafruit.com/interactive-map-with-cpx.md)
- [Circuit Playground Bike Light](https://learn.adafruit.com/circuit-playground-bike-light.md)
- [Crickit Lab Shaker](https://learn.adafruit.com/crickit-lab-shaker.md)
- [LED Breath Stats Mask](https://learn.adafruit.com/led-breath-stats-mask.md)
- [Light Up Paper Dragon Wall Sconce](https://learn.adafruit.com/light-up-paper-dragon-wall-sconce.md)
- [The Tilt Trumpet](https://learn.adafruit.com/the-tilt-trumpet.md)
- [Circuit Playground or Hallowing Jack-o'-Lantern](https://learn.adafruit.com/circuit-playground-jack-o-lantern.md)
- [Hello World of Robotics with Crickit](https://learn.adafruit.com/hello-world-of-robotics-with-crickit.md)
