Hello,
im quite new to this so i dont really understand it but im working on a project and i have to read out 8 analog sensors but the arduino that i have to use only has 5 analog pins, so i looked around and i found something called a multiplexer but i cant figure out how i use this. i found some example code but i cant figure out how it works and how i can get my readings for it.
im referring to this example code:
/*
* codeexample for useing a 4051 * analog multiplexer / demultiplexer
* by david c. and tomek n.* for k3 / malm� h�gskola
*
* edited by Ross R.
* edited by Igor de Oliveira Sá.
*/
int r0 = 0; //value of select pin at the 4051 (s0)
int r1 = 0; //value of select pin at the 4051 (s1)
int r2 = 0; //value of select pin at the 4051 (s2)
int s0 = 2;
int s1 = 3;
int s2 = 4;
int count = 0; //which y pin we are selecting
void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
}
void loop () {
for (count=0; count<=7; count++) {
// select the bit
r0 = bitRead(count,0); // use this with arduino 0013 (and newer versions)
r1 = bitRead(count,1); // use this with arduino 0013 (and newer versions)
r2 = bitRead(count,2); // use this with arduino 0013 (and newer versions)
//r0 = count & 0x01; // old version of setting the bits
//r1 = (count>>1) & 0x01; // old version of setting the bits
//r2 = (count>>2) & 0x01; // old version of setting the bits
digitalWrite(s0, r0);
digitalWrite(s1, r1);
digitalWrite(s2, r2);
//Either read or write the multiplexed pin here
}
}
i found it on this page: Arduino Playground - HomePage
and this is a example of the code where im doing my readings from:
int Vibsensor;
int readU1;
int readV1;
int readW1;
int readU2;
int readV2;
int readW2;
int readDC;
void setup()
{
Serial.begin(9600); //Only for debugging
Serial.println("TEST");
}
void loop()
{
readU1 = analogRead(A0);
get_tempU1();
readV1 = analogRead(A1);
get_tempV1();
readW1 = analogRead(A2);
get_tempW1();
readU2 = analogRead(A3);
get_tempU2();
readV2 = analogRead(A4);
get_tempV2();
readW2 = analogRead(A5);
get_tempW2();
readDC = analogRead(A6);
get_tempDC();
Vibsensor = analogRead(A7);
get_Vibration();
delay(8000);
}
//PowerSupply U1 pin: A0
void get_tempU1()
{
float tempU1C = readU1 * 0.217226044 - 61.1111111;
float tempU1F = tempU1C + 273.15;
Serial.println(" ");
Serial.println("****PowerSupply U1****");
Serial.println("Temp: ");
Serial.print(tempU1C);
Serial.print(" °C");
Serial.println(" ");
Serial.println("Fahr: ");
Serial.print(tempU1F);
Serial.print(" °F");
Serial.println(" ");
Serial.println("**********************");
Serial.println(" ");
delay(1000);
}
//PowerSupply V1 pin: A1
void get_tempV1()
{
float tempV1C = readV1 * 0.217226044 - 61.1111111;
float tempV1F = tempV1C + 273.15;
Serial.println(" ");
Serial.println("****PowerSupply V1****");
Serial.println("Temp: ");
Serial.print(tempV1C);
Serial.print(" °C");
Serial.println(" ");
Serial.println("Fahr: ");
Serial.print(tempV1F);
Serial.print(" °F");
Serial.println(" ");
Serial.println("**********************");
Serial.println(" ");
delay(1000);
}
void get_Vibration()
{
//While sensor is not moving, analog pin receive 1023~1024 value
if (Vibsensor < 1022) {
Serial.print("Sensor Value: ");
Serial.println(Vibsensor);
}
else {
Serial.print("Sensor Value: ");
Serial.println(Vibsensor);
}
delay(100); //Small delay
}
//PowerSupply W1 pin: A2
void get_tempW1() {
float tempW1C = readW1 * 0.217226044 - 61.1111111;
float tempW1F = tempW1C + 273.15;
Serial.println(" ");
Serial.println("****PowerSupply W2****");
Serial.println("Temp: ");
Serial.print(tempW1C);
Serial.print(" °C");
Serial.println(" ");
Serial.println("Fahr: ");
Serial.print(tempW1F);
Serial.print(" °F");
Serial.println(" ");
Serial.println("**********************");
Serial.println(" ");
delay(1000);
}
//Drive U2 pin: A3
void get_tempU2() {
float tempU2C = readU2 * 0.217226044 - 61.1111111;
float tempU2F = tempU2C + 273.15;
Serial.println(" ");
Serial.println("*******Drive U2*******");
Serial.println("Temp: ");
Serial.print(tempU2C);
Serial.print(" °C");
Serial.println(" ");
Serial.println("Fahr: ");
Serial.print(tempU2F);
Serial.print(" °F");
Serial.println(" ");
Serial.println("**********************");
Serial.println(" ");
delay(1000);
}
//Drive V2 pin: A4
void get_tempV2() {
float tempV2C = readV2 * 0.217226044 - 61.1111111;
float tempV2F = tempV2C + 273.15;
Serial.println(" ");
Serial.println("*******Drive v2*******");
Serial.println("Temp: ");
Serial.print(tempV2C);
Serial.print(" °C");
Serial.println(" ");
Serial.println("Fahr: ");
Serial.print(tempV2F);
Serial.print(" °F");
Serial.println(" ");
Serial.println("**********************");
Serial.println(" ");
delay(1000);
}
//Drive W2 pin: A5
void get_tempW2() {
float tempW2C = readW2 * 0.217226044 - 61.1111111;
float tempW2F = tempW2C + 273.15;
Serial.println(" ");
Serial.println("*******Drive W2*******");
Serial.println("Temp: ");
Serial.print(tempW2C);
Serial.print(" °C");
Serial.println(" ");
Serial.println("Fahr: ");
Serial.print(tempW2F);
Serial.print(" °F");
Serial.println(" ");
Serial.println("**********************");
Serial.println(" ");
delay(1000);
}
//DC Link pin: A6
void get_tempDC() {
float tempDCC = readDC * 0.217226044 - 61.1111111;
float tempDCF = tempDCC + 273.15;
Serial.println(" ");
Serial.println("******* DCLink *******");
Serial.println("Temp: ");
Serial.print(tempDCC);
Serial.print(" °C");
Serial.println(" ");
Serial.println("Fahr: ");
Serial.print(tempDCF);
Serial.print(" °F");
Serial.println(" ");
Serial.println("**********************");
Serial.println(" ");
delay(1000);
}