0

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ï¿1⁄2 hï¿1⁄2gskola
 *
 * 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);
}
Nick Gammon
38.9k13 gold badges69 silver badges125 bronze badges
asked Jul 30, 2015 at 0:47
2
  • In the linked code, the for loop repeatedly cycles count 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 of which 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. Commented Jul 30, 2015 at 3:14
  • You don't have a Serial.begin() call there so I'm a little surprised you see anything in the serial monitor. Commented Jul 30, 2015 at 4:47

1 Answer 1

2

I have a post about that chip at 74HC4051 multiplexer / demultiplexer.

I made up the circuit shown on my page above with modifications as described below:

74HC4051 example

I moved the A/B/C and O/I pins to match your code.

A is the low-order bit so pin 11 on the 74HC4051 should go to D2 on the Arduino.

74HC4051 Arduino Meaning
 11 D2 A
 10 D3 B
 9 D4 C
 3 A2 O/I

I put a pot on I/O0 (pin 13 on the 74HC4051) and randomly set the other pins to +5 V or Gnd, as shown.


In setup() I added:

Serial.begin (115200);

I also modified it to automatically sequence through all 8 possible ports, and then stop after 1000 iterations to stop flooding the USB port on my computer. Code is:

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() {
 Serial.begin (115200);
 pinMode(A2, INPUT); // mux1 input
 pinMode(2, OUTPUT); // s0
 pinMode(3, OUTPUT); // s1
 pinMode(4, OUTPUT); // s2
} // end of setup
unsigned long counter;
void loop () {
 // 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);
 if (++which > 7)
 which = 0;
 if (++counter >= 1000)
 {
 Serial.flush ();
 exit (1);
 } 
} // end of loop

The output from your sketch was as expected:

sensor number 0 val is 781
sensor number 1 val is 0
sensor number 2 val is 1023
sensor number 3 val is 1023
sensor number 4 val is 0
sensor number 5 val is 0
sensor number 6 val is 0
sensor number 7 val is 1023

I suggest you check that your wiring agrees with mine.

answered Jul 30, 2015 at 5:20
5
  • 1
    Re "I moved the A/B/C and O/I pins to match your code", your diagram connects 4051 A,B,C to Ard 6,5,4 instead of to Ard 2,3,4. Also, the code shown in the question doesn't have a loop, so it might be good to include your code in your answer Commented Jul 30, 2015 at 17:22
  • @nick, Thanks for your input on this. The omission of Serial.begin was an oversight on my part but has been added. Your answer resolved my issue: I had the digital pins connected in the wrong order. Thanks! Commented Jul 30, 2015 at 20:02
  • @jwpat7 - I have modified the post to include my test code. your diagram connects 4051 A,B,C to Ard 6,5,4 instead of to Ard 2,3,4 - true, that was one of my stock schematics. I noted underneath which changes had been made. The code on my linked page matches the schematic. I wanted to use the OP's exact code (as far as possible) in case there were subtle problems with those pins. Commented Jul 30, 2015 at 20:59
  • @jwpat7: To make my answer more useful, I redrew the schematic to match the code on this page, to avoid confusion. Commented Jul 31, 2015 at 0:37
  • @NickGammon, +1 Commented Jul 31, 2015 at 2:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.