Muxing Around With The CD74HC4067 + Arduino

Discussions area for bildr's blog posts.
From: http://bildr.org/2011/02/cd74hc4067-arduino/

Have you ever found yourself running out of pins to read an array of analog sensors? Don’t worry, you’re not alone, it happens to the best of us, and there is something you can do about it. An analog / digital multiplexer like the CD74HC4067 (mux for short) can help multiply the amount of pins you have, and it is insanely easy to connect to your arduino or other microcontroller
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Good article Adam. Just a thought, instead of having 'int muxChannel' as a 2D array, couldn't you have a 1d array that uses binary instead.

E.g.

uint8_t muxChannel[16] = {0b000,0b001....}

Then the code would be a lot more efficient?

Michael
micko
 
Posts: 50
Joined: Thu Jun 17, 2010 12:35 am

Very nice tutorial! I'll try it out as soon as possible :)
DrLuke
 
Posts: 364
Joined: Sun Jun 20, 2010 6:01 pm
Location: Germany

Quite possibly.
But I have had a lot of issue lately with getting single digits from the string by index. - I need to loop through them to set the pins high/low based on it.

Do you have a good way to get the digits independent based on index?

in php you can do '00100'[2] to access the 3rd character.


micko wrote:Good article Adam. Just a thought, instead of having 'int muxChannel' as a 2D array, couldn't you have a 1d array that uses binary instead.

E.g.

uint8_t muxChannel[16] = {0b000,0b001....}

Then the code would be a lot more efficient?

Michael
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

you can use simple left and right shifting for that:

yourdigit = (number & (0x01 << digit)) >> digit;
Only works with binary digits though.
DrLuke
 
Posts: 364
Joined: Sun Jun 20, 2010 6:01 pm
Location: Germany

Thank you very much for this tutorial. I am starting to play around with arduino boards and I find your tutorials basic and thorough. I wished you would have included a quick illustration of the board lightning some LEDs to clarify what you wrote at the end of the article. I am going to try this experiment with buttons and lights using two CD74HC4067. I would like to set it up so that some buttons work as memory buttons that when pressed for a couple seconds remember the LEDs that are on, and when pressed briefly it recalls the LEDs that are on and off. Ultimately I want to substitute the LEDs with some relays to control some lights. Am I in a good path or is there something that you would like to comment/recommend? Is there any tutorial that explains how can I set up/code those memory buttons? Again, thanks
tuxtlequino
 
Posts: 4
Joined: Mon Apr 11, 2011 11:33 pm

If you want to control LEDs with it you would hook it up like this. But keep in mind you can not turn on more than one LED at a time with it.

I wasn't able to really follow the idea you described. Does pressing a button turn on an led, and pressing it again turn it off?

What you would end up have is it could remember what ones were on/off, but it couldn't turn more than one on at the same time. Normally I would say you can jut switch between them so quickly you couldn't see that only LED was on, but when you move to relays it would be an issue. If you only need one on at a time, I can show you how it would be hooked up, and show you some simple code.

Screen shot 2011-04-12 at 8.03.07 AM.png
Screen shot 2011-04-12 at 8.03.07 AM.png (75.61 KiB) Viewed 18869 times




What kind of lamp are you wanting to turn on with the relay?




tuxtlequino wrote:Thank you very much for this tutorial. I am starting to play around with arduino boards and I find your tutorials basic and thorough. I wished you would have included a quick illustration of the board lightning some LEDs to clarify what you wrote at the end of the article. I am going to try this experiment with buttons and lights using two CD74HC4067. I would like to set it up so that some buttons work as memory buttons that when pressed for a couple seconds remember the LEDs that are on, and when pressed briefly it recalls the LEDs that are on and off. Ultimately I want to substitute the LEDs with some relays to control some lights. Am I in a good path or is there something that you would like to comment/recommend? Is there any tutorial that explains how can I set up/code those memory buttons? Again, thanks
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Again, thank you very much for the response.

I was disappointed to hear that you could only control a light at a time. May be you can keep them going with a capacitor and a transistor? (I am REALLY really new at this. In fact, I am remembering the transistor and capacitor part from a high school class many, many, many years ago.)

We have a light panel in our church. It is a pretty old system that uses simple on/off switches. Those switches control several lights in the auditorium, and platform. I would like to improve the panel by adding memory buttons. In that way we could add "scenes" and instead of flipping switches on and off after every part of our service we could just press a "scene" and get the lights we need. The panel has 16 switches, (16 inputs to control 16 outputs "relays") but I would also like to have visual feedback to know which lights are on or off (Another 16 outputs to light some LEDs); plus the "scene" or "memory" buttons (another 4 or 6 inputs and outputs). Is there a better way to accomplish this than to get lots of arduino boards (or a cheaper alternative such as the TI Launchpad)?

Sincerely,
Victor Gutierrez
tuxtlequino
 
Posts: 4
Joined: Mon Apr 11, 2011 11:33 pm

The easiest way to do this would be to use this as the input - for the switches.

The output I would use this: http://bildr.org/2011/02/74hc595/
The arduino code allows you to control individual pins.

