Sunday, 10 July 2016

Arduino MIDI Controller: Multiplexers

More, More, More!!


Over the last couple of tutorials we have been building up a MIDI Controller using a trusty Arduino UNO board. So far, we've learned how to connect buttons and potentiometers to the controller and generally had a lot of fun.

"But Dave!! I want to build a custom controller with a zillion pots and buttons! The UNO only has six measily analog inputs and a handful of digital ports! What can I do????"

Never fear! We can add lots of controls to our project with the help of Multiplexers.

Watch This!



What is a Mux?


A Multiplexer is a chip with multiple inputs that can be individually connected to a single output. By sending different combinations of signals to some control pins, each button or pot connected can be read by a single pin on the Arduino. The video explains this in detail.

We will be looking at two popular multiplexers in this lesson. The first is the 74HC4051 which has 8 inputs. The second is the 74HC4067 which has a impressive 16 inputs.

The Software


The Midi Controller program we have used in previous lessons can handle either type of multiplexer with some simple configuration. The video will show you how. Download the software HERE if you don't have it yet.

Stuff you need (With Amazon links)


Here are the parts you will need to do the experiment:

Arduino UNO or Arduino UNO (Official Adafruit Version)

2 x 10K Potentiometers (Linear taper) - Inexpensive
or model used in video

2 x Push Button Switch

74HC4051 Multiplexer (8 Input)
74HC4067 Multiplexer (16 Input)

Breadboard (63 pin width)

MIDI Jack

2 x 220 Ohm Resistors

Jumper Wires

MIDI Cable




