# analogRead() on 4051 multiplexer yields readings on all channels at once

I know similar questions have been posted in this forum that I’ve read and (tried to) follow up on but didn’t solve this problem. I have little knowledge of electronics and rely on what I can find on the internet.

I’m working on a project to control my old slotcar race track with an Arduino and cloud. To read the complete story so far go to http://www.codeproject.com/Articles/887003/Control-a-model-car-track-with-Azure-and-Arduino?msg=5029881#xx5029881xx as I entered it for the IoT contest. I’ve been completely stuck lately on reading the analog port on the Arduino Uno for detecting the presence/absence of a car on a separate track section. All six track sections I wired in with a 4051 multiplexer to control the speed on each section according to this article http://playground.arduino.cc/Learning/4051 and it works OK. For sensing the current on a section I made the same setup but in reverse with another 4051. As the track runs on 17V DC I wired in pull down resistors according to Measuring Current with the Arduino. Attached is the wiring diagram and the sketch. Each Motor symbol represents a single track section. When I run it without a car on the track it give 0 reading on all 6 channels as expected:

0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 0.00 | 5 = 0.00 |
0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 0.00 | 5 = 0.00 |
0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 0.00 | 5 = 0.00 |
0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 0.00 | 5 = 0.00 |
0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 0.00 | 5 = 0.00 |
0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 0.00 | 5 = 0.00 |
0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 0.00 | 5 = 0.00 |
0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 0.00 | 5 = 0.00 |
0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 0.00 | 5 = 0.00 |
0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 0.00 | 5 = 0.00 |
0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 0.00 | 5 = 0.00 |
0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 0.00 | 5 = 0.00 |

When however I run a car on the track all 6 channels start giving readings simultaneously (Also the 2 empty channels on the 4051 start to give readings):

0 = 26.70 | 1 = 15.27 | 2 = 26.22 | 3 = 18.99 | 4 = 17.25 | 5 = 14.12 |
0 = 29.45 | 1 = 23.21 | 2 = 25.43 | 3 = 22.00 | 4 = 10.57 | 5 = 18.12 |
0 = 24.59 | 1 = 12.69 | 2 = 21.04 | 3 = 32.73 | 4 = 13.33 | 5 = 17.70 |
0 = 9.83 | 1 = 10.05 | 2 = 18.57 | 3 = 25.16 | 4 = 11.03 | 5 = 17.15 |
0 = 22.45 | 1 = 11.69 | 2 = 20.72 | 3 = 12.02 | 4 = 6.73 | 5 = 25.14 |
0 = 26.93 | 1 = 10.13 | 2 = 21.80 | 3 = 32.70 | 4 = 12.83 | 5 = 18.87 |
0 = 7.04 | 1 = 12.95 | 2 = 24.14 | 3 = 41.98 | 4 = 9.54 | 5 = 16.66 |
0 = 26.36 | 1 = 13.27 | 2 = 20.21 | 3 = 30.57 | 4 = 10.15 | 5 = 20.72 |
0 = 28.90 | 1 = 16.17 | 2 = 19.38 | 3 = 21.08 | 4 = 16.35 | 5 = 22.32 |
0 = 8.89 | 1 = 22.33 | 2 = 22.41 | 3 = 25.47 | 4 = 2.69 | 5 = 5.89 |
0 = 36.35 | 1 = 18.50 | 2 = 16.24 | 3 = 19.45 | 4 = 4.36 | 5 = 19.51 |
0 = 17.36 | 1 = 9.65 | 2 = 17.63 | 3 = 29.54 | 4 = 12.65 | 5 = 13.16 |

I’ve adapted the setup in several ways:
Moved the pulldown resistors in front of the 4051 to reduce the current through it
Separated the sections from the shared ground to prevent them from creating an antenna
Added delay() everywhere to allow for settling down
I’ve read some articles and threads on crosstalk and added resistors on the two empty channels accordingly
Removed the multiplexer and connected the 6 sections with the 6 analog ports directly in case the multiplexer is malfunctioning
Switched the 5 empty analog ports explicitly to LOW to prevent them from floating

Nothing I’ve done so far made any difference at all to the readings. It almost looks like the readings are random and unrelated to the specific sections being switched on or off.
Anyone has any idea what’s going on? Help is very much appreciated as I’m at my wits end. Please let me know when you need additional information.

MuxDemux4051.ino (2.1 KB)

There are lot of mistake in code here. you can simplify the code further

There are two Mux IC you can use one common pin to control each & schematic als0

mux means multiple input line & single out line. But you are not using mux output properly.

If you planing use single input & multiple out u need use Demux IC

sample code as below