The downfall with this is that the outputs aren't strong enough to drive a relay on their own so you would want to smal transistors on them to boot the power output.

Do you know any of the information on the relays currently being used? Voltage or trigger amperage?

This way you could also easily set up scenes in the code as an array like so:

Code: Select all
bool scenes[4][16]={
    {0,0,0,0,0,1,1,1,0,1,1,0,0,1,0,1}, //scene 0
    {1,0,0,0,0,1,1,1,0,0,1,1,1,0,1,1}, //scene 1
    {0,1,0,0,0,1,1,1,0,0,0,0,1,1,1,0}, //scene 2
    {1,1,0,0,0,0,1,0,0,0,1,0,0,0,1,1}, //scene 3
}




tuxtlequino wrote:Again, thank you very much for the response.

I was disappointed to hear that you could only control a light at a time. May be you can keep them going with a capacitor and a transistor? (I am REALLY really new at this. In fact, I am remembering the transistor and capacitor part from a high school class many, many, many years ago.)

We have a light panel in our church. It is a pretty old system that uses simple on/off switches. Those switches control several lights in the auditorium, and platform. I would like to improve the panel by adding memory buttons. In that way we could add "scenes" and instead of flipping switches on and off after every part of our service we could just press a "scene" and get the lights we need. The panel has 16 switches, (16 inputs to control 16 outputs "relays") but I would also like to have visual feedback to know which lights are on or off (Another 16 outputs to light some LEDs); plus the "scene" or "memory" buttons (another 4 or 6 inputs and outputs). Is there a better way to accomplish this than to get lots of arduino boards (or a cheaper alternative such as the TI Launchpad)?

Sincerely,
Victor Gutierrez
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

That solution sounds great. It sounds as if I will have to use two CD74HC4067 to be able to control the 16 inputs, plus the "scene" inputs; and four daisy chained 74HC595. Two of those 74HX595 will be to control the LED feedback, and the other two to control the Relays. Am I correct? Can a single Arduino "Uno" pull this off?

I like the idea of controlling the scenes as arrays in the code, but I would still like to give the operator the ability to program "scenes" to his/her liking. May be this can be easily accomplished be keeping track of the inputs in an array that can then easily be copied into a "scene" array whenever desired. Would this convolute the code beyond what arduino can do?

With regards to the relay, I was looking at the SparkFun relay control PBC http://www.sparkfun.com/products/9096 . I do not know if I will still need a transistor to use that board. The sparkun board is something I found while googling for possible answers. I am not 100% percent sure is the best fit for my needs.

Thank you so much for taking the time to answer my questions and guide me through the process. If I end up doing this, I will document the whole ordeal for you.

Sincerely,
Victor Gutierrez
tuxtlequino
 
Posts: 4
Joined: Mon Apr 11, 2011 11:33 pm

I think you just need one mux for the 16 inputs... Or are you thinking you would have 16 controllable lights, and 16 scenes you can trigger as well?

You only need 2 of the shift registers, the same ones can be used to light up the LEDs and switch the relays, that is unless you want to be able to program the lights, see the feedback on LEDs then once you have it right, switch the relays. If that is the case, you may want 4 of the 595s.

An arduino can definitely handle all of this.

As for saving the scenes, that is the harder part. You will need to be able to save them to the eprom onboard the arduino, or some sort of memory. Doable, I just dont know how off the top of my head.

You will want it to be easy to save them interface wise, otherwise it wont be used. (seen it happen too many times)

Do you have a time in mind for this? There is a chance that sparkfun is coming out with a board that would make the shift registers easier. But it might take 2 months.

Are there relays currently being used? If so, I wouldn't replace them, as they are probably rated correctly for what they do. That relay board seems like it may not be good for more than 7-8 amps based on the comments. And lights, especially scene lights can be very high power.

But if there are existing relays you then only need to interface with them.

Depending on power needs you may want to do something like this: (NOT TO SCALE :D )
Screen shot 2011-04-13 at 12.12.26 AM.png
Screen shot 2011-04-13 at 12.12.26 AM.png (54.28 KiB) Viewed 18847 times
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

I can wait for sparkfun's new board. I noticed that you used those transistors mentioned in one of your blog posts; but in your blog post you said those were for DC current only. Were you just trying to illustrate how to do it? Where can I find recomendations about the kind of transistor that I need?

Sincerely,
Victor Gutierrez
tuxtlequino
 
Posts: 4
Joined: Mon Apr 11, 2011 11:33 pm

The relays are switched via DC current coming from the transistors. They then control an AC source.

The relay is just a physical switch that is controlled by an electro magnet.

The transistor you need is dependent on the relay, but these would work even for some of the larger relays.

I just happened to have half the illustration done for something else, so I just put it together. But it would work.


tuxtlequino wrote:I can wait for sparkfun's new board. I noticed that you used those transistors mentioned in one of your blog posts; but in your blog post you said those were for DC current only. Were you just trying to illustrate how to do it? Where can I find recomendations about the kind of transistor that I need?

Sincerely,
Victor Gutierrez
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

I have a question regarding the setup of the mux. I have set it up the same way as te tutorial but was wondering if I could make the readings a little more stable.

