I have spent a lot of time trying to get my 74HC4067 to work with my Arduino Mega 2560 but I have been unable to find any answers so I am resorting to creating my own post in hopes I can solve my issues.
**My goal: **
I plan to use the 4067 as a demux to change the connection from 1 device to one of 16 devices at will. Right now I just want to make a simple program that controls several different LEDs just as a beginner project so that I know how the demultiplexor works before I get into the more complicated solution I really need.
My problem:
I have been working off of examples on the internet on how to use the multiplexor, however I cannot find any tutorials on how to control LEDs with a demux on the Arduino Mega. I can only find tutorials for other Arduinos, mostly the uno. I have an uno so no big deal, I can go through the tutorial with the uno first and move to the mega. However the problem I have is that my program works perfectly on the uno but when I switch to the mega the LEDs never light up.
I have searched very hard for an answer to my issues and I am beginning to think there may be some sort of HW difference between the uno and mega that I dont understand (Im a software engineer so HW is often mysterious to me.) I can tell you however that when I moved my setup from the uno to the mega that all connections were kept exactly the same so there should be no need to change the code as far as I am aware.
The code:
very basic, just lights each LED one at a time. Again this is just so I can get a working setup and then apply it to my own solution.
/* Project: Ardu_Serie # 83
* 74HC4067 — DeMux For Arduino
* A multiplexer of this sort really just acts as a 16 to one 1 switch
*
* INO file: _83_74HC4067_multiplexer_demo_01.ino
*
* date: 8/30/19
*
* code by: https://tronixstuff.com
* hardware by: Ghttps://pt.aliexpress.com/wholesale?catId=0&initiative_id=SB_20190830133819&isPremium=y&SearchText=74HC4067+16-Channel+Analog+&switch_new_app=y
* software: Arduino IDE 1.8.9
*
* Description: Instead of upgrading your microcontroller, with this board
* you can to expand the I/O capabilities of your chosen microcontroller
*
* Visit: https://medium.com/jungletronics
*
* Tutorial: https://medium.com/jungletronics/74hc4067-demux-for-arduino-7f7892975edf
*
* License: CC-SA 3.0, feel free to use this code however you'd like.
* Please improve upon it! Let me know how you've made it better.
*/
// 74HC4067 demultiplexer demonstration (1 to 16)
// control pins output table in array form
// see truth table on page 2 of TI 74HC4067 data sheet
// connect 74HC4067 S0~S3 to Arduino D7~D4 respectively
// 5V to 74HC4067 pin 1 to power the LEDs

