Bare Conductive Starter Article 8

Connecting to a Computer

USB communication, the Serial Monitor, and sending touch data from the Touch Board to a computer in real time.

⏱ 16 min read serial USB communication Serial Monitor data

Two roles for the USB connection

When you connect the Touch Board to a computer via USB, it serves two purposes:

  1. Power — the 5V USB supply powers the board
  2. Communication — a virtual serial port lets the board send and receive data

The serial port appears in your operating system as:

  • Windows: COM3, COM4, etc. (check Device Manager)
  • macOS: /dev/tty.usbmodem... or /dev/cu.usbmodem...
  • Linux: /dev/ttyACM0 or /dev/ttyACM1

Sending data from the Touch Board

Any Arduino sketch can print data over serial. Here’s a minimal sketch that sends touch events:

#include <MPR121.h>
#include <Wire.h>

void setup() {
  Serial.begin(57600);
  while (!Serial);  // wait for serial connection (Leonardo behaviour)

  if (!MPR121.begin(0x5C)) {
    Serial.println("MPR121 init failed");
    while (1);
  }
  MPR121.setInterruptPin(4);
}

void loop() {
  if (MPR121.touchStatusChanged()) {
    MPR121.updateTouchData();

    for (int i = 0; i < 12; i++) {
      if (MPR121.isNewTouch(i)) {
        Serial.print("TOUCH ");
        Serial.println(i);
      }
      if (MPR121.isNewRelease(i)) {
        Serial.print("RELEASE ");
        Serial.println(i);
      }
    }
  }
}

Upload this sketch. Then open the Serial Monitor (Tools → Serial Monitor) and set the baud rate to 57600. Touch the electrodes — you’ll see messages like:

TOUCH 3
RELEASE 3
TOUCH 7
TOUCH 8
RELEASE 7
RELEASE 8

Sending structured data (JSON)

For reading from another application (Processing, Max/MSP, Python, p5.js), structured output is easier to parse:

void sendTouchState() {
  Serial.print("{\"touched\":[");
  for (int i = 0; i < 12; i++) {
    Serial.print(MPR121.isNewTouch(i) ? "1" : "0");
    if (i < 11) Serial.print(",");
  }
  Serial.println("]}");
}

Reading from Python

A simple Python script to read touch events:

import serial
import time

port = serial.Serial('/dev/ttyACM0', 57600, timeout=1)
time.sleep(2)  # wait for board to reset

while True:
    line = port.readline().decode('utf-8').strip()
    if line:
        print(line)

Install pyserial first: pip install pyserial

Reading in Processing

Processing has a built-in Serial library:

import processing.serial.*;

Serial board;
String lastMessage = "";

void setup() {
  size(600, 400);
  board = new Serial(this, "/dev/ttyACM0", 57600);
  board.bufferUntil('\n');
}

void draw() {
  background(20);
  fill(255);
  textSize(18);
  textAlign(CENTER, CENTER);
  text(lastMessage, width/2, height/2);
}

void serialEvent(Serial s) {
  lastMessage = s.readStringUntil('\n').trim();
}

Reading in p5.js (via Web Serial)

Using the Web Serial API covered in the p5.js Expert section:

let port, reader;
let touchStates = new Array(12).fill(false);

async function connectSerial() {
  port = await navigator.serial.requestPort();
  await port.open({ baudRate: 57600 });

  let decoder = new TextDecoderStream();
  port.readable.pipeTo(decoder.writable);
  reader = decoder.readable.getReader();

  let buffer = '';
  while (true) {
    const { value, done } = await reader.read();
    if (done) break;
    buffer += value;
    let lines = buffer.split('\n');
    buffer = lines.pop();
    for (let line of lines) {
      parseLine(line.trim());
    }
  }
}

function parseLine(line) {
  if (line.startsWith('TOUCH ')) {
    let n = parseInt(line.slice(6));
    if (!isNaN(n)) touchStates[n] = true;
  } else if (line.startsWith('RELEASE ')) {
    let n = parseInt(line.slice(8));
    if (!isNaN(n)) touchStates[n] = false;
  }
}

Baud rate

The baud rate must match on both sides. Common rates for Touch Board projects:

  • 57600 — default for Touch Board examples
  • 115200 — faster, good for high-frequency data
  • 9600 — slower, used in some older examples

If the Serial Monitor shows garbled characters, the baud rates don’t match.

Sending data TO the Touch Board

You can also send commands from the computer to the Touch Board:

void loop() {
  if (Serial.available()) {
    char cmd = Serial.read();
    if (cmd == 'p') {
      // Computer sent 'p' — play a sound
      playTrack(0);
    }
    if (cmd == 's') {
      // Stop playback
      MP3player.stopTrack();
    }
  }
  // ... normal touch handling
}

Key takeaways

  • USB connection provides both power and a virtual serial port
  • Serial.begin(57600) starts communication; Serial.print() / Serial.println() send data
  • Serial Monitor in the Arduino IDE shows live output — essential for debugging
  • Parse the output in Python (pyserial), Processing (Serial library), or p5.js (Web Serial API)
  • Match baud rates on both ends — 57600 is the Touch Board default
  • Two-way communication: the board can receive commands as well as send data