I wish to connect 32 switches to 5 inputs on a model train control setup.
The model train setup will decode the Binary.
I have wired up 2 cd4067 multiplexers with the data pins cascaded, the EN is permanently enabled and the Sig pins are wired seperately.
I was hoping to use an arduio nano to read the muxes and output a steady 5 bit data while one button is pressed (Only one button is pressed at a time).
I have tested one mux to nano with Adam Meyers code and have found several samples of multiple mux panning but as my Arduino programming skills are minimal at best I don't know how to go about converting the received data to an output on the 5 Binary pins.
Any helpful suggestions or code appreciated. I am using these components as I have them on hand.
Paul Andrews
What kind of signals are on the "switches"? What device(s) produce them? Do you mean electrical, or track switches?
1 person out of 10,000 here knows what Adam Meyers code is, you need to post links for things like that...
You did not post any code or schematic. That is "the end of the line, last station stop". ![]()
The switches are momentary push button. The output is only required while the button is pressed.
Adam Meyers code scans 16 switches on one mux and gives an indication on the serial monitor.
I tried unsuccessfully uploading a photo of my drawing but all that came up was a link.
I am sorry but there is no code as I am trying to get direction of how to proceed.
It's a good thing, because multiplexers are a poor way to read pushbutton switches. Please post complete hardware information, in order to get the best advice.
There is a delay while it loads. You probably cancelled out too soon.
That only shows a small portion of the mux circuit, and none of the hardware that it interfaces with. It looks basically okay, but again, usually a mux is a dumb way to read switches.
I asked to see the switch wiring so that I could suggest an alternative method. Others might just help you with the mux, so you should do it anyway, even if you reject my suggestion.
...even if you only show one switch out of the many, exactly how it is wired...
Switches are digital, you are using an analog mux.
We don't write such complicated code examples for members here, we only assist with code that they have themselves written, or modified. Of the few that might, probably none will do it without complete hardware information.
As I mentioned, I had these components on hand but am willing to consider alternatives.
The switched are wired to Gnd wirh pullups via resistor to Vcc. The oputput to the Train controller is 0 or 1 (0/5v)
I have a lot of useless parts. You wouldn't believe how many. I never allow them to steer my designs unless I'm truly desperate...
Most people here don't accept verbal hardware descriptions. Please document the switches properly.
32 switches can easily be read with something like a set of shift register IC's or I2C port expanders. The expanders are also available as modules, which might be helpful.
The thing is, a solution like the one you found is good in so far as it works. Because then you don't really need to know how it works. But what about when it doesn't work? Then what you need to do is use what you know, or learn what you need to know. But the bonus, you get to choose ways that are better for your application. The mux was a really good idea in the 1970-1980's, dwindling off as the primitiveness of the available ICs improved. It was a good hack but it's mainly only now perpetuated by copying.
Model railroad electronics isn't very standardized, which is not surprising given that it isn't the real emphasis of the hobby. But something like 12V relay logic from the old days, has been successfully merged with modern systems in many de facto standards for layouts. Too often, someone from that background stumbles into the digital realm and misses some cues (I'm not referring to you). 5V logic should be fine, but if the switches are any distance from the controller, you should also have EMI filtering and host the pull up resistors at the controller.
So how far away are the switches, do they run adjacent to other wiring, and what budget do you have for wire? Well the last question I'm just kidding about... there is debounce and encoding to consider, even without any mux.
How proximate are the switches to each other? If they are near, you could matrix read them, or Charlieplex them as mentioned before. A Charlieplex can works as a "bus", so for example a long set of 6 wires, that would have switches bridged to it anywhere along the bus. That would get you a lot of switches, I forget the formula for calculating the number.
You could, thinking out of the box, assign one of four Nanos to 8 inputs and have each report via serial to yet another controller...
I currently run halleffect sensors for locomotive detection with cabling up to 5 metres into the same inputs as this project would go. I also have servos controlled by arduino for point switching, boom gates etc. In this case cabling between the arduino, multiplexers and train logic boards is very short max 150mm but between the push button switches and mux is up to 2 metres of flat ribbon cable.
I had the impression that Charleplexing was used mainly for displays although I have never used it. The model train software uses its own program which is a modified form of the old basic but is still just 1's and 0's.
Regarding the mux's they come on a board as a package similar to the nano and are exceedingly cheap and easy to wire up. In the test that I did with a single Mux worked fine over the distance without any bounce.
So you want to read out of 32 switches which one is pressed and then convert it into binary?
Can i ask why are you using 32 switches.
And what the train model will do with those 0's and 1's
The model train setup is fully computer controlled but as points are located at random places on the layout is would be nice to be able to operate these points locally hence the push buttons but the program needs to know when something happens remotely on the layout. The aim is to press a button and feed it into an input on the train controller boards, the train program would then process that the button is pressed and activate the corresponding point and mirror this on the controlling PC screen and adjust the actions of running trains to suit. It knows what to do with 1's and 0's.
As there is over 40 points, I can get away with slightly less than 32 buttons as some points operate in parallel. I have a limited number of inputs available hence the mux and arduino. There are other options available but it gets expensive.
Then I would recommend you to use serial communication between Arduino and your train model since it will only use two pin
By that way you will only be able to send data of (2^n)-1 switches.
( i.e 31 switches )
Which would work fine with inputs that are pulled high with resistors and switches to pull them down when closed.
There is no such thing as a digital signal! It's just an analog signal with a certain meaning depending on where it falls amongst the digital switching thresholds defining high and low.
There is still always the matter of lead dress and EMI or Adam Meyers or whatever, but that would be true with "21st century" parts as well.
a7
This will print Value from your both multiplexer
//Mux control pins
int s0 = 3;
int s1 = 4;
int s2 = 5;
int s3 = 6;
//Mux in "SIG" pin
int SIG_pin = A0 ;
int SIG_pin2 = A1;
int val2 = 0;
void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
Serial.begin(9600);
}
void loop(){
//Loop through and read all 16 values
//Reports back Value at channel 6 is: 346
for(int i = 0; i < 16; i ++){
Serial.print("Value at channel ");
Serial.print(i);
Serial.print("is : ");
Serial.print(readMux(i));
Serial.print(" and ");
Serial.println(val2);
delay(1000);
}
}
int readMux(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]);
}
//read the value at the SIG pin
int val = analogRead(SIG_pin);
val2 = analogRead(SIG_pin2);
//return the value
return val;
}
Hmmm. An invitation to speculate... one flat ribbon would place all 32 switches in one location. Not what I visualized. Is this a dispatcher's panel? If it is, why not put the mux where the switches are? The alternative approach is to have track switches at each turnout/point location. Which one do you have?
... unless you have a strobe signal, yes.
