Arduino MIDI controller with MPR121

Hi guys ! I was wondering if someone has already setup a MIDI controller using MPR121 capacitive touch sensors or if anyone knows what to do? I’m currently working on a project using 3 of these iCs and a few pots and I’m getting stuck with the code… I don’t really get how I can get the MPR121 to work with any library like the Control Surface library… Any help or advice is more than welcome !

There are libraries for the MPR121 available in the IDE. Have you tried them ?

I shure did ! But i don’t know how to make the MPR121 library work with the MIDI library which is allowing to send midi signals

I am not familiar with either library, but I imagine that the MPR121 library allows you to detect when a key is pressed and that the MIDI library requires you to pass parameters to functions in order to carry out actions.

Is that correct ?

Exactly, the MPR121 library detects when any pin is either touched or released.
Yes, for example in the midi library I’m using, there is a class used to declare basic Buttons (which behave like I wand the MPR121 pins to behave), but I’m having trouble to adapt this class to make it work with touch sensors… I’m sorry I am pretty sure my problem is due to my lacking programming skills…

As I said, I am not familiar with either library, but why the need to adapt the MIDI library when the Arduino main code could do

if (pinX_hasBecomeTouched)
{
  //call the required MIDI function
}

That might be a solution… I’ll give it a try !

See the following topics:

The following examples will be useful as well:

Hi Pieter, thank’s for your work !
I’ll take a look at those links thank you !

Hello back Pieter,
So I managed to make all three MPR121 working all just great ! but now i’m facing another problem : I tried to add a CD74HC4067 multiplexer with 16 pots connected on it but I can’t find a way to make it work properly, when I turn a pot it seem that more than one signal is sent at a time, making the whole thing glitch out. Both codes, the MPR121 one and the CD74HC4067 one, run separately works all good, but the glitch happens when I put them together like following :

> #include <Control_Surface.h>
> #include <Adafruit_MPR121.h>
> 
> class MIDIMPR121Buttons : public MIDIOutputElement {
>   public:
>     MIDIMPR121Buttons(Adafruit_MPR121 &mpr, const Array<MIDIAddress, 12> &addresses)
>       : mpr(&mpr), addresses(addresses) {}
> 
>     // Initializing the MPR121 is left to the user
>     void begin() override {}
> 
>     // Update is called by Control Surface once every loop iteration
>     void update() override {
>       uint16_t newTouchStates = mpr->touched();
>       uint16_t oldTouchStates = this->touchStates;
>       this->touchStates = newTouchStates;
> 
>       if (newTouchStates == oldTouchStates)
>         return;
> 
>       for (uint8_t i = 0; i < 12; ++i) {
>         // if this pad just became touched
>         if ((newTouchStates & 1) == 1 && (oldTouchStates & 1) == 0) {
>           uint16_t mask = 1u << i;
>           latchStates ^= mask; // toggle the latched state
>           bool latchState = latchStates & mask;
>           // Send the appropriate MIDI message
>           latchState ? Control_Surface.sendCC(addresses[i], onValue)
>                      : Control_Surface.sendCC(addresses[i], offValue);
>         }
>         newTouchStates >>= 1; // Shift over the states to read the next pad
>         oldTouchStates >>= 1;
>       }
>     }
> 
>   private:
>     Adafruit_MPR121 *mpr;
>     Array<MIDIAddress, 12> addresses;
>     uint16_t latchStates = 0;
>     uint16_t touchStates = 0;
>     constexpr static uint8_t onValue = 0x7F;
>     constexpr static uint8_t offValue = 0x00;
> };
> 
> Adafruit_MPR121 add1;
> Adafruit_MPR121 add2;
> Adafruit_MPR121 add3;
> 
> MIDIMPR121Buttons midibuttons1 = {
>   add1,
>   {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, // addresses
> };
> 
> 
> MIDIMPR121Buttons midibuttons2 = {
>   add2,
>   {12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}, // addresses
> };
> 
> 
> MIDIMPR121Buttons midibuttons3 = {
>   add3,
>   {24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35}, // addresses
> };
> 
> USBDebugMIDI_Interface J;
> 
> CD74HC4067 mux = {
>   A0,       // Analog input pin
>   {2, 3, 4, 5} // Address pins S0, S1, S2
> };
>  
> // Create an array of potentiometers that send out
> // MIDI Control Change messages when you turn the
> // potentiometers connected to the eight input pins of
> // the multiplexer
> CCPotentiometer pots[] = {
>   {mux.pin(0), {MIDI_CC::Channel_Volume, CHANNEL_1}},
>   {mux.pin(1), {MIDI_CC::Channel_Volume, CHANNEL_2}},
>   {mux.pin(2), {MIDI_CC::Channel_Volume, CHANNEL_3}},
>   {mux.pin(3), {MIDI_CC::Channel_Volume, CHANNEL_4}},
>   {mux.pin(4), {MIDI_CC::Channel_Volume, CHANNEL_5}},
>   {mux.pin(5), {MIDI_CC::Channel_Volume, CHANNEL_6}},
>   {mux.pin(6), {MIDI_CC::Channel_Volume, CHANNEL_7}},
>   {mux.pin(7), {MIDI_CC::Channel_Volume, CHANNEL_8}},
>   {mux.pin(8), {MIDI_CC::Channel_Volume, CHANNEL_5}},
>   {mux.pin(9), {MIDI_CC::Channel_Volume, CHANNEL_6}},
>   {mux.pin(10), {MIDI_CC::Channel_Volume, CHANNEL_7}},
>   {mux.pin(11), {MIDI_CC::Channel_Volume, CHANNEL_8}},
> };
> 
> 
> 
> void setup() {
>   if(!add1.begin(0x5A))
>     FATAL_ERROR(F("MPR121_A not found, check wiring?"), 0x0121);
> 
>   if(!add2.begin(0x5B))
>     FATAL_ERROR(F("MPR121_B not found, check wiring?"), 0x0121);
> 
>   if(!add3.begin(0x5C))
>     FATAL_ERROR(F("MPR121_C not found, check wiring?"), 0x0121);
> 
>   Control_Surface.begin();
> }
> 
> void loop() {
>   Control_Surface.loop();
> }

If you have any idea of what is happening here, I would be really thankful !

The 7 here ↑
will be the same address as the this potentiometer here ↓

See Control Surface: MIDI_CC Namespace Reference

Other than that, I don't see why there would be interference. Control Surface applies a digital filter and hysteresis to the analog inputs, and performs a dummy read after changing the multiplexer channel, so the cross-talk between potentiometers should be minimal. What's the resistance of your pots? Do you have a stable power supply?