CircuitPython Code

GEMMA M0 boards can run CircuitPython — a different approach to programming compared to Arduino sketches. In fact, CircuitPython comes factory pre-loaded on GEMMA M0. If you’ve overwritten it with an Arduino sketch, or just want to learn the basics of setting up and using CircuitPython, this is explained in the Adafruit GEMMA M0 guide.

These directions are specific to the “M0” GEMMA board. The original GEMMA with an 8-bit AVR microcontroller doesn’t run CircuitPython…for those boards, use the Arduino sketch on the “Arduino code” page of this guide.

Below is CircuitPython code that works similarly (though not exactly the same) as the Arduino sketch shown on a prior page. To use this, plug the GEMMA M0 into USB…it should show up on your computer as a small flash drive…then edit the file “” with your text editor of choice. Select and copy the code below and paste it into that file, entirely replacing its contents (don’t mix it in with lingering bits of old code). When you save the file, the code should start running almost immediately (if not, see notes at the bottom of this page).

If GEMMA M0 doesn’t show up as a drive, follow the GEMMA M0 guide link above to prepare the board for CircuitPython.

import board
import pulseio
import time
from digitalio import DigitalInOut, Direction

# PWM (fading) LEDs are connected on D0 (PWM not avail on D1)
pwm_leds = board.D0     
pwm = pulseio.PWMOut(pwm_leds, frequency=1000, duty_cycle=0)

# digital LEDs connected on D2
digital_leds = DigitalInOut(board.D2)
digital_leds.direction = Direction.OUTPUT

brightness = 0          # how bright the LED is
fade_amount = 1285      # 2% steping of 2^16
counter = 0             # counter to keep track of cycles

while True:

    # And send to LED as PWM level
    pwm.duty_cycle = brightness     

    # change the brightness for next time through the loop:
    brightness = brightness + fade_amount


    # reverse the direction of the fading at the ends of the fade:
    if ( brightness <= 0 ):
        fade_amount = -fade_amount
        counter += 1
    elif ( brightness >= 65535 ):
        fade_amount = -fade_amount
        counter += 1

    # wait for 15 ms to see the dimming effect

    # turns on the other LEDs every four times through the fade by
    # checking the modulo of the counter.
    # the modulo function gives you the remainder of
    # the division of two numbers:
    if ( counter % 4 == 0 ):
        digital_leds.value = True    
        digital_leds.value = False
Last updated on 2018-03-01 at 10.23.11 PM Published on 2014-04-02 at 01.30.14 PM