Go Down

Topic: Urgent - Controlling multiplexer outputs - Sensor Network (Read 2350 times) previous topic - next topic

Feda

Hi there :D!

Okay so I'm using a CD4067B analogue multiplexer to control several sensor inputs. Now I want to control each channel which represents the different sensors. Below is an example of what I'm getting on the Serial Monitor. I have (0,1,2,3) 4 channels which represent different sensors. I want to be able to convert each of these channel from the voltage to the corresponding units eg.(temperature-degrees, gas -ppm, humidity % etc. ). How can I do that so i could convert those voltage values to the real value??

(Serial Monitor) - Example:

0 = 826   1 = 796   2 = 775   3 = 733   
0 = 826   1 = 796   2 = 777   3 = 735   
0 = 826   1 = 797   2 = 777   3 = 736   
0 = 826   1 = 796   2 = 777   3 = 735   
0 = 826   1 = 797   2 = 776   3 = 734   
0 = 826   1 = 796   2 = 776   3 = 735   
0 = 826   1 = 796   2 = 777   3 = 735   
......
   
  Help would be very much appreciated.
This is the code i used:

// This code reads the inputs using a CD 4067B multiplexer

/// the address pins will go in order from the first one:
const int A = 8; // First address pin corresponding to pin 8 on the microcontroller

void setup() {
  Serial.begin(9600);
  // set the output pins, from the first to the fourth:
  for (int pinNumber = A; pinNumber < A + 4; pinNumber++) {
    pinMode(pinNumber, OUTPUT);
    // set Address pins A,B,C and D to low
    //to connect input 0 to the output:
    digitalWrite(pinNumber, LOW);
  }
}

void loop() {

    //loop over all the 4 input channels
    for (int Channel = 0; Channel < 4; Channel++) {
    setChannel(Channel);
     //read the analog input and store it in the value array:
    int analogReading = analogRead(0); 
     //print the result out:
    Serial.print(Channel,  DEC); // print the channel number
    Serial.print(" = "); // insert = sign
    Serial.print(analogReading, DEC ); // read the channels
    Serial.print("\t"); // Insert tab between each channel
   
  }
   
   // linefeed after channels appear
  Serial.println();
delay(500);
}

//Which channel connected to the output:

void setChannel(int whichChannel) {
  // loop over all four bits in the channel number:
  for (int bitPosition = 0; bitPosition < 4; bitPosition++) {
    // read a bit in the channel number:
    int bitValue = bitRead(whichChannel, bitPosition); 
    // pick the appropriate address pin:
    int pinNumber = bitPosition + A;
    // set the address pin
    // with the bit value you read:
    digitalWrite(pinNumber, bitValue);

}

funkyguy4000

You need to do the math in your code.  To find out the equations for your particular sensors, you have to refer to their datasheet.
Accelerate to 88 miles per hour.

Feda

Yes definitely,

I do know all of these equations, the thing is I don't know if I can control these.
One of these channels represent the analogue value of a temperature sensor, but I want to convert it into Celsius and I know it's equation, yet where in the code and how can I control each channel separately?
Channel 0 - temperature values
Channel 1 - humidity values
Channel 2 - gas values
Channel 4 - pressure values

I hope you understand.

Feda

I want to print the real values of each sensor.  I have the equations I just don't know if I could do that. If so how and where in the code exactly? Maybe someone give examples please ?

Thank you

CrossRoads

You have a voltage from 0 to 5 that represents a value. The ADC is 10 bits, so 5/1023 represents 1 bit of data, about 0.00488V (4.88mV)

For temperature for example, 0 may represent 0 degrees C and 5V represents 200 degrees C.

So reading/500 = the percent of 200C that the reading represents. (Assumes your sensor for degrees C have a linear reading from 0 to 200. It may not).

Take a voltage reading, and now you have a digital number that represents that voltage.
Do some converting:
(digital #) * (5V/1023) = voltage that was read in.

(voltage read in)/5V * 200 = degrees read in.

Example: reading of 400
(400 * (5/1023)) / 5 * 200 = 78.2C.
(850 * (5/1023)) / 5 * 200 = 166.17C.

Your data sheets will tell you what the ADC readings between 0 and 1023 represent, you do a little math like above to convert that reading into your temperature, pressure, air density, whatever.

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Grumpy_Mike

Quote
yet where in the code and how can I control each channel separately?

You don't control sensors you read them. They are inputs. You only control outputs.

Your code is in a for loop reading the channels. You have to insert a switch statement, switching on the loop's index variable to process each reading according to it's required formula. If that is too hard then just read the analogue inputs separately and apply the formula after the read.

Feda

#6
Aug 20, 2012, 01:06 pm Last Edit: Aug 20, 2012, 01:20 pm by Feda Reason: 1
Thanks for the reply guys.

Yes CrossRoad I understand what you mean and I know how to do the maths, I have the formulas ready. All I meant was that I've been trying to read each channel individually and having it printing out the real values of each parameter but there was no luck.

I think Grumpy_Mike is closer to what I meant. I need to read the sensors at pretty much the same rate that's why I used the multiplexer. If I'm going to use a switch statement to switch between the the different index variables could you post an example of how? Please? A quick simple example would be fine.

Thanks again for the replies.

Grumpy_Mike

Your loop() function needs to look like this:-

Code: [Select]
void loop() {
int analogReading;
    //loop over all the 4 input channels
    for (int Channel = 0; Channel < 4; Channel++) {
    setChannel(Channel);
     //read the analog input and store it in the value array:
     analogReading = analogRead(0);
        switch (Channel) {
     case 0:
        // do calculations an print for sensor 0
     break;
      case 1:
        // do calculations an print for sensor 1
     break;
     case 2:
        // do calculations an print for sensor 2
     break;
     case 3:
        // do calculations an print for sensor 3
     break;   
        }
  }
   
   // linefeed after channels appear
  Serial.println();
delay(500);
}

Feda


Feda

/Okay now that I have the multiplexing bit working, how would I de-multiplex the signal? I want to demultiplex it so I can display each sensor through processing(graphical display). Do I need a de-multiplexer to do that? A friend of mine suggested a USB reader but I don't understand the logic behind that? So could anyone explain?

AWOL

You just need to make sure that what you print (== send to Processing) has some unique identifier appended to it that will allow the Processing application to decide which set of readings the value belongs.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Feda

Knowing that I'm a newbie, is is difficult to code what you explained?

AWOL

No, not difficult at all, in fact, it looks like you did something like that in your first post where you tabulated your results.
(I notice you're a low-count poster, but I have no idea whether you're a newbie or not)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


Grumpy_Mike

In the code you first posted each sensor had 0 = , 1= and so on in front of it. Just do the same for processing to latch onto so it knows what sensor the number is associated with.
Alternatively send all the numbers separated by a commer in one line, that is use print for all sensors and a println at the end. Processing the pickups a line and parses or splits the numbers up.

Go Up