LDR Multiplexing

Hi everyone,

Attached you can see the code and the setup of my arduino and 16 channel analog multiplexer.

Currently, I am attaching only 4 LDRs, but my goal is to have 12 LDRs and receive their values on the serial monitor of arduino(so only have them as inputs). Then these values will be taken to MAX MSP to have audio as output.

I am having a problem getting the right values on serial monitor. Can anyone help me to spot the mistake?

Thank you for your time. Any help is much appreciated!! Been reading many forums and tutorials but feeling very stuck.

Best,
Ilayda

//16 channels analog multiplexer
//Pinout

// S0 -> Arduino Digital 8
// S1 -> Arduino Digital 9
// S2 -> Arduino Digital 10
// S3 -> Arduino Digital 11
// E -> Ardunio Digital GND
// Z -> Arduino Analog A0
// VEE/ GND -> Arduino GND
// VCC -> Arduino + 5V
// Y0 to Y15 -> Analog Extended Pins


//#define S0 D8
//#define S1 D9
//#define S2 D10
//#define S3 D11
//#define Z A0


int valY0; // Assign the name sensorPinA0 as analog output value from channel Y0
int valY1;
int valY2;
int valY3;

int sensePin = A0;


//Variables
int value; //store value from photoresistor (0-1023)

void setup() {
  DDRB = 255;
  Serial.begin(9600);

pinMode(valY0, INPUT); // set sensorPinA0- as in input
pinMode(valY1, INPUT); //set sensorPinA1 - as an input
pinMode(valY2, INPUT); //set sensorPinA1 - as an input
pinMode(valY3, INPUT); //set sensorPinA1 - as an input


}

void loop() {
PORTB = B00000000;
valY0 = analogRead(sensePin);

PORTB = B00000001;
valY1 = analogRead(sensePin);

PORTB = B00000010;
valY2 = analogRead(sensePin);

PORTB = B00000011;
valY3 = analogRead(sensePin);

Serial.print("Channel0 = ");
Serial.println(valY0);

Serial.print("Channel1 = ");
Serial.println(valY1);

Serial.print("Channel2 = ");
Serial.println(valY2);

Serial.print("Channel3 = ");
Serial.println(valY3);

Serial.println(" ");
  
delay(500); //delay

Your code is incomplete.

What values do you expect to see?
What values do you see?

Try two readings per LDR, and throw away the first one.

I am trying to get light level values (light intensity) for each pin (it will be 12 in total). So numbers ranging from 0 to 1023.

What do you mean by two readings per ldr?

I mean, after selecting the mux channel, take two ADC readings, and discard the first.

What values are you seeing?

I am so sorry, I am very new to arduino and trying to understand what you mean. Do you mean dismissing the PORTB values? like this?

Also attached the serial monitor screenshot >

//16 channels analog multiplexer
//Pinout

// S0 -> Arduino Digital 8
// S1 -> Arduino Digital 9
// S2 -> Arduino Digital 10
// S3 -> Arduino Digital 11
// E -> Ardunio GND
// Z -> Arduino Analog A0
// VEE/ GND -> Arduino GND
// VCC -> Arduino + 5V
// Y0 to Y15 -> Analog Extended Pins


//#define S0 D8
//#define S1 D9
//#define S2 D10
//#define S3 D11
//#define Z A0


int valY0; // Assign the name sensorPinA0 as analog output value from channel Y0
int valY1;
int valY2;
int valY3;

int sensePin = A0;


//Variables
//int value; //store value from photoresistor (0-1023)

void setup() {
  DDRB = 255;
  Serial.begin(9600);

pinMode(valY0, INPUT); // set sensorPinA0- as in input
pinMode(valY1, INPUT); //set sensorPinA1 - as an input
pinMode(valY2, INPUT); //set sensorPinA1 - as an input
pinMode(valY3, INPUT); //set sensorPinA1 - as an input


}

void loop() {
PORTB = B00000000;
valY0 = analogRead(sensePin);

PORTB = B00000001;
valY1 = analogRead(sensePin);

//PORTB = B00000010;
valY2 = analogRead(sensePin);

//PORTB = B00000011;
valY3 = analogRead(sensePin);

Serial.print("Channel0 = ");
Serial.println(valY0);

Serial.print("Channel1 = ");
Serial.println(valY1);

Serial.print("Channel2 = ");
Serial.println(valY2);

Serial.print("Channel3 = ");
Serial.println(valY3);

Serial.println(" ");
  
  delay(500); //delay
  
}

pinMode(valY0, INPUT); // set sensorPinA0- as in input
pinMode(valY1, INPUT); //set sensorPinA1 - as an input
pinMode(valY2, INPUT); //set sensorPinA1 - as an input
pinMode(valY3, INPUT); //set sensorPinA1 - as an input

Each one of those sets pin zero to be an INPUT.
I don't know why you are doing that

Uncompiled, untested

//16 channels analog multiplexer
//Pinout

// S0 -> Arduino Digital 8
// S1 -> Arduino Digital 9
// S2 -> Arduino Digital 10
// S3 -> Arduino Digital 11
// E -> Ardunio GND
// Z -> Arduino Analog A0
// VEE/ GND -> Arduino GND
// VCC -> Arduino + 5V
// Y0 to Y15 -> Analog Extended Pins

const byte sensePin = A0;


void setup() 
{
  DDRB = 255;
  Serial.begin(9600);
}

void loop() 
{
  int valY [4];

  for (byte i = 0; i < 4; i++) {
    PORTB = i;
    (void)analogRead(sensePin); // discard the first reading
    valY [i] = analogRead(sensePin);
  }

  for (byte i = 0; i < 4; i++) {
    Serial.print(F("Channel"));
    Serial.print (i);
    Serial.print(F(" = "));
    Serial.println(valY [i]);
  }  
  Serial.println();
  
  delay(500);
}

Cannot make out where 5v and GND are connected to the solder-less breadboard power rails.

Row 57 LDR is not connect.

It’s best if you use red wires for +5v and black for GND.

The code is compiled but still getting random values. I was trying to get channel values from A0 pin that's why... But I am sure that it was a wrongful thinking.

//Oh yes okay, let me solder and try it again with this new code.
//and thanks again- just connected it!!

“ //Oh yes okay, let me solder and try it again. . .”

Not solder, just use wires in the solder-less breadboard.

Okay I see. Where do I connect EN, VCC and GND of the mux then? Not to arduino directly?

To the breadboard power rails.

Okay -> I think this is right? Still getting random values in the monitor

Do you have a volt meter ?

If so, measure the voltage across an LDR with light and without light.

Do this for all the LDRs.

Confirm the voltage varies with a change in light intensity.

Okay, just tried for all of 4 ldrs. The voltage varies once the light intensity changes.

What voltage do you get with light and then without light ?


Confirm the multiplexer PCB plugs into the solder-less breadboard, i.e. the mux pins need to actually go into the breadboard by at least 1/4 inch.

These images are very concerning.

The pcb header pins seem to be too long, they rise quite high above the breadboard. Are they soldered to the module?

4.5-4.7 -without light for all; 1.2-1.35 for 3 ldrs and 2.5 for 1 ldr

// Confirmed

They are not soldered- it looks like in the attached picture