Maker Portal

View Original

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].


See this content in the original post

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:

See this content in the original post

Once the Pi is updated, install the Mu editor using the following command:

See this content in the original post

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!


See this content in the original post

After plugging the Trinket M0 into the RPi, we can upload a simple sketch to control the onboard red LED using the following code:

See this content in the original post

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:

See this content in the original post

See this content in the original post

If you're seeing the blink pattern above, go back through your uploaded code and find the error or errors. 


See this content in the original post

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

See this content in the original post

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.

See this content in the original post

See this content in the original post

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.


See More in Arduino:

See this gallery in the original post