Go Down

Topic: 4051 Multiplexer example code? (Read 17342 times) previous topic - next topic

xelent

Can someone explain to me what the code in this example:

http://www.arduino.cc/playground/Learning/4051

is doing?

I can program, but I do not understand what exactly this program is doing.

Thanks,

Riley

MikMo

The short story is that the binary numbers 0 to 7   (000 - 111) is written to the 3 digital pins 2, 3 and 4.

This is done to addres the A, B, and C input on the 4051 to selct input 0 to 7.

The reason it s hard to understand is that they use some bitshifting to make the program fast and short.

xelent

Yes, it is a bit difficult to understand.

The part I am mainly missing is how to read the analog input from this signal.

It seems like this part of the process is left out, which is a bit strange.

How would I go about reading the values of the input selected using the sample code?

Thanks,

Riley

MikMo

Yes, you are right that part has been legft out.

You connect pin 3 from the 4051 to an analog pin on Arduino and do an analogRead() where the serilaPrint comment is in the code.


xelent

And this will return the value of the input currently selected by the count variable?

xelent

My god! This is so frustrating!

I've spent hours testing this thing out, and still have had no success.

As far as I can tell, from your advice about the analogRead, doing so will read the value of the input selected using the count variable.

But this does not seem to be the case.

What am I missing?


MikMo

#6
Apr 05, 2009, 10:08 pm Last Edit: Apr 05, 2009, 10:23 pm by MikMo Reason: 1
Have you connected Arduinos ground to the 4051's ground ?

What are you trying to read from the 4051 ? potentiometers ?

How is it hooked up ?

I have some code on another computer that i can't get to right now, it uses 2 4051's to read 16 slide pots, i can send it to you sometime tomorrow.

EDIT and did you connect the Enable pin (6) to ground ?

xelent

Yes, I am trying to read 8 potentiometers for a midi sequencer project.

For the moment, I have the circuit built with just one of the inputs hooked up to a pot.

Here is a photo of the wiring:
http://xelentdesign.com/arduino4051wiring.jpg

The 3 yellow wires on the top left of the chip (from the photos perspective) are the s0 - s1 - s2 wires, and the go, of course, to the arduino.

The black wire goes to analog pin 4.

One of the purple wires brings ground to the chip, from the arduino. The other purple wire that heads into the northern part of the photo is for ground to the potentiometer that I have hooked up to y0.

The yellow wires that you see at the bottom are actually just one wire, which connects those two pins of the chip together (ground).

The white wires are input (in the center) and 5v in from the arduino (on the left).

Is this correct?

MikMo

You must connect the enable pin (6) to ground

xelent

At the bottom of the picture, you can see two ends of one yellow wire. They connect pins 8 and 6 together.

Wouldn't that work?

MikMo

Is the pot hokked up as a voltage divider, like:

5V to one side pin, center pin to y0 on 4051 and other side pin to ground ?

Do you get any readings at all from the analogRead() ?

MikMo

that should be ok pins 6,7, and 8 should be grounded AND connectd to ARduinos ground

xelent

Yes, the pot is configured that way. I do get a reading, but it does not seem relevant at all to the input I have selected.

What I mean is, I want to read just input one, and I can't figure out if it's doing that or not.

This code, for instance:

/*

* codeexample for useing a 4051 * analog multiplexer / demultiplexer

* by david c. and tomek n.* for k3 / malm? h?gskola

*

*/  



int led = 13;    //just a led

int r0 = 0;      //value select pin at the 4051 (s0)

int r1 = 0;      //value select pin at the 4051 (s1)

int r2 = 0;      //value select pin at the 4051 (s2)

int row = 0;     // storeing the bin code

int count = 0;    // just a count

int  bin [] = {000, 1, 10, 11, 100, 101, 110, 111};//bin = bin?r, some times it is so easy

// int inputarray[8];

void setup(){



 pinMode(2, OUTPUT);    // s0

 pinMode(3, OUTPUT);    // s1

 pinMode(4, OUTPUT);    // s2

digitalWrite(led, HIGH);

 beginSerial(9600);

}



void loop () {


digitalWrite(led, HIGH);

 for (count=0; count<=7; count++) {

   row = bin[count];      

   r0 = row & 0x01;

   r1 = (row>>1) & 0x01;

   r2 = (row>>2) & 0x01;

   digitalWrite(2, r0);

   digitalWrite(3, r1);

   digitalWrite(4, r2);

   //Serial.println(bin[count]);


   delay (analogRead(4));
 
   digitalWrite(led, LOW);
 

 }  

}


allows me to use the potentiometer to blink the LED.

But what information is the analog input receiving?

If I change 'count' to be a static number '1' instead of cycling, it does not input the value of the pot.


MikMo

does the blink rate change if you torn the potentiometer ?

The analogRead should return a number in the 0 - 1023 range 0 when the pot is all to one side and 1023 when it's all to the other side.

Try changing the loop to this, and enable the serial monitor in Arduino

void loop () {


for (count=0; count<=7; count++) {

  row = bin[count];      

  r0 = row & 0x01;

  r1 = (row>>1) & 0x01;

  r2 = (row>>2) & 0x01;

  digitalWrite(2, r0);

  digitalWrite(3, r1);

  digitalWrite(4, r2);
 
   Serial.println(analogRead(4)/4);

     delay (200)

}  


This should write 8 bytes to the serial monitor, the first from your pot, should give you values between o and 255 when you turn the pot.

The other 7 could be anything since they are from the 7 pins on the 4051 with nothing connectd to them.

I'm of to bed in a second, it's very late over here :-) but i will chek up tomorrow.

MikMo

Code: [Select]
If I change 'count' to be a static number '1' instead of cycling, it does not input the value of the pot.


The array is 0 based so that count should be 0 if you do that

Go Up