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 “code.py” 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.

# SPDX-FileCopyrightText: 2018 Becky Stern for Adafruit Industries
# SPDX-FileCopyrightText: 2018 T Main for Adafruit Industries
#
# SPDX-License-Identifier: MIT

# Chirp Owl written by Becky Stern and T Main for Adafruit Industries
# Tutorial: http://learn.adafruit.com/chirping-plush-owl-toy/

# Includes animal sounds by Anne Barela
# http://learn.adafruit.com/adafruit-trinket-modded-stuffed-animal/animal-sounds
# based in part on Debounce
#  created 21 November 2006
#  by David A. Mellis
#  modified 30 Aug 2011
#  by Limor Fried
#  modified 28 Dec 2012
#  by Mike Walters
#  CircuitPython Port 2018
#  by Mikey Sklar
#  This example code is in the public domain.
#  http://www.arduino.cc/en/Tutorial/Debounce

import time
import board
import digitalio

# setup for vibration sensor
motion = digitalio.DigitalInOut(board.D0)
motion.direction = digitalio.Direction.INPUT
motion.pull = digitalio.Pull.UP

# setup for speaker output
speaker = digitalio.DigitalInOut(board.D2)
speaker.direction = digitalio.Direction.OUTPUT

def chirp():
    for i in range(200,180,-1):
        play_tone(i,9)

# emphasis ow "me"
def meow():
    play_tone(5100,50)           # "m" (short)
    play_tone(394,180)           # "eee" (long)
    for i in range(990,1022,2): # vary "ooo" down
        play_tone(i,8)
    play_tone(5100,40)           # "w" (short)

# cat meow (emphasis on "ow")
def meow2():
    play_tone(5100,55)          # "m" (short)
    play_tone(394,170)          # "eee" (long)
    time.sleep(0.03)            # wait a tiny bit
    for i in range(990,1022,2): # vary "ooo" down
        play_tone(i,8)
    play_tone(5100,40)          # "w" (short)

# dog ruff
def ruff():
    for i in range(890,910,2):  # "rrr"  (vary down)
        play_tone(i,3)
    play_tone(1664,150)         # "uuu" (hard to do)
    play_tone(12200,70)         # "ff"  (long, hard to do)

# dog arf
def arf():
    play_tone(890,25)             # "a"    (short)
    for i in range(890,910,2):    # "rrr"  (vary down)
        play_tone(i,5)
    play_tone(4545,80)            # intermediate
    play_tone(12200,70)           # "ff"   (shorter, hard to do)

def play_tone(tone_value, duration):
    microseconds = 10 ** 6              # duration divider, convert to microseconds

    for i in range(0, duration):
        i += tone_value * 2
        speaker.value = True
        time.sleep(tone_value / microseconds)
        speaker.value = False
        time.sleep(tone_value / microseconds)

# loop forever...
while True:

    # reverse logic for motion pins
    if not motion.value:
        # bird chirp noise
        # comment out chirp and uncomment a different sound below
        # for more animal noises
        chirp()
        # meow()
        # meow2()
        # ruff()
        # arf()
        time.sleep(.5)                  # leave some time to complete rotation

This guide was first published on Mar 11, 2014. It was last updated on Mar 11, 2014.

This page (CircuitPython Code) was last updated on Mar 18, 2023.

Text editor powered by tinymce.