Code Monkey home page Code Monkey logo

abcs-with-magtag's Introduction

ABCs with MagTag

Adafruit's MagTag IoT with learning activities for kids coded in CircuitPython.
Activities include ABCs, Songs, Colors, and Shapes.

After receiving AdaBox 017, I was thinking about a project I could do that was age appropriate for my two year old son. He's learning letters, colors, and shapes. Of course he enjoys music, pressing buttons, and lights. All of which the MagTag was capable of doing. Pictures below.

Generating Tones

I did have some difficulty with the music. To generate sounds, the sample code that came preloaded in the MagTag used the function magtag.peripherals.play_tone(frequency, duration)
At the time I wrote my code, using this function, the MagTag would not generate more than 4 unique tones per boot. I reached out the AdaFruit discord channel for some help. A thank you to GastroGeek who pointed me to the CircuitPython MagTag source code where I found the issue. Let's start from the top layer class "peripherals.py" It initializes the speaker with the following code:

import board
from digitalio import DigitalInOut, Direction, Pull

speaker_enable = DigitalInOut(board.SPEAKER_ENABLE)
speaker_enable.direction = Direction.OUTPUT
speaker_enable.value = False

The function play_tone(self, frequency, duration) is used to play tones:

play_tone(self, frequency, duration):
  self._speaker_enable.value = True
  simpleio.tone(board.SPEAKER, frequency, duration)

An error was occuring at this point when more than 4 distinct tones were trying to be played. So I dug into simpleio.
simpleio's tone(pin, frequency, duration=1, length=100) function is being called to generate the tone using the following code, which errors out when more than 4 distinct frequencies are given

with pulseio.PWMOut(pin, frequency=int(frequency), variable_frequency=False) as pwm:
            pwm.duty_cycle = 0x8000
            time.sleep(duration)

Error:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: No more timers available

My work around was initializing a single pulseio.PWMOut object with variable frequency, and changing the frequencies as needed. Enabling / disabling the speaker as needed.

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

# Enable the speaker
speaker_enable = DigitalInOut(board.SPEAKER_ENABLE)
speaker_enable.direction = Direction.OUTPUT
speaker_enable.value = False
pin = board.SPEAKER
pwm = pulseio.PWMOut(pin, duty_cycle=0x8000, frequency=131, variable_frequency=True)

# Play the frequencies 
for i, freq in enumerate([131, 147, 165, 175, 196, 220, 247]):
    if i == 0:
         speaker_enable.value = True
    pwm.frequency = freq
    time.sleep(0.25)

speaker_enable.value = False
pwm.deinit()

Parts & Documentation

Project parts from AdaBox 017: https://learn.adafruit.com/adabox017
MagTag Documentation: https://circuitpython.readthedocs.io/projects/magtag/en/latest/api.html#adafruit-magtag-magtag
Requires CircuitPython: https://learn.adafruit.com/welcome-to-circuitpython/installing-circuitpython

Images

Start up menu:
alt text

ABCs:
alt text

Songs (plays music through MagTag speaker):
alt text

Colors:
alt text

Shapes:
alt text

abcs-with-magtag's People

Contributors

esjmorales avatar

Stargazers

Max Glenister avatar James Starmer avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.