MIDI touch piano with two MPR121

Hi, totally beginner with Arduino here! Trying to write a sketch to create a simple MIDI touch piano with Arduino 1 using the Adafruit touch board MPR121. I would like to connect two MPR121 boards for a total of 24 touch keys. I saw several discussions on this topic and try several sketchs but due to the fact I don't know how to exactly code the Arduino's I didn't succeed. The only result I obtained is the following one I found on the forum where just one MPR121 is working with the Arduino but in a very limited way: it sends the same MIDI note for every touch pin of the MPR121. I suppose I should modify the code to define all the 24 midi values I want to use (and to add the second MPR121) but I really don't know how I can modify this code.

This is a library for the MPR121 12-channel Capacitive touch sensor

Designed specifically to work with the MPR121 Breakout in the Adafruit shop
----> https://www.adafruit.com/products/ 1

These sensors use I2C communicate, at least 2 pins are required
to interface

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution

#include <Wire.h>
#include <Adafruit_MPR121.h>
#include <Control_Surface.h> // Include the Control Surface library

// Instantiate a MIDI over USB interface
USBMIDI_Interface midi;

//*const uint8_t C4 = 60; // Note number 60 is defined as middle C in the MIDI specification

using namespace MIDI_Notes;

// MIDI address of the note to send
const MIDIAddress noteAddress = {note(C, 1), CHANNEL_1};
const uint8_t velocity = 0x7F;
//*0b1111111; // Maximum velocity (0b1111111 = 0x7F = 127)

#ifndef _BV
#define _BV(bit) (1 << (bit))

// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are ‘released’
uint16_t lasttouched = 0;
uint16_t currtouched = 0;

void setup() {
//Control_Surface.begin(); // Initialize Control Surface

Serial.println("Adafruit MPR121 Capacitive Touch sensor test");

// Default address is 0x5A, if tied to 3.3V its 0x5B
// If tied to SDA its 0x5C and if SCL then 0x5D
if (!cap.begin(0x5A)) {
Serial.println("MPR121 not found, check wiring?");
while (1);
Serial.println("MPR121 found!");

void loop() {

// Get the currently touched pads
currtouched = cap.touched();

for (uint8_t i=0; i<12; i++) {
// it if is touched and wasnt touched before, alert!
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
Serial.print(i); Serial.println(" touched");

midi.sendNoteOn(noteAddress, velocity);

// if it was touched and now isnt, alert!
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
Serial.print(i); Serial.println(" released");

midi.sendNoteOff(noteAddress, velocity);


// reset our state
lasttouched = currtouched;


Please read How to get the best out of this forum, and edit your post so the code is correctly formatted in a code block.

Unfortunately, you can't learn a programming language from a couple of forum posts. I'd highly recommend starting with a C++ or Arduino course/book/tutorial that provides a structured way to learn the basics. You'll have to learn the basic C++ syntax first, and do some simple exercises to get familiar with the language.
Once you feel confident enough, the first step to modifying the piece of code you posted is understanding it. Make sure you understand every line, every character. This takes time, you need to look things up in the Arduino documentation, library documentation, re-reading parts of your C++ book, etc.
If you have specific questions while trying to understand the code, you can ask them here.

If you're not interested in learning how to program, you can pay someone to write the code for you in the Gigs and Collaborations section of the forum.

thanks for the suggestions, will start learning the basics of C++. Are there any courses/books you can reccomend?

For C++, I can recommend https://www.learncpp.com/ as a free online “book”. It's not specifically targeted at Arduino users, though, so the chapters about setting up your development environment won't apply. There are some other differences as well, most notably the way you print text (cout vs Serial.print), and the fact that standard C++ uses a main function, whereas Arduino uses setup and loop.

If you know C++, you can easily pick up Arduino, but if your goal is to learn Arduino, then it might be better to start with a tutorial/book that focuses on Arduino specifically. I'm not up to date with the options there, others might be able to comment on that, or you can find it online.

There's the Arduino foundations page (https://www.arduino.cc/en/Tutorial/Foundations), but it doesn't seem point to any resources to actually learn programming.

You may prefer to watch and learn.

Google or search on youtube using terms like

c++ tutorial arduino

and try a few minutes of various teachers and their style to see if you like it.

It will take time, no matter how you decide to do this learning stuff… just keep in mind no one was born knowing this, and we all, at one point or another and in total have put in a great deal of time and work.

Which so far no one can do for you.


This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.