Monday, March 1, 2010

Mux

So having a security system that has only 6 analog inputs is kind of a bummer.  Of course the Arduino has 15 digital inputs along with the 6 analog but the way that I wanted to do the sensors was analog.

There are a couple of things that I could do to get more inputs.

One would be to upgrade to the Arduino Mega which has 16 analog and 54 digital.  This is better than my 6 and 16 and I probably could use the digital inputs for some of the sensors.  The Mega also has a lot more memory which would be cool but really don't need it (yet).  The big problem with this solution is that the Mega is about 60 bucks.

Since I already have a Arduino I am a little hesitant to go buy another even if it is bigger and better.  Fortunately you can make a few IOs into a bunch more IOs using a multiplexer ( mux for short ).  Look at the wikipedia link to see what a mux really is but basically it allows me to scrunch a bunch of input down to one or a few.  The circuit is more complicated than just reading a value on a wire but not too much so.

I went out looking for a mux.  There aren't a lot of places you can get these ICs on the web but I was able to find them for under a couple of bucks before shipping.  Add to that shipping and then I also need a board or shield to put it on with the pin connectors and I am up to at least 15 bucks.  It seemed like a big pain.

Then I came upon the Mux Shield from Mayhew Labs.  20 bucks for a shield that turns 4 digital and 3 analog IOs into 48.  It is designed to work with the Arduino and the only assembly is soldering a few headers on.

About 4 days later I got it in the mail.  The Mayhew Labs site has some example code on their site but it is so simple to use that I just took a line or two from their stuff and ripped out something a little more compact to test the board with.  Very cool, very easy.  Now I have gone from having to worry about "how I am going to access all of the sensors that I am planning" to "what can I do with all of these IOs".  I think that I will have to play with some other home automation after I finish the security system.

The code below reads inputs 16 and 17.  These are the 0th and 1st IOs on M1 which is the middle multiplexer.  Plug a couple potentiometers into these headers.  One row of headers is ground, one is Vcc and the bottom is the one that will be read.

void setup() { 
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT); 
  
  Serial.begin(9600);
}

void loop() { 
  int val = getSensorValue(16);
  Serial.print( "16:" );
  Serial.println( val );
  val = getSensorValue(17);
  Serial.print( "17:" );
  Serial.println( val );
  delay(200);
}

int getSensorValue( int sensor ) { 
  int muxId = sensor >> 4;
  int inputId = sensor % 16;
  
  digitalWrite(5, (inputId&15)>>3); 
  digitalWrite(4, (inputId&7)>>2);  
  digitalWrite(3, (inputId&3)>>1);  
  digitalWrite(2, (inputId&1));
  
  return analogRead(muxId);
}


I don't know if this is the best code but it works great.  I will post the security system code change for the new shield next post along with the change with for the real siren. 

I had to think about how to plug the sensors into the mux shield in a more secure way.  If I just plug the wires into the header like I would when I am prototyping they will probably fall out.  I probably should have ordered the mux shield without the headers on and soldered in male headers instead of the female headers. I will have to find out if he sells them that way.  Then I found some 4 pin female connectors to put on the ends of my sensor wires.  That will probably stay pretty secure.  UPDATE:  The mux shield without headers is what I was thinking it was.  If I were starting this from the beginning I might have just bought it and then soldered on the male headers.  Maybe Mark will offer a "Production Version" of his mux shield already assembled like this.

Pics of the mux shield on the Arduino, breakaway male headers and headers in the shield with a connector plugged in to input 17 or the input 1 on M1.

1 comment:

  1. hi,I need your help!!!, can you write a code to turn on and off leds on the pin 15 or 16 (M0_1 and M0_2) and another?

    ReplyDelete