Friday, June 08, 2007

8-to-1 hard mux, soft demux

The aim here is to read 8 pots into the Arduino using a single analog input and to send this data to Max/MSP. A hardware multiplexer is used for this (a 4051). Max then does software-based demultiplexing. The process consumes one analog input and three digital pins on the Arduino (plus the RX and TX pins of course).

The 4051 (in multiplex mode) has 3 control pins, 8 inputs and 1 output. The control pins set which input channel is presently routed to the output. 3 Arduino digital pins (2,3 and 4) are controlling this process. 8 pots are connected to the 8 channels on the 4051. The output of the 4051 is connected to Arduino analog input pin 0.

Max "requests" to see data from the Arduino. This is achieved by sending the byte "76" ('L' for Listen'). Upon receiving a 76 from the host computer, the Arduino goes through a loop of 8 cycles in length. Each repetition reads from a different pot and prints the data back to the host computer.

Max waits for a small amount of time, and groups 8 bytes together in a list that can then be used elsewhere. By using this "call and response" type setup, the computer knows which byte is which when it is coming out of the computer ie. it's not just a stream of never-ending numbers.

See below for the Max patch, a breadboard photo and the Arduino sketch.


byte data_in = 0;
byte data_send = 0;

int anPin = 0;

byte setPort = 0;

void setup() {

void loop() {
if(Serial.available() > 0) {
data_in =;
if(data_in == 76) {
for(int i = 0; i <= 7; i ++)
setPort = i * 4;
PORTD = setPort;
data_send = analogRead(anPin) / 4;


richard carrigan said...

This code doesn't want to upload on to my arduino:
error: expected declaration before '}' token so i don't quite know what's going wrong.

Sebastian Tomczak said...

The code got a bit stuffed up when I copied and pasted it - try it now, I think I've cleaned it up.

richard carrigan said...

is there any chance you can have a look at my code i have been posting on the arduino forum but the people that are helping me are not familiar with max.
here's the place:

I would really appreciate any help

João Menezes said...

Hi Sebastian,

I'm wondering, how can i put this code running with firmata?

João Menezes said...

I totally agree with the article.