Go Down

Topic: LED array and CD4067BE multiplexer (Read 3493 times) previous topic - next topic

Physicsfanatic

Hi guys

im a complete newbie with arduinos and coding so im wondering if anyone will be able to help me.

I am trying to use an Arduino along with a CD4067BE multiplexer (please find data sheet attached) to create an array of LEDS. I would like to send a voltage to each LED in turn really quickly and loop this over. I also want to record the time as this process takes place.

Does anyone have any example code which I could use for this experiment? I'd appreciate any help thanks!

PaulRB

Hi and welcome.

Is this a class assignment? Is it allowed to ask people on the internet to do you homework for you these days? When i was at school we weren't allowed to use calculators in exams - times change!

I think you should have a try yourself and we can give you pointers if you go wrong. Try making a schematic too, before you wire anything up and risk burning it. Post that and we can check that also, pencil & paper photo'ed on your phone will be fine.

Paul

Paul__B

Yes, I think we know what a 4067 is, though the datasheet you have cited is for a generally obsolete part.

Now while it is OK to use that - or its enhanced version - for multiplexing your phototransistors, and when we see your circuit schematic we may be able to critique it, it is pretty useless for displaying to LEDs though to be honest, I have done pretty much that some 30 years ago.

But that was long before we had ICs such as the MAX7219 which can control 64 LEDs, or more simply, the TPIC6B595 or even - though it cannot drive the LEDs to a full 20 mA - the humble 74HC595.

{Sorry, PaulRB, I am jumping the gun a bit ...}

Physicsfanatic

#3
Jan 10, 2016, 04:32 pm Last Edit: Jan 10, 2016, 05:10 pm by Physicsfanatic
This is a project I'm doing and yes I'm allowed to ask for help. Ofcourse I wouldn't ask for help without trying it myself, I've been trying to do this with phototransistors for a while but only a few pins on the multiplexer seem to be working so I thought I'd test it out with LED's first as it seems many people have had a go at this.

Here's my code and schematic, its a little messy but displays what I'm doing

Code: [Select]
unsigned long time;
int sensorValue = 0;
float voltage7 = 0.0; //Pin 7 on the multiplexer
float voltage8 = 0.0; //Pin 8 on the multiplexer
int timeDelay = 50; // For reading pins after signalling multiplexer

void setup(){
Serial.begin(9600);
for (int i=0; i<8; i++){
   pinMode(i,OUTPUT); //set pins as output
}
}

void loop(){
 time=millis();
 
 int sens = analogRead(A1); //read analog input 1
 Serial.print("Direct readings "); //direct reading to compare
 Serial.println(sens);

 digitalWrite(0,HIGH);  // multiplexer pin A
 digitalWrite(1,HIGH);  // multiplexer pin B
 digitalWrite(2,HIGH);  // multiplexer pin C
 digitalWrite(3,LOW);  // multiplexer pin D

 // First read pin 7 on the Multiplexer:
 //PORTD = B00000111;
 digitalWrite(3,LOW);  // pin 0 on multiplexer
 delay(timeDelay);
 sensorValue = analogRead(A0);
 voltage7 = sensorValue * (5.0/1023.0);
 //delay(50);
 sensorValue = 0;
 // Now for pin 8:
 //PORTD = B00000110;
 digitalWrite(3,HIGH);  // pin 1 on multiplexer
 delay(timeDelay);
 sensorValue = analogRead(A0); //read input from common output of multiplexer
 voltage8 = sensorValue * (5.0/1023.0);
 sensorValue = 0;
 delay(50);
 //
   
 Serial.print("Time  ");
 Serial.print(time+timeDelay, DEC);
 Serial.print(",  ");  
 Serial.print(voltage7, DEC);      
 Serial.print(",  ");
 Serial.print(time+2*timeDelay, DEC);
 Serial.print(",  ");
 Serial.print(voltage8, DEC);    
 Serial.println();  

 //Keep a delay after you have finished all readings
 delay(1000);    
}

PaulRB

#4
Jan 10, 2016, 04:37 pm Last Edit: Jan 10, 2016, 04:42 pm by PaulRB
Edit that post and put code tags in please. <\> icon.

