The Adafruit MacroPad RP2040 features a 3x4 key pad with NeoPixel LEDs, and a small buzzer/speaker This example plays a different tone for each key pressed, and lights up each key a different color while pressed.

Update your code.py to the following.

Click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the folder that matches your CircuitPython version, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.

CIRCUITPY
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
"""
MacroPad tone demo. Plays a different tone for each key pressed and lights up each key a different
color while the key is pressed.
"""
from rainbowio import colorwheel
from adafruit_macropad import MacroPad

macropad = MacroPad()

tones = [196, 220, 246, 262, 294, 330, 349, 392, 440, 494, 523, 587]

while True:
    key_event = macropad.keys.events.get()

    if key_event:
        if key_event.pressed:
            macropad.pixels[key_event.key_number] = colorwheel(
                int(255 / 12) * key_event.key_number
            )
            macropad.start_tone(tones[key_event.key_number])

        else:
            macropad.pixels.fill((0, 0, 0))
            macropad.stop_tone()

Now, press any key! It plays a unique tone and lights up a different color of the rainbow while pressed.

The MacroPad library includes the ability to play tones, either for a specified duration, for example, 0.5 seconds, or until you tell it to stop in your code, for example, the duration of a key press.

The common thing required for playing a tone, regardless of which method you choose, is to specify the tone frequency in Hz as an integer (meaning a whole number without a decimal). For example, to play a "middle C" tone, you would specify 262.

To play that tone for a specified duration of 0.5 seconds, you would include macropad.play_tone(262, 0.5) in your code.

This example uses the start_tone and stop_tone features of the MacroPad library. The first, start_tone, requires only a tone frequency in Hz. However, when you call it in your code, it will play until you tell it to stop. So, if you call start_tone without calling stop_tone(), it will continue to play indefinitely! So, you always want to call stop_tone() somewhere after start_tone.

In this example, you import colorwheel from rainbowio, and you import and instantiate the MacroPad library.

Next, you create a list of 12 tone frequencies in Hz. You must include 12 for it to work properly, as there are 12 keys. You can customise the tones easily by changing the numbers.

Inside the loop, the first thing you do is setup to look for the key press by creating the key_event variable and assigning it to macropad.keys.events.get(). Then, you check to see if there is a key_event (i.e. a key being pressed). If it is a key being pressed (key_event.pressed), you light up the key using the key_number to generate a colorwheel() value, and you start playing the tone from the list that is the same number in the list as the key number being pressed. Remember that Python begins counting from 0. So if you press the first key, the first tone will be played, 196Hz. If you press the fifth key, the fifth tone in the list will be played, 294Hz.

Otherwise, as soon as the key is no longer being pressed (i.e. released), you turn off all the LEDs, and stop playing the tone.

That's all there is to playing a tone using the CircuitPython MacroPad library!

This guide was first published on Jun 30, 2021. It was last updated on Mar 29, 2024.

This page (MacroPad Tone) was last updated on Mar 28, 2024.

Text editor powered by tinymce.