I am trying to use two 4051
ICs with an Arduino Micro right now. The idea was to have an 16-sensor demo station (8 sensors per 4051
) wherein each 4051
pin selection is controlled by a potentiometer as input. I am new to using MUX's, so I mocked the following up as a quick demo based on this help document via Arduino (it's basically the same), which only uses one 4051
and does not use a potentiometer as selector input. So, I should be able to set the variable which
to whatever number (0 - 7) to change the sensor selection that shows up at A2
on the Arduino.
Setting which
to 0 is fine, but when I have more than two sensors plugged into 4051
and set the selector pin to any other number than 0 there is a strong likelihood that the output that shows up in the Serial Monitor
is both unresponsive to the selected sensor change and appears to be simply noise (random numbers around 200 or so). I have been trying to isolate where this noise is coming from and am totally stumped. Has anyone had this issue and, if so, any suggestions on how to further diagnose what is happening? I am currently powering everything off of the Arduino 5V
, which I am under the impression should be okay, though I have tried this with external power supplies (12V at 2A
, for example) and gotten the same results (so I am fairly certain power is not the issue here).
Measuring voltage on pins 2, 3, and 4, I am seeing 1mV on pin 2, 1mV on pin 3, and 0.9mV on pin 4 following the initial spike during upload. When comparing against the Arduino 4051 doc I linked to above, I see that in that file pins 2, 3, and 4 show a Voltage of 2.5V following the initial spike during upload. Any idea what I am doing incorrectly?
/*
Sensor Station via MUX
* code example for useing a 4051 * analog multiplexer / demultiplexer
* by david c. and tomek n.* for k3 / malm� h�gskola
*
* edited by Ross R.
*/
int r0 = 0; //value of select pin at the 4051 (s0)
int r1 = 0; //value of select pin at the 4051 (s1)
int r2 = 0; //value of select pin at the 4051 (s2)
int which = 0; //which y pin we are selecting
int val;
void setup() {
pinMode(A2, INPUT); // mux1 input
pinMode(2, OUTPUT); // s0
pinMode(3, OUTPUT); // s1
pinMode(4, OUTPUT); // s2
}
void loop () {
which = (0);
// select the bit
r0 = bitRead(which, 0);
r1 = bitRead(which, 1);
r2 = bitRead(which, 2); // use this with arduino 0013 (and newer versions)
digitalWrite(2, r0);
digitalWrite(3, r1);
digitalWrite(4, r2);
//Either read or write the multiplexed pin here
// refactor, this should be digitalWrite with millis or something
val = analogRead(A2); // MUX ouputs here
Serial.print("sensor number ");
Serial.print(which);
Serial.print('\t');
Serial.print("val is ");
Serial.println(val);
delay(2);
}
for
loop repeatedly cyclescount
from 0 to 7, so Ard. pins 2-3-4 will average 2.5 V. In your code, they should sit close to 0 V. What do you have 4051 pin 6, INH, set at? What is another value ofwhich
that you tried, and what was connected to the corresponding 4051 input pin? Try connecting all of the 4051 input pins (NO0-NO7, pins 13, 1, 15, 2, 14, 5, 12, 4) together and to the sensor or a pot and see if you get the same reading on all 8 channels. – James Waldby - jwpat7 Jul 30 '15 at 3:14Serial.begin()
call there so I'm a little surprised you see anything in the serial monitor. – Nick Gammon♦ Jul 30 '15 at 4:47