Installing Project Code

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.

Thankfully, we can do this in one go. In the example below, 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 directory PyPortal_Guitar_Tuner/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.

Your CIRCUITPY drive should now look similar to the following image:

CIRCUITPY
# SPDX-FileCopyrightText: 2020 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time
from adafruit_button import Button
from adafruit_pyportal import PyPortal

pyportal = PyPortal(default_bg="/stock-pyportal.bmp")

lowE = "/sounds/lowE.wav"
A = "/sounds/A.wav"
D = "/sounds/D.wav"
G = "/sounds/G.wav"
B = "/sounds/B.wav"
highE = "/sounds/highE.wav"

notes = [lowE, A, D, G, B, highE]

pegs = [
    {'label': "lowE", 'pos': (53, 0), 'size': (65, 90)},
    {'label': "A", 'pos': (124, 0), 'size': (65, 90)},
    {'label': "D", 'pos': (194, 0), 'size': (65, 90)},
    {'label': "G", 'pos': (194, 150), 'size': (65, 90)},
    {'label': "B", 'pos': (124, 150), 'size': (65, 90)},
    {'label': "highE", 'pos': (53, 150), 'size': (65, 90)}
    ]

buttons = []
for peg in pegs:
    button = Button(x=peg['pos'][0], y=peg['pos'][1],
                    width=peg['size'][0], height=peg['size'][1],
                    style=Button.RECT,
                    fill_color=None, outline_color=0x5C3C15,
                    name=peg['label'])
    pyportal.splash.append(button.group)
    buttons.append(button)

note_select = None

while True:
    touch = pyportal.touchscreen.touch_point
    if not touch and note_select:
        note_select = False
    if touch:
        for i in range(6):
            tuning = notes[i]
            button = buttons[i]
            if button.contains(touch) and not note_select:
                print("Touched", button.name)
                note_select = True
                for z in range(3):
                    pyportal.play_file(tuning)
    time.sleep(0.1)

This guide was first published on Jun 09, 2020. It was last updated on Mar 28, 2024.

This page (Coding the PyPortal Guitar Tuner) was last updated on Mar 28, 2024.

Text editor powered by tinymce.