Forums > MaxMSP

Arduino SensorBox firmware (with 4051 support!)

November 21, 2009 | 1:16 am

Having beat my head against the wall enough times with Firmata in educational environments, I put together a fairly simple Arduino firmware called SensorBox to just grab all the inputs, pack them up into a nice tidy bundle, and send them on to Max. On the Max side, every effort was made to avoid special voodoo, file dependencies, or obfuscation.

For those who need more than 6 analog inputs (who doesn’t?), I also made a configurable version that allows you to interface a 4051 8×1 multiplexer chip to the analog pins, offering up to 96 possible inputs.

have fun.

Andrew B.

December 8, 2009 | 4:33 pm

Is there some sort of schematic for how to connect the multiplexer? Sounds interesting…

December 8, 2009 | 7:26 pm

Hi Stefan,

this may help you:


December 8, 2009 | 8:42 pm

The link Patrick gave should get you going. The 4051 chip is really easy to get connected, and just requires 3 I/0 pins. If you want to configure it to use multiple 4051 chips, you can control all of them with the same IO.


December 9, 2009 | 3:59 pm

Great! I’m going to use this in a project right now. Will be sure to report any problems/suggestions I have.

June 25, 2010 | 9:42 am

this is hot like summer andrew. Got it workin in no time flat….
Keep up the great work,
(alot of Max/MSP work in my portfolio)

April 18, 2011 | 1:58 pm

Hi Andrew, I think I was supposed to post here. Sorry about multiple posts – i cant delete the old one :-(

I am trying to hook up four 4051 multiplexers to use with your Sensorbox but wanted to check that I have the below code right, to get all four muxes in via the Arduino A0, A1, A2, A3 analog channels. Is this correct? Many thanks in advance.

//select analog channels to multiplex
char is_muxPIN[6] = {1,1,1,1,0,0};//This is the only line of code I need to // change; for A0, A1, A2, A3 analog pins hosting four muxes
char muxcount;//variable to store the number of multiplexed inputs

August 25, 2012 | 8:00 pm

Is the data coming in supposed to only be 7bit? I’m only getting values from 0-127 on my first three analog ins and my second 3 inputs don’t move at all (constant reading of ’43’ or something in that range).

Also is there a way to use this with i2c analog ins?

After (literally) months of banging my head against a wall trying to get a consistent and fast connection going (the last approach was using call-response, which kept breaking) this seems to work bang on (much better than my other approaches), but I’m having a hard time adapting my code to this (or vice versa).

I’m rocking a 9DOF setup with 3axis accel (pins 0, 1, 2), 3axis gyro (pins 6, 7, 3) and 3axis magnetometer (i2c). No digital I/O (other than an onboard LED for troubleshooting purposes).

Here’s my attempted adaptation:

// i2c Arduino Library

// 0011110b, i2c 7bit address of HMC5883
#define address 0x1E

int AccX, AccY, AccZ, GyroX, GyroY, GyroZ, MagX, MagY, MagZ;
char finish = 255;

#define aX A0
#define aY A1
#define aZ A2
#define gX A6
#define gY A7
#define gZ A3
#define LED1 5

void setup() {
//set LEDs as output pins
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);

// Start serial port at 57600 bps:

// Start i2c communication

// Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode

// Send a byte to establish contact until receiver responds
while (establishContact()==0){delay(100);} //wait for 99 byte


void loop() {
// Tell the HMC5883 where to begin reading data
Wire.write(0x03); //select register 3, X MSB register

AccX = analogRead(aX);
AccY = analogRead(aY);
AccZ = analogRead(aZ);
GyroX = analogRead(gX);
GyroY = analogRead(gY);
GyroZ = analogRead(gZ);

// Read data from each axis of magnetometer, 2 registers per axis
Wire.requestFrom(address, 6);
if(6< =Wire.available()){
MagX =< <8; // X msb
MagX |=; // X lsb
MagZ =< <8; // Z msb
MagZ |=; // Z lsb
MagY =< <8; // Y msb
MagY |=; // Y lsb
// }

// Send sensor data out
digitalWrite(LED1, HIGH);

// Accelerometer X

// Accelerometer Y

// Accelerometer Z

// Gyroscope X

// Gyroscope Y

// Gyroscope Z

// Magnetometer X

// Magnetometer Y

// Magnetometer Z


if(establishContact()==1) {
while(establishContact()==0) {delay(100);}//go into idle mode

char establishContact(void){
if (Serial.available() > 0) {
char checkup =;
if (checkup==99) return 1;
else return 0;
else return 0;

Viewing 8 posts - 1 through 8 (of 8 total)

Forums > MaxMSP