This section of the guide will bring touch and sound to our project. First, we're going to learn how to use the capacitive touch pads on the Circuit Playground Express. We'll have each one print a response so we know it's working.
We'll start with touch pad A1.
Rename your current code.py
if you'd like to keep it. Then, download the following file. Rename it to code.py
and copy it to your CPX.
# SPDX-FileCopyrightText: 2017 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT from adafruit_circuitplayground import cp while True: if cp.touch_A1: print('Touched 1!')
Now let's take a look at the code.
In this code, we create a loop. The board is constantly checking to see if you've touched A1, and prints a message for you in the serial REPL when you do. The code basically reads, "If you touch A1, print 'Touched 1!'" That's all there is to it.
The following file includes the rest of the touch pads. Download the following file. Rename it to code.py
and copy it to your CPX.
# SPDX-FileCopyrightText: 2017 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT from adafruit_circuitplayground import cp while True: if cp.touch_A1: print('Touched 1!') elif cp.touch_A2: print('Touched 2!') elif cp.touch_A3: print('Touched 3!') elif cp.touch_A4: print('Touched 4!') elif cp.touch_A5: print('Touched 5!') elif cp.touch_A6: print('Touched 6!') elif cp.touch_A7: print('Touched 7!')
Now you can touch any of the capacitive touch pads and the serial REPL will let you know which one you touch. Give it a try!
Let's look at the difference in the code.
while True: if cp.touch_A1: print('touched 1!') elif cp.touch_A2: print('touched 2!')
So far we've used while
loops, if
, and else
statements. This code includes an elif
statement. elif
is combination of else
and if
, which essentially means, "Otherwise, if."
There are 7 touch pads. The board needs to know which one you're touching, and provide a response based on which pad you're touching. The code says, "If you're touching A1, I will print 'Touched 1!
', otherwise, if you're touching A2, I will print 'Touched 2!
', otherwise, if you're touching A3, I will print 'Touched 3!
'" and so on for each touch pad. We cannot use else
in this piece of code because this isn't simply an "on/off" situation. It's more granular with 7 possible states, one for each touch pad.
Now we'll learn how to make tones using the onboard speaker.
There are two ways to make tones using the onboard speaker. One is play_tone
, which plays a given tone for a given duration. The other is start_tone
and stop_tone
, which require you to provide a trigger, such as pressing a button (or touching a touch pad!), to play a tone for the duration of the trigger event.
We'll start by making a single tone. Download the file. Rename it to code.py
and copy it to your CPX.
# SPDX-FileCopyrightText: 2017 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT from adafruit_circuitplayground import cp cp.play_tone(440, 1)
In the first example, we use play_tone
, which requires you to provide a frequency (in Hz) and a duration (in seconds). We've provided 440 Hz (which is Middle A) and 1 second.
In this case, the tone plays as soon as the code runs, which, since there is no other code included, occurs as soon as the board starts up. This code doesn't repeat the tone - it plays it once. You can save your code.py
again, or reload the serial REPL to make it play the tone again.
You can change both the frequency and duration to change the pitch of the tone and the length for which it plays. If you're looking for specific pitches, check out an online tone generator. Have have fun with it!
For our tone piano, we'll be using start_tone
and stop_tone
. For this example, we'll use touch pads A1 and A2 on your Circuit Playground Express.
Remember, if you made any changes to your code that you'd like to keep, rename your current code.py
. Download the following file, rename it to code.py
and copy it to your CPX.
# SPDX-FileCopyrightText: 2017 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT from adafruit_circuitplayground import cp while True: if cp.touch_A1: cp.start_tone(262) elif cp.touch_A2: cp.start_tone(294) else: cp.stop_tone()
This second example uses start_tone
, which requires you to provide a frequency (in Hz), and stop_tone
which does exactly that, stops the tone.
If you touch A1, you'll hear a tone until you stop touching A1. If you touch A2, you'll hear a tone until you stop touching A2. We've again used while
, if
, and elif
and, this time, we included else
. This code essentially means, "As long as you're touching A1, play the given tone, otherwise stop playing. Otherwise as long as you're touching A2, play the given tone, otherwise stop playing." The else
statement applies to both the if
and the elif
separately.
This is the start of a touch tone piano! We're almost there. In the next section, we'll put together everything we've learned into the final project!
Page last edited January 22, 2025
Text editor powered by tinymce.