``````int SO_enable=2;
int S1_enable=3;
int S2_enable=4;
int Enablepin=5;//this pin you can connected to ground
int Sensor_Value0=0;
float Current_Mux1[8];
float ARDUINO_ANALOG_SCALING=0.004789833822091887;
int row,column;
static float  Mux1_array[8]={
0.0};

int array[8][4]={
{
0,0,0,0      }
,
{
0,0,0,1      }
,
{
0,0,1,0      }
,
{
0,0,1,1      }
,
{
0,1,0,0      }
,
{
0,1,0,1      }
,
{
0,1,1,0      }
,
{
0,1,1,1      }

};
void setup()
{
pinMode(SO_enable, OUTPUT) ;// pin can enable/disable using digital IO 7 of arduino
pinMode(S1_enable, OUTPUT) ;// pin can enable/disable using digital IO 6 of arduino
pinMode(S2_enable, OUTPUT) ;// pin can enable/disable using digital IO 5 of arduino
pinMode(Enablepin, OUTPUT) ;// pin can enable/disable using digital IO 4 of arduino
pinMode(A0, INPUT) ;
Serial.begin(9600);
}
void loop()
{
for(row=0;row<8;row++)
{
digitalWrite(Enablepin,array[row][0]);
digitalWrite(SO_enable,array[row][1]);
digitalWrite(S1_enable,array[row][2]);
digitalWrite(S2_enable,array[row][3]);
delay(20);
Mux1_array[row]=(Sensor_Value0 * ARDUINO_ANALOG_SCALING);
Serial.print( Mux1_array[row]);
Serial.print("\t");
Serial.println( Current_Mux1[row]);

}
}
``````

Thanks for your reply, I know it's either mux or demux for many-to-one or one-to-many. I changed the code according to your suggestions but the output is still unchanged: There are many readings from the channels where I expect only one (The section the car is on) so there must be some other problem.

I cant understand your problem ; SO you need to elobrate properly

There are many readings from the channels where I expect only one (The section the car is on)

When you get multiple reading Choose array that make you Car status on

Thanks for your help. Sorry it's not clear: When the car is on a specific section e.g. section 3 I get reading from all channels: 0 = 26.36 | 1 = 13.27 | 2 = 20.21 | 3 = 30.57 | 4 = 10.15 | 5 = 20.72 |

I expect only a reading from the section it's on like this for example when it's on section 3: 0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 30.57 | 4 = 0.00 | 5 = 0.00 |

and when it runs over to section 4 like this: 0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 10.15 | 5 = 0.00 |

Could it also be electro-magnetic interference? The slot-car track is old and sits close to the Arduino. As I said in my original post I have very little knowledge of electronics.

It wont read value like that Because it store the Previous value.

Ok, so how can I change that?

Sorry but that whole circuit is screwed. It will not work. Only your errors have stopped the whole thing from being distroyed.

You can not feed a voltage of more than 5V into any pin of an Arduino. What sort of 4051 do you have? Check the data sheet to see the maximum voltage on that.

The logic signals you need to drive the 4051 are dependant on the supply voltage you give it. All inputs to it must be less than or equal to the supply voltage.

I disconnected the slot-car track and put a multimeter on the channels. The multimeter will allow for some current to flow and the output is much more as expected:

0 = 2.12 | 1 = 0.00 | 2 = 0.11 | 3 = 0.09 | 4 = 0.00 | 5 = 0.00 | 0 = 1.88 | 1 = 0.00 | 2 = 0.11 | 3 = 0.08 | 4 = 0.00 | 5 = 0.00 | 0 = 2.38 | 1 = 0.00 | 2 = 0.24 | 3 = 0.24 | 4 = 0.00 | 5 = 0.00 | 0 = 2.26 | 1 = 0.00 | 2 = 0.19 | 3 = 0.17 | 4 = 0.00 | 5 = 0.00 | 0 = 2.04 | 1 = 0.00 | 2 = 0.06 | 3 = 0.07 | 4 = 0.00 | 5 = 0.00 | 0 = 1.10 | 1 = 0.00 | 2 = 0.00 | 3 = 0.05 | 4 = 0.00 | 5 = 0.00 | 0 = 0.00 | 1 = 0.00 | 2 = 0.00 | 3 = 0.00 | 4 = 0.00 | 5 = 0.00 | 0 = 0.13 | 1 = 0.00 | 2 = 0.08 | 3 = 0.05 | 4 = 0.00 | 5 = 1.15 | 0 = 0.26 | 1 = 0.00 | 2 = 0.17 | 3 = 0.18 | 4 = 0.00 | 5 = 1.95 | 0 = 0.20 | 1 = 0.00 | 2 = 0.16 | 3 = 0.18 | 4 = 0.00 | 5 = 1.97 | 0 = 0.20 | 1 = 0.00 | 2 = 0.19 | 3 = 0.18 | 4 = 0.00 | 5 = 1.88 | 0 = 0.21 | 1 = 0.00 | 2 = 0.17 | 3 = 0.15 | 4 = 0.00 | 5 = 2.14 |

