# CircuitPython Essentials

## CircuitPython Essentials

![](https://cdn-learn.adafruit.com/assets/assets/000/051/507/medium800/circuitpython_adafruit_circuit_python_stacked.png?1520127503)

You've gone through the [Welcome to CircuitPython guide](../../../../welcome-to-circuitpython). You've already gotten everything setup, and you've gotten CircuitPython running. Great! Now what? CircuitPython Essentials!

There are a number of core modules built into CircuitPython and commonly used libraries available. This guide will introduce you to these and show you an example of how to use each one.

Each section will present you with a piece of code designed to work with different boards, and explain how to use the code with each board. These examples work with any board designed for CircuitPython, including **Circuit Playground Express** , **Trinket M0** , **Gemma M0** , **QT Py** , **ItsyBitsy M0 Express** , **ItsyBitsy M4 Express,**  **Feather M0 Express, Feather M4 Express, Metro M4 Express,** &nbsp; **Metro M0 Express, Trellis M4 Express, and Grand Central M4 Express**.

Some examples require external components, such as switches or sensors. You'll find wiring diagrams where applicable to show you how to wire up the necessary components to work with each example.

Let's get started learning the CircuitPython Essentials!

# CircuitPython Essentials

## CircuitPython Pins and Modules

CircuitPython is designed to run on microcontrollers and allows you to interface with all kinds of sensors, inputs and other hardware peripherals. There are tons of guides showing how to wire up a circuit, and use CircuitPython to, for example, read data from a sensor, or detect a button press. Most CircuitPython code includes hardware setup which requires various modules, such as `board` or `digitalio`. You import these modules and then use them in your code. How does CircuitPython know to look for hardware in the specific place you connected it, and where do these modules come from?

This page explains both. You'll learn how CircuitPython finds the pins on your microcontroller board, including how to find the available pins for your board and what each pin is named. You'll also learn about the modules built into CircuitPython, including how to find all the modules available for your board.

# CircuitPython Pins

When using hardware peripherals with a CircuitPython compatible microcontroller, you'll almost certainly be utilising pins. This section will cover how to access your board's pins using CircuitPython, how to discover what pins and board-specific objects are available in CircuitPython for your board, how to use the board-specific objects, and how to determine all available pin names for a given pin on your board.

## `import board`

When you're using any kind of hardware peripherals wired up to your microcontroller board, the import list in your code will include `import board`. The `board` module is built into CircuitPython, and is used to provide access to a series of board-specific objects, including pins. Take a look at your microcontroller board. You'll notice that next to the pins are pin labels. You can always access a pin by its pin label. However, there are almost always multiple names for a given pin.

To see all the available board-specific objects and pins for your board, enter the REPL (`>>>`) and run the following commands:

```python
import board
dir(board)
```

Here is the output for the QT Py SAMD21. **You may have a different board, and this list will vary, based on the board.**

![](https://cdn-learn.adafruit.com/assets/assets/000/099/189/medium800/circuitpython_dir-board-output-qt-py.png?1612291495)

The following pins have labels on the physical QT Py SAMD21 board: A0, A1, A2, A3, SDA, SCL, TX, RX, SCK, MISO, and MOSI. You see that there are many more entries available in `board` than the labels on the QT Py.

You can use the pin names on the physical board, regardless of whether they seem to be specific to a certain protocol.

For example, you do not _have_ to use the SDA pin for I2C - you can use it for a button or LED.

On the flip side, there may be multiple names for one pin. For example, on the QT Py SAMD21, pin **A0** is labeled on the physical board silkscreen, but it is available in CircuitPython as both `A0` and `D0`. For more information on finding all the names for a given pin, see the [What Are All the Available Pin Names?](https://learn.adafruit.com/circuitpython-essentials/circuitpython-pins-and-modules#what-are-all-the-available-names-3082670-14) section below.

The results of `dir(board)` for CircuitPython compatible boards will look similar to the results for the QT Py SAMD21 in terms of the pin names, e.g. A0, D0, etc. However, some boards, for example, the Metro ESP32-S2, have different styled pin names. Here is the output for the Metro ESP32-S2.

![](https://cdn-learn.adafruit.com/assets/assets/000/099/215/medium800/circuitpython_Essentials_dir_board_Metro_ESP32-S2.png?1612374794)

Note that most of the pins are named in an IO# style, such as **IO1** and **IO2**. Those pins on the physical board are labeled only with a number, so an easy way to know how to access them in CircuitPython, is to run those commands in the REPL and find the pin naming scheme.

Info: 

## I2C, SPI, and UART

You'll also see there are often (_ **but not always!** _) three special board-specific objects included: `I2C`, `SPI`, and `UART` - each one is for the default pin-set used for each of the three common protocol busses they are named for. These are called _singletons_.

What's a singleton? When you create an object in CircuitPython, you are _instantiating_ ('creating') it. Instantiating an object means you are creating an instance of the object with the unique values that are provided, or "passed", to it.

For example, When you instantiate an I2C object using the `busio` module, it expects two pins: clock and data, typically SCL and SDA. It often looks like this:

```python
i2c = busio.I2C(board.SCL, board.SDA)
```

Then, you pass the I2C object to a driver for the hardware you're using. For example, if you were using the TSL2591 light sensor and its CircuitPython library, the next line of code would be:

```python
tsl2591 = adafruit_tsl2591.TSL2591(i2c)
```

However, CircuitPython makes this simpler by including the `I2C` singleton in the `board` module. Instead of the two lines of code above, you simply provide the singleton as the I2C object. So if you were using the TSL2591 and its CircuitPython library, the two above lines of code would be replaced with:

```python
tsl2591 = adafruit_tsl2591.TSL2591(board.I2C())
```

Info: 

This eliminates the need for the `busio` module, and simplifies the code. Behind the scenes, the `board.I2C()`&nbsp; object is instantiated when you call it, but not before, and on subsequent calls, it returns the same object. Basically, it does not create an object until you need it, and provides the same object every time you need it. You can call `board.I2C()` as many times as you like, and it will always return the same object.

Info: 

## What Are All the Available Names?

Many pins on CircuitPython compatible microcontroller boards have multiple names, however, typically, there's only one name labeled on the physical board. So how do you find out what the other available pin names are? Simple, with the following script! Each line printed out to the serial console contains the set of names for a particular pin.

On a microcontroller board running CircuitPython, first, connect to the serial console.

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/Pin\_Map\_Script/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_Pin_Map_Script.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/Pin_Map_Script/code.py

Here is the result when this script is run on QT Py SAMD21:

![](https://cdn-learn.adafruit.com/assets/assets/000/122/945/medium800/leds_CPE_pin_map_script_QT_Py_M0.png?1690233285)

Each line represents a single pin. Find the line containing the pin name that's labeled on the physical board, and you'll find the other names available for that pin. For example, the first pin on the board is labeled **A0**. The first line in the output is `board.A0 board.D0 (PA02)`. This means that you can access pin **A0** in CircuitPython using both `board.A0` and `board.D0`.

The pins in parentheses are the microcontroller pin names. See the next section for more info on those.

You'll notice there are two "pins" that aren't labeled on the board but appear in the list: `board.NEOPIXEL` and `board.NEOPIXEL_POWER`. Many boards have several of these special pins that give you access to built-in board hardware, such as an LED or an on-board sensor. The QT Py SAMD21 only has one on-board extra piece of hardware, a NeoPixel LED, so there's only the one available in the list. But you can also control whether or not power is applied to the NeoPixel, so there's a separate pin for that.

That's all there is to figuring out the available names for a pin on a compatible microcontroller board in CircuitPython!

## Microcontroller Pin Names

The pin names available to you in the CircuitPython `board` module are not the same as the names of the pins on the microcontroller itself. The board pin names are aliases to the microcontroller pin names. If you look at the datasheet for your microcontroller, you'll likely find a pinout with a series of pin names, such as "PA18" or "GPIO5". If you want to get to the actual microcontroller pin name in CircuitPython, you'll need the `microcontroller.pin` module. As with `board`, you can run `dir(microcontroller.pin)` in the REPL to receive a list of the microcontroller pin names.

![](https://cdn-learn.adafruit.com/assets/assets/000/099/290/medium800/circuitpython_Essentials_microcontroller_pin_names.png?1612822277 Microcontroller pin names for QT Py SAMD21.)

# CircuitPython Built-In Modules

There is a set of modules used in most CircuitPython programs. One or more of these modules is always used in projects involving hardware. Often hardware requires installing a separate library from the Adafruit CircuitPython Bundle. But, if you try to find `board` or `digitalio` in the same bundle, you'll come up lacking. So, where do these modules come from? They're built into CircuitPython! You can find an comprehensive list of built-in CircuitPython modules and the technical details of their functionality from CircuitPython [here](https://circuitpython.readthedocs.io/en/latest/shared-bindings/index.html#modules) and the Python-like modules included [here](https://circuitpython.readthedocs.io/en/latest/docs/library/index.html). However, **not every module is available for every board** due to size constraints or hardware limitations. How do you find out what modules are available for your board?

There are two options for this. You can check the [support matrix](https://circuitpython.readthedocs.io/en/latest/shared-bindings/support_matrix.html#), and search for your board by name. Or, you can use the REPL.

Plug in your board, connect to the serial console and enter the REPL. Type the following command.

```python
help("modules")
```

![](https://cdn-learn.adafruit.com/assets/assets/000/099/208/medium800/circuitpython_Essentials_help_modules_QT_Py.png?1612372532 help("modules") results for QT Py)

That's it! You now know two ways to find all of the modules built into CircuitPython for your compatible microcontroller board.

# CircuitPython Essentials

## CircuitPython Built-Ins

CircuitPython comes 'with the kitchen sink' - _a lot_ of the things you know and love about classic Python 3 (sometimes called CPython) already work. There are a few things that don't but we'll try to keep this list updated as we add more capabilities!

Info: 

# Thing That Are Built In and Work
## Flow Control

All the usual `if`, `elif`, `else`, `for`, `while` work just as expected.

## Math

`import math` will give you a range of handy mathematical functions.

`>>> dir(math)`  
`[' __name__', 'e', 'pi', 
'sqrt', 'pow', 'exp', 'log', 'cos', 'sin', 'tan', 'acos', 'asin', 
'atan', 'atan2', 'ceil', 'copysign', 'fabs', 'floor', 'fmod', 'frexp', 
'ldexp', 'modf', 'isfinite', 'isinf', 'isnan', 'trunc', 'radians', 
'degrees']`

CircuitPython supports 30-bit wide floating point values so you can use `int` and `float` whenever you expect.

## Tuples, Lists, Arrays, and Dictionaries

You can organize data in `()`,&nbsp; `[]`, and `{}` including strings, objects, floats, etc.

## Classes, Objects and Functions

We use objects and functions extensively in our libraries so check out one of our many examples like this [MCP9808 library](https://github.com/adafruit/Adafruit_CircuitPython_MCP9808/blob/master/adafruit_mcp9808.py) for class examples.

## Lambdas

Yep! You can create function-functions with `lambda` just the way you like em:

`>>> g = lambda x: x**2`  
`>>> g(8)`  
`64`

## Random Numbers

To obtain random numbers:

`import random`

`random.random()` will give a floating point number from `0` to `1.0`.

`random.randint(min, max)` will give you an integer number between `min` and `max`.

# CircuitPython Essentials

## CircuitPython Digital In & Out

The first part of interfacing with hardware is being able to manage digital inputs and outputs. With CircuitPython, it's super easy!

This example shows how to use both a digital input and output. You can use a switch _input_ with pullup resistor (built in) to control a digital _output_ - the built in red LED.

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_Digitial\_In\_Out/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_Digital_In_Out.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_Digital_In_Out/code.py

Note that we made the code a little less "Pythonic" than necessary. The `if/else` block could be replaced with a simple `led.value = not switch.value` but we wanted to make it super clear how to test the inputs. The interpreter will read the digital input when it evaluates `switch.value`.

For **Gemma M0,**  **Trinket M0, Metro M0 Express, Metro M4 Express, ItsyBitsy M0 Express, ItsyBitsy M4 Express** , no changes to the initial example are needed.

Info: 

For **Feather M0 Express and Feather M4 Express** , comment out `switch = DigitalInOut(board.D2)` (and/or `switch = DigitalInOut(board.D7)` depending on what changes you already made), and uncomment `switch = DigitalInOut(board.D5)`.

For **Circuit Playground Express** , you'll need to comment out `switch = DigitalInOut(board.D2)` (and/or `switch = DigitalInOut(board.D5)` depending on what changes you already made), and uncomment&nbsp;`switch = DigitalInOut(board.D7)`.

Warning: 

For **QT Py M0** , you'll need to comment out `led = DigitalInOut(board.LED)` and uncomment `led = DigitalInOut(board.SCK)`. The switch code remains the same.

To find the pin or pad suggested in the code, see the list below. For the boards that require wiring, wire up a switch (also known as a tactile switch, button or push-button), following the diagram for guidance. Press or slide the switch, and the onboard red LED will turn on and off.

Note that on the M0/SAMD based CircuitPython boards, at least, you can also have internal pulldowns with **Pull.DOWN** and if you want to turn off the pullup/pulldown just assign **switch.pull = None.**

## Find the pins!

The list below shows each board, explains the location of the Digital pin suggested for use as input, and the location of the D13 LED.

 **Circuit Playground Express**

&nbsp;

We're going to use the switch, which is pin D7, and is located between the battery connector and the reset switch on the board. The LED is labeled D13 and is located next to the USB micro port.

&nbsp;

To use D7, comment out the current pin setup line, and uncomment the line labeled for Circuit Playground Express. See the details above!

![circuitpython_CircuitPlaygroundExpress_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/501/medium640/circuitpython_CircuitPlaygroundExpress_bb.jpg?1520121934)

 **Trinket M0**

&nbsp;

D2 is connected to the blue wire, labeled "2", and located between "3V" and "1" on the board. The LED is labeled "13" and is located next to the USB micro port.

![circuitpython_TrinketM0Button_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/505/medium640/circuitpython_TrinketM0Button_bb.jpg?1520122206)

 **Gemma M0**

&nbsp;

D2 is an alligator-clip-friendly pad labeled both "D2" and "A1", shown connected to the blue wire, and is next to the USB micro port. The LED is located next to the "GND" label on the board, above the "On/Off" switch.

&nbsp;

Use alligator clips to connect your switch to your Gemma M0!

![circuitpython_GemmaM0Button_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/506/medium640/circuitpython_GemmaM0Button_bb.jpg?1520122250)

 **QT Py M0**

D2 is labeled A2, shown connected to the blue wire, and is near the USB port between A1 and A3.

**There is no little red LED built-in to the QT Py M0.** Therefore, you must connect an external LED for this example to work.

To wire up an external LED:

- **LED +** to **QT Py SCK**  
- **LED -** to **470Ω resistor**
- **470Ω resistor** to **QT Py GND**

The button and the LED share the same GND pin.

To use the external LED, comment out the current LED setup line, and uncomment the line labeled for QT Py M0. See the details above!

![circuitpython_QT_Py_Essentials_Digital_In_Out_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/097/805/medium640/circuitpython_QT_Py_Essentials_Digital_In_Out_bb.jpg?1607985768)

 **Feather M0 Express and Feather M4 Express**

&nbsp;

D5 is labeled "5" and connected to the blue wire on the board. The LED is labeled "#13" and is located next to the USB micro port.

&nbsp;

To use D5, comment out the current pin setup line, and uncomment the line labeled for Feather M0 Express. See the details above!

![circuitpython_FeatherM0ExpressButton_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/502/medium640/circuitpython_FeatherM0ExpressButton_bb.jpg?1520122037)

 **ItsyBitsy M0 Express and ItsyBitsy M4 Express**

&nbsp;

D2 is labeled "2", located between the "MISO" and "EN" labels, and is connected to the blue wire on the board. The LED is located next to the reset button between the "3" and "4" labels on the board.

![circuitpython_ItsyBitsyM0ExpressButton_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/503/medium640/circuitpython_ItsyBitsyM0ExpressButton_bb.jpg?1520122085)

 **Metro M0 Express and Metro M4 Express**

&nbsp;

D2 is located near the top left corner, and is connected to the blue wire. The LED is labeled "L" and is located next to the USB micro port.

![circuitpython_MetroM0ExpressButton_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/504/medium640/circuitpython_MetroM0ExpressButton_bb.jpg?1520122123)

## Read the Docs

For a more in-depth look at what `digitalio` can do, check out [the `DigitalInOut` page in Read the Docs](https://circuitpython.readthedocs.io/en/latest/shared-bindings/digitalio/DigitalInOut.html).

# CircuitPython Essentials

## CircuitPython Analog In

This example shows you how you can read the analog voltage on the A1 pin on your board.

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_Analogin/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_AnalogIn.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_AnalogIn/code.py

Warning: 

## Creating the analog input

`analog1in = AnalogIn(board.A1)`

Creates an object and connects the object to A1 as an analog input.

## `get_voltage` Helper

`get_voltage(pin)` is our little helper program. By default, analog readings will range from 0 (minimum) to 65535 (maximum). This helper will convert the 0-65535 reading from `pin.value` and convert it a 0-3.3V voltage reading.

## Main Loop

The main loop is simple. It `prints` out the voltage as floating point values by calling `get_voltage` on our analog object. Connect to the serial console to see the results.

![](https://cdn-learn.adafruit.com/assets/assets/000/051/621/medium800/circuitpython_AnalogInMuPlotted.png?1520389632)

## Changing It Up

By default the pins are _floating_ so the voltages will vary. While connected to the serial console, try touching a wire from **A1** to the **GND** pin or **3Vo** pin to see the voltage change.

You can also add a potentiometer to control the voltage changes. From the potentiometer to the board, connect the **left**  **pin** to **ground** , the **middle**  **pin** to **A1** , and the **right**  **pin** to **3V**. If you're using Mu editor, you can see the changes as you rotate the potentiometer on the plotter like in the image above! (Click the Plotter icon at the top of the window to open the plotter.)

Info: 

## Wire it up

The list below shows wiring diagrams to help find the correct pins and wire up the potentiometer, and provides more information about analog pins on your board!

 **Circuit Playground Express**

&nbsp;

A1 is located on the right side of the board. There are multiple ground and 3V pads (pins).

&nbsp;

Your board has 7 analog pins that can be used for this purpose. For the full list, see the [pinout page](../../../../adafruit-circuit-playground-express/pinouts) on the main guide.

![circuitpython_CPXPot_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/607/medium640/circuitpython_CPXPot_bb.jpg?1520380773)

 **Trinket M0**

&nbsp;

**A1 is labeled as 2!** It's located between "1~" and "3V" on the same side of the board as the little red LED. Ground is located on the opposite side of the board. 3V is located next to 2, on the same end of the board as the reset button.

&nbsp;

You have 5 analog pins you can use. For the full list, see the [pinouts page](../../../../adafruit-trinket-m0-circuitpython-arduino/pinouts) on the main guide.

![circuitpython_TrinketM0Pot_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/618/medium640/circuitpython_TrinketM0Pot_bb.jpg?1520383153)

 **Gemma M0**

&nbsp;

A1 is located near the top of the board of the board to the left side of the USB Micro port. Ground is on the other side of the USB port from A1. 3V is located to the left side of the battery connector on the bottom of the board.

&nbsp;

Your board has 3 analog pins. For the full list, see the [pinout page](../../../../adafruit-gemma-m0/pinouts) on the main guide.

![circuitpython_GemmaM0Pot_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/611/medium640/circuitpython_GemmaM0Pot_bb.jpg?1520381028)

 **QT Py M0**

A1, shown connected to the blue wire, is near the USB port between A0 and A2. Ground is on the opposite side of the QT Py, near the USB port, between 3V and 5V. 3V is the next pin, between GND and MO.

&nbsp;

Your board has 10 analog pins. For the full list, see the [pinouts page](https://learn.adafruit.com/adafruit-qt-py/pinouts) in the main guide.

![circuitpython_QT_Py_Essentials_Analog_In_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/097/844/medium640/circuitpython_QT_Py_Essentials_Analog_In_bb.jpg?1608044740)

 **Feather M0 Express and Feather M4 Express**

&nbsp;

A1 is located along the edge opposite the battery connector. There are multiple ground pins. 3V is located along the same edge as A1, and is next to the reset button.

&nbsp;

Your board has 6 analog pins you can use. For the full list, see the [pinouts page](../../../../adafruit-feather-m0-express-designed-for-circuit-python-circuitpython/adafruit2-pinouts) on the main guide.

![circuitpython_FeatherM0ExpressPot_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/616/medium640/circuitpython_FeatherM0ExpressPot_bb.jpg?1520382411)

 **ItsyBitsy M0 Express and ItsyBitsy M4 Express**

&nbsp;

A1 is located in the middle of the board, near the "A" in "Adafruit". Ground is labled "G" and is located next to "BAT", near the USB Micro port. 3V is found on the opposite side of the USB port from Ground, next to RST.

&nbsp;

You have 6 analog pins you can use. For a full list, see the [pinouts page](https://learn.adafruit.com/introducing-itsy-bitsy-m0/pinouts) on the main guide.

![circuitpython_ItsyBitsyM0ExpressPot_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/619/medium640/circuitpython_ItsyBitsyM0ExpressPot_bb.jpg?1520386178)

 **Metro M0 Express and Metro M4 Express**

&nbsp;

A1 is located on the same side of the board as the barrel jack. There are multiple ground pins available. 3V is labeled "3.3" and is located in the center of the board on the same side as the barrel jack (and as A1).

&nbsp;

Your **Metro M0 Express** board has 6 analog pins you can use. For the full list, see the [pinouts page](../../../../adafruit-metro-m0-express-designed-for-circuitpython/pinouts) on the main guide.

&nbsp;

Your **Metro M4 Express** board has 6 analog pins you can use. For the full list, see the [pinouts page](../../../../adafruit-metro-m4-express-featuring-atsamd51/pinouts) on the main guide.

![circuitpython_MetroM0ExpressPot_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/052/733/medium640/circuitpython_MetroM0ExpressPot_bb.jpg?1522873347)

# Reading Analog Pin Values

The `get_voltage()` helper used in the potentiometer example above reads the raw analog pin value and converts it to a voltage level. You can, however, directly read an analog pin value in your code by using `pin.value`. For example, to simply read the raw analog pin value from the potentiometer, you would run the following code:

```
import time
import board
from analogio import AnalogIn

analog_in = AnalogIn(board.A1)

while True:
    print(analog_in.value)
    time.sleep(0.1)
```

This works with any analog pin or input. Use the `<pin_name>.value` to read the raw value and utilise it in your code.

# CircuitPython Essentials

## CircuitPython Analog Out

This example shows you how you can use the DAC (Digital to Analog Converter).

Warning: Many chips do not have a DAC, including Espressif ESP32-S3, nRF52840, and various others. If `AnalogOut` is not available, its use will raise `NotImplementedError.` Check the board guide for your board.

Info: 

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_AnalogOut/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_AnalogOut.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_AnalogOut/code.py

## Creating an analog output

`analog_out = AnalogOut(A0)`

Creates an object `analog_out` and connects the object to **A0** , the only DAC pin available on both the M0 and the M4 boards. (The M4 has two, A0 and A1.)

## Setting the analog output

The DAC on the SAMD21 is a 10-bit output, from 0-3.3V. So in theory you will have a resolution of 0.0032 Volts per bit. To allow CircuitPython to be general-purpose enough that it can be used with chips with anything from 8 to 16-bit DACs, the DAC takes a 16-bit value and divides it down internally.

For example, writing 0 will be the same as setting it to 0 - 0 Volts out.

Writing 5000 is the same as setting it to 5000 / 64 = 78, and 78 / 1024 \* 3.3V = 0.25V output.

Writing 65535 is the same as 1023 which is the top range and you'll get 3.3V output

## Main Loop

The main loop is fairly simple, it goes through the entire range of the DAC, from 0 to 65535, but increments 64 at a time so it ends up clicking up one bit for each of the 10-bits of range available.

CircuitPython is not terribly fast, so at the fastest update loop you'll get 4 Hz. The DAC isn't good for audio outputs as-is.

**Express boards like the Circuit Playground Express, Metro M0 Express, ItsyBitsy M0 Express, ItsyBitsy M4 Express, Metro M4 Express, Feather M4 Express, or Feather M0 Express have more code space and can perform audio playback capabilities via the DAC. QT Py M0, Gemma M0 and Trinket M0 cannot!**

Check out [the Audio Out section of this guide](https://learn.adafruit.com/circuitpython-essentials/circuitpython-audio-out) for examples!

![](https://cdn-learn.adafruit.com/assets/assets/000/051/634/medium800/circuitpython_SaleaeAnalogOut.png?1520444380)

## Find the pin

Use the diagrams below to find the A0 pin marked with a magenta arrow!

 **Circuit Playground Express**

&nbsp;

A0 is located between VOUT and A1 near the battery port.

![circuitpython_CPXBoardAnalogOut.png](https://cdn-learn.adafruit.com/assets/assets/000/051/696/medium640/circuitpython_CPXBoardAnalogOut.png?1520475373)

 **Trinket M0**

&nbsp;

**A0 is labeled "1~" on Trinket!** A0 is located between "0" and "2" towards the middle of the board on the same side as the red LED.

![circuitpython_TrinketA0AnalogOut.png](https://cdn-learn.adafruit.com/assets/assets/000/051/697/medium640/circuitpython_TrinketA0AnalogOut.png?1520475473)

 **Gemma M0**

&nbsp;

A0 is located in the middle of the right side of the board next to the On/Off switch.

![circuitpython_GemmaM0BoardAnalogOut.png](https://cdn-learn.adafruit.com/assets/assets/000/051/698/medium640/circuitpython_GemmaM0BoardAnalogOut.png?1520475574)

 **QT Py M0**

A0 is located next to the USB port, by the "QT" label on the board silk.

![circuitpython_QT_Py_Essentials_Analog_Out.png](https://cdn-learn.adafruit.com/assets/assets/000/097/800/medium640/circuitpython_QT_Py_Essentials_Analog_Out.png?1607983936)

 **Feather M0 Express**

&nbsp;

A0 is located between GND and A1 on the opposite side of the board from the battery connector, towards the end with the Reset button.

![circuitpython_FeatherM0ExpressBoardAnalogOut.png](https://cdn-learn.adafruit.com/assets/assets/000/051/699/medium640/circuitpython_FeatherM0ExpressBoardAnalogOut.png?1520475691)

 **Feather M4 Express**

&nbsp;

A0 is located between GND and A1 on the opposite side of the board from the battery connector, towards the end with the Reset button, and the pin pad has left and right white parenthesis markings around it

![circuitpython_FeatherM4A0.png](https://cdn-learn.adafruit.com/assets/assets/000/057/531/medium640/circuitpython_FeatherM4A0.png?1531343425)

 **ItsyBitsy M0 Express**

&nbsp;

A0 is located between VHI and A1, near the "A" in "Adafruit", and the pin pad has left and right white parenthesis markings around it.

![circuitpython_ItsyBitsyM0ExpressBoardAnalogOut.png](https://cdn-learn.adafruit.com/assets/assets/000/051/700/medium640/circuitpython_ItsyBitsyM0ExpressBoardAnalogOut.png?1520475849)

 **ItsyBitsy M4 Express**

&nbsp;

A0 is located between VHI and A1, and the pin pad has left and right white parenthesis markings around it.

![circuitpython_ItsyBitsyM4A0.png](https://cdn-learn.adafruit.com/assets/assets/000/057/532/medium640/circuitpython_ItsyBitsyM4A0.png?1531343837)

 **Metro M0 Express**

&nbsp;

A0 is between VIN and A1, and is located along the same side of the board as the barrel jack adapter towards the middle of the headers found on that side of the board.

![circuitpython_MetroM0ExpressBoardAnalogOut.png](https://cdn-learn.adafruit.com/assets/assets/000/051/701/medium640/circuitpython_MetroM0ExpressBoardAnalogOut.png?1520476033)

 **Metro M4 Express**

&nbsp;

A0 is between VIN and A1, and is located along the same side of the board as the barrel jack adapter towards the middle of the headers found on that side of the board.

&nbsp;

**On the Metro M4 Express, there are TWO true analog outputs: A0 and A1.**

![circuitpython_MetroM4Board.jpg](https://cdn-learn.adafruit.com/assets/assets/000/053/100/medium640/circuitpython_MetroM4Board.jpg?1524155402)

# CircuitPython Essentials

## CircuitPython Audio Out

CircuitPython comes with `audioio`, which provides built-in audio output support. You can play generated tones. You can also play, pause and resume wave files. You can have 3V-peak-to-peak analog output or I2S digital output. In this page we will show using analog output.

This is great for all kinds of projects that require sound, like a tone piano or anything where you'd like to add audio effects!

Danger: 

The first example will show you how to generate a tone and play it using a button. The second example will show you how to play, pause, and resume a wave file using a button to resume. Both will play the audio through an audio jack. The default volume on both of these examples is painfully high through headphones. So, we've added a potentiometer and included some code in the tone generation example to control volume.

In our code, we'll use pin A0 for our audio output, as this is the only DAC pin available on every Express board. The M0 Express boards have audio output on A0. The M4 Express boards have two audio output pins, A0 and A1, however we'll be using only A0 in this guide.

## Play a Tone

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_Audio\_Out\_Tone/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_Audio_Out_Tone.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_Audio_Out_Tone/code.py

First we create the button object, assign it to pin `A1`, and set it as an input with a pull-up. Even though the button switch involves `digitalio`, we're using an A-pin so that the same setup code will work across all the boards.

Since the default volume was incredibly high, we included a `tone_volume` variable in the sine wave code. You can use the code to control the volume by increasing or decreasing this number to increase or decrease the volume. You can also control volume with the potentiometer by rotating the knob.

To set the frequency of the generated tone, change the number assigned to the `frequency` variable to the Hz of the tone you'd like to generate.

Then, we generate one period of a sine wave with the `math.sin` function, and assign it to `sine_wave`.

Next, we create the audio object, and assign it to pin `A0`.

We create a sample of the sine wave by using `RawSample` and providing the `sine_wave` we created.

Inside our loop, we check to see if the button is pressed. The button has two states `True` and `False`. The `button.value` defaults to the `True` state when not pressed. So, to check if it has been pressed, we're looking for the `False` state. So, we check to see `if not button.value` which is the equivalent of `not True`, or `False`.

Once the button is pressed, we `play` the sample we created and we loop it. The `time.sleep(1)` tells it to loop (play) for 1 second. Then we `stop` it after 1 second is up. You can increase or decrease the length of time it plays by increasing or decreasing the number of seconds provided to `time.sleep()`. Try changing it from `1` to `0.5`. Now try changing it to `2`. You can change it to whatever works for you!

That's it!

## Play a Wave File

You can use any supported wave file you like. CircuitPython supports **mo**** no or **** stereo **, at** 22 KHz sample rate**(or less) and**16-bit** WAV format. The M0 boards support ONLY MONO. The reason for mono is that there's only one analog output on those boards! The M4 boards support stereo as they have two outputs. The 22 KHz or less because the circuitpython can't handle more data than that (and also it will not sound much better) and the DAC output is 10-bit so anything over 16-bit will just take up room without better quality.

Since the WAV file must fit on the CircuitPython file system, it must be under 2 MB.

Info: 

[We have a detailed guide on how to generate WAV files here](https://learn.adafruit.com/adafruit-wave-shield-audio-shield-for-arduino/convert-files).

We've included the one we used here. Download it and copy it to your board.

[StreetChicken.wav](https://cdn-learn.adafruit.com/assets/assets/000/057/463/original/StreetChicken.wav?1531255658)
We're going to play the wave file for 6 seconds, pause it, wait for a button to be pressed, and then resume the file to play through to the end. Then it loops back to the beginning and starts again! Let's take a look.

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_Audio\_Out\_Wave/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_Audio_Out_Wave.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_Audio_Out_Wave/code.py

First we create the button object, assign it to pin `A1`, and set it as an input with a pull-up.

Next we then open the file, `"StreetChicken.wav"` as a **r** eadable **b** inary and store the file object in `wave_file` which is what we use to actually read audio from: `wave_file = open("StreetChicken.wav", "rb")`.

Now we will ask the audio playback system to load the wave data from the file `wave = audiocore.WaveFile(wave_file)` and finally request that the audio is played through the A0 analog output pin `audio = audioio.AudioOut(board.A0)`.

The audio file is now ready to go, and can be played at any time with `audio.play(wave)`!

Inside our loop, we start by playing the file.

Next we have the block that tells the code to wait 6 seconds before pausing the file. We chose to go with using `time.monotonic()` because it's **non-blocking** which means you can do other things while the file is playing, like control servos or NeoPixels! At any given point in time, `time.monotonic()` is equal to the number seconds since your board was last power-cycled. (The soft-reboot that occurs with the auto-reload when you save changes to your CircuitPython code, or enter and exit the REPL, does not start it over.) When it is called, it returns a number with a decimal. When you assign `time.monotonic()` to a variable, that variable is equal to the number of seconds that `time.monotonic()` was equal to at the moment the variable was assigned. You can then call it again and subtract the variable from `time.monotonic()` to get the amount of time that has passed. For more details, check out [this example](https://learn.adafruit.com/hacking-ikea-lamps-with-circuit-playground-express/passing-time#time-dot-monotonic-example).

So, we assign `t = time.monotonic()` to get a starting point. Then we say `pass`, or "do nothing" until the difference between `t` and `time.monotonic()` is greater than `6`seconds. In other words, continue playing until 6 seconds passes. Remember, you can add in other code here to do other things while you're playing audio for 6 seconds.

Then we `pause` the audio and `print` to the serial console, `"Waiting for button press to continue!"`

Now we're going to wait for a button press in the same way we did for playing the generated tone. We're saying `while button.value`, or while the button is returning `True`, `pass`. Once the button is pressed, it returns `False`, and this tells the code to continue.

Once the button is pressed, we `resume` playing the file. We tell it to finish playing saying `while audio.playing: pass`.

Finally, we `print` to the serial console, `"Done!"`

You can do this with any supported wave file, and you can include all kinds of things in your project while the file is playing. Give it a try!

## Wire It Up

Along with your **microcontroller board** , we're going to be using:

### Breadboard-Friendly 3.5mm Stereo Headphone Jack

[Breadboard-Friendly 3.5mm Stereo Headphone Jack](https://www.adafruit.com/product/1699)
Pipe audio in or out of your project with this very handy breadboard-friendly audio jack. It's a stereo jack with disconnect-switches on Left and Right channels as well as a center ground pin. You can use any 3.5mm stereo cable with this jack. The pins are on 0.1" spacing so it plugs...

Out of Stock
[Buy Now](https://www.adafruit.com/product/1699)
[Related Guides to the Product](https://learn.adafruit.com/products/1699/guides)
![Close-up of 3.5mm stereo headphone jack.](https://cdn-shop.adafruit.com/640x480/1699-00.jpg)

### Tactile Switch Buttons (12mm square, 6mm tall) x 10 pack

[Tactile Switch Buttons (12mm square, 6mm tall) x 10 pack](https://www.adafruit.com/product/1119)
Medium-sized clicky momentary switches are standard input "buttons" on electronic projects. These work best in a PCB but [can be used on a solderless breadboard as shown in this tutorial](https://learn.adafruit.com/adafruit-arduino-lesson-6-digital-inputs?view=all). The...

In Stock
[Buy Now](https://www.adafruit.com/product/1119)
[Related Guides to the Product](https://learn.adafruit.com/products/1119/guides)
![Angled shot of 10 12mm square tactile switch buttons.](https://cdn-shop.adafruit.com/640x480/1119-03.jpg)

### Panel Mount 10K potentiometer (Breadboard Friendly)

[Panel Mount 10K potentiometer (Breadboard Friendly)](https://www.adafruit.com/product/562)
This potentiometer is a two-in-one, good in a breadboard or with a panel. It's a fairly standard linear taper 10K ohm potentiometer, with a grippy shaft. It's smooth and easy to turn, but not so loose that it will shift on its own. We like this one because the legs are 0.2" apart...

In Stock
[Buy Now](https://www.adafruit.com/product/562)
[Related Guides to the Product](https://learn.adafruit.com/products/562/guides)
![Breadboard Friendly Panel Mount 10K potentiometer linear.](https://cdn-shop.adafruit.com/640x480/562-00.jpg)

### 100uF 16V Electrolytic Capacitors - Pack of 10

[100uF 16V Electrolytic Capacitors - Pack of 10](https://www.adafruit.com/product/2193)
We like capacitors so much we made a&nbsp;[kids' show about them.](https://www.youtube.com/watch?v=sy_G1oYRQmM)&nbsp; &nbsp;They're super handy and it's really helpful to have a variety lying around to pick and choose from.

<p...></p...>In Stock
[Buy Now](https://www.adafruit.com/product/2193)
[Related Guides to the Product](https://learn.adafruit.com/products/2193/guides)
![Pack of 10 through hole 100uF 16V Electrolytic Capacitors](https://cdn-shop.adafruit.com/640x480/2193-00.jpg)

### Full Sized Premium Breadboard - 830 Tie Points

[Full Sized Premium Breadboard - 830 Tie Points](https://www.adafruit.com/product/239)
This is a 'full-size' premium quality breadboard, 830 tie points. Good for small and medium projects. It's 2.2" x 7" (5.5 cm x 17 cm) with a standard double-strip in the middle and two power rails on both sides. You can pull the power rails off easily to make the...

In Stock
[Buy Now](https://www.adafruit.com/product/239)
[Related Guides to the Product](https://learn.adafruit.com/products/239/guides)
![Angled shot of full sized breadboard.](https://cdn-shop.adafruit.com/640x480/239-03.jpg)

### Premium Male/Male Jumper Wires - 20 x 6" (150mm)

[Premium Male/Male Jumper Wires - 20 x 6" (150mm)](https://www.adafruit.com/product/1957)
These Male/Male Jumper Wires are handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 6" (150mm) long and come in a 'strip' of 20 (2&nbsp;pieces of each of ten rainbow colors). They have 0.1" male header contacts on either...

In Stock
[Buy Now](https://www.adafruit.com/product/1957)
[Related Guides to the Product](https://learn.adafruit.com/products/1957/guides)
![Premium Male/Male Jumper Wires - 20 x 6 (150mm) folded over](https://cdn-shop.adafruit.com/640x480/1957-02.jpg)

And to make it easier to wire up the Circuit Playground Express:

Button switches with four pins are really two pairs of pins. When wiring up a button switch with four pins, the easiest way to verify that you're wiring up the correct pins is to **wire up opposite corners of the button switch**. Then there's no chance that you'll accidentally wire up the same pin twice.

Here are the steps you're going to follow to wire up these components:

- Connect the **ground pin on your board** to a **ground rail on the breadboard** because you'll be connecting all three components to ground.&nbsp;
- Connect **one pin on the button switch** to **pin A1 on your board** , and the **opposite pin on the button switch** to the **ground rail on the breadboard**.
- Connect the **left and right pin on the audio jack to each other**.
- Connect the **center pin on the audio jack** to the **ground rail on the breadboard**.
- Connect the **left pin** to the **negative side of a 100uF capacitor.**
- Connect the **positive side of the capacitor** to the **center pin on the potentiometer**.
- Connect the **right pin on the potentiometer** to **pin A0 on your board**.
- Connect the **left pin of the potentiometer** to **the ground rail on the breadboard.**

The list below shows wiring diagrams to help with finding the correct pins and wiring up the different components. The ground wires are black. The wire for the button switch is yellow. The wires involved with audio are blue.

Wiring is the same for the M4 versions of the boards as it is for the M0 versions. Follow the same image for both.

&nbsp;

Use a breadboard to make your wiring neat and tidy!

![circuitpython_ItsyBitsyM0AudioJackButtonPot_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/479/medium640/circuitpython_ItsyBitsyM0AudioJackButtonPot_bb.jpg?1531328765)

![circuitpython_FeatherM0AudioJackButtonPot_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/576/medium640/circuitpython_%10FeatherM0AudioJackButtonPot_bb.jpg?1531437202)

![circuitpython_MetroM0AudioJackButtonPot_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/577/medium640/circuitpython_MetroM0AudioJackButtonPot_bb.jpg?1531437227)

 **Circuit Playground Express** is wired electrically the same as the ItsyBitsy/Feather/Metro above but we use alligator clip to jumper wires instead of plain jumpers

![circuitpython_CPXAudioJackButtonPot_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/486/medium640/circuitpython_CPXAudioJackButtonPot_bb.jpg?1531332766)

# CircuitPython Essentials

## CircuitPython MP3 Audio

Warning: 

https://youtu.be/zDq1XEL-eBc?loop=1

Compressed audio can be a nice alternative to uncompressed WAV files - especially when you have a small filesystem like that on many CircuitPython boards: those WAV files get pretty big fast! Thanks to the expiration of the MP3 patent pool, we can now include MP3 decoding as a core CircuitPython capability and _you can even play multiple MP3s at a time!_

CircuitPython supports any MP3 file you like. We've found that **mono and stereo** files from **32kbit** /s to **128kbit** /s work, with sample rates from **16kHz** to **44.1kHz**. The DAC output on the SAMD51 M4 is just 12-bit so there's not much point in using higher bitrates.

We're going to play one short mp3 file, wait for a button to be pressed, and then play a second short mp3 file. Use the same wiring as the [other audio examples](https://learn.adafruit.com/circuitpython-essentials/circuitpython-audio-out).

Because creating an `MP3Decoder` object takes a lot of memory, it's best to do this just once when your program starts, and then call the `open()` function of the `MP3Decoder` when you want to play a different file.&nbsp; Otherwise, you may encounter the dreaded `MemoryError`.

## Installing Project Code

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_Audio\_Out\_MP3/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_Audio_Out_MP3.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_Audio_Out_MP3/code.py

# CircuitPython Essentials

## CircuitPython PWM

Your board has `pwmio` support, which means you can PWM LEDs, control servos, beep piezos, and manage "pulse train" type devices like DHT22 and Infrared.

_Nearly_ every pin has PWM support! For example, all ATSAMD21 board have an **A0** &nbsp;pin which is 'true' analog out and _does not_ have PWM support.

## **PWM with Fixed Frequency**

This example will show you how to use PWM to fade the little red LED on your board.

Warning: 

The following illustrates how to connect an external LED to a QT Py M0.

- **LED +** to **QT Py SCK**  
- **LED -** to **470Ω resistor**
- **470Ω resistor** to **QT Py GND**

![circuitpython_QT_Py_Essentials_PWM_fixed_freq_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/097/845/medium640/circuitpython_QT_Py_Essentials_PWM_fixed_freq_bb.jpg?1608045764)

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_PWM/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_PWM.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_PWM/code.py

Info: 

 **To use with QT Py M0,** you must comment out `led = pwmio.PWMOut(board.LED, frequency=5000, duty_cycle=0)` and uncomment `led = pwmio.PWMOut(board.SCK, frequency=5000, duty_cycle=0)`. Your setup lines should look like this for the example to work with QT Py M0:

```python
# LED setup for most CircuitPython boards:
# led = pwmio.PWMOut(board.LED, frequency=5000, duty_cycle=0)
# LED setup for QT Py M0:
led = pwmio.PWMOut(board.SCK, frequency=5000, duty_cycle=0)
```

## Create a PWM Output

`led = pwmio.PWMOut(board.LED, frequency=5000, duty_cycle=0)`

Since we're using the onboard LED, we'll call the object `led`, use `pwmio.PWMOut` to create the output and pass in the `D13` LED pin to use.

## Main Loop

The main loop uses `range()` to cycle through the loop. When the range is below 50, it PWMs the LED brightness up, and when the range is above 50, it PWMs the brightness down. This is how it fades the LED brighter and dimmer!

The `time.sleep()` is needed to allow the PWM process to occur over a period of time. Otherwise it happens too quickly for you to see!

## **PWM Output with Variable Frequency**
Fixed frequency outputs are great for pulsing LEDs or controlling servos. But if you want to make some beeps with a piezo, you'll need to vary the frequency.

The following example uses `pwmio` to make a series of tones on a piezo.

To use with any of the M0 boards, no changes to the following code are needed.

Info: 

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_PWM\_Piezo/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_PWM_Piezo.png )

 **To use with the Metro M4 Express, ItsyBitsy M4 Express or the Feather M4 Express, you must comment out the** `piezo = pwmio.PWMOut(board.A2, duty_cycle=0, frequency=440, variable_frequency=True)` **line and uncomment the** `piezo = pwmio.PWMOut(board.A1, duty_cycle=0, frequency=440, variable_frequency=True)` **line. A2 is not a supported PWM pin on the M4 boards!**

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_PWM_Piezo/code.py

The following example uses a nice little helper in the `simpleio` library that makes a tone for you on a piezo with a single command.

To use with any of the M0 boards, no changes to the following code are needed.

**To use with the Metro M4 Express, ItsyBitsy M4 Express or the Feather M4 Express, you must comment out the** `simpleio.tone(board.A2, f, 0.25)` **line and uncomment the** `simpleio.tone(board.A1, f, 0.25)` **line. A2 is not a supported PWM pin on the M4 boards!**

## Installing Project Code

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your **CIRCUITPY** drive. Then you need to update **code.py** with the example script.

Thankfully, we can do this in one go. In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_PWM\_Piezo\_simpleio/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_PWM_Piezo_simpleio.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_PWM_Piezo_simpleio/code.py

As you can see, it's much simpler!

## Wire it up

Use the diagrams below to help you wire up your piezo. Attach one leg of the piezo to pin **A2** on the M0 boards or **A1** on the M4 boards, and the other leg to **ground**. It doesn't matter which leg is connected to which pin. They're interchangeable!

 **Circuit Playground Express**

&nbsp;

Use alligator clips to attach **A2** and any one of the **GND** to different legs of the piezo.

&nbsp;

CPX has PWM on the following pins: A1, A2, A3, A6, RX, LIGHT, A8, TEMPERATURE, A9, BUTTON\_B, D5, SLIDE\_SWITCH, D7, D13, REMOTEIN, IR\_RX, REMOTEOUT, IR\_TX, IR\_PROXIMITY, MICROPHONE\_CLOCK, MICROPHONE\_DATA, ACCELEROMETER\_INTERRUPT, ACCELEROMETER\_SDA, ACCELEROMETER\_SCL, SPEAKER\_ENABLE.

&nbsp;

There is NO PWM on: A0, SPEAKER, A4, SCL, A5, SDA, A7, TX, BUTTON\_A, D4, NEOPIXEL, D8, SCK, MOSI, MISO, FLASH\_CS.

![circuitpython_CPXPiezo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/861/medium640/circuitpython_CPXPiezo_bb.jpg?1520742314)

 **Trinket M0**

&nbsp;

**Note: A2 on Trinket is also labeled Digital "0"!**

&nbsp;

Use jumper wires to connect **GND** and **D0&nbsp;** to different legs of the piezo.

&nbsp;

Trinket has PWM available on the following pins: D0, A2, SDA, D2, A1, SCL, MISO, D4, A4, TX, MOSI, D3, A3, RX, SCK, D13, APA102\_MOSI, APA102\_SCK.

&nbsp;

There is NO PWM on: A0, D1.

![circuitpython_TrinketM0Piezo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/864/medium640/circuitpython_TrinketM0Piezo_bb.jpg?1520742427)

 **Gemma M0**

&nbsp;

Use alligator clips to attach **A2** and **GND** to different legs on the piezo.

&nbsp;

Gemma has PWM available on the following pins: A1, D2, RX, SCL, A2, D0, TX, SDA, L, D13, APA102\_MOSI, APA102\_SCK.

&nbsp;

There is NO PWM on: A0, D1.

![circuitpython_GemmaM0Piezo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/866/medium640/circuitpython_GemmaM0Piezo_bb.jpg?1520742633)

 **QT Py M0**

Use jumper wires to attach **A2** and **GND** to different legs of the piezo.

&nbsp;

The QT Py M0 has PWM on the following pins: A2, A3, A6, A7, A8, A9, A10, D2, D3, D4, D5, D6, D7, D8, D9, D10, SCK, MISO, MOSI, NEOPIXEL, RX, TX, SCL, SDA.

There is NO A0, A1, D0, D1, NEOPIXEL\_POWER.

![circuitpython_QT_Py_Essentials_PWM_Variable_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/097/846/medium640/circuitpython_QT_Py_Essentials_PWM_Variable_bb.jpg?1608046612)

 **Feather M0 Express**

&nbsp;

Use jumper wires to attach **A2** and one of the two **GND** to different legs of the piezo.

&nbsp;

Feather M0 Express has PWM on the following pins: A2, A3, A4, SCK, MOSI, MISO, D0, RX, D1, TX, SDA, SCL, D5, D6, D9, D10, D11, D12, D13, NEOPIXEL.

&nbsp;

There is NO PWM on: A0, A1, A5.

![circuitpython_FeatherM0ExpressPiezo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/868/medium640/circuitpython_FeatherM0ExpressPiezo_bb.jpg?1520742704)

 **Feather M4 Express**

&nbsp;

Use jumper wires to attach **A1** and one of the two **GND** to different legs of the piezo.

&nbsp;

To use A1, comment out the current pin setup line, and uncomment the line labeled for the M4 boards. See the details above!

&nbsp;

Feather M4 Express has PWM on the following pins: A1, A3, SCK, D0, RX, D1, TX, SDA, SCL, D4, D5, D6, D9, D10, D11, D12, D13.

&nbsp;

There is NO PWM on: A0, A2, A4, A5, MOSI, MISO.

![circuitpython_FeatherM4ExpressPiezo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/590/medium640/circuitpython_FeatherM4ExpressPiezo_bb.jpg?1531504323)

 **ItsyBitsy M0 Express**

&nbsp;

Use jumper wires to attach **A2** and **G** to different legs of the piezo.

&nbsp;

ItsyBitsy M0 Express has PWM on the following pins: D0, RX, D1, TX, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13, L, A2, A3, A4, MOSI, MISO, SCK, SCL, SDA, APA102\_MOSI, APA102\_SCK.

&nbsp;

There is NO PWM on: A0, A1, A5.

![circuitpython_ItsyBitsyM0Piezo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/870/medium640/circuitpython_ItsyBitsyM0Piezo_bb.jpg?1520742770)

 **ItsyBitsy M4 Express**

&nbsp;

Use jumper wires to attach **A1** and **G** to different legs of the piezo.

&nbsp;

To use A1, comment out the current pin setup line, and uncomment the line labeled for the M4 boards. See the details above!

&nbsp;

ItsyBitsy M4 Express has PWM on the following pins: A1, D0, RX, D1, TX, D2, D4, D5, D7, D9, D10, D11, D12, D13, SDA, SCL.

There is NO PWM on: A2, A3, A4, A5, D3, SCK, MOSI, MISO.

![circuitpython_ItsyBitsyM4Piezo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/591/medium640/circuitpython_ItsyBitsyM4Piezo_bb.jpg?1531504491)

 **Metro M0 Express**

&nbsp;

Use jumper wires to connect **A2** and any one of the **GND** to different legs on the piezo.

&nbsp;

Metro M0 Express has PWM on the following pins: A2, A3, A4, D0, RX, D1, TX, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13, SDA, SCL, NEOPIXEL, SCK, MOSI, MISO.

&nbsp;

There is NO PWM on: A0, A1, A5, FLASH\_CS.

![circuitpython_MetroM0ExpressPiezo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/871/medium640/circuitpython_MetroM0ExpressPiezo_bb.jpg?1520742810)

 **Metro M4 Express**

&nbsp;

Use jumper wires to connect **A1** and any one of the **GND** to different legs on the piezo.

&nbsp;

To use A1, comment out the current pin setup line, and uncomment the line labeled for the M4 boards. See the details above!

Metro M4 Express has PWM on: A1, A5, D0, RX, D1, TX, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13, SDA, SCK, MOSI, MISO

&nbsp;

There is No PWM on: A0, A2, A3, A4, SCL, AREF, NEOPIXEL, LED\_RX, LED\_TX.

![circuitpython_MetroM4ExpressPiezo.jpg](https://cdn-learn.adafruit.com/assets/assets/000/053/102/medium640/circuitpython_MetroM4ExpressPiezo.jpg?1524164090)

## Where's My PWM?

Want to check to see which pins have PWM yourself? We've written this handy script! It attempts to setup PWM on every pin available, and lets you know which ones work and which ones don't. Check it out!

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/PWM\_Test\_Script/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_PWM_Test_Script.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/PWM_Test_Script/code.py

# CircuitPython Essentials

## CircuitPython Servo

In order to use servos, we take advantage of `pwmio`. Now, in theory, you could just use the raw `pwmio`&nbsp;calls to set the frequency to 50 Hz and then set the pulse widths. But we would rather make it a little more elegant and easy!

So, instead we will use `adafruit_motor` which manages servos for you quite nicely! `adafruit_motor` is a library so be sure to [grab it from the library bundle if you have not yet](https://circuitpython.org/libraries)! If you need help installing the library, check out the [CircuitPython Libraries page](../../../../welcome-to-circuitpython/circuitpython-libraries).

Servos come in two types:&nbsp;

- A **standard hobby servo** - the horn moves 180 degrees (90 degrees in each direction from zero degrees).
- A **continuous servo** - the horn moves in full rotation like a DC motor. Instead of an angle specified, you set a throttle value with 1.0 being full forward, 0.5 being half forward, 0 being stopped, and -1 being full reverse, with other values between.

## Servo Wiring
Warning: 

The connections for a servo are the same for standard servos and continuous rotation servos.

Connect the servo's **brown** or **black** ground wire to ground on the CircuitPython board.

Connect the servo's **red** power wire to 5V power. USB power can in some cases good for a servo or two. But some USB ports supply limited current, and operating a servo from the USB 5V line may cause a power brownout and board crash.

For more servos, you'll need an external battery pack or external power supply. Do not use 3.3V for powering a servo!

Connect the servo's **yellow** or **white** signal wire to the control/data pin, in this case **A1 or**  **A2** but you can use any PWM-capable pin.

For example, to wire a servo to **Trinket** , connect the ground wire to **GND** , the power wire to **USB** , and the signal wire to **0**.

&nbsp;

Remember, **A2 on Trinket is labeled "0"**.

&nbsp;

&nbsp;

![circuitpython_TrinketM0Servo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/927/medium640/circuitpython_TrinketM0Servo_bb.jpg?1520822300)

For **Gemma** , use jumper wire alligator clips to connect the ground wire to **GND** , the power wire to **VOUT** , and the signal wire to **A2**.

![circuitpython_GemmaM0Servo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/928/medium640/circuitpython_GemmaM0Servo_bb.jpg?1520822465)

For **Circuit Playground Express and Circuit Playground Bluefruit** , use jumper wire alligator clips to connect the ground wire to **GND** , the power wire to **VOUT** , and the signal wire to **A2**.

![circuitpython_CPXServo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/991/medium640/circuitpython_CPXServo_bb.jpg?1521080410)

For **QT Py M0** , connect the ground wire to **GND** , the power wire to **5V** , and the signal wire to **A2**.

![circuitpython_QT_Py_Essentials_Servo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/097/847/medium640/circuitpython_QT_Py_Essentials_Servo_bb.jpg?1608050431)

For boards like **Feather M0 Express** , **ItsyBitsy M0 Express** and **Metro M0 Express** , connect the ground wire to any **GND** , the power wire to **USB or 5V** , and the signal wire to **A2**.

![circuitpython_FeatherM0ExpressServo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/929/medium640/circuitpython_FeatherM0ExpressServo_bb.jpg?1520822602)

For the **Metro M4 Express** , **ItsyBitsy M4 Express** and the **Feather M4 Express** , connect the ground wire to any **G or**  **GND** , the power wire to **USB or**  **5V** , and the signal wire to **A2**.

![leds_EssentialsMetroM4ExpressServo_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/104/844/medium640/leds_EssentialsMetroM4ExpressServo_bb.jpg?1632758045)

## Standard Servo Code

Here's an example that will sweep a servo connected to pin **A2** from 0 degrees to 180 degrees (-90 to 90 degrees) and back.

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your **CIRCUITPY** drive. Then you need to update **code.py** with the example script.

Thankfully, we can do this in one go. In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_Servo/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_Servo.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_Servo/code.py

## Continuous Servo Code

There are two differences with Continuous Servos vs. Standard Servos:

1. The `servo` object is created like&nbsp;`my_servo = servo.ContinuousServo(pwm)` instead of&nbsp;`my_servo = servo.Servo(pwm)`
2. Instead of using `myservo.angle`, you use `my_servo.throttle` using a throttle value from 1.0 (full on) to 0.0 (stopped) to -1.0 (full reverse). Any number between would be a partial speed forward (positive) or reverse (negative). This is very similar to standard DC motor control with the **adafruit\_motor** library.

This example runs full forward for 2 seconds, stops for 2 seconds, runs full reverse for 2 seconds, then stops for 4 seconds.

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your **CIRCUITPY** drive. Then you need to update **code.py** with the example script.

Thankfully, we can do this in one go. In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_Continuous\_Servo/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_Continuous_Servo.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_Continuous_Servo/code.py

Pretty simple!

Note that we assume that 0 degrees is 0.5ms and 180 degrees is a pulse width of 2.5ms. That's a bit wider than the&nbsp;_official_ 1-2ms pulse widths. If you have a servo that has a different range you can initialize the `servo` object with a different `min_pulse` and `max_pulse`. For example:

`my_servo = servo.Servo(pwm, min_pulse = 500, max_pulse = 2500)`&nbsp;

For more detailed information on using servos with CircuitPython, check out the [CircuitPython section of the servo guide](../../../../using-servos-with-circuitpython/circuitpython)!

# CircuitPython Essentials

## CircuitPython Cap Touch

Nearly all CircuitPython boards provide capacitive touch capabilities. This means each board has at least one pin that works as an input when you touch it! For SAMD21 (M0) boards, the capacitive touch is done in hardware, so no external resistors, capacitors or ICs required. On SAMD51 (M4), nRF52840, and some other boards, Adafruit uses a software solution: you will need to add a 1M (1 megaohm) resistor from the pin to ground.

On the Circuit Playground Bluefruit (nrf52840) board, the necessary resistors are already on the board, so you don't need to add them.

This example will show you how to use a capacitive touch pin on your board.

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_CapTouch/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_CapTouch.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_CapTouch/code.py

## Create the Touch Input

First, we assign the variable `touch_pad` to a pin. The example uses **A0** , so we assign `touch_pad = board.A0`. You can choose any touch capable pin from the list below if you'd like to use a different pin. Then we create the touch object, name it `touch` and attach it to `touch_pad`.

To use with Circuit Playground Express, comment out `touch_pad = board.A0` and uncomment `touch_pad = board.A1`.

## Main Loop

Next, we create a loop that checks to see if the pin is touched. If it is, it `prints` to the serial console. Connect to the serial console to see the printed results when you touch the pin!

Info: 

No extra hardware is required, because you can touch the pin directly. However, you may want to attach alligator clips or copper tape to metallic or conductive objects. Try metal flatware, fruit or other foods, liquids, aluminum foil, or other items lying around your desk!

![](https://cdn-learn.adafruit.com/assets/assets/000/051/781/medium800/circuitpython_GemmaBananaReady_bb.png?1520640676)

You may need to reload your code or restart your board after changing the attached item because the capacitive touch code "calibrates" based on what it sees when it first starts up. So if you get too many touch responses or not enough, reload your code through the serial console or eject the board and tap the reset button!

## Find the Pin(s)

Your board may have more touch capable pins beyond A0. We've included a list below that helps you find A0 (or A1 in the case of CPX) for this example, identified by the magenta arrow. This list also includes information about any other pins that work for touch on each board!

To use the other pins, simply change the number in **A0** to the pin you want to use. For example, if you want to use **A3** instead, your code would start with `touch_pad = board.A3`.

If you would like to use more than one pin at the same time, your code may look like the following. If needed, you can modify this code to include pins that work for your board.

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_CapTouch\_2Pins/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_CapTouch_2Pins.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_CapTouch_2Pins/code.py

Info: 

Use the list below to find out what pins you can use with your board. Then, try adding them to your code and have fun!

 **Trinket M0**

&nbsp;

There are three touch capable pins on Trinket: **A0** , **A3** , and **A4**.

&nbsp;

Remember, **A0 is labeled "1~" on Trinket M0!**

&nbsp;

![circuitpython_TrinketM0A0.png](https://cdn-learn.adafruit.com/assets/assets/000/051/773/medium640/circuitpython_TrinketM0A0.png?1520638238)

 **Gemma M0**

&nbsp;

There are three pins on Gemma, in the form of alligator-clip-friendly pads, that work for touch input: **A0** , **A1** and **A2**.

![circuitpython_GemmaM0BoardA0.png](https://cdn-learn.adafruit.com/assets/assets/000/051/774/medium640/circuitpython_GemmaM0BoardA0.png?1520638323)

 **QT Py M0**

&nbsp;

There are six pins on QT Py that work for touch input: **A0 - A3, TX,** and **RX**.

![circuitpython_QT_Py_Essentials_Analog_Out.png](https://cdn-learn.adafruit.com/assets/assets/000/097/849/medium640/circuitpython_QT_Py_Essentials_Analog_Out.png?1608051005)

 **Feather M0 Express**

&nbsp;

There are 6 pins on the Feather that have touch capability: **A0 - A5**.

![circuitpython_FeatherM0ExpressBoardA0.png](https://cdn-learn.adafruit.com/assets/assets/000/051/775/medium640/circuitpython_FeatherM0ExpressBoardA0.png?1520639284)

 **ItsyBitsy M0 Express**

&nbsp;

There are 6 pins on the ItsyBitsy that have touch capability: **A0 - A5**.

![circuitpython_ItsyBitsyM0ExpressBoardA0.png](https://cdn-learn.adafruit.com/assets/assets/000/051/776/medium640/circuitpython_ItsyBitsyM0ExpressBoardA0.png?1520639475)

 **Metro M0 Express**

&nbsp;

There are 6 pins on the Metro that have touch capability: **A0 - A5**.

![circuitpython_MetroM0ExpressBoardA0.png](https://cdn-learn.adafruit.com/assets/assets/000/051/777/medium640/circuitpython_MetroM0ExpressBoardA0.png?1520639558)

 **Circuit Playground Express**

&nbsp;

Circuit Playground Express has seven touch capable pins!&nbsp; You have **A1 - A7** available, in the form of alligator-clip-friendly pads. See the [CPX guide Cap Touch section](../../../../adafruit-circuit-playground-express/adafruit2-circuitpython-cap-touch) for more information on using these pads for touch!

&nbsp;

**Remember:** A0 does **NOT** have touch capabilities on CPX.

![circuitpython_CPXTouchA1.png](https://cdn-learn.adafruit.com/assets/assets/000/051/993/medium640/circuitpython_CPXTouchA1.png?1521082516)

# CircuitPython Essentials

## CircuitPython Internal RGB LED

Every board has a built in RGB LED. You can use CircuitPython to control the color and brightness of this LED. There are two different types of internal RGB LEDs: [DotStar](../../../../adafruit-dotstar-leds/overview) and [NeoPixel](../../../../adafruit-neopixel-uberguide/the-magic-of-neopixels). This section covers both and explains which boards have which LED.

![](https://cdn-learn.adafruit.com/assets/assets/000/051/796/medium800/circuitpython_NeoPixelDotStar.jpg?1520652450 Un NeoPixel en la izquierda, un DotStar a la derecha. No es a escala... ¡los DotStars integrados son pequeños!)

The first example will show you how to change the color and brightness of the internal RGB LED.

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your **CIRCUITPY** drive. Then you need to update **code.py** with the example script.

Thankfully, we can do this in one go. In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_Internal\_RGB\_LED\_colors/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_Internal_RGB_LED_colors.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_Internal_RGB_LED_colors/code.py

## Create the LED

First, we create the LED object and attach it to the correct pin or pins. In the case of a NeoPixel, there is only one pin necessary, and we have called it `NEOPIXEL` for easier use. In the case of a DotStar, however, there are two pins necessary, and so we use the pin names&nbsp;`APA102_MOSI` and&nbsp;`APA102_SCK` to get it set up. Since we're using the single onboard LED, the last thing we do is tell it that there's only `1` LED!

**Trinket M0** , **Gemma M0** , **ItsyBitsy M0 Express** , and **ItsyBitsy M4 Express** each have an onboard **Dotstar** LED, so **no changes are needed** to the initial version of the example.

Info: 

 **QT Py M0, Feather M0 Express, Feather M4 Express, Metro M0 Express, Metro M4 Express, and Circuit Playground Express** each have an onboard **NeoPixel** LED, so you must **comment out&nbsp;** `import adafruit_dotstar` **and** `led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)`, and **uncomment&nbsp;** `import neopixel` **and** `led = neopixel.NeoPixel(board.NEOPIXEL, 1)`.

## Brightness

To set the brightness you simply use the `brightness` attribute. Brightness is set with a number between `0` and `1`, representative of a percent from 0% to 100%. So, `led.brightness = (0.3)` sets the LED brightness to 30%. The default brightness is `1` or 100%, and at it's maximum, the LED is blindingly bright! You can set it lower if you choose.

## Main Loop

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.

The main loop is quite simple. It sets the first LED to **red** using `(255, 0, 0)`, then **green** using `(0, 255, 0)`, and finally **blue** using `(0, 0, 255)`. Next, we give it a `time.sleep()` so it stays each color for a period of time. We chose `time.sleep(0.5)`, or half a second. Without the `time.sleep()` it'll flash really quickly and the colors will be difficult to see!

Note that we set `led[0]`. This means the first, and in the case of most of the boards, the only LED. In CircuitPython, counting starts at 0. So the first of any object, list, etc will be `0`!

![](https://cdn-learn.adafruit.com/assets/assets/000/051/941/medium800thumb/circuitpython_ItsyBitsyRGB2.jpg?1520880888)

Try changing the numbers in the tuples to change your LED to any color of the rainbow. Or, you can add more lines with different color tuples to add more colors to the sequence. Always add the `time.sleep()`, but try changing the amount of time to create different cycle animations!

## Making Rainbows (Because Who Doesn't Love 'Em!)
![](https://cdn-learn.adafruit.com/assets/assets/000/051/784/medium800thumb/circuitpython_GemmaRainbow.jpg?1520648803)

Coding a rainbow effect involves a little math and a helper function called `colorwheel`. For details about how wheel works, see [this explanation here](https://learn.adafruit.com/hacking-ikea-lamps-with-circuit-playground-express/generate-your-colors#colorwheel-or-wheel-explained-2982566-3)!

The last example shows how to do a rainbow animation on the internal RGB LED.

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your **CIRCUITPY** drive. Then you need to update **code.py** with the example script.

Thankfully, we can do this in one go. In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_Internal\_RGB\_LED\_rainbow/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_Internal_RGB_LED_rainbow.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_Internal_RGB_LED_rainbow/code.py

We add the `colorwheel` function in after setup but before our main loop.

And right before our main loop, we assign the variable `i = 0`, so it's ready for use inside the loop.

The main loop contains some math that cycles `i` from `0` to `255` and around again repeatedly. We use this value to cycle `colorwheel()` through the rainbow!

The `time.sleep()` determines the speed at which the rainbow changes. Try a higher number for a slower rainbow or a lower number for a faster one!

## Circuit Playground Express Rainbow

Note that here we use `led.fill` instead of `led[0]`. This means it turns on all the LEDs, which in the current code is only one. So why bother with `fill`? Well, you may have a Circuit Playground Express, which as you can see has TEN NeoPixel LEDs built in. The examples so far have only turned on the first one. If you'd like to do a rainbow on all ten LEDs, change the `1` in:

`led = neopixel.NeoPixel(board.NEOPIXEL, 1)`

to `10` so it reads:

` led = neopixel.NeoPixel(board.NEOPIXEL, 10)`.

This tells the code to look for 10 LEDs instead of only 1. Now save the code and watch the rainbow go! You can make the same `1` to `10` change to the previous examples as well, and use `led.fill` to light up all the LEDs in the colors you chose! For more details, check out the [NeoPixel section of the CPX guide](../../../../adafruit-circuit-playground-express/circuitpython-neopixel)!

# CircuitPython Essentials

## CircuitPython NeoPixel

NeoPixels are a revolutionary and ultra-popular way to add lights and color to your project. These stranded RGB lights have the controller inside the LED, so you just push the RGB data and the LEDs do all the work for you. They're a perfect match for CircuitPython!

You can drive 300 NeoPixel LEDs with brightness control (set `brightness=1.0` in object creation) and 1000 LEDs without. That's because to adjust the brightness we have to dynamically recreate the data-stream each write.

![](https://cdn-learn.adafruit.com/assets/assets/000/052/065/medium800/circuitpython_NeoPixelStripPurple.jpg?1521243999)

## Wiring It Up

You'll need to solder up your NeoPixels first. Verify your connection is on the **DATA INPUT** or **DIN** side. Plugging into the DATA OUT or DOUT side is a common mistake! The connections are labeled and some formats have arrows to indicate the direction the data must flow.

For powering the pixels from the board, the 3.3V regulator output can handle about 500mA peak which is about 50 pixels with 'average' use. If you want really bright lights and a lot of pixels, we recommend powering direct from an external power source.

- On Gemma M0 and Circuit Playground Express this is the **Vout** pad - that pad has direct power from USB or the battery, depending on which is higher voltage.
- On Trinket M0, Feather M0 Express, Feather M4 Express, ItsyBitsy M0 Express and ItsyBitsy M4 Express the **USB** or **BAT** pins will give you direct power from the USB port or battery.
- On Metro M0 Express and Metro M4 Express, use the **5V** pin regardless of whether it's powered via USB or the DC jack.
- On QT Py M0, use the **5V** pin.

If the power to the NeoPixels is greater than 5.5V you may have some difficulty driving some strips, in which case you may need to lower the voltage to 4.5-5V or use a level shifter.

Warning: 

![](https://cdn-learn.adafruit.com/assets/assets/000/052/463/medium800/circuitpython_CircuitPythonNeoPIxelGemma_bb.jpg?1522029450)

Info: 

## The Code

This example includes multiple visual effects.

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your **CIRCUITPY** drive. Then you need to update **code.py** with the example script.

Thankfully, we can do this in one go. In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_NeoPixel/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_NeoPixel.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_NeoPixel/code.py

## Create the LED

The first thing we'll do is create the LED object. The NeoPixel object has two required arguments and two optional arguments. You are required to set the pin you're using to drive your NeoPixels and provide the number of pixels you intend to use. You can optionally set `brightness` and `auto_write`.

**NeoPixels can be driven by any pin.** We've chosen **A1**. To set the pin, assign the variable `pixel_pin` to the pin you'd like to use, in our case `board.A1`.

To provide the number of pixels, assign the variable `num_pixels` to the number of pixels you'd like to use. In this example, we're using a strip of `8`.

We've chosen to set `brightness=0.3`, or 30%.

By default, `auto_write=True`, meaning any changes you make to your pixels will be sent automatically. Since `True` is the default, if you use that setting, you don't need to include it in your LED object at all. We've chosen to set&nbsp;`auto_write=False`. If you set `auto_write=False`, you must include `pixels.show()` each time you'd like to send data to your pixels. This makes your code more complicated, but it can make your LED animations faster!

## NeoPixel Helpers

Next we've included a few helper functions to create the super fun visual effects found in this code. First is `wheel()` which we just learned with the [Internal RGB LED](../../../../circuitpython-essentials/circuitpython-internal-rgb-led). Then we have `color_chase()` which requires you to provide a `color` and the amount of time in seconds you'd like between each step of the chase. Next we have `rainbow_cycle()`, which requires you to provide the mount of time in seconds you'd like the animation to take. Last, we've included a list of variables for our colors. This makes it much easier if to reuse the colors anywhere in the code, as well as add more colors for use in multiple places. Assigning and using RGB colors is explained in [this section of the CircuitPython Internal RGB LED page](../../../../circuitpython-essentials/circuitpython-internal-rgb-led#main-loop).

## Main Loop

Thanks to our helpers, our main loop is quite simple. We include the code to set every NeoPixel we're using to red, green and blue for 1 second each. Then we call `color_chase()`, one time for each `color` on our list with `0.1` second delay between setting each subsequent LED the same color during the chase. Last we call `rainbow_cycle(0)`, which means the animation is as fast as it can be. Increase both of those numbers to slow down each animation!

Note that the longer your strip of LEDs, the longer it will take for the animations to complete.

Info: 

## NeoPixel RGBW

NeoPixels are available in RGB, meaning there are three LEDs inside, red, green and blue. They're also available in RGBW, which includes four LEDs, red, green, blue and white. The code for RGBW NeoPixels is a little bit different than RGB.

_If you run RGB code on RGBW NeoPixels, approximately 3/4 of the LEDs will light up and the LEDs will be the incorrect color even though they may appear to be changing._ This is because NeoPixels require a piece of information for each available color (red, green, blue and possibly white).

Therefore, RGB LEDs require three pieces of information and RGBW LEDs require FOUR pieces of information to work. So when you create the LED object for RGBW LEDs, you'll include `pixel_order=(1, 0, 2, 3)`, which sets the pixel order and indicates four pieces of information involved.

Then, you must include an extra number in every color tuple you create. For example, red will be `(255, 0, 0, 0)`. This is how you send the fourth piece of information. Check out the example below to see how our NeoPixel code looks for using with RGBW LEDs!

## The Code

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your **CIRCUITPY** drive. Then you need to update **code.py** with the example script.

Thankfully, we can do this in one go. In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_NeoPixel\_RGBW/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_NeoPixel_RGBW.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_NeoPixel_RGBW/code.py

## Read the Docs

For a more in depth look at what `neopixel` can do, check out [NeoPixel on Read the Docs](https://circuitpython.readthedocs.io/projects/neopixel/en/latest/).

# CircuitPython Essentials

## CircuitPython DotStar

DotStars use two wires, unlike NeoPixel's one wire. They're very similar but you can write to DotStars much faster with hardware SPI _and_ they have a faster PWM cycle so they are better for light painting.

Any pins can be used **but** if the two pins can form a hardware SPI port, the library will automatically switch over to hardware SPI. If you use hardware SPI then you'll get 4 MHz clock rate (that would mean updating a 64 pixel strand in about 500uS - that's 0.0005 seconds). If you use non-hardware SPI pins you'll drop down to about 3KHz, 1000 times as slow!

You can drive 300 DotStar LEDs with brightness control (set `brightness=1.0` in object creation) and 1000 LEDs without. That's because to adjust the brightness we have to dynamically recreate the data-stream each write.

You'll need the **adafruit\_dotstar.mpy** library if you don't already have it in your /lib folder! You can get it from the [CircuitPython Library Bundle](https://circuitpython.org/libraries). If you need help installing the library, check out the [CircuitPython Libraries page](../../../../welcome-to-circuitpython/circuitpython-libraries).

![](https://cdn-learn.adafruit.com/assets/assets/000/052/119/medium800/circuitpython_leds_dotstar-banner.jpg?1521345001)

## Wire It Up

You'll need to solder up your DotStars first. Verify your connection is on the **DATA INPUT** or **DI** and **CLOCK INPUT** or **CI** side. Plugging into the DATA OUT/DO or CLOCK OUT/CO side is a common mistake! The connections are labeled and some formats have arrows to indicate the direction the data must flow. Always verify your wiring with a visual inspection, as the order of the connections can differ from strip to strip!

For powering the pixels from the board, the 3.3V regulator output can handle about 500mA peak which is about 50 pixels with 'average' use. If you want really bright lights and a lot of pixels, we recommend powering direct from an external power source.

- On Gemma M0 and Circuit Playground Express this is the **Vout** pad - that pad has direct power from USB or the battery, depending on which is higher voltage.
- On Trinket M0, Feather M0 Express, Feather M4 Express, ItsyBitsy M0 Express and ItsyBitsy M4 Express the **USB** or **BAT** pins will give you direct power from the USB port or battery.
- On Metro M0 Express and Metro M4 Express, use the **5V** pin regardless of whether it's powered via USB or the DC jack.
- On QT Py M0, use the **5V** pin.

If the power to the DotStars is greater than 5.5V you may have some difficulty driving some strips, in which case you may need to lower the voltage to 4.5-5V or use a level shifter.

Warning: 

![](https://cdn-learn.adafruit.com/assets/assets/000/052/115/medium800/circuitpython_TrinketDotStarWiring.png?1521336877)

Info: 

## The Code

This example includes multiple visual effects.

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your **CIRCUITPY** drive. Then you need to update **code.py** with the example script.

Thankfully, we can do this in one go. In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_DotStar/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_DotStar.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_DotStar/code.py

Info: 

## Create the LED

The first thing we'll do is create the LED object. The DotStar object has three required arguments and two optional arguments. You are required to set the pin you're using for data, set the pin you'll be using for clock, and provide the number of pixels you intend to use. You can optionally set `brightness` and `auto_write`.

**DotStars can be driven by any two pins**. We've chosen **A1** for clock and **A2** for data. To set the pins, include the pin names at the beginning of the object creation, in this case `board.A1` and `board.A2`.

To provide the number of pixels, assign the variable `num_pixels` to the number of pixels you'd like to use. In this example, we're using a strip of `72`.

We've chosen to set `brightness=0.1`, or 10%.

By default, `auto_write=True`, meaning any changes you make to your pixels will be sent automatically. Since `True` is the default, if you use that setting, you don't need to include it in your LED object at all. We've chosen to set&nbsp;`auto_write=False`. If you set `auto_write=False`, you must include `pixels.show()` each time you'd like to send data to your pixels. This makes your code more complicated, but it can make your LED animations faster!

## DotStar Helpers

We've included a few helper functions to create the super fun visual effects found in this code.&nbsp;

First is `wheel()` which we just learned with the [Internal RGB LED](../../../../circuitpython-essentials/circuitpython-internal-rgb-led). Then we have `color_fill()` which requires you to provide a `color` and the length of time you'd like it to be displayed. Next, are `slice_alternating()`, `slice_rainbow()`, and `rainbow_cycle()` which require you to provide the amount of time in seconds you'd between each step of the animation.

Last, we've included a list of variables for our colors. This makes it much easier if to reuse the colors anywhere in the code, as well as add more colors for use in multiple places. Assigning and using RGB colors is explained in [this section of the CircuitPython Internal RGB LED page](../../../../circuitpython-essentials/circuitpython-internal-rgb-led#main-loop).

The two slice helpers utilise a nifty feature of the DotStar library that allows us to use math to light up LEDs in repeating patterns. `slice_alternating()` first lights up the even number LEDs and then the odd number LEDs and repeats this back and forth. `slice_rainbow()` lights up every sixth LED with one of the six rainbow colors until the strip is filled. Both use our handy color variables. This slice code only works when the total number of LEDs is divisible by the slice size, in our case 2 and 6. DotStars come in strips of 30, 60, 72, and 144, all of which are divisible by 2 and 6. In the event that you cut them into different sized strips, the code in this example may not work without modification. However, as long as you provide a total number of LEDs that is divisible by the slices, the code will work.

## Main Loop

Our main loop begins by calling `color_fill()` once for each `color` on our list and sets each to hold for `0.5` seconds. You can change this number to change how fast each color is displayed. Next, we call `slice_alternating(0.1)`, which means there's a 0.1 second delay between each change in the animation. Then, we fill the strip white to create a clean backdrop for the rainbow to display. Then, we call `slice_rainbow(0.1)`, for a 0.1 second delay in the animation. Last we call `rainbow_cycle(0)`, which means it's as fast as it can possibly be. Increase or decrease either of these numbers to speed up or slow down the animations!

Note that the longer your strip of LEDs is, the longer it will take for the animations to complete.

Info: 

## Is it SPI?

We explained at the beginning of this section that the LEDs respond faster if you're using hardware SPI. On some of the boards, there are HW SPI pins directly available in the form of MOSI and SCK. However, hardware SPI is available on more than just those pins. But, how can you figure out which? Easy! We wrote a handy script.

We chose pins **A1** and **A2** for our example code. To see if these are hardware SPI on the board you're using, copy&nbsp;and paste the code into **code.py** using your favorite editor, and save the file. Then connect to the serial console to see the results.

To check if other pin combinations have hardware SPI, change the pin names on the line reading: `if is_hardware_SPI(board.A1, board.A2):` to the pins you want to use. Then, check the results in the serial console. Super simple!

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/SPI\_Test\_Script/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_SPI_Test_Script.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/SPI_Test_Script/code.py

## Read the Docs

For a more in depth look at what `dotstar` can do, check out [DotStar on Read the Docs](https://circuitpython.readthedocs.io/projects/dotstar/en/latest/).

# CircuitPython Essentials

## CircuitPython UART Serial

In addition to the USB-serial connection you use for the REPL, there is also a _hardware_ UART you can use. This is handy to talk to UART devices like GPSs, some sensors, or other microcontrollers!

This quick-start example shows how you can create a UART device for communicating with hardware serial devices.

To use this example, you'll need something to generate the UART data. We've used a GPS! Note that the GPS will give you UART data without getting a fix on your location. You can use this example right from your desk! You'll have data to read, it simply won't include your actual location.

Warning: 

- **LED +** to **QT Py SCK**  
- **LED -** to **470Ω resistor**
- **470Ω resistor** to **QT Py GND**

![circuitpython_QT_Py_Essentials_PWM_fixed_freq_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/102/008/medium640/circuitpython_QT_Py_Essentials_PWM_fixed_freq_bb.jpg?1620158456)

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_UART/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_UART.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_UART/code.py

Info: 

For **QT Py M0** , you'll need to comment out `led = DigitalInOut(board.LED)` and uncomment `led = DigitalInOut(board.SCK)`. The UART code remains the same.

## The Code

First we create the UART object. We provide the pins we'd like to use, `board.TX` and `board.RX`, and we set the `baudrate=9600`. While these pins are labeled on most of the boards, be aware that RX and TX are not labeled on Gemma, and are labeled on the bottom of Trinket. See the diagrams below for help with finding the correct pins on your board.

Once the object is created you read data in with `read(numbytes)` where you can specify the max number of bytes. It will return a byte array type object if anything was received already. Note it will always return immediately because there is an internal buffer! So read as much data as you can 'digest'.

If there is no data available, `read()` will return `None`, so check for that before continuing.

The data that is returned is in a byte array, if you want to convert it to a string, you can use this handy line of code which will run `chr()` on each byte:

`datastr = ''.join([chr(b) for b in data]) # convert bytearray to string`

Your results will look something like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/052/402/medium800/circuitpython_CPUARTREPLOutputMu.png?1521665325)

Info: 

## Wire It Up

You'll need a couple of things to connect the GPS to your board.

For Gemma M0 and Circuit Playground Express, you can use use alligator clips to connect to the Flora Ultimate GPS Module.

For Trinket M0, Feather M0 Express, Metro M0 Express and ItsyBitsy M0 Express, you'll need a breadboard and jumper wires to connect to the Ultimate GPS Breakout.

We've included diagrams show you how to connect the GPS to your board. In these diagrams, the wire colors match the same pins on each board.

- The **black** wire connects between the **ground** pins.
- The **red** wire connects between the **power** pins on the GPS and your board.
- The **blue** wire connects from **TX** on the GPS to **RX** on your board.
- The **white** wire connects from **RX** on the GPS to **TX** on your board.

Check out the list below for a diagram of your specific board!

Warning: 

 **Circuit Playground Express and Circuit Playground Bluefruit**

- Connect **3.3v** on your CPX to **3.3v** on your GPS.
- Connect **GND** on your CPX to **GND** on your GPS.
- Connect **RX/A6** on your CPX to **TX** on your GPS.
- Connect **TX/A7** on your CPX to **RX** on your GPS.

![circuitpython_CPXUARTGPS_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/052/309/medium640/circuitpython_CPXUARTGPS_bb.jpg?1521584997)

 **Trinket M0**

- Connect **USB** on the Trinket to **VIN** on the GPS.
- Connect **Gnd** on the Trinket to **GND** on the GPS.
- Connect **D3** on the Trinket to **TX** on the GPS.
- Connect **D4** on the Trinket to **RX** on the GPS.

![circuitpython_TrinketM0UARTGPS_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/052/310/medium640/circuitpython_TrinketM0UARTGPS_bb.jpg?1521585416)

 **Gemma M0**

- Connect **3vo** on the Gemma to **3.3v** on the GPS.
- Connect **GND** on the Gemma to **GND** on the GPS.
- Connect **A1/D2** on the Gemma to **TX** on the GPS.
- Connect **A2/D0** on the Gemma to **RX** on the GPS.

![circuitpython_GemmaM0UARTGPS_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/052/311/medium640/circuitpython_GemmaM0UARTGPS_bb.jpg?1521585480)

 **QT Py M0**

- Connect **3V** on the QT Py to **VIN** on the GPS.
- Connect **GND** on the QT Py to **GND** on the GPS.
- Connect **RX** on the QT Py to **TX** on the GPS.
- Connect **TX** on the QT Py to **RX** on the GPS.

![circuitpython_QT_Py_Essentials_UART_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/097/860/medium640/circuitpython_QT_Py_Essentials_UART_bb.jpg?1608066669)

 **Feather M0 Express and Feather M4 Express**

- Connect **USB** on the Feather to **VIN** on the GPS.
- Connect **GND** on the Feather to **GND** on the GPS.
- Connect **RX** on the Feather to **TX** on the GPS.
- Connect **TX** on the Feather to **RX** on the GPS.

![circuitpython_FeatherM0UARTGPS_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/052/312/medium640/circuitpython_FeatherM0UARTGPS_bb.jpg?1521585749)

 **ItsyBitsy M0 Express and ItsyBitsy M4 Express**

- Connect **USB** on the ItsyBitsy to **VIN** on the GPS
- Connect **G** on the ItsyBitsy to **GND** on the GPS.
- Connect **RX/0** on the ItsyBitsy to **TX** on the GPS.
- Connect **TX/1** on the ItsyBitsy to **RX** on the GPS.

![circuitpython_ItsyBitsyM0ExpressUARTGPS_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/052/324/medium640/circuitpython_ItsyBitsyM0ExpressUARTGPS_bb.jpg?1521586969)

 **Metro M0 Express and Metro M4 Express**

- Connect **5V** on the Metro to **VIN** on the GPS.
- Connect **GND** on the Metro to **GND** on the GPS.
- Connect **RX/D0** on the Metro to **TX** on the GPS.
- Connect **TX/D1** on the Metro to **RX** on the GPS.

![circuitpython_MetroM0UARTGPS_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/052/328/medium640/circuitpython_MetroM0UARTGPS_bb.jpg?1521587216)

## Where's my UART?

On the SAMD21, we have the flexibility of using a wide range of pins for UART. Compare this to some chips like the ESP8266 with _fixed_ UART pins. The good news is you can use many but not _all_ pins. Given the large number of SAMD boards we have, its impossible to guarantee anything other than the labeled 'TX' and 'RX'.&nbsp;So, if you want some other setup, or multiple UARTs, how will you find those pins? Easy! We've written a handy script.

These are the results from a Trinket M0, your output may vary and it might be&nbsp;_very_ long. [For more details about UARTs and SERCOMs check out our detailed guide here](../../../../using-atsamd21-sercom-to-add-more-spi-i2c-serial-ports)

![](https://cdn-learn.adafruit.com/assets/assets/000/052/360/medium800/circuitpython_CPUARTTestScriptTrinketOutput.png?1521604071)

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/UART\_Test\_Script/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_UART_Test_Script.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/UART_Test_Script/code.py

## Trinket M0: Create UART before I2C

On the Trinket M0 (only), if you are using both UART and I2C, you must create the UART object first, e.g.:

```python
&gt;&gt;&gt; import board
&gt;&gt;&gt; uart = board.UART()   # Uses pins 4 and 3 for TX and RX, baudrate 9600.
&gt;&gt;&gt; i2c = board.I2C()     # Uses pins 2 and 0 for SCL and SDA.

# or alternatively,
```

Creating the I2C object first does not work:

```python
&gt;&gt;&gt; import board
&gt;&gt;&gt; i2c = board.I2C()     # Uses pins 2 and 0 for SCL and SDA.
&gt;&gt;&gt; uart = board.UART()   # Uses pins 4 and 3 for TX and RX, baudrate 9600.
Traceback (most recent call last):
File "", line 1, in 
ValueError: Invalid pins
```

# CircuitPython Essentials

## CircuitPython I2C

I2C is a 2-wire protocol for communicating with simple sensors and devices, meaning it uses two connections for transmitting and receiving data. There are many I2C devices available and they're really easy to use with CircuitPython. We have libraries available for many I2C devices in the [library bundle](https://circuitpython.org/libraries). (If you don't see the sensor you're looking for, keep checking back, more are being written all the time!)

In this section, we're going to do is learn how to scan the I2C bus for all connected devices. Then we're going to learn how to interact with an I2C device.

We'll be using the [Adafruit TSL2591](https://www.adafruit.com/product/1980), a common, low-cost light sensor. While the exact code we're running is specific to the TSL2591 the overall process is the same for just about any I2C sensor or device.

These examples will use the TSL2591 lux sensor breakout. The first thing you'll want to do is get the sensor connected so your board has I2C to talk to.

## Wire It Up

You'll need a couple of things to connect the TSL2591 to your board. The TSL2591 comes with STEMMA QT / QWIIC connectors on it, which makes it super simple to wire it up. No further soldering required!

For Gemma M0, Circuit Playground Express and Circuit Playground Bluefruit, you can use use the [STEMMA QT to alligator clips cable](https://www.adafruit.com/product/4398) to connect to the TSL2591.

For Trinket M0, Feather M0 and M4 Express, Metro M0 and M4 Express and ItsyBitsy M0 and M4 Express, you'll need a breadboard and [STEMMA QT to male jumper wires cable](https://www.adafruit.com/product/4209) to connect to the TSL2591.

For QT Py M0, you'll need a [STEMMA QT cable](https://www.adafruit.com/product/4210) to connect to the TSL2591.

We've included diagrams show you how to connect the TSL2591 to your board. In these diagrams, the wire colors match the STEMMA QT cables and connect to the same pins on each board.

- The **black** wire connects from **GND** on the TSL2591 to **ground** on your board.
- The **red** wire connects from **VIN** on the TSL2591 to **power** on your board.
- The **yellow** wire connects from **SCL** on the TSL2591 to **SCL** on your board.
- The **blue** wire connects from **SDA** on the TSL2591 to **SDA** on your board.

Check out the list below for a diagram of your specific board!

Warning: 

 **Circuit Playground Express and Circuit Playground Bluefruit**

&nbsp;

- Connect **3.3v** on your CPX to **3.3v** on your TSL2591.
- Connect **GND** on your CPX to **GND** on your TSL2591.
- Connect **SCL/A4** on your CPX to **SCL** on your TSL2591.
- Connect **SDL/A5** on your CPX to **SDA** on your TSL2591.

![circuitpython_Essentials_I2C_CPX_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/097/884/medium640/circuitpython_Essentials_I2C_CPX_bb.jpg?1608073399)

 **Trinket M0**

&nbsp;

- Connect **USB** on the Trinket to **VIN** on the TSL2591.
- Connect **Gnd** on the Trinket to **GND** on the TSL2591.
- Connect **D2** on the Trinket to **SCL** on the TSL2591.
- Connect **D0** on the Trinket to **SDA** on the TSL2591.

![circuitpython_Essentials_I2C_Trinket_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/097/885/medium640/circuitpython_Essentials_I2C_Trinket_bb.jpg?1608073437)

 **Gemma M0**

&nbsp;

- Connect **3vo** on the Gemma to **3V** on the TSL2591.
- Connect **GND** on the Gemma to **GND** on the TSL2591.
- Connect **A1/D2** on the Gemma to **SCL** on the TSL2591.
- Connect **A2/D0** on the Gemma to **SDA** on the TSL2591.

![circuitpython_Essentials_I2C_GEMMA_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/097/886/medium640/circuitpython_Essentials_I2C_GEMMA_bb.jpg?1608073480)

 **QT Py M0**

If using the STEMMA QT cable:

- Connect the **STEMMA QT cable** from **the connector on the QT Py** to **the connector on the TSL2591.**

Alternatively, if using a breadboard:

- Connect **3V** on the QT Py to **VIN** on the TSL2591.
- Connect **GND** on the QT Py to **GND** on the TSL2591.
- Connect **SCL** on the QT Py to **SCL** on the TSL2591.
- Connect **SDA** on the QT Py to **SDA** on the TSL2591.

![circuitpython_Essentials_I2C_QT_Py_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/097/890/medium640/circuitpython_Essentials_I2C_QT_Py_bb.jpg?1608073677)

 **Feather M0 Express and Feather M4 Express**

&nbsp;

- Connect **USB** on the Feather to **VIN** on the TSL2591.
- Connect **GND** on the Feather to **GND** on the TSL2591.
- Connect **SCL** on the Feather to **SCL** on the TSL2591.
- Connect **SDA** on the Feather to **SDA** on the TSL2591.

![circuitpython_Essentials_I2C_Feather_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/097/887/medium640/circuitpython_Essentials_I2C_Feather_bb.jpg?1608073520)

 **ItsyBitsy M0 Express and ItsyBitsy M4 Express**

&nbsp;

- Connect **USB** on the ItsyBitsy to **VIN** on the TSL2591
- Connect **G** on the ItsyBitsy to **GND** on the TSL2591.
- Connect **SCL** on the ItsyBitsy to **SCL** on the TSL2591.
- Connect **SDA** on the ItsyBitsy to **SDA** on the TSL2591.

![circuitpython_Essentials_I2C_Itsy_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/097/888/medium640/circuitpython_Essentials_I2C_Itsy_bb.jpg?1608073571)

 **Metro M0 Express and Metro M4 Express**

&nbsp;

- Connect **5V** on the Metro to **VIN** on the TSL2591.
- Connect **GND** on the Metro to **GND** on the TSL2591.
- Connect **SCL** on the Metro to **SCL** on the TSL2591.
- Connect **SDA** on the Metro to **SDA** on the TSL2591.

![circuitpython_Essentials_I2C_Metro_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/097/889/medium640/circuitpython_Essentials_I2C_Metro_bb.jpg?1608073608)

## Find Your Sensor

The first thing you'll want to do after getting the sensor wired up, is make sure it's wired correctly. We're going to do an I2C scan to see if the board is detected, and if it is, print out its I2C address.

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_I2C\_Scan/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_I2C_Scan.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_I2C_Scan/code.py

First we create the `i2c` object, using `board.I2C()`. This convenience routine creates and saves a `busio.I2C` object using the default pins `board.SCL` and `board.SDA`. If the object has already been created, then the existing object is returned. No matter how many times you call `board.I2C()`, it will return the same object. This is called a **singleton**.

To be able to scan it, we need to lock the I2C down so the only thing accessing it is the code. So next we include a loop that waits until I2C is locked and then continues on to the scan function.

Last, we have the loop that runs the actual scan, `i2c_scan()`. Because I2C typically refers to addresses in hex form, we've included this bit of code that formats the results into hex format: `[hex(device_address) for device_address in i2c.scan()]`.

Open the serial console to see the results! The code prints out an array of addresses. We've connected the TSL2591 which has a 7-bit I2C address of 0x29. The result for this sensor is `I2C addresses found: ['0x29']`. If no addresses are returned, refer back to the wiring diagrams to make sure you've wired up your sensor correctly.

## I2C Sensor Data

Now we know for certain that our sensor is connected and ready to go. Let's find out how to get the data from our sensor!

## Installing Project Code

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your **CIRCUITPY** drive. Then you need to update **code.py** with the example script.

Thankfully, we can do this in one go. In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_I2C\_TSL2591/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_I2C_TSL2591.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_I2C_TSL2591/code.py

This code begins the same way as the scan code. We've included the scan code so you have verification that your sensor is wired up correctly and is detected. It prints the address once. After the scan, we unlock I2C with `i2c_unlock()` so we can use the sensor for data.

We create our sensor object using the sensor library. We call it `tsl2591` and provide it the `i2c` object.

Then we have a simple loop that prints out the lux reading using the sensor object we created. We add a `time.sleep(1.0)`, so it only prints once per second. Connect to the serial console to see the results. Try shining a light on it to see the results change!

![](https://cdn-learn.adafruit.com/assets/assets/000/097/891/medium800/circuitpython_Essentials_I2C_Mu_serial.png?1608075417)

## Where's my I2C?

On the SAMD21, SAMD51 and nRF52840, we have the flexibility of using a wide range of pins for I2C. On the nRF52840, any pin can be used for I2C! Some chips, like the ESP8266, require using bitbangio, but can also use any pins for I2C. There's some other chips that may have fixed I2C pin.&nbsp;&nbsp;

The good news is you can use many but not _all_ pins. Given the large number of SAMD boards we have, its impossible to guarantee anything other than the labeled 'SDA' and 'SCL'.&nbsp;So, if you want some other setup, or multiple I2C interfaces, how will you find those pins? Easy! We've written a handy script.

These are the results from an ItsyBitsy M0 Express. Your output may vary and it might be&nbsp;_very_ long. For more details about I2C and SERCOMs,[check out our detailed guide here](../../../../using-atsamd21-sercom-to-add-more-spi-i2c-serial-ports).

![](https://cdn-learn.adafruit.com/assets/assets/000/052/421/medium800/circuitpython_CircuitPythonI2CTestScriptOutputItsyBitsy.png?1521761098)

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/I2C\_Test\_Script/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_I2C_Test_Script.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/I2C_Test_Script/code.py

## Clock Speed

For the majority of use cases, the default I2C clock setting (typically 100 kHz) should be fine. However, there are some use cases where changing the I2C clock speed is useful. This will typically be taken care of under the hood by the support library for whatever hardware has this requirement. However, if this needs to be done directly in user code it can be done when setting up the I2C bus.

More information here:

[Clock Speed](https://learn.adafruit.com/working-with-i2c-devices/clock-speed)
# CircuitPython Essentials

## CircuitPython HID Keyboard and Mouse

One of the things we baked into CircuitPython is 'HID' ( **H** uman **I** nterface **D** evice) control - that means keyboard and mouse capabilities. This means your CircuitPython board can act like a keyboard device and press key commands, or a mouse and have it move the mouse pointer around and press buttons. This is really handy because even if you cannot adapt your software to work with hardware, there's almost always a keyboard interface - so if you want to have a capacitive touch interface for a game, say, then keyboard emulation can often get you going really fast!

This section walks you through the code to create a keyboard or mouse emulator. First we'll go through an example that uses pins on your board to emulate keyboard input. Then, we will show you how to wire up a joystick to act as a mouse, and cover the code needed to make that happen.

## CircuitPython Keyboard Emulator

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your **CIRCUITPY** drive. Then you need to update **code.py** with the example script.

Thankfully, we can do this in one go. In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_HID\_Keyboard/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_HID_Keyboard.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_HID_Keyboard/code.py

Connect pin **A1** _or_ **A2** to ground, using a wire or alligator clip, then disconnect it to send the key press "A" or the string "Hello world!"

This wiring example shows A1 and A2 connected to ground.

&nbsp;

Remember, on Trinket, A1 and A2 are labeled 2 and 0! On other boards, you will have A1 and A2 labeled as expected.

![circuitpython_CirucitPythonHIDTrinketA1A2Ground_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/052/710/medium640/circuitpython_CirucitPythonHIDTrinketA1A2Ground_bb.jpg?1522702554)

## Create the Objects and Variables

First, we assign some variables for later use. We create three arrays assigned to variables: `keypress_pins`, `key_pin_array`, and `keys_pressed`. The first is the pins we're going to use. The second is empty because we're going to fill it later. The third is what we would like our "keyboard" to output - in this case the letter "A" and the phrase, "Hello world!". We create our last variable assigned to `control_key` which allows us to later apply the shift key to our keypress. We'll be using two keypresses, but you can have up to six keypresses at once.

Next `keyboard` and `keyboard_layout` objects are created. We only have US right now (if you make other layouts please submit a GitHub pull request!). The `time.sleep(1)` avoids an error that can happen if the program gets run as soon as the board gets plugged in, before the host computer finishes connecting to the board.

Then we take the pins we chose above, and create the pin objects, set the direction and give them each a pullup. Then we apply the pin objects to `key_pin_array` so we can use them later.

Next we set up the little red LED to so we can use it as a status light.

The last thing we do before we start our loop is `print`, "Waiting for key pin..." so you know the code is ready and waiting!

## The Main Loop

Inside the loop, we check each pin to see if the state has changed, i.e. you connected the pin to ground. Once it changes, it prints, "Pin # grounded." to let you know the ground state has been detected. Then we turn on the red LED. The code waits for the state to change again, i.e. it waits for you to unground the pin by disconnecting the wire attached to the pin from ground.

Then the code gets the corresponding keys pressed from our array. If you grounded and ungrounded A1, the code retrieves the keypress `a`, if you grounded and ungrounded A2, the code retrieves the string, `"Hello world!"`

If the code finds that it's retrieved a string, it prints the string, using the `keyboard_layout` to determine the keypresses. Otherwise, the code prints the keypress from the `control_key` and the keypress "a", which result in "A". Then it calls `keyboard.release_all()`. You always want to call this soon after a keypress or you'll end up with a stuck key which is really annoying!

Instead of using a wire to ground the pins, you can try wiring up buttons like we did in [CircuitPython Digital In & Out](../../../../circuitpython-essentials/circuitpython-digital-in-out). Try altering the code to add more pins for more keypress options!

## Non-US Keyboard Layouts

The code above uses KeyboardLayoutUS. If you would like to emulate a non-US keyboard, a number of other keyboard layout classes [are available](https://github.com/Neradoc/Circuitpython_Keyboard_Layouts).

# CircuitPython Mouse Emulator

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your **CIRCUITPY** drive. Then you need to update **code.py** with the example script.

Thankfully, we can do this in one go. In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_HID\_Mouse/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_HID_Mouse.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_HID_Mouse/code.py

For this example, we've wired up a 2-axis thumb joystick with a select button. We use this to emulate the mouse movement and the mouse left-button click. To wire up this joytick:

- Connect **VCC** on the joystick to the **3V** on your board. Connect **ground** to **ground**.
- Connect **Xout** on the joystick to pin **A0** on your board.
- Connect **Yout** on the joystick to pin **A1** on your board.
- Connect **Sel** on the joystick to pin **A2** on your board.

Remember, Trinket's pins are labeled differently. Check the [Trinket Pinouts page](../../../../adafruit-trinket-m0-circuitpython-arduino/pinouts#unique-pad-capabilities) to verify your wiring.

&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/052/466/medium800/circuitpython_CircuitPythonHIDItsyBitsyMouse_bb.jpg?1522032433)

To use this demo, simply move the joystick around. The mouse will move slowly if you move the joystick a little off center, and more quickly if you move it as far as it goes. Press down on the joystick to click the mouse. Awesome! Now let's take a look at the code.

## Create the Objects and Variables

First we create the mouse object.

Next, we set `x_axis` and `y_axis` to pins `A0` and `A1`. Then we set `select` to `A2`, set it as input and give it a pullup.

The x and y axis on the joystick act like 2 potentiometers. We'll be using them just like we did in [CircuitPython Analog In](../../../../circuitpython-essentials/circuitpython-analog-in). We set `pot_min` and `pot_max` to be the minimum and maximum voltage read from the potentiometers. We assign `step = (pot_max - pot_min) / 20.0` to use in a helper function.

## CircuitPython HID Mouse Helpers

First we have the `get_voltage()` helper so we can get the correct readings from the potentiometers. Look familiar? We [learned about it in Analog In](../../../../circuitpython-essentials/circuitpython-analog-in#get-voltage-helper).

Second, we have `steps(axis)`. To use it, you provide it with the axis you're reading. This is where we're going to use the `step` variable we assigned earlier. The potentiometer range is 0-3.29. This is a small range. It's even smaller with the joystick because the joystick sits at the center of this range, 1.66, and the + and - of each axis is above and below this number. Since we need to have thresholds in our code, we're going to map that range of 0-3.29 to while numbers between 0-20.0 using this helper function. That way we can simplify our code and use larger ranges for our thresholds instead of trying to figure out tiny decimal number changes.

## Main Loop

First we assign `x` and `y` to read the voltages from `x_axis` and `y_axis`.

Next, we check to see when the state of the select button is `False`. It defaults to `True` when it is not pressed, so if the state is `False`, the button has been pressed. When it's pressed, it sends the command to click the left mouse button. The `time.sleep(0.2)` prevents it from reading multiple clicks when you've only clicked once.

Then we use the `steps()` function to set our mouse movement. There are two sets of two `if` statements for each axis. Remember that `10` is the center step, as we've mapped the range `0-20`. The first set for each axis says if the joystick moves 1 step off center (left or right for the x axis and up or down for the y axis), to move the mouse the appropriate direction by 1 unit. The second set for each axis says if the joystick is moved to the lowest or highest step for each axis, to move the mouse the appropriate direction by 8 units. That way you have the option to move the mouse slowly or quickly!

To see what `step` the joystick is at when you're moving it, uncomment the `print` statements by removing the `# ` from the lines that look like `# print(steps(x))`, and connecting to the serial console to see the output. Consider only uncommenting one set at a time, or you end up with a huge amount of information scrolling very quickly, which can be difficult to read!

Info: 

# CircuitPython Essentials

## CircuitPython Storage

CircuitPython-compatible microcontrollers show up as a&nbsp; **CIRCUITPY** &nbsp;drive when plugged into your computer, allowing you to edit code directly on the board. Perhaps you've wondered whether or not you can write data&nbsp;_from CircuitPython_&nbsp;directly to the board to act as a data logger. The answer is&nbsp; **yes**!

The&nbsp;`storage`&nbsp;module in CircuitPython enables you to write code that allows CircuitPython to write data to the&nbsp; **CIRCUITPY** &nbsp;drive. This process requires you to include a&nbsp; **boot.py** &nbsp;file on your&nbsp; **CIRCUITPY** &nbsp;drive, along side your&nbsp; **code.py** &nbsp;file.

The&nbsp; **boot.py** &nbsp;file is special - the code within it is executed when CircuitPython starts up, either from a hard reset or powering up the board. It is not run on soft reset, for example, if you reload the board from the serial console or the REPL. This is in contrast to the code within&nbsp; **code.py** , which is executed after CircuitPython is already running.

The **CIRCUITPY** drive is typically writable by your computer; this is what allows you to edit your code directly on the board. The reason you need a&nbsp; **boot.py** &nbsp;file is that you have to set the filesystem to be read-only by your computer to allow it to be writable by CircuitPython. This is because CircuitPython cannot write to the filesystem at the same time as your computer. Doing so can lead to filesystem corruption and loss of all content on the drive, so CircuitPython is designed to only allow one at at time.

Danger: 

## boot.py
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_Logger/boot.py

The&nbsp;`storage.remount()`&nbsp;command has a&nbsp;`readonly`&nbsp;keyword argument.&nbsp; **This argument refers to the read/write state of CircuitPython.&nbsp;** It does NOT refer to the read/write state of your computer.

When the physical pin is connected to ground, it returns&nbsp;`False`. The&nbsp;`readonly`&nbsp;argument in boot.py is set to the&nbsp;`value`&nbsp;of the pin. When the&nbsp;`value=True`, the CIRCUITPY drive is read-only to CircuitPython (and writable by your computer).&nbsp; **When the&nbsp;`value=False`, the CIRCUITPY drive is writable by CircuitPython** &nbsp;(and read-only by your computer).

For **Gemma M0,**  **Trinket M0, Metro M0 Express, Metro M4 Express, ItsyBitsy M0 Express and ItsyBitsy M4 Express** , no changes to the initial code are needed.

For **Feather M0 Express and Feather M4 Express** , comment out `switch = digitalio.DigitalInOut(board.D2)`, and uncomment `switch = digitalio.DigitalInOut(board.D5)`.

For **Circuit Playground Express and Circuit Playground Bluefruit** , comment out `switch = digitalio.DigitalInOut(board.D2)`, and uncomment&nbsp;`switch = digitalio.DigitalInOut(board.D7)`. Remember, D7 is the onboard slide switch, so there's no extra wires or alligator clips needed.

On the Circuit Playground Express or Circuit Playground Bluefruit, the switch is in the right position (closer to the ear icon on the silkscreen) it returns `False`, and the **CIRCUITPY** drive will be writable by CircuitPython. If the switch is in the left position (closer to the music icon on the silkscreen), it returns `True`, and the **CIRCUITPY** drive will be writable by your computer.

Info: 

## Installing Project Code

In the example below, click the **Download Project Bundle** button below to download the necessary libraries and the **code.py** file in a zip file. Extract the contents of the zip file, open the directory **CircuitPython\_Essentials/CircuitPython\_Logger/** and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your **CIRCUITPY** drive.

Your **CIRCUITPY** drive should now look similar to the following image:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_Essentials_CircuitPython_Logger.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_Logger/code.py

Info: 

## Logging the Temperature
The way **boot.py** works is by checking to see if the pin you specified in the switch setup in your code is connected to a ground pin. If it is, it changes the read-write state of the file system, so the CircuitPython core can begin logging the temperature to the board.

For help finding the correct pins, see the wiring diagrams and information in the [Find the Pins section of the CircuitPython Digital In & Out guide](../../../../adafruit-trinket-m0-circuitpython-arduino/circuitpython-digital-in-out#find-the-pins). Instead of wiring up a switch, however, you'll be connecting the pin directly to ground with alligator clips or jumper wires.

![](https://cdn-learn.adafruit.com/assets/assets/000/052/450/medium800/circuitpython_GemmaItsyBitsyD2toGround_bb.jpg?1521847355)

Warning: 

Once you copied the files to your board, eject it and unplug it from your computer. If you're using your Circuit Playground Express, all you have to do is make sure the switch is to the right. Otherwise, use alligator clips or jumper wires to connect the chosen pin to ground. Then, plug your board back into your computer.

You will not be able to edit code on your **CIRCUITPY** drive anymore!

![](https://cdn-learn.adafruit.com/assets/assets/000/052/451/medium800/circuitpython_CircuitPythyonStorageReadOnly.png?1521848129)

The red LED should blink once a second and you will see a new **temperature.txt** file on **CIRCUITPY**.

![](https://cdn-learn.adafruit.com/assets/assets/000/052/452/medium800/circuitpython_CircuitPythonStorageTempTxt.png?1521848156)

This file gets updated once per second, but you won't see data come in live. Instead, when you're ready to grab the data, eject and unplug your board. For CPX, move the switch to the left, otherwise remove the wire connecting the pin to ground. Now it will be possible for you to write to the filesystem from your computer again, but it will not be logging data.

We have a more detailed guide on this project available here: [CPU Temperature Logging with CircuitPython](../../../../cpu-temperature-logging-with-circuit-python). If you'd like more details, check it out!

# CircuitPython Essentials

## CircuitPython CPU Temp

There is a CPU temperature sensor built into every ATSAMD21, ATSAMD51 and nRF52840 chips. CircuitPython makes it really simple to read the data from this sensor. This works on the Adafruit CircuitPython boards it's built into the microcontroller used for these boards.

The data is read using two simple commands. We're going to enter them in the REPL. Plug in your board, [connect to the serial console](../../../../welcome-to-circuitpython/kattni-connecting-to-the-serial-console), and [enter the REPL](../../../../welcome-to-circuitpython/the-repl). Then, enter the following commands into the REPL:

```
import microcontroller
microcontroller.cpu.temperature
```

That's it! You've printed the temperature in Celsius to the REPL. Note that it's not exactly the ambient temperature and it's not super precise. But it's close!

![](https://cdn-learn.adafruit.com/assets/assets/000/052/289/medium800/circuitpython_CircuitPythonCPUTemp.png?1521573346)

If you'd like to print it out in Fahrenheit, use this simple formula: Celsius \* (9/5) + 32. It's super easy to do math using CircuitPython. Check it out!

![](https://cdn-learn.adafruit.com/assets/assets/000/052/290/medium800/circuitpython_CircuitPythonCPUTempF.png?1521573354)

Info: 

# CircuitPython Essentials

## CircuitPython Expectations

Danger: 

# Always Run the Latest Version of CircuitPython and Libraries

As we continue to develop CircuitPython and create new releases, we will stop supporting older releases. **You need to [update to the latest CircuitPython](https://circuitpython.org/downloads).**

You need to download the CircuitPython Library Bundle that matches your version of CircuitPython. **Please update CircuitPython and then [download the latest bundle](https://circuitpython.org/libraries)**.

As we release new versions of CircuitPython, we will stop providing the previous bundles as automatically created downloads on the Adafruit CircuitPython Library Bundle repo. If you must continue to use an earlier version, you can still download the appropriate version of `mpy-cross` from the particular release of CircuitPython on the CircuitPython repo and create your own compatible .mpy library files. **However, it is best to update to the latest for both CircuitPython and the library bundle.**

# I have to continue using CircuitPython 3.x or 2.x, where can I find compatible libraries?

**We are no longer building or supporting the CircuitPython 2.x and 3.x library bundles. We highly encourage you to [update CircuitPython to the latest version](https://circuitpython.org/downloads) and use [the current version of the libraries](https://circuitpython.org/libraries).** However, if for some reason you cannot update, you can find [the last available 2.x build here](https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/download/20190903/adafruit-circuitpython-bundle-2.x-mpy-20190903.zip) and [the last available 3.x build here](https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/download/20190903/adafruit-circuitpython-bundle-3.x-mpy-20190903.zip).

# Switching Between CircuitPython and Arduino

Many of the CircuitPython boards also run Arduino. But how do you switch between the two? Switching between CircuitPython and Arduino is easy.

If you're currently running Arduino and would like to start using CircuitPython, follow the steps found in [Welcome to CircuitPython: Installing CircuitPython](https://learn.adafruit.com/welcome-to-circuitpython/installing-circuitpython).

If you're currently running CircuitPython and would like to start using Arduino, plug in your board, and then load your Arduino sketch. If there are any issues, you can double tap the reset button to get into the bootloader and then try loading your sketch. Always backup any files you're using with CircuitPython that you want to save as they could be deleted.

That's it! It's super simple to switch between the two.

# The Difference Between Express And Non-Express Boards

We often reference "Express" and "Non-Express" boards when discussing CircuitPython. What does this mean?

Express refers to the inclusion of an extra 2MB flash chip on the board that provides you with extra space for CircuitPython and your code. This means that we're able to include more functionality in CircuitPython and you're able to do more with your code on an Express board than you would on a non-Express board.

Express boards include Circuit Playground Express, ItsyBitsy M0 Express, Feather M0 Express, Metro M0 Express and Metro M4 Express.

Non-Express boards include Trinket M0, Gemma M0, QT Py, Feather M0 Basic, and other non-Express Feather M0 variants.

# Non-Express Boards: Gemma, Trinket, and QT Py

CircuitPython runs nicely on the Gemma M0, Trinket M0, or QT Py M0 but there are some constraints

### Small Disk Space

Since we use the internal flash for disk, and that's shared with runtime code, its limited! Only about 50KB of space.

### No Audio or NVM

Part of giving up that FLASH for disk means we couldn't fit everything in. There is, at this time, no support for hardware audio playpack or NVM 'eeprom'. Modules `audioio`&nbsp;and `bitbangio` are&nbsp;not included. For that support, check out the Circuit Playground Express or other Express boards.

However, I2C, UART, capacitive touch, NeoPixel, DotStar, PWM, analog in and out, digital IO, logging storage, and HID do work! Check the CircuitPython Essentials for examples of all of these.

# Differences Between CircuitPython and MicroPython

For the differences between CircuitPython and MicroPython, check out the [CircuitPython documentation](https://circuitpython.readthedocs.io/en/latest/README.html#differences-from-micropython).

# Differences Between CircuitPython and Python

Python (also known as CPython) is the language that MicroPython and CircuitPython are based on. There are many similarities, but there are also many differences. This is a list of a few of the differences.

### Python Libraries

Python is advertised as having "batteries included", meaning that many standard libraries are included. Unfortunately, for space reasons, many Python libraries are not available. So for instance while we wish you could `import numpy`, `numpy` isn't available (look for the `ulab` library for similar functions to `numpy` which works on many microcontroller boards). So you may have to port some code over yourself!

### Integers in CircuitPython

On the non-Express boards, integers can only be up to 31 bits long. Integers of unlimited size are not supported. The largest positive integer that can be represented is 2<sup>30</sup>-1,&nbsp;1073741823, and the most negative integer possible is -2<sup>30</sup>,&nbsp;-1073741824.

As of CircuitPython 3.0, Express boards have arbitrarily long integers as in Python.

### Floating Point Numbers and Digits of Precision for Floats in CircuitPython

Floating point numbers are single precision in CircuitPython (not double precision as in Python). The largest floating point magnitude that can be represented is about +/-3.4e38. The smallest magnitude that can be represented with full accuracy is about +/-1.7e-38, though numbers as small as +/-5.6e-45 can be represented with reduced accuracy.

CircuitPython's floats have 8 bits of exponent and 22 bits of mantissa (not 24 like regular single precision floating point), which is about five or six decimal digits of precision.

### Differences between MicroPython and Python

For a more detailed list of the differences between CircuitPython and Python, you can look at the MicroPython documentation. [We keep up with MicroPython stable releases, so check out the core 'differences' they document here.](http://docs.micropython.org/en/latest/pyboard/genrst/index.html)

# CircuitPython Essentials

## CircuitPython Resetting

Most CircuitPython boards have a physical reset button. Pressing that button will perform a hardware reset, similar to unplugging and plugging in the USB cable. There's no code involved. So the reset button should always work.

More detailed information about what happens when you press the reset button in various ways and what the status LED indicates, including a flowchart, is [here](https://learn.adafruit.com/welcome-to-circuitpython/troubleshooting#circuitpython-rgb-status-light-2978455).

But what if you want to reset from your program? Maybe you want to just kick the board to recover from some bad state. Or maybe you have some use case where you want to reset into bootloader mode. We cover these various options here.

## Soft Reset

To preform a soft reset, similar to hitting `<CTRL><D>` at the REPL prompt, use `supervisor.reload()`. First, you need to import the `supervisor` module:

```python
import supervisor
```

And then at the point in your code where you want to reset, call `reload()`:

```python
supervisor.reload()
```

## Hard Reset

To perform a hard reset, similar to hitting the RESET button, use [`microcontroller.reset()`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/microcontroller/index.html#microcontroller.reset).

Warning: 

First you need to import the `microcontroller` module:

```python
import microcontroller
```

And then at the point in your code where you want to reset, call `reset()`:

```python
microcontroller.reset()
```

## Reset Into Specific Mode

It is also possible to specify the mode to reset into. For example, you can reset into bootloader mode if you want. To do this, use `on_next_reset()` to specify the mode **before** calling `reset()`. The available options are defined in the `microcontroller.RunMode` class:

- `NORMAL`
- `SAFE_MODE`
- `UF2`
- `BOOTLOADER`

For example, to reset into BOOTLOADER mode:

```python
import microcontroller
microcontroller.on_next_reset(microcontroller.RunMode.UF2)
microcontroller.reset()
```

## More Info

- [`supervisor` module docs](https://circuitpython.readthedocs.io/en/latest/shared-bindings/supervisor/index.html)
- [`microcontroller` module docs](https://circuitpython.readthedocs.io/en/latest/shared-bindings/microcontroller/index.html#microcontroller.RunMode)

# CircuitPython Essentials

## CircuitPython Libraries

We have tons of CircuitPython libraries that can be used by microcontroller boards or single board computers such as Raspberry Pi. Here's a quick listing that is automatically generated

https://github.com/adafruit/Adafruit_CircuitPython_Bundle/blob/master/circuitpython_library_list.md

# CircuitPython Essentials

## CircuitPython Libraries and Drivers


## 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)
### Adafruit Metro M4 feat. Microchip ATSAMD51

[Adafruit Metro M4 feat. Microchip ATSAMD51](https://www.adafruit.com/product/3382)
Are you ready? Really ready? Cause here comes the fastest, most powerful Metro ever. The **Adafruit Metro M4** featuring the **Microchip ATSAMD51**. This Metro is like a bullet train, with its 120MHz Cortex M4 with floating point support. Your code will zig and zag...

In Stock
[Buy Now](https://www.adafruit.com/product/3382)
[Related Guides to the Product](https://learn.adafruit.com/products/3382/guides)
### Adafruit METRO M0 Express - designed for CircuitPython

[Adafruit METRO M0 Express - designed for CircuitPython](https://www.adafruit.com/product/3505)
Metro is our series of microcontroller boards for use with the Arduino IDE. This new **Metro M0 Express** board looks a whole lot like our&nbsp;[original Metro 328](https://www.adafruit.com/product/2488), but with a huge upgrade. Instead of the ATmega328, this Metro...

In Stock
[Buy Now](https://www.adafruit.com/product/3505)
[Related Guides to the Product](https://learn.adafruit.com/products/3505/guides)
### Adafruit ItsyBitsy M4 Express featuring ATSAMD51

[Adafruit ItsyBitsy M4 Express featuring ATSAMD51](https://www.adafruit.com/product/3800)
What's smaller than a Feather but larger than a Trinket? It's an **Adafruit ItsyBitsy M4 Express** featuring the **Microchip ATSAMD51**! Small, powerful, with a ultra fast ATSAMD51 Cortex M4 processor running at 120 MHz - this microcontroller board is perfect...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3800)
[Related Guides to the Product](https://learn.adafruit.com/products/3800/guides)
### Adafruit ItsyBitsy M0 Express - for CircuitPython & Arduino IDE

[Adafruit ItsyBitsy M0 Express - for CircuitPython & Arduino IDE](https://www.adafruit.com/product/3727)
What's smaller than a Feather but larger than a Trinket? It's an **Adafruit ItsyBitsy M0 Express**! Small, powerful, with a rockin' ATSAMD21 Cortex M0 processor running at 48 MHz - this microcontroller board is perfect when you want something very compact, but still...

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

Out of Stock
[Buy Now](https://www.adafruit.com/product/3500)
[Related Guides to the Product](https://learn.adafruit.com/products/3500/guides)
### 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 QT Py - SAMD21 Dev Board with STEMMA QT

[Adafruit QT Py - SAMD21 Dev Board with STEMMA QT](https://www.adafruit.com/product/4600)
What a cutie pie! Or is it... a QT Py? This diminutive dev board comes with our favorite lil chip, the SAMD21 (as made famous in our GEMMA M0 and Trinket M0 boards).

This time it comes with [our favorite connector - the STEMMA QT](http://adafruit.com/stemma), a chainable I2C...

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

## Related Guides

- [Adafruit TSL2591 High Dynamic Range Digital Light Sensor](https://learn.adafruit.com/adafruit-tsl2591.md)
- [Adafruit Feather M0 Express](https://learn.adafruit.com/adafruit-feather-m0-express-designed-for-circuit-python-circuitpython.md)
- [Adafruit Metro M0 Express](https://learn.adafruit.com/adafruit-metro-m0-express.md)
- [Adafruit Trinket M0](https://learn.adafruit.com/adafruit-trinket-m0-circuitpython-arduino.md)
- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Adafruit Metro M4 Express featuring ATSAMD51](https://learn.adafruit.com/adafruit-metro-m4-express-featuring-atsamd51.md)
- [Introducing ItsyBitsy M0 Express](https://learn.adafruit.com/introducing-itsy-bitsy-m0.md)
- [Adafruit QT Py SAMD21](https://learn.adafruit.com/adafruit-qt-py.md)
- [Circuit Playground Gizmo Ornaments](https://learn.adafruit.com/display-ornaments-with-circuit-playground.md)
- [Make It a Keyboard](https://learn.adafruit.com/make-it-a-keyboard.md)
- [Circuit Playground or Hallowing Jack-o'-Lantern](https://learn.adafruit.com/circuit-playground-jack-o-lantern.md)
- [CircuitPython 101: Basic Builtin Data Structures](https://learn.adafruit.com/basic-datastructures-in-circuitpython.md)
- [Slider Crank Mechanism -- from Cardboard and Craft Sticks](https://learn.adafruit.com/cardboard-slider-crank.md)
- [Chinese Dragon Puppet with Motion-Reactive Flame Effect](https://learn.adafruit.com/chinese-dragon-puppet-with-motion-reactive-flame-effect.md)
- [Stand-alone programming AVRs using CircuitPython](https://learn.adafruit.com/stand-alone-programming-avrs-using-circuitpython.md)
- [The Foul Fowl -- Keystroke Injection Attack Tool with Gemma M0](https://learn.adafruit.com/the-foul-fowl-keyboard-injection-payload-gemma-m0.md)
- [Illuminated City Skyline with MakeCode](https://learn.adafruit.com/city-skyline-with-makecode-for-cpx.md)
- [CPX Mystery Dreidel](https://learn.adafruit.com/cpx-mystery-dreidel.md)
- [Circuit Playground Express USB MIDI Controller and Synthesizer](https://learn.adafruit.com/cpx-midi-controller.md)
- [RGB LED Matrix Basics](https://learn.adafruit.com/32x16-32x32-rgb-led-matrix.md)
- [Circuit Playground Express Serial Communications](https://learn.adafruit.com/circuit-playground-express-serial-communications.md)
- [Stumble-Bot](https://learn.adafruit.com/stumble-bot-with-circuit-playground-and-crickit.md)
- [Makecode para la Circuit Playground Express](https://learn.adafruit.com/makecode-es.md)
