This version (02 May 2019 16:59) was approved by Wenting Zhang.

Tutorial: Using hardware peripherals with MicroPython

One advantage of MicroPython over a normal Python running on Linux for the SHARC Audio Module is that it has direct support of many hardware features, such as GPIO, TWI etc. This page gives a few examples of using these peripherals.

Getting Help

MicroPython has a help() function builtin. You can always use the help to get a list of supported properties and functions:

>>> help(machine)
object <module 'umachine'> is of type module
  __name__ -- umachine
  info -- <function>
  unique_id -- <function>
  reset -- <function>
  soft_reset -- <function>
  freq -- <function>
  Pin -- <class 'Pin'>
  Signal -- <class 'Signal'>
  I2C -- <class 'I2C'>
  SPI -- <class 'SPI'>
  SD -- <SDCard>
  SDCard -- <class 'SDCard'>

Machine Module

The machine module is the base module for almost all hardware related functions. On some MicroPython ports it is also called pyb, an acronym of pyboard.


The GPIO support of MicroPython is part of the machine library, called the Pin class.

Here is a example using the Pin class to light up an LED:

from machine import Pin
led = Pin("LED10", Pin.OUT)

Besides the name LED10, one may also refer to that pin by its number (PD1), or any alternative functions it has (SPI0_SEL2, ACM0_A4, SMC0_AOE, and SPI0_SS). One may also select the alternative function when initializing the pin (but generally not necessary).

Pin mode can be either Pin.OUT or Pin.IN, and value can be either 0 or 1.

Continue with the previous example, to blink the LED in a loop:

import time
while True:

You can use Ctrl-C to stop the loop.

Here is another example to use a pin in input mode:

button = Pin("BTN1", Pin.IN)

When the SW1 is not pressed, value() should return 0. When the SW1 is pressed, it should return 1.

GPIO Interrupt

GPIO interrupt is supported. See example below:

from machine import Pin
button = Pin("BTN1", Pin.IN)
button.irq(lambda p:print(p), trigger=Pin.IRQ_RISING)

In the example, the interrupt service routine (ISR) is a lambda function. It can also be a full Python function.

The trigger can be Pin.IRQ_RISING, Pin.IRQ_FALLING, or Pin.IRQ_LOW.

Though this is in Python, the interrupt is still the real hardware interrupt. Just like in C, restrictions apply:
  1. One may not allocate memory inside an ISR.
  2. One needs to process all the errors raised before exiting the ISR.
  3. Keep the ISR as short as possible.


SPI support is also part of the machine library, called the SPI class. On the SHARC Audio Module main board, SPI0 is routed to the expansion interface, SPI1 is routed to the Sigma Studio connector, and SPI2 is connected to the SPI Flash. To test SPI1 on the Sigma Studio connector:

from machine import SPI
spi = SPI(1, baudrate=600000, polarity=1, phase=0)

Here is a list of possible initialization arguments:

  1. baudrate: SCK clock rate in Hz.
  2. polarity: 0 or 1, the level when the clock line is idle.
  3. phase: 0 or 1, 0 means to sample data on the first clock edge, and 1 means the second.
  4. bits: Can be 8, 16, or 32. It is the number of bits in each transferred word.


TWI, which is also called as I2C, is also supported by the machine library. On the SHARC Audio Module main board, TWI0 (I2C0) connects to the audio codec and A2B transceiver, also routed to the Sigma Studio connector. To scan the devices connected to the I2C0:

from machine import I2C
i2c = I2C(0, freq=400000)

Note the scan is accomplished by simply sending the address and wait for acknowledge. If no acknowledge is received, no such device exists. Unlike normal transmission, no actual data bytes will be sent out during the scan process.

SD card

If an SD card is inserted before powering up, it will automatically initialize the SD card and mount the filesystem to /sd during boot. So there is no need to manually go through the process.

The SD card is connected to the ADSP-SC589 through the MSI (or called RSI in the ADI drivers). SD is also a class of the machine module.

To print out the information about the SD card:

from machine import SD

To interface with the SD driver directly without using the filesystem, the SD driver provides these API:

  • present
  • power
  • info
  • read
  • write
  • readblocks
  • writeblocks
  • ioctl
In another way, any driver with this block device interface can work with the filesystem. drivers\sdcard\ provides a good example of writing an SD card driver purely in Python language.

Refer to MicroPython's documentation for more information about the block device interface.


MicroPython only works on the ARM core. In order to utilize the SHARC core, a user can use MicroPython to boot the SHARC core with a pre-compiled loader file, and then use shared memory to exchange data with the SHARC core.

Here is a example of loading an audio pass-through application located on the SD card:

f = open('pass_through.ldr', 'rb')
stream =
import sharc

Navigation - SHARC Audio Module

resources/tools-software/sharc-audio-module/micropython/peripherals.txt · Last modified: 02 Apr 2019 21:30 by Chad Wentworth