Go Down

### Topic: Strange numbers from CD4051BE (Read 361 times)previous topic - next topic

#### DangerToMyself

##### May 06, 2019, 02:27 amLast Edit: May 06, 2019, 02:34 am by DangerToMyself
I'm trying to use a CD4051BE to get readings from 2 potentiometers (2K Ohm). I would expect the high number for each to be 1024 (or very close) and near 0 on the low end. Pot0 is returning expected results no matter where I turn the knob to. However, Pot1 is erratic at best. With both all the way up, Pot1 returns 895+/-. If I turn Pot1 all the way down, it still reads 895+/-. Turning Pot0 all the way down to 0 results in Pot1 reading 8+/-. Even if it's turned all the way up

Here is the sketch i'm using
Code: [Select]
`int totalChannels = 2;int addressA = 5;int addressB = 6;int addressC = 7;int A = 0;      //Address pin Aint B = 0;      //Address pin Bint C = 0;      //Address pin Cvoid setup() {  Serial.begin(9600);  // Prepare address pins for output  pinMode(addressA, OUTPUT);  pinMode(addressB, OUTPUT);  pinMode(addressC, OUTPUT);  // Prepare read pin   pinMode(A1, INPUT);}void loop() {  //Select each pin and read value  for(int i=0; i<totalChannels; i++){    A = bitRead(i,0); //Take first bit from binary value of i channel.    B = bitRead(i,1); //Take second bit from binary value of i channel.    C = bitRead(i,2); //Take third bit from value of i channel.    //Write address to mux    digitalWrite(addressA, A);    digitalWrite(addressB, B);    digitalWrite(addressC, C);delay(10);    //Read and print value    Serial.print("Channel ");    Serial.print(i);    Serial.print(" value: ");    Serial.println(analogRead(A1));  }  delay(2000);}`

Maybe someone can explain what I'm doing wrong or what's going on.

And here is a sketch of how it's all hooked up.
(NOTE: the sketch shows the CD4051 as having 14 pins. It actually has 16. I just need to learn to count!)

EDIT: Inserted the proper code sketch. copied the wrong one in the first time round.

#### larryd

#1
##### May 06, 2019, 02:51 amLast Edit: May 06, 2019, 02:53 am by larryd
What do you get on A1?

What do you get on A1?

No technical PMs.
If you need clarification, ask for help.

#### DangerToMyself

#2
##### May 06, 2019, 03:08 amLast Edit: May 06, 2019, 03:09 am by DangerToMyself Reason: Doesn't make sense
What do you get on A1?

What do you get on A1?

When 0 is singled out (both script and on breadboard), it performs as it should.

When 1 is singled out, it will only return 144-928.

Does the same with another CD4051 installed as well. And the same with swapping pots.

I'm at a bit of a loss.

Not enough current? Only things connected are as it is in my schematic (if one can call it that).

#### larryd

#3
##### May 06, 2019, 03:26 am
Show us a good image of your wiring.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0.

No technical PMs.
If you need clarification, ask for help.

#### DangerToMyself

#4
##### May 06, 2019, 03:30 am
Show us a good image of your wiring.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0.

Not sure there is a need for that. I just moved the chip to a different area of this cheap Chinese breadboard. Now I'm getting readings all over the place. Wiggle a wire and it goes nuts.

Who sells decent breadboards for a decent price in the US?

#### larryd

#5
##### May 06, 2019, 03:35 am
No technical PMs.
If you need clarification, ask for help.

#### DangerToMyself

#6
##### May 06, 2019, 03:38 am
These are reasonable:

I appreciate the links! Funny thing is, I have the page loaded and was just putting the eyes on the exact same one you pictured above. Too funny!

Thanks for your time and help. I'll order a new board and then set this up again. Hopefully, that takes care of the issue. If not, I'll be back on this thread scratching my head again.

#### DangerToMyself

#7
##### May 18, 2019, 03:54 amLast Edit: May 18, 2019, 04:12 am by DangerToMyself
Well, I got my really nice Jameco breadboard this week. Thanks for the suggestion larryd. This board is so much nicer than the cheap Chinese boards I've been using. Money well spent even though the cheap boards were not the problem.

I did get it figured out though.

For anyone having a similar issue with the CD4051 who finds this thread, in my code above, I originally set "totalChannels = 2;" as I was only utilizing 2 of the 8 available channels.

Apparently, for the code above to work, it needs to read all 8 channels of the CD4051. Set "totalChannels = 8;" and it appears to do what I needed it to do.

EDIT: Forgot to mention that I also tied all the unused channels to ground.
Thanks once again to larryd!!

#### DangerToMyself

#8
##### May 18, 2019, 04:09 amLast Edit: May 18, 2019, 08:59 pm by DangerToMyself
Might have spoke too soon. The serial monitor is showing Pot 1 on Channel 0 AND Channel 4. And Pot 2 on Channel 1 AND Channel 5. This is not really an issue for what I'm wanting to do at this point. However, it may be an issue if/when I add more potentiometers. I'll just have to wait and see.

EDIT: This was due to the fact that I had an error in my sketch/connections. I had pins 5, 6 and 7 as my address pins. When in fact they were connected to 4, 5 and 6. Doh!!!! I have also tested with 5 potentiometers and all works as it should. Yay!!!

#### larryd

#9
##### May 18, 2019, 09:23 pm
Expert !

No technical PMs.