Loading Example Sketches
Explore the Touch Board library's built-in examples, understand what each does, and make your first code modification.
Finding the examples
Once you’ve installed the Touch Board library (article 5), the examples appear in:
File → Examples → (scroll down to) Bare Conductive Touch Board
The exact names depend on the library version, but you’ll typically find:
| Sketch name | What it does |
|---|---|
Touch_MP3 |
Default firmware — touch to play MP3 |
Touch_MP3_Toggle |
Touch to toggle play/stop instead of retrigger |
Touch_MP3_Proximity |
Uses proximity detection to fade volume |
Serial_Comms |
Sends touch events over USB serial |
MIDI_Piano |
Sends MIDI note messages |
LED_Fade |
Fades an LED based on touch |
Anatomy of a Touch Board sketch
Open Touch_MP3 and look at the structure:
// Include the libraries we need
#include <MPR121.h>
#include <Wire.h>
#include <SPI.h>
#include <SdFat.h>
#include <FreeStack.h>
#include <SFEMP3Shield.h>
// Define the number of electrodes
#define numElectrodes 12
// These variables track touch state
boolean touchStates[numElectrodes];
SFEMP3Shield MP3player;
void setup() {
// Initialise serial communication
Serial.begin(57600);
// Initialise the MPR121 touch sensor
if (!MPR121.begin(0x5C)) {
Serial.println("error setting up MPR121");
// handle error...
}
MPR121.setInterruptPin(4);
MPR121.setTouchThreshold(40);
MPR121.setReleaseThreshold(20);
// Initialise the MP3 player
// ... MP3player.begin() etc.
// Reset touch states
for (int i = 0; i < numElectrodes; i++) {
touchStates[i] = false;
}
}
void loop() {
// Read touch sensor
if (MPR121.touchStatusChanged()) {
MPR121.updateTouchData();
for (int i = 0; i < numElectrodes; i++) {
if (MPR121.isNewTouch(i)) {
// Electrode i was just touched
playTrack(i);
}
if (MPR121.isNewRelease(i)) {
// Electrode i was just released
}
}
}
}
The key pattern is:
- Check if anything changed (
touchStatusChanged()) - Update the data (
updateTouchData()) - Loop through electrodes and check for new touches (
isNewTouch(i)) and releases (isNewRelease(i))
Your first modification — adding LED feedback
A small change: flash the onboard LED (D13) when any electrode is touched:
// At the top of your sketch, after the includes:
const int LED_PIN = 13;
// In setup():
pinMode(LED_PIN, OUTPUT);
// In the loop, inside the isNewTouch block:
if (MPR121.isNewTouch(i)) {
digitalWrite(LED_PIN, HIGH);
playTrack(i);
}
// Inside the isNewRelease block:
if (MPR121.isNewRelease(i)) {
digitalWrite(LED_PIN, LOW);
}
Upload this. Now the LED lights up while any electrode is being touched.
Modifying which sounds play
The playTrack(i) function maps electrode number to a filename. To change the mapping, modify the function:
void playTrack(int trackNum) {
char filename[12];
// Default: TRACK000.mp3 for electrode 0, TRACK001.mp3 for electrode 1, etc.
sprintf(filename, "TRACK%03d.mp3", trackNum);
// Custom mapping example: electrodes 0-5 play a major scale (different track range)
int customMap[] = {0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 17, 19};
sprintf(filename, "TRACK%03d.mp3", customMap[trackNum]);
if (MP3player.isPlaying()) MP3player.stopTrack();
MP3player.playMP3(filename);
}
Touch threshold adjustment
The touch and release thresholds control how sensitive the detection is. Lower values = more sensitive:
MPR121.setTouchThreshold(40); // default
MPR121.setReleaseThreshold(20); // default
// More sensitive:
MPR121.setTouchThreshold(20);
MPR121.setReleaseThreshold(10);
// Less sensitive (reduces false triggers):
MPR121.setTouchThreshold(60);
MPR121.setReleaseThreshold(40);
You can also set per-electrode thresholds:
MPR121.setTouchThreshold(40, 0); // electrode 0 only
MPR121.setTouchThreshold(20, 3); // electrode 3 more sensitive
Serial Monitor — seeing what the Touch Board is doing
The Serial Monitor lets you print debug information from the Touch Board to your computer screen:
// In setup():
Serial.begin(57600);
// In the touch handler:
if (MPR121.isNewTouch(i)) {
Serial.print("Touched electrode: ");
Serial.println(i);
}
Open the Serial Monitor: Tools → Serial Monitor (or Ctrl+Shift+M). Set the baud rate to 57600.
You’ll see messages printed every time an electrode is touched. This is invaluable for debugging.
Key takeaways
- Examples are in File → Examples → Bare Conductive Touch Board
- The main loop pattern:
touchStatusChanged()→updateTouchData()→ checkisNewTouch(i)andisNewRelease(i) - Modify
playTrack()to remap sounds to electrodes setTouchThreshold()andsetReleaseThreshold()control sensitivity — lower = more sensitiveSerial.println()+ Serial Monitor is your best debugging tool