Adafruit Metro M4 Express AirLift (WiFi) Created by Brent Rubell Last updated on 2019-08-20 12:31:14 AM UTC
Overview Give your next project a lift with AirLift - our witty name for the ESP32 co-processor that graces this Metro M4. You already know about the Adafruit Metro M4 featuring the Microchip ATSAMD51, with it's 120MHz Cortex M4 with floating point support. With a train-load of FLASH and RAM, your code will be fast and roomy.
and SPI/UART/I2C hardware support in the same spot as the Metro 328 and M0. But! It's powered with an ATSAMD51J19: Cortex M4 core running at 120 MHz Floating point support with Cortex M4 DSP instructions (https://adafru.it/ENz) 512 KB flash, 192 KB RAM 32-bit, 3.
two RX/TX LEDs for data being sent over USB, and a red LED connected. Next to the reset button there is an RGB NeoPixel that can be used for any purpose. 2 MB QSPI Flash storage chip is included on board. You can use the SPI Flash storage like a very tiny hard drive. When used in Circuit Python, the 2 MB flash acts as storage for all your scripts, libraries and files.
Pinouts The Metro M4 Express AirLift is chock-full of microcontroller goodness. There's also a lot of pins and ports. We'll take you a tour of them now! Power Connections There's a lot of ways to power the Metro M4 Express AirLift, and a lot of ways toget power out as well. © Adafruit Industries https://learn.adafruit.
There are two primary ways to power the Metro: Through the Micro USB port up at the top left Through the DC jack at the bottom left The MicroUSB jack provides 5V at 500mA or so, there is a fuse that will shut off temporarily when more than 1000mA is drawn, this is to protect a computer USB port. You can plug this into any computer or USB charger with a USB cable. You can draw up to 500mA between the Vin, 5V and 3.3V supplies (combined). The DC Jack is a 5.5mm/2.
Arduino SPIWIFI_RESET or 38 ) For advanced use or reprogramming, we also connect the main RX/TX UART to the ESP32 - on Arduino that's Serial2 and in CircuitPython use board.ESP_RX and board.ESP_TX . This is a unique/unshared Serial port that is not the same as the RX/TX pins on the Metro breakout pins. You can also connect to the ESP32 RTS pin (used in some serial contexts) on board.ESP_RTS or Arduino 34 . The ESP32 GPIO0 pin for bootloader enable is connected to board.
A2 thru A5 - These are each analog input as well as digital I/O pins. Please note, A3 functions as expected but happens to be a different microcontroller pin than the one on the Metro M4 non-WiFi version. This shouldn't affect you but if you're doing something special with that pin, like a timer or SERCOM config, check the schematic for the underlying hardware pin name Right side SCK/MOSI/MISO - These are the hardware SPI pins, are are connected to the 2x3 header on the right hand side.
The NeoPixel is connected to pin #40 in Arduino, so just use our NeoPixel library (https://adafru.it/dhw) and set it up as a single-LED strand on pin 40. In CircuitPython, the NeoPixel is board.NEOPIXEL and the library for it is here (https://adafru.it/wby) and in the bundle (https://adafru.it/uap). The NeoPixel is powered by the 3.3V power supply but that hasn't shown to make a big difference in brightness or color.
RST - this is the Reset pin, tie to ground to manually reset the ATSAMD51, as well as launch the bootloader manually ARef - the analog reference pin. Normally the reference voltage is the same as the chip logic voltage (3.3V) but if you need an alternative analog reference, connect it to this pin and select the external AREF in your firmware. Can't go higher than 3.3V! On the Metro M4 Express AirLift, at least for now, AREF is tied to 3.
SEGGER J-Link EDU - JTAG/SWD Debugger $69.95 IN STOCK ADD TO CART SEGGER J-Link BASE - JTAG/SWD Debugger $399.95 IN STOCK ADD TO CART SEGGER J-Link EDU Mini - JTAG/SWD Debugger $19.95 IN STOCK ADD TO CART You'll need an adapter and cable to convert the 2x10 JTAG cable to SWD © Adafruit Industries https://learn.adafruit.
10-pin 2x5 Socket-Socket 1.27mm IDC (SWD) Cable - 150mm long $2.95 IN STOCK ADD TO CART JTAG (2x10 2.54mm) to SWD (2x5 1.27mm) Cable Adapter Board $4.95 IN STOCK ADD TO CART If you want to use the J-Link with Segger Ozone, check out this handy informative post on the forums (https://adafru.it/Cuk) © Adafruit Industries https://learn.adafruit.
UF2 Bootloader Details This is an information page for advanced users who are curious how we get code from your computer into your Express board! Adafruit SAMD21 (M0) and SAMD51 (M4) boards feature an improved bootloader that makes it easier than ever to flash different code onto the microcontroller. This bootloader makes it easy to switch between Microsoft MakeCode, CircuitPython and Arduino.
Entering Bootloader Mode The first step to loading new code onto your board is triggering the bootloader. It is easily done by double tapping the reset button. Once the bootloader is active you will see the small red LED fade in and out and a new drive will appear on your computer with a name ending in BOOT.
If the bootloader couldn't start, you will get a red NeoPixel LED. That could mean that your USB cable is no good, it isn't connected to a computer, or maybe the drivers could not enumerate. Try a new USB cable first. Then try another port on your computer! Once the bootloader is running, check your computer. You should see a USB Disk drive... © Adafruit Industries https://learn.adafruit.
Once the bootloader is successfully connected you can open the drive and browse the virtual filesystem. This isn't the same filesystem as you use with CircuitPython or Arduino. It should have three files: CURRENT.UF2 - The current contents of the microcontroller flash. INDEX.HTM - Links to Microsoft MakeCode. INFO_UF2.TXT - Includes bootloader version info. Please include it on bug reports. Using the Mass Storage Bootloader To flash something new, simply drag any UF2 onto the drive.
You may get an alert from the OS that the file is being copied without it's properties. You can just click Yes You may also get get a complaint that the drive was ejected without warning. Don't worry about this. The drive only ejects once the bootloader has verified and completed the process of writing the new code Using the BOSSA Bootloader As mentioned before, the bootloader is also compatible with BOSSA, which is the standard method of updating boards when in the Arduino IDE.
Download and run the installer. We recommend just selecting all the serial port drivers available (no harm to do so) and installing them. Verifying Serial Port in Device Manager If you're running Windows, its a good idea to verify the device showed up. Open your Device Manager from the control panel and look under Ports (COM & LPT) for a device called Feather M0 or Circuit Playground or whatever! If you see something like this, it means you did not install the drivers.
Running bossac on the command line If you are using the Arduino IDE, this step is not required. But sometimes you want to read/write custom binary files, say for loading CircuitPython or your own code. We recommend using bossac v 1.7.0 (or greater), which has been tested. The Arduino branch is most recommended (https://adafru.it/vQb). You can download the latest builds here. (https://adafru.it/s1B) The mingw32 version is for Windows, apple-darwin for Mac OSX and various linux options for Linux.
For example, bossac -p=/dev/cu.usbmodem14301 -e -w -v -R adafruit-circuitpython-feather_m0_express-3.0.0.bin Using bossac Versions 1.9 or Later For M0 boards, which have an 8kB bootloader, you must specify -offset=0x2000 , for example: bossac -p=/dev/cu.usbmodem14301 -e -w -v -R --offset=0x2000 adafruit-circuitpython-feather_m0_express-3.0.0.bin For M4 boards, which have a 16kB bootloader, you must specify -offset=0x4000 , for example: bossac -p=/dev/cu.
UF2 Bootloader v2.0.0-adafruit.5 SFHWRO Model: Metro M0 Board-ID: SAMD21G18A-Metro-v0 Lastly, reload your code from Arduino or MakeCode or flash the latest CircuitPython core (https://adafru.it/Em8). Below are the latest updaters for various boards. The latest versions can always be found here (https://adafru.it/Bmg). Look for the update-bootloader... files, not the bootloader... files. https://adafru.it/ECV https://adafru.it/ECV https://adafru.it/ECW https://adafru.it/ECW https://adafru.
Go to the Control Panel. Click on the Hardware and Sound header Click on the Autoplay header Uncheck the box at the top, labeled Use Autoplay for all devices © Adafruit Industries https://learn.adafruit.
Making your own UF2 Making your own UF2 is easy! All you need is a .bin file of a program you wish to flash and the Python conversion script (https://adafru.it/vZb). Make sure that your program was compiled to start at 0x2000 (8k) for M0 boards or 0x4000 (16kB) for M4 boards. The bootloader takes up the first 8kB (M0) or 16kB (M4). CircuitPython's linker script (https://adafru.it/CXh) is an example on how to do that. Once you have a .bin file, you simply need to run the Python conversion script over it.
What is CircuitPython? CircuitPython is a programming language designed to simplify experimenting and learning to program on low-cost microcontroller boards. It makes getting started easier than ever with no upfront desktop downloads needed. Once you get your board set up, open any text editor, and get started editing code. It's that simple. CircuitPython is based on Python Python is the fastest growing programming language. It's taught in schools and universities.
You're new to programming. CircuitPython is designed with education in mind. It's easy to start learning how to program and you get immediate feedback from the board. Easily update your code. Since your code lives on the disk drive, you can edit it whenever you like, you can also keep multiple files around for easy experimentation. The serial console and REPL. These allow for live feedback from your code and interactive programming. File storage.
Install CircuitPython CircuitPython (https://adafru.it/tB7) is a derivative of MicroPython (https://adafru.it/BeZ) designed to simplify experimentation and education on low-cost microcontrollers. It makes it easier than ever to get prototyping by requiring no upfront desktop software downloads. Simply copy and edit files on the CIRCUITPY drive to iterate. Set up CircuitPython Quick Start! Follow this quick step-by-step for super-fast Python power :) https://adafru.it/EDA https://adafru.
You will see a new disk drive appear called METROM4BOOT. Drag the adafruit_circuitpython_etc.uf2 file to METROM4BOOT. The LED will flash. Then, the METROM4BOOT drive will disappear and a new disk drive called CIRCUITPY will appear. That's it, you're done! :) © Adafruit Industries https://learn.adafruit.
Installing Mu Editor Mu is a simple code editor that works with the Adafruit CircuitPython boards. It's written in Python and works on Windows, MacOS, Linux and Raspberry Pi. The serial console is built right in so you get immediate feedback from your board's serial output! Mu is our recommended editor - please use it (unless you are an experienced coder with a favorite editor already!) Download and Install Mu Download Mu from https://codewith.mu (https://adafru.it/Be6).
Mu attempts to auto-detect your board, so please plug in your CircuitPython device and make sure it shows up as a CIRCUITPY drive before starting Mu Now you're ready to code! Lets keep going.... © Adafruit Industries https://learn.adafruit.
Creating and Editing Code One of the best things about CircuitPython is how simple it is to get code up and running. In this section, we're going to cover how to create and edit your first CircuitPython program. To create and edit code, all you'll need is an editor. There are many options.
It will look like this - note that under the while True: line, the next four lines have spaces to indent them, but they're indented exactly the same amount. All other lines have no spaces before the text. Save this file as code.py on your CIRCUITPY drive. On each board you'll find a tiny red LED. It should now be blinking. Once per second © Adafruit Industries https://learn.adafruit.
Congratulations, you've just run your first CircuitPython program! Editing Code To edit code, open the code.py file on your CIRCUITPY drive into your editor. Make the desired changes to your code. Save the file. That's it! Your code changes are run as soon as the file is done saving. There's just one warning we have to give you before we continue... Don't Click Reset or Unplug! The CircuitPython code on your board detects when the files are changed or written and will automatically re-start your code.
vim (https://adafru.it/ek9) / vi safely writes all changes. But set up vim to not write swapfiles (https://adafru.it/ELO) (.swp files: temporary records of your edits) to CIRCUITPY. Run vim with vim -n , set the no swapfile option, or set the directory option to write swapfiles elsewhere. Otherwise the swapfile writes trigger restarts of your program. The PyCharm IDE (https://adafru.it/xNC) is safe if "Safe Write" is turned on in Settings->System Settings>Synchronization (true by default).
Back to Editing Code... Now! Let's try editing the program you added to your board. Open your code.py file into your editor. We'll make a simple change. Change the first 0.5 to 0.1 . The code should look like this: import board import digitalio import time led = digitalio.DigitalInOut(board.D13) led.direction = digitalio.Direction.OUTPUT while True: led.value = True time.sleep(0.1) led.value = False time.sleep(0.5) Leave the rest of the code as-is. Save your file.
Here is the original code again: import board import digitalio import time led = digitalio.DigitalInOut(board.D13) led.direction = digitalio.Direction.OUTPUT while True: led.value = True time.sleep(0.5) led.value = False time.sleep(0.5) Imports & Libraries Each CircuitPython program you run needs to have a lot of information to work. The reason CircuitPython is so simple to use is that most of that information is stored in other files and works in the background. These files are called libraries.
while True: led.value = True time.sleep(0.5) led.value = False time.sleep(0.5) First, we have led.value = True . This line tells the LED to turn on. On the next line, we have time.sleep(0.5) . This line is telling CircuitPython to pause running code for 0.5 seconds. Since this is between turning the led on and off, the led will be on for 0.5 seconds. The next two lines are similar. led.value = False tells the LED to turn off, and time.sleep(0.5) tells CircuitPython to pause for another 0.5 seconds.
More Changes We don't have to stop there! Let's keep going. Change the second 0.5 to 0.1 so it looks like this: while True: led.value = True time.sleep(0.1) led.value = False time.sleep(0.1) Now it blinks really fast! You decreased the both time that the code leaves the LED on and off! Now try increasing both of the 0.1 to 1 . Your LED will blink much more slowly because you've increased the amount of time that the LED is turned on and off.
Connecting to the Serial Console One of the staples of CircuitPython (and programming in general!) is something called a "print statement". This is a line you include in your code that causes your code to output text. A print statement in CircuitPython looks like this: print("Hello, world!") This line would result in: Hello, world! However, these print statements need somewhere to display.
Once in Mu, look for the Serial button in the menu and click it. Setting Permissions on Linux On Linux, if you see an error box something like the one below when you press the Serial button, you need to add yourself to a user group to have permission to connect to the serial console. On Ubuntu and Debian, add yourself to the dialout group by doing: sudo adduser $USER dialout After running the command above, reboot your machine to gain access to the group.
Interacting with the Serial Console Once you've successfully connected to the serial console, it's time to start using it. The code you wrote earlier has no output to the serial console. So, we're going to edit it to create some output. Open your code.py file into your editor, and include a print statement. You can print anything you like! Just include your phrase between the quotation marks inside the parentheses. For example: import board import digitalio import time led = digitalio.DigitalInOut(board.
the board reboots. Then you'll see your new change! The Traceback (most recent call last): is telling you the last thing your board was doing before you saved your file. This is normal behavior and will happen every time the board resets. This is really handy for troubleshooting. Let's introduce an error so we can see how it is used. Delete the e at the end of True from the line led.value = True so that it says led.value = Tru Save your file.
The Traceback (most recent call last): is telling you that the last thing it was able to run was line 10 in your code. The next line is your error: NameError: name 'Tru' is not defined . This error might not mean a lot to you, but combined with knowing the issue is on line 10, it gives you a great place to start! Go back to your code, and take a look at line 10. Obviously, you know what the problem is already. But if you didn't, you'd want to look at line 10 and see if you could figure it out.
The REPL The other feature of the serial connection is the Read-Evaluate-Print-Loop, or REPL. The REPL allows you to enter individual lines of code and have them run immediately. It's really handy if you're running into trouble with a particular program and can't figure out why. It's interactive so it's great for testing new ideas. To use the REPL, you first need to be connected to the serial console. Once that connection has been established, you'll want to press Ctrl + C.
If you have trouble getting to the >>> prompt, try pressing Ctrl + C a few more times. The first thing you get from the REPL is information about your board. This line tells you the version of CircuitPython you're using and when it was released. Next, it gives you the type of board you're using and the type of microcontroller the board uses. Each part of this may be different for your board depending on the versions you're working with. This is followed by the CircuitPython prompt.
Type help("modules") into the REPL next to the prompt, and press enter. This is a list of all the core libraries built into CircuitPython. We discussed how board contains all of the pins on the board that you can use in your code. From the REPL, you are able to see that list! Type import board into the REPL and press enter. It'll go to a new prompt.
This is a list of all of the pins on your board that are available for you to use in your code. Each board's list will differ slightly depending on the number of pins available. Do you see D13 ? That's the pin you used to blink the red LED! The REPL can also be used to run code. Be aware that any code you enter into the REPL isn't saved anywhere.
© Adafruit Industries https://learn.adafruit.
CircuitPython Libraries As we continue to develop CircuitPython and create new releases, we will stop supporting older releases. Visit https://circuitpython.org/downloads to download the latest version of CircuitPython for your board. You must download the CircuitPython Library Bundle that matches your version of CircuitPython. Please update CircuitPython and then visit https://circuitpython.org/libraries to download the latest Library Bundle.
We're constantly updating and improving our libraries, so we don't (at this time) ship our CircuitPython boards with the full library bundle. Instead, you can find example code in the guides for your board that depends on external libraries. Some of these libraries may be available from us at Adafruit, some may be written by community members! Either way, as you start to explore CircuitPython, you'll want to know how to get libraries on board.
Now open the lib folder. When you open the folder, you'll see a large number of mpy files and folders Example Files All example files from each library are now included in the bundles, as well as an examples-only bundle. These are included for two main reasons: Allow for quick testing of devices. Provide an example base of code, that is easily built upon for individualized purposes. Copying Libraries to Your Board First you'll want to create a lib folder on your CIRCUITPY drive.
This demonstration will only return an error if you do not have the required library loaded into the lib folder on your CIRCUITPY drive. Let's use a modified version of the blinky example. import board import time import simpleio led = simpleio.DigitalOut(board.D13) while True: led.value = True time.sleep(0.5) led.value = False time.sleep(0.5) Save this file. Nothing happens to your board. Let's check the serial console to see what's going on. We have an ImportError .
Library Install on Non-Express Boards If you have a Trinket M0 or Gemma M0, you'll want to follow the same steps in the example above to install libraries as you need them. You don't always need to wait for an ImportError as you probably know what library you added to your code. Simply open the lib folder you downloaded, find the library you need, and drag it to the lib folder on your CIRCUITPY drive.
Frequently Asked Questions These are some of the common questions regarding CircuitPython and CircuitPython microcontrollers. Is ESP8266 or ESP32 supported in CircuitPython? Why not? We are dropping ESP8266 support as of 4.x - For more information please read about it here! https://learn.adafruit.com/welcome-to-circuitpython/circuitpython-for-esp8266 © Adafruit Industries https://learn.adafruit.
How do I connect to the Internet with CircuitPython? If you'd like to add WiFi support, check out our guide on ESP32/ESP8266 as a co-processor. © Adafruit Industries https://learn.adafruit.
Is there asyncio support in CircuitPython We do not have asyncio support in CircuitPython at this time © Adafruit Industries https://learn.adafruit.
My RGB NeoPixel/DotStar LED is blinking funny colors - what does it mean? The status LED can tell you what's going on with your CircuitPython board. Read more here for what the colors mean! © Adafruit Industries https://learn.adafruit.
What is a MemoryError ? Memory allocation errors happen when you're trying to store too much on the board. The CircuitPython microcontroller boards have a limited amount of memory available. You can have about 250 lines of code on the M0 Express boards. If you try to import too many libraries, a combination of large libraries, or run a program with too many lines of code, your code will fail to run and you will receive a MemoryError in the serial console (REPL).
How do I check how much memory I have free? import gc gc.mem_free() Will give you the number of bytes available for use. Does CircuitPython support interrupts? No. CircuitPython does not currently support interrupts. We do not have an estimated time for when they will be included. Does Feather M0 support WINC1500? No, WINC1500 will not fit into the M0 flash space. Can AVRs such as ATmega328 or ATmega2560 run CircuitPython? No. Commonly Used Acronyms CP or CPy = CircuitPython (https://adafru.
Welcome to the Community! CircuitPython is a programming language that's super simple to get started with and great for learning. It runs on microcontrollers and works out of the box. You can plug it in and get started with any text editor. The best part? CircuitPython comes with an amazing, supportive community. Everyone is welcome! CircuitPython is Open Source. This means it's available for anyone to use, edit, copy and improve upon.
There are many different channels so you can choose the one best suited to your needs. Each channel is shown on Discord as "#channelname". There's the #projecthelp channel for assistance with your current project or help coming up with ideas for your next one. There's the #showandtell channel for showing off your newest creation. Don't be afraid to ask a question in any channel! If you're unsure, #general is a great place to start.
Be sure to include the steps you took to get to where you are. If it involves wiring, post a picture! If your code is giving you trouble, include your code in your post! These are great ways to make sure that there's enough information to help you with your issue. You might think you're just getting started, but you definitely know something that someone else doesn't.
When working with CircuitPython, you may find problems. If you find a bug, that's great! We love bugs! Posting a detailed issue to GitHub is an invaluable way to contribute to improving CircuitPython. Be sure to include the steps to replicate the issue as well as any other information you think is relevant. The more detail, the better! Testing new software is easy and incredibly helpful. Simply load the newest version of CircuitPython or a library onto your CircuitPython hardware, and use it.
Advanced Serial Console on Windows Windows 7 Driver If you're using Windows 7, use the link below to download the driver package. You will not need to install drivers on Mac, Linux or Windows 10. https://adafru.it/AB0 https://adafru.it/AB0 What's the COM? First, you'll want to find out which serial port your board is using. When you plug your board in to USB on your computer, it connects to a serial port. The port is like a door through which your board can communicate with your computer using USB.
Sometimes the item will refer to the name of the board. Other times it may be called something like USB Serial Device, as seen in the image above. Either way, there is a new (COM#) following the name. This is the port your board is using. Install Putty If you're using Windows, you'll need to download a terminal program. We're going to use PuTTY. The first thing to do is download the latest version of PuTTY (https://adafru.it/Bf1). You'll want to download the Windows installer file.
Once your settings are entered, you're ready to connect to the serial console. Click "Open" at the bottom of the window. A new window will open. If no code is running, the window will either be blank or will look like the window above. Now you're ready to see the results of your code. Great job! You've connected to the serial console! © Adafruit Industries https://learn.adafruit.
Advanced Serial Console on Mac and Linux Connecting to the serial console on Mac and Linux uses essentially the same process. Neither operating system needs drivers installed. On MacOSX, Terminal comes installed. On Linux, there are a variety such as gnome-terminal (called Terminal) or Konsole on KDE. What's the Port? First you'll want to find out which serial port your board is using. When you plug your board in to USB on your computer, it connects to a serial port.
Now, plug your board. Using Mac, type: ls /dev/tty.* This will show you the current serial connections, which will now include your board. Using Mac, a new listing has appeared called /dev/tty.usbmodem141441 . The tty.usbmodem141441 part of this listing is the name the example board is using. Yours will be called something similar. Using Linux, type: ls /dev/ttyACM* This will show you the current serial connections, which will now include your board. © Adafruit Industries https://learn.adafruit.
Using Linux, a new listing has appeared called /dev/ttyACM0 . The ttyACM0 part of this listing is the name the example board is using. Yours will be called something similar. Connect with screen Now that you know the name your board is using, you're ready connect to the serial console. We're going to use a command called screen . The screen command is included with MacOS. Linux users may need to install it using their package manager. To connect to the serial console, use Terminal.
Press enter to run the command. It will open in the same window. If no code is running, the window will be blank. Otherwise, you'll see the output of your code. Great job! You've connected to the serial console! Permissions on Linux If you try to run screen and it doesn't work, then you may be running into an issue with permissions. Linux keeps track of users and groups and what they are allowed to do and not do, like access the hardware associated with the serial connection for running screen .
The second way is to add yourself to the group associated with the hardware. To figure out what that group is, use the command ls -l as shown below. The group name is circled in red. Then use the command adduser to add yourself to that group. You need elevated privileges to do this, so you'll need to use sudo . In the example below, the group is adm and the user is ackbar. After you add yourself to the group, you'll need to logout and log back in, or in some cases, reboot your machine.
The examples above use screen , but you can also use other programs, such as putty or picocom , if you prefer. © Adafruit Industries https://learn.adafruit.
Uninstalling CircuitPython A lot of our boards can be used with multiple programming languages. For example, the Circuit Playground Express can be used with MakeCode, Code.org CS Discoveries, CircuitPython and Arduino.
Your MakeCode is now running and CircuitPython has been removed. Going forward you only have to single click the reset button Moving to Arduino If you want to change your firmware to Arduino, it's also pretty easy. Start by plugging in your board, and double-clicking reset until you get the green onboard LED(s) - just like with MakeCode Within Arduino IDE, select the matching board, say Circuit Playground Express Select the correct matching Port: © Adafruit Industries https://learn.adafruit.
Create a new simple Blink sketch example: // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 13 as an output.
Troubleshooting From time to time, you will run into issues when working with CircuitPython. Here are a few things you may encounter and how to resolve them. As we continue to develop CircuitPython and create new releases, we will stop supporting older releases. If you are running CircuitPython 2.x, you need to update to 3.x. You must download the CircuitPython Library Bundle that matches your version of CircuitPython. Please update to CircuitPython 3.x and then download the 3.x bundle.
Now install the new 2.3.0.0 (or higher) Adafruit Windows Drivers Package: https://adafru.it/AB0 https://adafru.it/AB0 When running the installer, you'll be shown a list of drivers to choose from. You can check and uncheck the boxes to choose which drivers to install. You should now be done! Test by unplugging and replugging the board.
Please let us know in the forums if you test thi.s Hard Disk Sentinel Kaspersky anti-virus: To fix, you may need to disable Kaspersky completely. Disabling some aspects of Kaspersky does not always solve the problem. This problem has been reported to Kaspersky. CIRCUITPY Drive Does Not Appear Kaspersky anti-virus can block the appearance of the CIRCUITPY drive. We haven't yet figured out a settings change that prevents this. Complete uninstallation of Kaspersky fixes the problem.
Express, Gemma M0, and Trinket M0 all have a single NeoPixel or DotStar RGB LED on the board that indicates the status of CircuitPython. Circuit Playground Express does NOT have a status LED. The LEDs will pulse green when in the bootloader. They do NOT indicate any status while running CircuitPython. Here's what the colors and blinking mean: steady GREEN: code.py (or code.txt , main.py , or main.txt ) is running pulsing GREEN: code.py (etc.) has finished or does not exist steady YELLOW at start up: (4.0.
If still broken - When the CIRCUITPY disk is not safely ejected before being reset by the button or being disconnected from USB, it may corrupt the flash drive. It can happen on Windows, Mac or Linux. In this situation, the board must be completely erased and CircuitPython must be reloaded onto the board. You WILL lose everything on the board when you complete the following steps. If possible, make a copy of your code before continuing. Easiest Way: Use storage.erase_filesystem() Starting with version 2.3.
https://adafru.it/DjD https://adafru.it/DBA https://adafru.it/DBA https://adafru.it/Eca https://adafru.it/Eca 2. Double-click the reset button on the board to bring up the boardnameBOOT drive. 3. Drag the erase .uf2 file to the boardnameBOOT drive. 4. The onboard NeoPixel will turn yellow or blue, indicating the erase has started. 5. After approximately 15 seconds, the mainboard NeoPixel will light up green. On the NeoTrellis M4 this is the first NeoPixel on the grid 6.
Just follow these directions to reload CircuitPython using bossac (https://adafru.it/Bed), which will erase and re-create CIRCUITPY . Running Out of File Space on Non-Express Boards The file system on the board is very tiny. (Smaller than an ancient floppy disk.) So, its likely you'll run out of space but don't panic! There are a couple ways to free up space. The board ships with the Windows 7 serial driver too! Feel free to delete that if you don't need it or have already installed it. Its ~12KiB or so.
mdutil -i off /Volumes/CIRCUITPY cd /Volumes/CIRCUITPY rm -rf .{,_.}{fseventsd,Spotlight-V*,Trashes} mkdir .fseventsd touch .fseventsd/no_log .metadata_never_index .Trashes cd - Replace /Volumes/CIRCUITPY in the commands above with the full path to your board's volume if it's different. At this point all the hidden files should be cleared from the board and some hidden files will be prevented from being created. However there are still some cases where hidden files will be created by Mac OSX.
Lets remove the ._ files first. Whoa! We have 13Ki more than before! This space can now be used for libraries and code! © Adafruit Industries https://learn.adafruit.
CircuitPython Setup To use all the amazing features of your Metro AirLift with CircuitPython, you must first install a number of libraries. This page covers that process. Adafruit CircuitPython Bundle Download the Adafruit CircuitPython Library Bundle. You can find the latest release here: https://adafru.it/ENC https://adafru.it/ENC Download the adafruit-circuitpython-bundle-version-mpy-*.zip bundle zip file, and unzip a folder of the same name. Inside you'll find a lib folder.
Internet Connect! Once you have CircuitPython setup and libraries installed we can get your board connected to the Internet. Note that access to enterprise level secured WiFi networks is not currently supported, only WiFi networks that require SSID and password. To get connected, you will need to start by creating a secrets file . What's a secrets file? We expect people to share tons of projects as they build CircuitPython WiFi widgets.
Into your lib folder. Once that's done, load up the following example using Mu or your favorite editor: import board import busio from digitalio import DigitalInOut import adafruit_esp32spi.adafruit_esp32spi_socket as socket from adafruit_esp32spi import adafruit_esp32spi import adafruit_requests as requests print("ESP32 SPI webclient test") TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.
print() print("Fetching json from", JSON_URL) r = requests.get(JSON_URL) print('-'*40) print(r.json()) print('-'*40) r.close() print("Done!") And save it to your board, with the name code.py . This first connection example doesn't use a secrets file - you'll hand-enter your SSID/password to verify connectivity first! Then go down to this line esp.
Initializes the ESP32 over SPI using the SPI port and 3 control pins: esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) esp32_reset = DigitalInOut(board.ESP_RESET) spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) Tells our requests library the type of socket we're using (socket type varies by connectivity type - we'll be using the adafruit_esp32spi_socket for this example).
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" print("Fetching text from", TEXT_URL) r = requests.get(TEXT_URL) print('-'*40) print(r.text) print('-'*40) r.close() Or, if the data is in structured JSON, you can get the json pre-parsed into a Python dictionary that can be easily queried or traversed. (Again, only for nRF52840, M4 and other high-RAM boards) JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json" print("Fetching json from", JSON_URL) r = requests.
# Initialize a requests object with a socket and esp32spi interface requests.set_socket(socket, esp) TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" JSON_GET_URL = "http://httpbin.org/get" JSON_POST_URL = "http://httpbin.org/post" print("Fetching text from %s"%TEXT_URL) response = requests.get(TEXT_URL) print('-'*40) print("Text Response: ", response.text) print('-'*40) response.close() print("Fetching JSON data from %s"%JSON_GET_URL) response = requests.
import board import busio from digitalio import DigitalInOut import adafruit_esp32spi.adafruit_esp32spi_socket as socket from adafruit_esp32spi import adafruit_esp32spi import adafruit_requests as requests # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) esp32_reset = DigitalInOut(board.ESP_RESET) # # # # If you have an externally connected ESP32: esp32_cs = DigitalInOut(board.D9) esp32_ready = DigitalInOut(board.
While some servers respond with text, some respond with json-formatted data consisting of attribute–value pairs. CircuitPython_Requests can convert a JSON-formatted response from a server into a CPython dict. object. We can also fetch and parse json data. We'll send a HTTP get to a url we know returns a json-formatted response (instead of text data). Then, the code calls response.json() to convert the response to a CPython dict . print("Fetching JSON data from %s"%JSON_GET_URL) response = requests.
import board import busio from digitalio import DigitalInOut import adafruit_esp32spi.adafruit_esp32spi_socket as socket from adafruit_esp32spi import adafruit_esp32spi import adafruit_requests as requests # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) esp32_reset = DigitalInOut(board.ESP_RESET) # # # # If you have an externally connected ESP32: esp32_cs = DigitalInOut(board.D9) esp32_ready = DigitalInOut(board.
That simpletest example works but its a little finicky - you need to constantly check WiFi status and have many loops to manage connections and disconnections. For more advanced uses, we recommend using the WiFiManager object. It will wrap the connection/status/requests loop for you - reconnecting if WiFi drops, resetting the ESP32 if it gets into a bad state, etc.
response = wifi.post( "https://io.adafruit.com/api/v2/"+secrets['aio_username']+"/feeds/"+feed+"/data", json=payload, headers={"X-AIO-KEY":secrets['aio_key']}) print(response.json()) response.close() counter = counter + 1 print("OK") except (ValueError, RuntimeError) as e: print("Failed to get data, retrying\n", e) wifi.reset() continue response = None time.sleep(15) You'll note here we use a secrets.py file to manage our SSID info.
© Adafruit Industries https://learn.adafruit.
CircuitPython Essentials You've gone through the Welcome to CircuitPython guide (https://adafru.it/cpy-welcome). 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.
CircuitPython BuiltIns 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! This is not an exhaustive list! It's simply some of the many features you can use. Thing That Are Built In and Work Flow Control All the usual if , elif , else , for , while work just as expected.
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. Copy and paste the code into code.py using your favorite editor, and save the file to run the demo.
DigitalInOut(board.D7) . 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.
Gemma M0 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. D13 is located next to the "GND" label on the board, above the "On/Off" switch. Use alligator clips to connect your switch to your Gemma M0! Feather M0 Express and Feather M4 Express D5 is labeled "5" and connected to the blue wire on the board. D13 is labeled "#13" and is located next to the USB micro port.
Metro M0 Express and Metro M4 Express D2 is located near the top left corner, and is connected to the blue wire. D13 is labeled "L" and is located next to the USB micro port. Read the Docs For a more in-depth look at what digitalio can do, check out the DigitalInOut page in Read the Docs (https://adafru.it/C4c). © Adafruit Industries https://learn.adafruit.
CircuitPython Analog In This example shows you how you can read the analog voltage on the A1 pin on your board. Copy and paste the code into code.py using your favorite editor, and save the file to run the demo. # CircuitPython AnalogIn Demo import time import board from analogio import AnalogIn analog_in = AnalogIn(board.A1) def get_voltage(pin): return (pin.value * 3.3) / 65536 while True: print((get_voltage(analog_in),)) time.sleep(0.1) Make sure you're running CircuitPython 3.
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.
Circuit Playground Express A1 is located on the right side of the board. There are multiple ground and 3V pads (pins). Your board has 7 analog pins that can be used for this purpose. For the full list, see the pinout page (https://adafru.it/AM9) on the main guide. Trinket M0 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.
Gemma M0 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. Your board has 3 analog pins. For the full list, see the pinout page (https://adafru.it/AMa) on the main guide. Feather M0 Express and Feather M4 Express A1 is located along the edge opposite the battery connector. There are multiple ground pins.
ItsyBitsy M0 Express and ItsyBitsy M4 Express 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. You have 6 analog pins you can use. For a full list, see the pinouts page (https://adafru.it/BMg) on the main guide. Metro M0 Express and Metro M4 Express A1 is located on the same side of the board as the barrel jack.
CircuitPython Analog Out This example shows you how you can set the DAC (true analog output) on pin A0. A0 is the only true analog output on the M0 boards. No other pins do true analog output! Copy and paste the code into code.py using your favorite editor, and save the file. # CircuitPython IO demo - analog output import board from analogio import AnalogOut analog_out = AnalogOut(board.
Find the pin Use the diagrams below to find the A0 pin marked with a magenta arrow! Circuit Playground Express A0 is located between VOUT and A1 near the battery port. Trinket M0 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. © Adafruit Industries https://learn.adafruit.
Gemma M0 A0 is located in the middle of the right side of the board next to the On/Off switch. Feather M0 Express 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.
ItsyBitsy M0 Express 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. ItsyBitsy M4 Express A0 is located between VHI and A1, and the pin pad has left and right white parenthesis markings around it. Metro M0 Express 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. © Adafruit Industries https://learn.
Metro M4 Express 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. On the Metro M4 Express, there are TWO true analog outputs: A0 and A1. © Adafruit Industries https://learn.adafruit.
CircuitPython PWM Your board has pulseio 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 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. Copy and paste the code into code.py using your favorite editor, and save the file.
To use with the Metro M4 Express, ItsyBitsy M4 Express or the Feather M4 Express, you must comment out the piezo = pulseio.PWMOut(board.A2, duty_cycle=0, frequency=440, variable_frequency=True) line and uncomment the piezo = pulseio.PWMOut(board.A1, duty_cycle=0, frequency=440, variable_frequency=True) line. A2 is not a supported PWM pin on the M4 boards! Remember: To "comment out" a line, put a # and a space before it. To "uncomment" a line, remove the # + space from the beginning of the line.
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 Use alligator clips to attach A2 and any one of the GND to different legs of the piezo.
Gemma M0 Use alligator clips to attach A2 and GND to different legs on the piezo. Gemma has PWM available on the following pins: A1, D2, RX, SCL, A2, D0, TX, SDA, L, D13, APA102_MOSI, APA102_SCK. There is NO PWM on: A0, D1. Feather M0 Express Use jumper wires to attach A2 and one of the two GND to different legs of the piezo. 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. There is NO PWM on: A0, A1, A5.
ItsyBitsy M0 Express Use jumper wires to attach A2 and G to different legs of the piezo. 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. There is NO PWM on: A0, A1, A5. ItsyBitsy M4 Express Use jumper wires to attach A1 and G to different legs of the piezo. To use A1, comment out the current pin setup line, and uncomment the line labeled for the M4 boards.
Metro M4 Express Use jumper wires to connect A1 and any one of the GND to different legs on the piezo. 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 There is No PWM on: A0, A2, A3, A4, SCL, AREF, NEOPIXEL, LED_RX, LED_TX.
CircuitPython Servo In order to use servos, we take advantage of pulseio . Now, in theory, you could just use the raw pulseio 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://adafru.
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. For Circuit Playground Express, use jumper wire alligator clips to connect the ground wire to GND, the power wire to VOUT, and the signal wire to A2. 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. © Adafruit Industries https://learn.adafruit.
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 A1. 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: import time import board import pulseio from adafruit_motor import servo # create a PWMOut object on Pin A2. pwm = pulseio.PWMOut(board.
# Continuous Servo Test Program for CircuitPython import time import board import pulseio from adafruit_motor import servo # create a PWMOut object on Pin A2. pwm = pulseio.PWMOut(board.A2, frequency=50) # Create a servo object, my_servo. my_servo = servo.ContinuousServo(pwm) while True: print("forward") my_servo.throttle time.sleep(2.0) print("stop") my_servo.throttle time.sleep(2.0) print("reverse") my_servo.throttle time.sleep(2.0) print("stop") my_servo.throttle time.sleep(4.0) = 1.0 = 0.0 = -1.
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 (https://adafru.it/kDg) and NeoPixel (https://adafru.it/Bej). This section covers both and explains which boards have which LED. The first example will show you how to change the color and brightness of the internal RGB LED. Copy and paste the code into code.py using your favorite editor, and save the file.
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. 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 import adafruit_dotstar and led = adafruit_dotstar.DotStar(board.APA102_SCK, board.
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!) Coding a rainbow effect involves a little math and a helper function called wheel . For details about how wheel works, see this explanation here (https://adafru.
The last example shows how to do a rainbow animation on the internal RGB LED. Copy and paste the code into code.py using your favorite editor, and save the file. Remember to comment and uncomment the right lines for the board you're using, as explained above (https://adafru.it/Bel). import time import board # For Trinket M0, Gemma M0, ItsyBitsy M0 Express and ItsyBitsy M4 Express import adafruit_dotstar led = adafruit_dotstar.DotStar(board.APA102_SCK, board.
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 (https://adafru.it/Bem)! © Adafruit Industries https://learn.adafruit.
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.
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. Do not use the VIN pin directly on Metro M0 Express or Metro M4 Express! The voltage can reach 9V and this can destroy your NeoPixels! Note that the wire ordering on your NeoPixel strip or shape may not exactly match the diagram above.
pixels[i] = color time.sleep(wait) pixels.show() time.sleep(0.5) def rainbow_cycle(wait): for j in range(255): for i in range(num_pixels): rc_index = (i * 256 // num_pixels) + j pixels[i] = wheel(rc_index & 255) pixels.show() time.sleep(wait) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) while True: pixels.fill(RED) pixels.show() # Increase or decrease to change the speed of the solid color change. time.sleep(1) pixels.
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 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.
pixel_pin = board.A1 num_pixels = 8 pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False, pixel_order=(1, 0, 2, 3)) def wheel(pos): # Input a value 0 to 255 to get a color value. # The colours are a transition r - g - b - back to r.
color_chase(GREEN, 0.1) color_chase(CYAN, 0.1) color_chase(BLUE, 0.1) color_chase(PURPLE, 0.1) rainbow_cycle(0) # Increase the number to slow down the rainbow Read the Docs For a more in depth look at what neopixel can do, check out NeoPixel on Read the Docs (https://adafru.it/C5m). © Adafruit Industries https://learn.adafruit.
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).
Note that the wire ordering on your DotStar strip or shape may not exactly match the diagram above. Check the markings to verify which pin is DIN, CIN, 5V and GND The Code This example includes multiple visual effects. Copy and paste the code into code.py using your favorite editor, and save the file. # CircuitPython demo - Dotstar import time import adafruit_dotstar import board num_pixels = 30 pixels = adafruit_dotstar.DotStar(board.A1, board.A2, num_pixels, brightness=0.
pixels.show() time.sleep(wait) pixels[::2] = [YELLOW] * (num_pixels // 2) pixels.show() time.sleep(wait) pixels[1::2] = [GREEN] * (num_pixels // 2) pixels.show() time.sleep(wait) pixels[::2] = [TEAL] * (num_pixels // 2) pixels.show() time.sleep(wait) pixels[1::2] = [CYAN] * (num_pixels // 2) pixels.show() time.sleep(wait) pixels[::2] = [BLUE] * (num_pixels // 2) pixels.show() time.sleep(wait) pixels[1::2] = [PURPLE] * (num_pixels // 2) pixels.show() time.
ORANGE = (255, 40, 0) GREEN = (0, 255, 0) TEAL = (0, 255, 120) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) MAGENTA = (255, 0, 20) WHITE = (255, 255, 255) while True: # Change this number to change how long it stays on each solid color. color_fill(RED, 0.5) color_fill(YELLOW, 0.5) color_fill(ORANGE, 0.5) color_fill(GREEN, 0.5) color_fill(TEAL, 0.5) color_fill(CYAN, 0.5) color_fill(BLUE, 0.5) color_fill(PURPLE, 0.5) color_fill(MAGENTA, 0.5) color_fill(WHITE, 0.
set 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. First is wheel() which we just learned with the Internal RGB LED (https://adafru.it/Bel).
import board import busio def is_hardware_spi(clock_pin, data_pin): try: p = busio.SPI(clock_pin, data_pin) p.deinit() return True except ValueError: return False # Provide the two pins you intend to use. if is_hardware_spi(board.A1, board.A2): print("This pin combination is hardware SPI!") else: print("This pin combination isn't hardware SPI.") Read the Docs For a more in depth look at what dotstar can do, check out DotStar on Read the Docs (https://adafru.it/C4d). © Adafruit Industries https://learn.
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.
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: For more information about the data you're reading and the Ultimate GPS, check out the Ultimate GPS guide: https://learn.adafruit.com/adafruit-ultimate-gps Wire It Up You'll need a couple of things to connect the GPS to your board.
Check out the list below for a diagram of your specific board! Watch out! A common mixup with UART serial is that RX on one board connects to TX on the other! However, sometimes boards have RX labeled TX and vice versa. So, you'll want to start with RX connected to TX, but if that doesn't work, try the other way around! Circuit Playground Express 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.
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. 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. © Adafruit Industries https://learn.adafruit.
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. 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.
import board import busio from microcontroller import Pin def is_hardware_uart(tx, rx): try: p = busio.UART(tx, rx) p.deinit() return True except ValueError: return False def get_unique_pins(): exclude = ['NEOPIXEL', 'APA102_MOSI', 'APA102_SCK'] pins = [pin for pin in [ getattr(board, p) for p in dir(board) if p not in exclude] if isinstance(pin, Pin)] unique = [] for p in pins: if p not in unique: unique.
>>> import board,busio >>> i2c = busio.I2C(board.SCL, board.SDA) >>> uart = busio.UART(board.TX, board.RX) Traceback (most recent call last): File "", line 1, in ValueError: Invalid pins © Adafruit Industries https://learn.adafruit.
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://adafru.it/uap).
Circuit Playground Express Connect 3.3v on your CPX to 3.3v on your TSL2561. Connect GND on your CPX to GND on your TSL2561. Connect SCL/A4 on your CPX to SCL on your TSL2561. Connect SDL/A5 on your CPX to SDA on your TSL2561. Trinket M0 Connect USB on the Trinket to VIN on the TSL2561. Connect Gnd on the Trinket to GND on the TSL2561. Connect D2 on the Trinket to SCL on the TSL2561. Connect D0 on the Trinket to SDA on the TSL2561. Gemma M0 Connect 3vo on the Gemma to 3V on the TSL2561.
Feather M0 Express and Feather M4 Express Connect USB on the Feather to VIN on the TSL2561. Connect GND on the Feather to GND on the TSL2561. Connect SCL on the Feather to SCL on the TSL2561. Connect SDA on the Feather to SDA on the TSL2561. ItsyBitsy M0 Express and ItsyBitsy M4 Express Connect USB on the ItsyBitsy to VIN on the TSL2561 Connect G on the ItsyBitsy to GND on the TSL2561. Connect SCL on the ItsyBitsy to SCL on the TSL2561. Connect SDA on the ItsyBitsy to SDA on the TSL2561.
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. Copy and paste the code into code.py using your favorite editor, and save the file. # CircuitPython demo - I2C scan import time import board import busio i2c = busio.I2C(board.SCL, board.SDA) while not i2c.
# CircuitPython Demo - I2C sensor import time import adafruit_tsl2561 import board import busio i2c = busio.I2C(board.SCL, board.SDA) # Lock the I2C device before we try to scan while not i2c.try_lock(): pass # Print the addresses found once print("I2C addresses found:", [hex(device_address) for device_address in i2c.scan()]) # Unlock I2C now that we're done scanning. i2c.unlock() # Create library object on our I2C port tsl2561 = adafruit_tsl2561.
Where's my I2C? On the SAMD21, we have the flexibility of using a wide range of pins for I2C. Some chips, like the ESP8266 can use any pins for I2C, using bitbangio. There's some other chips that may have fixed I2C pin. 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'.
import board import busio from microcontroller import Pin def is_hardware_I2C(scl, sda): try: p = busio.I2C(scl, sda) p.deinit() return True except ValueError: return False except RuntimeError: return True def get_unique_pins(): exclude = ['NEOPIXEL', 'APA102_MOSI', 'APA102_SCK'] pins = [pin for pin in [ getattr(board, p) for p in dir(board) if p not in exclude] if isinstance(pin, Pin)] unique = [] for p in pins: if p not in unique: unique.
CircuitPython HID Keyboard and Mouse One of the things we baked into CircuitPython is 'HID' (Human Interface Device) 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.
print("Waiting for key pin...") while True: # Check each pin for key_pin in key_pin_array: if not key_pin.value: # Is it grounded? i = key_pin_array.index(key_pin) print("Pin #%d is grounded." % i) # Turn on the red LED led.value = True while not key_pin.value: pass # Wait for it to be ungrounded! # "Type" the Keycode or string key = keys_pressed[i] # Get the corresponding Keycode or string if isinstance(key, str): # If it's a string... keyboard_layout.write(key) # ...
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.
def get_voltage(pin): return (pin.value * 3.3) / 65536 def steps(axis): """ Maps the potentiometer voltage range to 0-20 """ return round((axis - pot_min) / step) while True: x = get_voltage(x_axis) y = get_voltage(y_axis) if select.value is False: mouse.click(Mouse.LEFT_BUTTON) time.sleep(0.2) # Debounce delay if steps(x) > 11.0: # print(steps(x)) mouse.move(x=1) if steps(x) < 9.0: # print(steps(x)) mouse.move(x=-1) if steps(x) > 19.0: # print(steps(x)) mouse.move(x=8) if steps(x) < 1.
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.
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.
CircuitPython Expectations As we continue to develop CircuitPython and create new releases, we will stop supporting older releases. If you are running CircuitPython 2.x, you need to update to 3.x. You must download the CircuitPython Library Bundle that matches your version of CircuitPython. Please update to CircuitPython 3.x and then download the 3.x bundle.
CircuitPython runs nicely on the Gemma M0 or Trinket 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 and bitbangio are not included.
© Adafruit Industries https://learn.adafruit.
Arduino IDE Setup The first thing you will need to do is to download the latest release of the Arduino IDE. You will need to be using version 1.8 or higher for this guide https://adafru.it/f1P https://adafru.it/f1P After you have downloaded and installed the latest version of Arduino IDE, you will need to start the IDE and navigate to the Preferences menu. You can access it from the File menu in Windows or Linux, or the Arduino menu on OS X. A dialog will pop up just like the one shown below.
We will be adding a URL to the new Additional Boards Manager URLs option. The list of URLs is comma separated, and you will only have to add each URL once. New Adafruit boards and updates to existing boards will automatically be picked up by the Board Manager each time it is opened. The URLs point to index files that the Board Manager uses to build the list of available & installed boards.
Here's a short description of each of the Adafruit supplied packages that will be available in the Board Manager when you add the URL: Adafruit AVR Boards - Includes support for Flora, Gemma, Feather 32u4, Trinket, & Trinket Pro.
Using with Arduino IDE The Feather/Metro/Gemma/Trinket M0 and M4 use an ATSAMD21 or ATSAMD51 chip, and you can pretty easily get it working with the Arduino IDE. Most libraries (including the popular ones like NeoPixels and display) will work with the M0 and M4, especially devices & sensors that use I2C or SPI. Now that you have added the appropriate URLs to the Arduino IDE preferences in the previous page, you can open the Boards Manager by navigating to the Tools->Board menu.
Install Adafruit SAMD Next you can install the Adafruit SAMD package to add the board file definitions Make sure you have Type All selected to the left of the Filter your search... box You can type Adafruit SAMD in the top search bar, then when you see the entry, click Install Even though in theory you don't need to - I recommend rebooting the IDE Quit and reopen the Arduino IDE to ensure that all of the boards are properly installed.
Install Drivers (Windows 7 & 8 Only) When you plug in the board, you'll need to possibly install a driver Click below to download our Driver Installer https://adafru.it/EC0 https://adafru.it/EC0 Download and run the installer Run the installer! Since we bundle the SiLabs and FTDI drivers as well, you'll need to click through the license © Adafruit Industries https://learn.adafruit.
Select which drivers you want to install, the defaults will set you up with just about every Adafruit board! Click Install to do the installin' Blink Now you can upload your first blink sketch! Plug in the M0 or M4 board, and wait for it to be recognized by the OS (just takes a few seconds). It will create a serial/COM port, you can now select it from the drop-down, it'll even be 'indicated' as Trinket/Gemma/Metro/Feather/ItsyBitsy/Trellis! © Adafruit Industries https://learn.adafruit.
Now load up the Blink example // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 13 as an output.
Compilation Issues If you get an alert that looks like Cannot run program "{runtime.tools.arm-none-eabi-gcc.path}\bin\arm-non-eabi-g++" Make sure you have installed the Arduino SAMD boards package, you need both Arduino & Adafruit SAMD board packages Manually bootloading If you ever get in a 'weird' spot with the bootloader, or you have uploaded code that crashes and doesn't auto-reboot into the bootloader, click the RST button twice (like a double-click)to get back into the bootloader.
The fix for this issue is to make sure Adafruit's custom udev rules are applied to your system. One of these rules is made to configure modem manager not to touch the Feather board and will fix the programming difficulty issue. Follow the steps for installing Adafruit's udev rules on this page. (https://adafru.it/iOE) © Adafruit Industries https://learn.adafruit.
WiFi Test Library Install We're using a variant of the Arduino WiFiNINA library, which is amazing and written by the Arduino team! We made a fork that you can install. Click here to download the library: https://adafru.it/Evm https://adafru.it/Evm Within the Arduino IDE, select Install library from ZIP... And select the zip you just downloaded. First Test OK now you have it wired and library installed, time to test it out! Lets start by scanning the local networks.
At the top you'll see a section where the GPIO pins are defined If you don't see this, you may have the wrong WiFiNINA library installed. Uninstall it and re-install the Adafruit one linked above. You do not need to change any of these pins - they're pre-defined in the board definition for the Metro M4 WiFi but you still will want to have our version of the library installed because you can switch to the AirLift shield or breakout.
If you don't even get the MAC address printed out, check your wiring. If you get the MAC address but cannot scan any networks, check your power supply. You need a solid 3-5VDC into Vin in order for the ESP32 not to brown out. WiFi Connection Test Now that you have your wiring checked, time to connect to the Internet! Open up the WiFiWebClient example Open up the secondary tab, arduino_secrets.h. This is where you will store private data like the SSID/password to your network.
You must change these string values before updating to your board! After you've set it correctly, upload and check the serial monitor. You should see the following. If not, go back, check wiring, power and your SSID/password Secure Connection Example Many servers today do not allow non-SSL connectivity. Lucky for you the ESP32 has a great TLS/SSL stack so you can have that all taken care of for you. Here's an example of a secure WiFi connection: © Adafruit Industries https://learn.adafruit.
Note we use WiFiSSLClient client; instead of WiFiClient client; to require an SSL connection! © Adafruit Industries https://learn.adafruit.
JSON Parsing Demo This example is a little more advanced - many sites will have API's that give you JSON data. We'll use ArduinoJSON (https://adafru.it/Evn) to convert that to a format we can use and then display that data on the serial port (which can then be re-directed to a display of some sort) First up, use the Library manager to install ArduinoJSON (https://adafru.it/Evo).
Updating ESP32 Firmware There may come a time when you want to update the firmware on the ESP32 itself. This isn't something we expect you'll do often if at all, but its good to know how if you need to. There are two ways to do it. The first uses the SAMD51 main chip as a USB->Serial converter, then you can use any computer and esptool (https://adafru.it/E9p) to update the ESP32. This is recommended because esptool is very powerful and fast.
Downloads Files ATSAMD51J19 Product page w/datasheets (https://adafru.it/Bf8) (the main chip on the Metro M4 Express AirLift Lite) Fritzing Object in the Adafruit Fritzing Library (https://adafru.it/ENN) EagleCAD files on GitHub (https://adafru.it/ENO) Schematic Fab Print © Adafruit Industries https://learn.adafruit.
© Adafruit Industries https://learn.adafruit.
© Adafruit Industries Last Updated: 2019-08-20 12:31:14 AM UTC Page 187 of 187