You can see on moving the multimeter from channel 0 to channel 5 the reading moves along with it. I don't think I'm doing everything wrong but I'm definitely missing something. Anyone else any idea's?

You might not think you are doing anything wrong but you are. You asked for advice and that was the top advice. Unlike the other contribution you got.

Thanks guys for the input. I think I'll get it working now.

Hi, Do you realise because you are sensing track voltage, if there is NO car on the track section, but the power is applied the track section, the arduino will sense that there must be a car on the track section?

If the control is OFF, then the arduino will sense that there is NO CAR on the track section, even if there is.

Sorry if I have the concept wrong. Is there a reason to have a separate MOSFET for each section of the circuit?

I believe what you want to do is detect if a car is on a track section so that the you can feed back to the cloud track position.

Is it as simple as that?

The diode your drawing should be in the positive of the 17V supply with the Anode to positive of supply. The 17Vdc, where is it from, and is it rectified AC or smoothed/filtered rectified AC?

Tom.... :)

Hi Tom,

Yes, that's the idea and I'm slowly getting there. As I said I'm not an electronics man but the readings only appear when the car is on the specific section and current starts flowing. The reason for the multiple mosfets is the same as for the sensors, I think the current of the load is too much for the multiplexer as it didn't work with the one mosfet upfront (I want to control the speed separately for each section). The trafo I use is the same that comes with a model train track and I don't know rectified or smoothed/filtered AC but it's not a 'real' DC supply like a battery. If you like you can read some more on the project on CodeProject:

http://www.codeproject.com/Articles/887003/Control-a-model-car-track-with-Azure-and-Arduino

Hi,
I just cobbled this schematic up, I think this is how you want to control the car speed, I have added resistors to help control the MOSFET.
What is the part number of the MOSFET, it must be what is called logic level input in this case.

If you use separate section control you will have problems with continuous car motion as you can only have one MOSFET active at a time with the 4051.

With this configuration, Neg supply control, you will have problems detecting track section occupancy. There is a solution using opto couplers and diodes.

If you use,Pos supply control, detection can be a piece of cake. This will require a different MOSFET and extra components however.

For Grumpy_Mike 4051 is a MUX/DEMUX IC.

Tom…
I’ll post the alternatives soon.

4051_datasheet.pdf (125 KB)

Hi, What was the reason for not using the AC715 current sensors? Looking at the graph and the picture of the AC715 you have the current running the wrong way through the device.

Identifying each individual car is another problem, not solved by analyzing the current load.

Tom.... :)

for ACS715 model you need filtering; Its has lot of fluctuated analog output

@Tom Thanks for informing me that the 4051 was both a multiplexer and demultiplexer, maybe you would like to tell my Grandmar how to suck eggs while you are at it.

That schematic you posted will only allow the driving of one car at a time and will not detect there is a car on the track appart from that it fulfills everything that the OP's original circuit was trying to do. However reducing the capability of a design in order to fit the use of a single component is not the way grown ups design stuff. What grown ups do is to use the correct components for the job which in this case is not a 4051.

By the way Tom did you know there are many types of 4051 some have higher voltage capabilities and some even allow the passage / switching of negitave voltages providing you have a negitave supply?

Hi;
Find attached a suggested section detection circuit, it will not detect a car on an unpowered section however.

The U1 to U4 are opto couplers that have a logic output,
ACPL-P480
http://www.mouser.com/ds/2/38/AV02-1305EN-187387.pdf

The you will loose about 0.7V through each diode in the series array, but considering you are using 17V supply I don’t think that will be a problem.

The voltage drop across the three diodes when current flows should be 3 x 0.7 = 2.1V.

The opto needs 1.5V at 10mA
So 2.1 - 1.5 = 0.6V
The opto input resistor will be 0.6/0.01= 60R, close enough to 56R.

The R-C network at the input to the 4051 is to filter out the PWM, the values will depend on the PWM frequency.

Hope this helps, its a concept, not a tried circuit, so pick it apart if you want.

Tom…

Hi Tom,

I'm not using the AC715 sensor anymore because they are rather expensive (around 10 euro) and I would need one for each section. With thanks to AMPS-N and Grumpy Mike pointing me in the right direction I found out far too much current was flowing through the multiplexer so I put some big resistors in front and no I get much better readings. I'll let know sometime about further progress (or none ;-))

Hi Tom,

I overlooked your last reply. Thanks for the elaborate schema you posted. I'll have a look at the optocoupler you suggest.