I am working on a project where I want to use 25 Hall-effect sensors to accurately measure displacements. After browsing the internet for solutions on how to expand the amount of analog inputs, I came across 74HC4051 multiplexers to fix this. I tried using three of these and they do work, but I've noticed that the output voltages read by the Arduino are not consistent.
While the voltage read by the Arduino is stable for short instances, every once in a while this voltage seems to jump to other values (e.g. yesterday I read 984 bits for one of the sensors and today that was 952, while the magnet was at exactly the same distance). This messes up my calibration which means I can't reliable calculate the distance. When I'm not using multiplexers I don't seem to have this problem. I have heard that multiplexers can be unreliable for an application like this.
What would be the best solution to expand the amount of analog pins? Using two Arduino Mega's for example? I am not very skilled with electronics so I would prefer a simple solution if there is one. Thanks in advance!
It's probably not the mux itself, but something in how it's applied. I have used 4051's in front of 12-bit ADCs and gotten the full resolution out of them. There are better quality analog switches, but first it's best to figure out why what you have isn't working as expected.
So, what hall sensors are you using and let's have a look at your circuit: the answer is possibly there.
The circuit is quite complicated but I'll do my best to explain it (sorry about all of the wires).
On the left breadboard there are three multiplexers and four hall sensors. On the right breadboard there are 20 hall sensors. As you can see in the picture, I connected the two halves of the right breadboard to tidily connect the sensors in pairs, to reduce the amount of long wires. The long wires of the sensors are going into the multiplexers' analog inputs. Everything is in parallel (right?). The two breadboards' + and - are also connected.
I don't notice any .1µF decoupling caps in use. Rule of thumb is one per digital IC, between Vcc and GND.
So only for the multiplexers you mean? They are analog, but digitally controlled. Are they also necessary for the hall sensors? In the spec sheet of the hall sensors, for the Vcc pin it does say "Input power supply; tie to GND with
bypass capacitor".
That's what is shown on page 10 of the datasheet, one .1µF cap per each. Decoupling caps should be mounted as close as physically possible between Vcc and GND.
Just an observation, but a more careful analysis might lead to a quicker solution than scattershot adding or replacing parts.
A schematic would help with that...
I've only drawn two hall sensors because there are 24 of them and that would have made the schematic even messier than it already is ;). All of them are connected in the same way to an analog input of the multiplexer.
I've now also added 0.1uF caps to all of the sensors (as close as possible) and a 0.01 uF and 0.1 uF to the Vcc pin of each multiplexer.
So the power supply first goes to a hall sensor and then to another one on the other side of the breadboard, while the ground is connected in the same way, but originates from the other side.
The adress selector pins are directly connected to the Arduino, and the Vout's of the hall sensors go directly into the analog inputs of the multiplexer.
I have noticed by the way that moving the USB cable or the circuit causes the offset of the signal. If I keep it in the same place for a long time without touching anything, the output is very stable. Maybe the cable is faulty? I am using the USB for both the power supply and the serial monitor.
That's a total of 172+ mA just for sensors - although the graph in the characteristics section indicates around 190mA (7.6mA each x 25). Your schematic shows the sensors powered by the Arduino. IIRC, 200mA is the most the onboard supply will safely provide. It looks like your pretty close to that number.
First, your circuit looks fine. There are opportunities for simplification, but nothing glaringly wrong.
Potential problem #1: breadboards are infamously bad at handling low-level signals. Going by the number of bits you're seeing a change on, there is likely something else going on, but be aware that the breadboard is probably contributing to it.
Suggestion #1: measure the voltage on your breadboard across 5 & GND. Make sure it is actually 5V or close to it. The arduino typically can't supply a lot of current and each Hall sensor consumes around 7mA so that may be causing the supply to droop.
Suggestion: switch the mux to a single Hall sensor and measure the voltage at the output of the mux and compare that measurement to the output at the Hall sensor itself. They should for all intents and purposes be identical.
Next, read the same input from the arduino continously. Don't switch the mux. The voltage read by the arduino should match to within 15mV or so (allowing for 3 bits of error or jitter). If that works as expected, the problem is likely related to switching the mux channels. And that would most probably be software-related.