What kind of Arduino? If Uno, you can't use pins 0 & 1 as digital outputs AND use them for sending debug messages to Serial Monitor. Suggest you use pins 2 and up.

Did you forget the schematic, or still working on it?

Physicsfanatic

#5
Jan 10, 2016, 04:42 pm Last Edit: Jan 10, 2016, 04:52 pm by Physicsfanatic
sorry It didn't attach, so I'm reattaching it

Yep I'm using the Uno, okay I'll fix that thanks.

The arduino only seems to be able to read pins 7 and 15 of the multiplexer, for all the other pins there is no change in voltage. I've asked a few people but no one seems to know why its behaving this way

PaulRB

#6
Jan 10, 2016, 04:49 pm Last Edit: Jan 10, 2016, 04:52 pm by PaulRB
...and the code tags?



So where are these leds you were asking about. Are you getting your two threads mixed up?

Physicsfanatic

#7
Jan 10, 2016, 05:11 pm Last Edit: Jan 10, 2016, 05:37 pm by Physicsfanatic
No I tried this with phototransistors and ran into quite a few issues so I just wanted to know if there were any examples where people have tried to do this with LED's so that I could compare and see where I am going wrong. I thought I would get further if I tried this will LED's first, that was how I originally tested the multiplexer

runaway_pancake

#8
Jan 10, 2016, 05:40 pm Last Edit: Jan 10, 2016, 05:42 pm by Runaway Pancake
The connections of your phototransistor circuits, vis-a-vis +5/Gnd, are wrong.
R1 and R2 should go to +5. The emitters, shown connected to +5, should go to Gnd.

> > > Ordinarily I would correct the/your dwg, but...
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

PaulRB

#9
Jan 10, 2016, 09:31 pm Last Edit: Jan 10, 2016, 09:43 pm by PaulRB
The arduino only seems to be able to read pins 7 and 15 of the multiplexer,
Did you expect the code you posted, exactly as you posted it, so read the other pins? Or are you modifying the code to read other pins and that is not working?

The code you posted will read pins 7 and 15 and nothing else. So if that's the problem, I can modify it for you to fix that:

Code: [Select]
unsigned long time;
int sensorValue = 0;
float voltage = 0.0;

const byte multiplexerPin[4] = {2, 3, 4, 5};

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 4; i++) {
    pinMode(multiplexerPin[i], OUTPUT); //set pins as output
  }
}

void loop() {
  time = millis();

  for (int c = 0; c < 16; c++) {

    for (int i = 0; i < 4; i++) {
      digitalWrite(multiplexerPin[i], bitRead(c, i));
    }
   
    sensorValue = analogRead(A0); // discard first reading
    sensorValue = analogRead(A0);
    voltage = sensorValue * (5.0 / 1023.0);
    Serial.print("voltage  ");
    Serial.print(c);
    Serial.print(" = ");
    Serial.println(voltage);
 
    //Keep a delay after you have finished all readings
    delay(1000);
  }
}

Physicsfanatic

Thank you so much!
I have slightly changed my circuit in order to rectify some mistakes and have tweaked your code to include all the multiplexer channels. Would you mind explaining to me what the attached code means, so what its function is so that I can understand your code better please.

Code: [Select]
for (int c = 0; c < 16; c++) {

    for (int i = 0; i < 4; i++) {

PaulRB

These are two nested for-loops. The outer loop cycles through the 16 channels of the multiplexer, so that each channel can be tested. The inner loop sets the 4 outputs that control which channel is selected on the multiplexer.

Physicsfanatic

Okay that makes more sense now. Could you also explain this line please.
Code: [Select]
const byte multiplexerPin[4] = {2, 3, 4, 5};

PaulRB

This declares an array of bytes. There are 4 elements and their values are shown in the list. The values represent Arduino pin numbers. The word "const" means that the contents of the array are not intended to be changed by the code when it runs. If you were to accidentally write a piece of code that attempted to change them, the C compiler would give an error message and you would realise your mistake.

Physicsfanatic

Okay so would that be the digital output pins 2,3,4,5 on the Arduino?

Go Up