Python Microcontroller: Getting Started with Adafruit's Trinket M0 and CircuitPython
Microcontrollers were traditionally programmed using assembly code which made interpretation from code to controller simple. Later, compilers and C code were adopted which permitted a more simplified human experience. Most recently, strides have been made toward interfacing between higher level programming languages and microcontrollers, which is geared toward earlier adoption by younger electronics enthusiasts and education situations.
In this tutorial I will cover one of the newest microcontroller interface languages, Python, and demonstrate Adafruit's powerful Trinket M0 microcontroller and its capabilities using Python as its programming language. Much of what is outlined below can be seen on Adafruit's website [UART communication, Trinket info, CircuitPython Basics].
Downloading Mu - The CircuitPython Editor
In my case, I will be using a Raspberry Pi (Linux) computer to interface with the Adafruit Trinket. To start, we want to update the RPi and ensure it has the necessary modifications to handle the Mu editor:
Once the Pi is updated, install the Mu editor using the following command:
Be sure to allow any prompts made by the install. The Mu editor should now be installed. Next, open Mu by navigating from "Programming -> Mu".
The editor should open and present the following window:
Since we're using Adafruit's Trinket M0, we want to select 'Adafruit CircuitPython' and start adding code to the editor!
Uploading a Sketch to The Trinket M0
After plugging the Trinket M0 into the RPi, we can upload a simple sketch to control the onboard red LED using the following code:
The code above should be saved as 'code.py' in the folder under 'media -> pi -> CIRCUITPY'. There should be other files such as 'main.py' and 'README.txt'. The code above uses the onboard LED (D13) and blinks it 5 times per second. After saving the sketch above, it should mimic the video below:
Below is the blink pattern displayed when inaccurate syntaxes are used in the 'code.py' script
If you're seeing the blink pattern above, go back through your uploaded code and find the error or errors.
Taking Things a Step Further: Communication using Bluetooth
In this section I turn the Trinket M0 into a Bluetooth-enabled switch to control the onboard LED from a smartphone using an HM-10 Bluetooth module. The Trinket and Bluetooth module should be wired as follows:
The code used to communicate between the two is shown below. Using an iPhone BLE App (I use this one).
The code above turns the onboard LED on when the HM-10 receives a '1' and turns the LED off when it receives a '0'. A video of this process is shown below with the BLE 4.0 App.
In this tutorial I outlined how to use the hugely popular Python programming language to interface with Adafruit's new M0 Trinket microcontroller. The platform, called CircuitPython, makes it easy to interface with powerful microcontrollers using the high-level easy-to-understand programming language. Using only a few lines demonstrated above, one can tranform a powerful, inexpensive microcontroller into a wireless Bluetooth-enabled smart device, all while being only slightly larger than a quarter.