Mux0 - 391.00 -- Analog0 value: 394
Mux0 - 359.00 -- Analog0 value: 352
Mux0 - 343.00 -- Analog0 value: 350
Mux0 - 334.00 -- Analog0 value: 327
Mux0 - 352.00 -- Analog0 value: 354
Mux0 - 330.00 -- Analog0 value: 330

It seems it is all over the place. Any ideas? thanks
cbabilotte
 
Posts: 4
Joined: Mon Jun 20, 2011 1:56 pm

are you measuring the values at the very exact same time? If not, it could be that the sensor's output (or whatever it is) is jittery and thus causing differences in measured value.
DrLuke
 
Posts: 364
Joined: Sun Jun 20, 2010 6:01 pm
Location: Germany

yes the Mux0 and Analog0 are taken at the same time. The sensor hooked up on Mux us a LM335A. If I hookup the temp senor directly to Analog0 the values are a lot more stable and less jittery.
cbabilotte
 
Posts: 4
Joined: Mon Jun 20, 2011 1:56 pm

It could just be the extra length in the line causing it. But that seems like a lot of fluctuation.

What of you try it without the mux, but use a long wire to the analog pin just to see if it is the extra length.
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Here are the values from Analog0

Analog0 value: 602
Analog0 value: 602
Analog0 value: 602
Analog0 value: 602
Analog0 value: 602
Analog0 value: 602

so without the mux the value is pretty constant.
cbabilotte
 
Posts: 4
Joined: Mon Jun 20, 2011 1:56 pm

Does the output on the Mux actually jitter when when the value on the analog pin remains constant?
DrLuke
 
Posts: 364
Joined: Sun Jun 20, 2010 6:01 pm
Location: Germany

Or if you tie the input to vcc/gnd ?
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Hi;
I wanted to try using the SparkFun Mux board to write from an Arduino rather than read and decided the easiest way to learn how to do this was to use the 'turn-on leds' example (sorry!) and mod. the sample code you provided - but I couldn't make it work.

Could you possibly put up some sample code to demo. the Mux-write option as well as the Mux-read?

Cheers;
Dave
hassydirekt
 
Posts: 2
Joined: Sun Jul 10, 2011 8:50 pm

Yeah, the code is basically completely the same.

All the mux is, is a switch that connects one pin (SIG) to one of 14 other pins (the channels).

So we will connect SIG to digital 3 for this code, and then anything connected to the mux will be controllable via digital pin 3.

Code: Select all

//Mux control pins
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;


void setup(){
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
 
  pinMode(3, OUTPUT); //digital pin 3

  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);

  Serial.begin(9600);
}

void loop(){

  digitalWrite(3, HIGH); //turn digital 3 HIGH (on)
  switchMux(5);//switch the mux to channel 5
  //So now digital 3 on the arduino is connected to channel 5 on the mux

  delay(250); //just to hold it for now

  switchMux(12);//switch the mux to channel 12
  //So now digital 3 on the arduino is connected to channel 12 on the mux
 
  delay(250); //just to hold it for now

  switchMux(3);//switch the mux to channel 3
  //So now digital 3 on the arduino is connected to channel 3 on the mux
 
  delay(250); //just to hold it for now

  //and so on ...


}

void switchMux(int channel){
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4]={
    {0,0,0,0}, //channel 0
    {1,0,0,0}, //channel 1
    {0,1,0,0}, //channel 2
    {1,1,0,0}, //channel 3
    {0,0,1,0}, //channel 4
    {1,0,1,0}, //channel 5
    {0,1,1,0}, //channel 6
    {1,1,1,0}, //channel 7
    {0,0,0,1}, //channel 8
    {1,0,0,1}, //channel 9
    {0,1,0,1}, //channel 10
    {1,1,0,1}, //channel 11
    {0,0,1,1}, //channel 12
    {1,0,1,1}, //channel 13
    {0,1,1,1}, //channel 14
    {1,1,1,1}  //channel 15
  };

  //loop through the 4 sig
  for(int i = 0; i < 4; i ++){
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }
}






hassydirekt wrote:Hi;
I wanted to try using the SparkFun Mux board to write from an Arduino rather than read and decided the easiest way to learn how to do this was to use the 'turn-on leds' example (sorry!) and mod. the sample code you provided - but I couldn't make it work.

Could you possibly put up some sample code to demo. the Mux-write option as well as the Mux-read?

Cheers;
Dave
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Many thanks - now I see what I was doing wrong (Duh!).
Cheers;
Dave
hassydirekt
 
Posts: 2
Joined: Sun Jul 10, 2011 8:50 pm

No problem!


hassydirekt wrote:Many thanks - now I see what I was doing wrong (Duh!).
Cheers;
Dave
ameyer
Founder
 
Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

I have fix my issue. The issue was a loose connection. I now get constant values from the mux which is great. Thanks for all your help.
cbabilotte
 
Posts: 4
Joined: Mon Jun 20, 2011 1:56 pm

Next

Return to Blog Posts

Who is online

Users browsing this forum: No registered users and 0 guests