// 74HC4067 multiplexer demonstration (16 to 1)
// control pins output table in array form
// see truth table on page 2 of TI 74HC4067 data sheet
// connect 74HC4067 S0~S3 to Arduino D7~D4 respectively
// connect 74HC4067 pin 1 to Arduino A0
byte controlPins[] = {B00000000,
B10000000,
B01000000,
B11000000,
B00100000,
B10100000,
B01100000,
B11100000,
B00010000,
B10010000,
B01010000,
B11010000,
B00110000,
B10110000,
B01110000,
B11110000 };
// holds incoming values from 74HC4067
byte muxValues[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
void setup()
{
Serial.begin(9600);
DDRD = B11110000; // set PORTD (digital ) to outputs
}
void setPin(int outputPin)
// function to select pin on 74HC4067
{
PORTD = controlPins[outputPin];
}
void displayData()
// dumps captured data from array to serial monitor
{
Serial.println();
Serial.println("Values from multiplexer:");
Serial.println("========================");
for (int i = 0; i < 16; i++)
{
Serial.print("input I");
Serial.print(i);
Serial.print(" = ");
Serial.println(muxValues[i]);
}
Serial.println("========================");
}
void loop()
{
for (int i = 0; i < 16; i++)
{
setPin(i); // choose an input pin on the 74HC4067
Serial.println(i);
delay(500);
muxValues[i]=analogRead(0); // read the vlaue on that pin and store in array
}
// display captured data
displayData();
delay(2000);
}
Not sure what happened but it cut off my code in the original post so I added that here. It also cut off a part where I explain that I attached the connection diagram for my setup to the post. That image is actually taken from the tutorial I am using but I did the exact same thing. When I change from the uno to the mega I use the exaact same pins on the mega that I used on the uno.
If it works in the Uno but not on the Mega then it is likely that the code uses timers and interrupts. The Meag has a different internal structure to the Uno in this respect.
If you could post all your code then we would be able to see.
Edit. Your code is using direct port addressing, and the Mega is different from the Uno in this respect as well.
As you are a software person then you know that the code is very poorly written.
However, only an idiot would dream of using the 4067 to light LEDs, sure you can do it but is is not the right chip to use to get more outputs, it should only be used for getting more inputs.
The code:
very basic, just lights each LED one at a time.
That is not what the code does at all. The code reads each input one at a time. There is nothing that will set any LED, so why did you say this?
I uploaded the schematic I followed from the tutorial I used. Please see my last comment.
As for the actual wiring I will insert them below. My actual wiring is a little different at this point as I only rewired it for 1 LED since I am still just troubleshooting.
Grumpy_Mike:
If it works in the Uno but not on the Mega then it is likely that the code uses timers and interrupts. The Meag has a different internal structure to the Uno in this respect.
If you could post all your code then we would be able to see.
Edit. Your code is using direct port addressing, and the Mega is different from the Uno in this respect as well.
As you are a software person then you know that the code is very poorly written.
However, only an idiot would dream of using the 4067 to light LEDs, sure you can do it but is is not the right chip to use to get more outputs, it should only be used for getting more inputs.
Yes, I know the code is poorly written but I am just using it to verify that I can make this thing work. Why is the 4067 not the right chip to use for multiple outputs? The sparkfun description says it can be used as a mux or demux so why do you say only an idiot would use it as a demux?
The direct port addressing does seem to be the problem though so I guess I will need to figure out what I need to change to address the right ports for the mega.
“The direct port addressing does seem to be the problem though so I guess I will need to figure out what I need to change to address the right ports for the mega.”
Wow :o
Why are you using port addressing in the first place ?
@larryd - That is how every tutorial on the 4067 that I have found does it, although I am not so sure why they choose to do it that way other than just to make the code cleaner when you have to actually change the pins. I was actually going to ask if there was a good reason I have seen so many people do it this way. I have not seen anyone using pin addressing until I started trying to use this demux and its the only way I have seen examples done for this chip.
So I take it by everyones reaction there isnt actually a reason I should do it this way and I will be fine just setting the pins to out put with pinMode() and address the 4067 through the pins with digitalWrite() like I would with anything else?
Why is the 4067 not the right chip to use for multiple outputs?
Asking the question shows you don’t understand quite a lot.
When you are using it as an output then only one output is being driven at any one time. So if you want to drive a LED then only one LED will be on, the one you are addressing. All the others will be off. You can get the illusion there are many on at the same time by multiplexing, that is rapidly turning each one in turn, so quickly that the persistence of vision of the eye makes it look like they are all on at the same time. But you pay a price.
as one LED can be on at once it will only have 1/16th of the current going through it, so they will appear very much dimmer.
you will have to arrange this constant switching as part of your program, you can not stop it. This is a pain to implement and will slow dow the processor for doing useful stuff. This is stupid when you can use chips like a serial in parallel out shift register, addressable latch, or power expander chip that gives you a set and forget output.
But the big problem is that your code is NOT doing this. It is using the chip to select inputs and steer them to the analogue input in order to READ 16 analogue signals. I have mentioned this three times now, but you want to seem to ignore this very big fact. What does this say about you and your desire to learn?
I plan to use the 4067 as a demux to change the connection from 1 device to one of 16 devices at will. Right now I just want to make a simple program that controls several different LEDs just as a beginner project so that I know how the demultiplexor works before I get into the more complicated solution I really need.
Testing/learning using leds like this is fine, it might even demonstrate to you, first hand, the limitations Mike is talking about. But what is this solution you really need? We can advise if the multiplexer is suitable for that.