106 comments:

  1. Hi!
    How to install leds for the buttons, if i want that when I push the button the led is lightning?
    Thanks You! :)

    ReplyDelete
    Replies
    1. I also have this question!

      Delete
    2. Buttons are when active connected to GND, so you need to add a small inverter parallel to the button. Search for 'transistor inverter with led'

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. How do I put 2 74hc4067? They are connected in series or parallel?

    ReplyDelete
  4. I can not use more buttons in ... Y2... Y7 imputs. I need to change something to use only buttons?

    ReplyDelete
  5. I used buttons Yo, Y1, Y2, Y3 but not get in the other Imputs.

    ReplyDelete
  6. Can I use Analog and Digital at the same time with this Program?

    ReplyDelete
  7. Hi. Many Tks for this blog and this prog.
    May I share my experiment.
    Yes you can use analog and digital at the same time but it has to be one DEMUX board for each.
    I have manage to use up to 3 full DEMUX board but the arduino won't cope a fourth full used board due to lack of memory.
    My project is a 30 fader midi interface for a lighting program.
    RGDS.

    ReplyDelete
  8. FYI modified Controller.cpp for the MEGA on pin 22-25:
    #if defined(__AVR_ATmega2560__)
    void Button::muxUpdate()
    {
    // set S pins on MUX in correct value to read value : eg read Y2: S2-S1-S0 = B010
    // pins used on MEGA: 22 (PA0), 23 (PA1), 24 (PA2), (25 (PA3))
    // set MUX S-pins LOW
    if (_numMuxPins > 8) PORTA = PORTA & B11110000;
    else PORTA = PORTA & B11111000;

    // add value of temp to PORTA (= add value of the required channel, so set the correct pins)
    PORTA = PORTA | _muxpin;
    }

    #elif defined(__AVR_ATmega328P__)

    void Button::muxUpdate()
    {
    byte temp = _muxpin;
    temp = temp << 2;
    if (_numMuxPins > 8) PORTD = PORTD & B11000011;
    else PORTD = PORTD & B11100011;
    PORTD = PORTD | temp;
    }
    #endif

    ReplyDelete
    Replies
    1. How can i use it?

      Delete
    2. I guess by replacing (in Controller.cpp):

      void Button::muxUpdate()
      {
      byte temp = _muxpin;
      temp = temp << 2;
      if (_numMuxPins > 8) PORTD = PORTD & B11000011;
      else PORTD = PORTD & B11100011;
      PORTD = PORTD | temp;
      }


      WITH:

      #if defined(__AVR_ATmega2560__)
      void Button::muxUpdate()
      {
      // set S pins on MUX in correct value to read value : eg read Y2: S2-S1-S0 = B010
      // pins used on MEGA: 22 (PA0), 23 (PA1), 24 (PA2), (25 (PA3))
      // set MUX S-pins LOW
      if (_numMuxPins > 8) PORTA = PORTA & B11110000;
      else PORTA = PORTA & B11111000;

      // add value of temp to PORTA (= add value of the required channel, so set the correct pins)
      PORTA = PORTA | _muxpin;
      }

      #elif defined(__AVR_ATmega328P__)

      void Button::muxUpdate()
      {
      byte temp = _muxpin;
      temp = temp << 2;
      if (_numMuxPins > 8) PORTD = PORTD & B11000011;
      else PORTD = PORTD & B11100011;
      PORTD = PORTD | temp;
      }
      #endif



      Delete
    3. And then placing the mux S0 to S3 on pin: 22, 23 , 24 , 25

      I'll let you know for sure after I get my mega in the mail :P

      Delete
    4. hey know how to change the controller.cpp for 32u4 micro much appreciated ?

      Delete
    5. I used it but the multiplexers don't work correctly. I think that I nedd more changes in the code, isn'it?. Have you the code for mega?. I need this for a 9 multiplexers with 79 buttons and 24 pots! 4 works properly in the Arduino UNO, but I need more memory for more.

      Delete
    6. Dave, your multiplexer tutorial it's fantastic! I used it for a couple of projects!

      Delete
    7. you need changes in controller.cpp :
      //from this
      Mux::Mux(byte outpin_, byte numPins_, bool analog_)
      {
      outpin = outpin_;
      //enablepin = enablepin_;
      numPins = numPins_;
      analog = analog_;
      if (analog == false) pinMode(outpin, INPUT_PULLUP);
      //pinMode(enablepin, OUTPUT);
      pinMode(2, OUTPUT);
      pinMode(3, OUTPUT);
      pinMode(4, OUTPUT);
      if (numPins > 8) pinMode(5, OUTPUT);
      }


      //to this
      Mux::Mux(byte outpin_, byte numPins_, bool analog_)
      {
      outpin = outpin_;
      //enablepin = enablepin_;
      numPins = numPins_;
      analog = analog_;
      if (analog == false) pinMode(outpin, INPUT_PULLUP);
      //pinMode(enablepin, OUTPUT);
      pinMode(22, OUTPUT);
      pinMode(23, OUTPUT);
      pinMode(24, OUTPUT);
      if (numPins > 8) pinMode(25, OUTPUT);
      }

      Delete
    8. fantastic thanks

      Delete
    9. I don't understand why these changes are needed. Can someone explain it ?

      Delete
    10. Diffrent kind of arduino...the base project is uno but lack of memory then many others try with mega as they has more i/o pin and flash memory...just express my thought...please correct me if im wrong

      Delete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Thanks for this well-explained video tutorial! I don't know enough about C to figure out whether your controller library can be modified to allow "coarse" (7-bit) NPRN parameters to be assigned to pots / buttons on this shield project.

    However, it seems if an NRPN flag is added to the "command" variable, and if the "note number" / "byte command" variables can hold a 14-bit value for the number of the NRPN parameter to change, then a routine could be added to issue the 9 bytes that make up the NPRN parameter change command (CC / #99 / MSB of NPRN parameter / CC/ #98 / LSB of NPRN parameter / CC/ #6 / 7-bit parameter value to send).

    Is this possible?

    ReplyDelete
  11. Thanks notesandvolts for this tutorial! and thanks again!

    ReplyDelete
  12. hi, i dont understand why but i can't manage to get more pots on multiplexer going right. I use Arduino atmega2560 i did everything as in the tutorial and when i connect more than one pot to it, change program, and suddently it does not work. The first one what was working like a charm when was single on multiplexer after connecting second one start giving random CC number and channel too, the second one does not react at all.

    ReplyDelete
    Replies
    1. Hi Neytal Dub,

      Kind of have the same problem with the Arduino Uno. The Mux seems to send a lot of noisy data. Did you manage to solve that issue ? Dave, have you ever encountered such situation ?

      Thank you for the great tutorials btw !

      Delete
    2. Hi,
      I have the same issue with the signal out of the multplexer. I don't know how to correct it. I tryed resistors IN or OUT, nothing works.
      I have 2 midi signals, one when I push and one when I released the button. I don't understand that too.
      If you have informations, I take !

      Thanks a lot for the tutorials Dave

      Delete
    3. me pasa lo mismo con y sin el multiplexor ayuda !!

      Delete
  13. This has been the tutorial I have dreamed of for midi on arduino. The only issue I'm having is it doesn't appear the arduino is storing the sketch. I can upload the sketch and it functions correctly. Once I disconnect the USB cable and reconnect, the sketch does not boot. Google says I have a bad bootloader, however any arduino example sketch uploads and stores like normal. The program doesn't appear to be too big. What am I missing? Thanks in advance and love the channel!

    ReplyDelete
    Replies
    1. Fácil de solucionar ese problema, tienes que convertir tu Arduino en un dispositivo HID, BUSCALO EN GOOGLE 'HIDuino' , a mí me funciona excelente.

      Delete
  14. Hi, i wonder if there is a way how increase read of difference between old and new value on pots. So the pot will not react on small changes. the thing is that i have used cca 30 pots on mixconsole but some of them keep sending messages with very small change. I would like to make all of knobs bit less sensitive so when i want to learn midi knob in software it would not get milions of noise signals. I belive that has to do something with sheet controller.cpp but i am not sure witch part is it. Thank you for any idea

    ReplyDelete
  15. hi dave,
    i'm using 27 potentiomete and 16 buttons to arduino uno
    but how can connect 27 pots via two 4067 mux and 16 buttons via 4067 mux
    i'm making to control vdj 8.2

    ReplyDelete
  16. Hi Dave, I would like to know if it is possible to connect several multiplexers at the same time, and what modifications would have to be made apart from the array ... I would like to connect as many as possible because I would like to make a midi mixing console

    ReplyDelete
  17. Dalek DIY Arduino MIDI floor keyboard

    Totally based on Notes and Volts awesome work:
    http://www.notesandvolts.com/…/midi-and-arduino-circuit-ana…
    http://www.notesandvolts.com/…/midi-for-arduino-build-midi-…
    http://www.notesandvolts.com/…/arduino-midi-controller-pote…
    http://www.notesandvolts.com/…/arduino-midi-controller-butt…
    http://www.notesandvolts.com/…/arduino-midi-controller-mult…

    Features:

    - Chromatic footswitches (12 notes)
    - Latching retro illuminated sustain footswitch sends MIDI CC 64 and lights when active
    - Latching retro illuminated modulation "wheel" footswitch sends MIDI CC 1 and lights when active
    - External footswitch jack can receive 1 or 2 external non latching button pedals tip to gnd and ring to gnd (mode selectable via internal switch) programmable via Arduino sketch to send any MIDI message
    - External expression pedal jack programmable via Arduino sketch to send any MIDI message
    - Power ON footswitch
    - Power ON LED
    - TX activity LED lights when MIDI messages are sent
    - DuPont jumper cables to Arduino pins and 2.1 DC plug from ON switch to Arduino DC jack allow to extract the board for easy reprogramming.

    Recipe: https://drive.google.com/drive/u/1/folders/148cmGwMxRVZijUVTqGz-qSvRcxxbOWm9?usp=drive_open

    ReplyDelete
  18. hi. im new in arduino and i want to make midi control. i gave arduino mega and i want to make full digital and full analog midi control. can i make full 54 button and 16 fader from arduino mega without multiplexer?
    can i use your sketch for it?
    thankyou and i really hope you replay as son as posible.

    ReplyDelete
    Replies
    1. i have te same question and how to write the code then?

      Delete
    2. It is not possible without using multiplexer.
      We use multiplexer to extend the analog and digital inputs to arduino

      Delete
    3. but its only use the available digitial and analog pin of arduino mega, not more.

      Delete
  19. hoe gebruik ik de arduino mega met 3 analoge multiplexers met faders en 4 digitale multiplexers met knoppen met deze code als dat kan met deze code
    en bedankt voor de mooie tutorials

    ReplyDelete
  20. Hello Excellent !!!!
    But could I send PC midi messages with buttons (multiplexed or not) ?

    ReplyDelete
  21. Is the code compatable with HIDUINO??

    ReplyDelete
  22. Hey , I have a question , if I want to use this with an arduino MEGA2560 would I need to change something else other than the pots , buttons or mux inputs on the code?? is there a separate config for other arduino boards other than the UNO??? thanks

    ReplyDelete
  23. Excellent piece of code thanks. Have used it on an UNO and later on a Mega because my project required 14 pots and thirty something buttons so did not need to use multiplexers.

    My project kinda grew and now I need to use three multiplexers for buttons so the burning question is..

    How do we tweak this code to use more multiplexers?

    ReplyDelete
  24. Hi Dave,fantastic code.I have managed to modify it to work Buttons with ARDUINO MICRO but can't figure out how to get MULTIPLEXER to work with ARDUINO MICRO.I have spent countless hours on this, and have learnt a lot,but have now reached the end of the line. Please help.
    Thanks

    ReplyDelete
  25. problema con el Arduino Uno. El Mux parece enviar una gran cantidad de datos ruidosos. ¿Se las arregló para resolver ese problema? Dave, ¿alguna vez te has encontrado con tal situación?

    ReplyDelete
  26. has anybody had any luck converting the controller cpp so this will work with a pro micro 32u4 ? any help much appreciated

    ReplyDelete
  27. Has anyone managed to tweak the code to work with a Teensy 2.0? Any help greatly appreciated.

    ReplyDelete
  28. 10*12 matrix circuit and code in program change

    ReplyDelete
  29. I found the AS6408 65 channel multiplexer board that can integrate with a minatureised "CORE" duino.

    This gives incredibly easy hardware build to access 64 analoglines. used this guide and some others to build an analog input desk for QLCplus lighting controller.

    ReplyDelete
  30. Hi. I have built using the 74HC4051N multiplexer from Philips. I am trying to attach 6 buttons but i can only get signal from pins Y4 and Y0.

    ReplyDelete
  31. I want to use it in Arduino pro micro and use it with midi over usb. Is there any chance with the program?

    ReplyDelete
  32. I built a Midicontroler with 4051 multiplexers, switches and buttons.
    Works very well.
    Now I want to add a potentiometer that sends expression on 8 channels simultaneously.
    Can I define a button or potentiometer more than once?

    ReplyDelete
  33. I'm interested in using this set up to detect electrochemical DNA changes. Can the inputs be used to pick up small changes in electrical potential?

    ReplyDelete
  34. Hello there,
    Please can you help me. Yes I read, it's only for UNO. I built a project with 6 16 port mux. but the memory isn't enough and I also need another 8 channel pot mus to configure.
    It will load on a mega but the multiplexers dosn't function.

    My project is building an Pianolorollplayer over MIDI.

    What can I do?
    GRTZ KOOS

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  35. https://www.youtube.com/watch?v=je3EDVs8Fno No need so many multiplexer for buttons, the correct way is a MATRIZ " look the video.

    Delete

    ReplyDelete
  36. hi thanks for the tutorial. i'm working on a controller with mega2560 6button 31pot, but when i connect 16channel mux the controller doesnt recieve any message from the pot. can you help me ?

    ReplyDelete
  37. Is it possible to change the sketch in order to make it work via USB?

    ReplyDelete
  38. When I add too many buttons I get the message

    "Low memory available, stability problems may occur."

    I need 376 buttons for a microtonal controller. Will adding too many buttons create latency? Can I store variables in other places?

    ReplyDelete
  39. ¿Cual es el codigo para el arduino mega?

    What is the code for the arduino mega?

    ReplyDelete
  40. is it possible to use 3 multiplexer in a single Arduino board 1 mux for digital output and 2 mux for analog input ?? if yes, how can i use them ??
    thank you

    ReplyDelete
  41. Hi, the tutorial is very good and detailed, but I can't compile it. The download code here is different from that in the video. I don't know how to modify it

    ReplyDelete
  42. Hi, the tutorial is very good and detailed, but I can't compile it. The downloaded code is different from that in the video. I don't know how to modify it. Do you have the same code in your video?

    ReplyDelete
  43. Hello, I am using an Arduino Nano and 4051 mux but only 1 input button is triggered at a time. Do anyone knows what is the reason?

    ReplyDelete
  44. saludos! ¿Cómo incluir 2 encoder ky-040 al programa?

    ReplyDelete

  45. hello! How to include 2 encoder ky-040 to the program?

    ReplyDelete
  46. The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought you have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

    Habotao.com
    Information
    Click Here
    Visit Web

    ReplyDelete
  47. Excellent post. I enjoy reading and also appreciate your work. This concept is a good way to enhance knowledge about bottle cap torque tester suppliers. Keep sharing this kind of article. Thank you.

    ReplyDelete
  48. I regularly frequented your website. This article makes several thought-provoking historical points, albeit I'm not sure I understand them completely. There is some truth to it, but I'll reserve judgment till I do more research. Thank you for the good article; we need more!

    ReplyDelete
  49. Can we have the sketch shown in the video

    ReplyDelete
  50. The Manhattan district attorney’s office questioned the city’s buildings chief as 점보카지노 part of of} an investigation into illegal playing. The Commission added that the inclusion of casinos on the EU-wide AML/CFT framework has had a “mitigating effect” on the chance of cash laundering at these venues. At Algamus, we imagine that a playing dysfunction must be handled with the identical urgency and seriousness as a chronic bodily health concern. If you imagine that you just or a liked one is fighting a playing dysfunction, please reach out to our playing counselors, and we may help you identify the next best steps to take.

    ReplyDelete
  51. i hope you will add Program Change on command
    thank you notes and volts very educational

    ReplyDelete
  52. is it possible to write a simple code whith changes to use for arduino mega? i tried this code on a mega whith the same build of muxs and pots but the code does not work, the same build on uno works fine. could not find na answer in the internet

    ReplyDelete