Analog Multiplexing

Hello,

I am using some sensors with UNO R2 board. Since it has only 6 ADC ports, I am trying to build multiplexer to attach all sensors at once.
Here is the code:

#include <SD.h>  //http://www.ladyada.net/products/microsd/
#include <Wire.h>
#include <SPI.h>
int CS_pin = 10;
int SD_led = 8;
#include "floatToString.h"
#include "RTClib.h"
RTC_DS1307 RTC;
float COValue = 0;
float NO2Value = 0;
float O3Value = 0;
float e_NO2Value = 0;
float e_NOValue = 0;
float e_COValue = 0;
float celsius = 0, kelvin=0;
#include <math.h>
int dustPin=0;
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
float ZeroPercentVoltage = 0.8;
float val = 0;
float max_voltage = 3.27;

  char date_time[20];
  char CO_Vout[20];
  char NO2_Vout[20];
  char O3_Vout[20];
  char cel[20];
  char e_NO2_Vout[20];
  char e_NO_Vout[20];
  char e_CO_Vout[20];
  char dust[20];
  
  #include <avr/pgmspace.h>

int _outputserialchar( char c, FILE *t) {
	Serial.write( c );
	return 1;
} 

void setup()
{
  pinMode(4, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  
  fdevopen( &_outputserialchar, 0); 
  Wire.begin();
  Serial.begin(9600);
  SPI.begin();
  Serial.println("Initializing Card");
  pinMode(10, OUTPUT);
  RTC.begin();
  pinMode(ledPower,OUTPUT);
  pinMode(5, OUTPUT);
  
  if (!SD.begin(CS_pin))
  {
      Serial.println("Card Failure");
      return;
  }
  Serial.println("Card Ready");
  
  //Write Log File Header
  File logFile = SD.open("ARD1.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(", , , , , , , ,"); //Just a leading blank line, incase there was previous data
    String header = "Date_Time, Temp, e_NO2_Vout, e_NO_Vout, e_CO_Vout, CO_Vout, NO2_Vout, O3_Vout, Dust_Vout";
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
  
}

void loop()
{
  delay(3000);
//Read Value of 4051 analog-in 5 by setting the values of s0,s1 and s2
  digitalWrite(4, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  delay(3000);
  int readInFive = analogRead(0);
  kelvin = readInFive * 0.004882812 * 100;
  celsius = (kelvin - 273.15) - 5;
  floatToString(cel, celsius, 1);
  //Read Value of 4051 analog-in 1 by setting the values of s0,s1 and s2
  digitalWrite(4, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  delay(2000);
  int readInOne = analogRead(0);
  val = readInOne;
  digitalWrite(ledPower,LOW);
  delayMicroseconds(delayTime);
  int dust_Vout = analogRead(dustPin);
  delayMicroseconds(delayTime2);
  digitalWrite(ledPower,HIGH);
  delayMicroseconds(offTime);
  floatToString(dust, dust_Vout, 1);
  //Read Value of 4051 analog-in 2 by setting the values of s0,s1 and s2
  digitalWrite(4, LOW);
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  delay(3000);
  int readInTwo= analogRead(0);
  e_NO2Value = readInTwo * 0.004882812;
  floatToString(e_NO2_Vout, e_NO2Value, 8);
  //Read Value of 4051 analog-in 3 by setting the values of s0,s1 and s2
  digitalWrite(4, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  delay(3000);
  int readInThree= analogRead(0);
  e_COValue = readInThree * 0.004882812;
  floatToString(e_CO_Vout, e_COValue, 8);
  //Read Value of 4051 analog-in 4 by setting the values of s0,s1 and s2
  digitalWrite(4, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  delay(3000);
  int readInFour= analogRead(0);
  e_NOValue = readInFour * 0.004882812;
  floatToString(e_NO_Vout, e_NOValue, 8);
    
  NO2Value = analogRead(1) * 0.004882812;
  COValue = analogRead(2) * 0.004882812;
  O3Value = analogRead(3) * 0.004882812;
  
  floatToString(CO_Vout, COValue, 8);
  floatToString(NO2_Vout, NO2Value, 8);
  floatToString(O3_Vout, O3Value, 8);
  
  DateTime now = RTC.now();
  sprintf_P(date_time,PSTR("%d/%d/%d %d:%d:%d"),now.day(),now.month(),now.year(),now.hour(),now.minute(),now.second());
  String dataString = String(date_time) + ", " + String(cel) + ", " + String(e_NO2_Vout) + ", " + String(e_NO_Vout) + ", " + String(e_CO_Vout) + ", " + String(CO_Vout) + ", " + String(NO2_Vout) + ", " + String(O3_Vout) + ", " + String(dust_Vout);
  File logFile = SD.open("ARD1.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(dataString);
    logFile.close();
    printf_P(PSTR("%s, %s, %s, %s, %s, %s, %s, %s, %s\r\n"), date_time, cel, e_NO2_Vout, e_NO_Vout, e_CO_Vout, CO_Vout, NO2_Vout, O3_Vout, dust); 
    pinMode(SD_led, OUTPUT);
    digitalWrite(SD_led, HIGH);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
  
  delay(15000);
  digitalWrite(SD_led, LOW);
  delay(1000);
}

For multiplexing (with 4051) I followed a tutorial on this page Andrew Burrell | Home
My circuit is connected exactly as the photo on the website suggests to.

Problems:

  1. This code doesn't write values to the csv file on SD card except for header. Although when I remove the multiplexing part from the code, it does work perfectly (writes data on SD and serial monitor).
  2. On serial monitor I see values (but not on SD card). Also, after 5-6 measurement cycles, code stops working.

Kindly help me fix these issues.

Thanks a lot.

Best,

Z

Someone?

You have far too many delays. You do not need them after switching the multiplexer.

You seem to be opening and closing the file without writing any data to it.

You have all the code in the loop() function, try splitting it up into small functions so it is easier to follow.

Try just printing out all your data to the monitor first, get that right before trying to write it to a card.

Learn how to use arrays so you can have a simple way of writing to the multiplexer select line, see:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

Look at the code here for an example of controlling a multiplexer:-
http://www.thebox.myzen.co.uk/Hardware/MIDI_Footsteps.html

Basically start simply, use functions, build up the complexity.

Thanks for reply, Mike.

Grumpy_Mike:
You have all the code in the loop() function, try splitting it up into small functions so it is easier to follow.
Try just printing out all your data to the monitor first, get that right before trying to write it to a card.

Here is my new code:

#include <SD.h>  //http://www.ladyada.net/products/microsd/
#include <Wire.h>
#include <SPI.h>
#include "floatToString.h"
#include "RTClib.h"
#include <avr/pgmspace.h>
#include <math.h>
int CS_pin = 10, SD_led = 8;
RTC_DS1307 RTC;
float COValue = 0, NO2Value = 0, O3Value = 0;
float e_NO2Value = 0, e_NOValue = 0, e_COValue = 0;
float celsius = 0;
int dust_Vout = 0, ledPower = 5, delayTime = 280, delayTime2 = 40;
char date_time[15];
char cel[15];
char e_NO2_Vout[15];
char e_NO_Vout[15];
char e_CO_Vout[15];
char dust[15];
char CO_Vout[15];
char NO2_Vout[15];
char O3_Vout[15];
int _outputserialchar( char c, FILE *t) {
	Serial.write( c );
	return 1;
} 
int pins [3] = {2, 3, 4};

void setup()
{
  for(int i = 2; i<6; i++) pinMode(i, OUTPUT);
  
  fdevopen( &_outputserialchar, 0); 
  Wire.begin();
  Serial.begin(9600);
  SPI.begin();
  Serial.println("Initializing Card");
  pinMode(10, OUTPUT);
  RTC.begin();
  headerW();
  
}

void loop()
{
  
  dustsensor();  //Dust sensor
  E_NO2(); //E_NO2 sensor
  E_CO(); //E_CO sensor
  E_NO(); //E_NO sensor
  MOS();
  temperature(); //Temp sensor
  FtoS();
  DnT();
  SDWrite();
}

void temperature() {
  //Read Value of 4051 analog-in 5
  boolean five [3] = {HIGH, LOW, HIGH};
  for(int i=0; i<3; i++) digitalWrite(pins[i], five[i]);
  delay(1);
  int readInFive = analogRead(0);
  float kelvin = readInFive * 0.004882812 * 100;
  celsius = (kelvin - 273.15) - 5;
}

void dustsensor() {
  //Read Value of 4051 analog-in 1
  boolean one [3] = {HIGH, LOW, LOW};
  for(int i=0; i<3; i++) digitalWrite(pins[i], one[i]);
  delay(1);
  digitalWrite(ledPower,LOW);
  delayMicroseconds(delayTime);
  dust_Vout = analogRead(0);
  delayMicroseconds(delayTime2);
  digitalWrite(ledPower,HIGH);
}

void E_NO2() {
  //Read Value of 4051 analog-in 2
  boolean two [3] = {LOW, HIGH, LOW};
  for(int i=0; i<3; i++) digitalWrite(pins[i], two[i]);
  delay(1);
  int readInTwo= analogRead(0);
  e_NO2Value = readInTwo * 0.004882812;
}

void E_CO() {
//Read Value of 4051 analog-in 3
  boolean three [3] = {HIGH, HIGH, LOW};
  for(int i=0; i<3; i++) digitalWrite(pins[i], three[i]);
  delay(1);
  int readInThree= analogRead(0);
  e_COValue = readInThree * 0.004882812;
}

void E_NO() {
//Read Value of 4051 analog-in 4
  boolean four [3] = {LOW, LOW, HIGH};
  for(int i=0; i<3; i++) digitalWrite(pins[i], four[i]);
  delay(1);
  int readInFour= analogRead(0);
  e_NOValue = readInFour * 0.004882812;
}

void MOS() {
  NO2Value = analogRead(1) * 0.004882812;
  COValue = analogRead(2) * 0.004882812;
  O3Value = analogRead(3) * 0.004882812;
}

void FtoS() {
  floatToString(cel, celsius, 2);
  floatToString(dust, dust_Vout, 1);
  floatToString(e_NO2_Vout, e_NO2Value, 8);
  floatToString(e_CO_Vout, e_COValue, 8);
  floatToString(e_NO_Vout, e_NOValue, 8);
  floatToString(CO_Vout, COValue, 8);
  floatToString(NO2_Vout, NO2Value, 8);
  floatToString(O3_Vout, O3Value, 8);
}

void DnT() {
  DateTime now = RTC.now();
  sprintf_P(date_time,PSTR("%d/%d/%d %d:%d:%d"),now.day(),now.month(),now.year(),now.hour(),now.minute(),now.second());
}

void headerW() {
  if (!SD.begin(CS_pin))
  {
      Serial.println("Card Failure");
      return;
  }
  Serial.println("Card Ready");
  
  //Write Log File Header
  File logFile = SD.open("ARD1.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(", , , , , , , ,"); //Just a leading blank line, incase there was previous data
    String header = "Date_Time, Temp, e_NO2_Vout, e_NO_Vout, e_CO_Vout, CO_Vout, NO2_Vout, O3_Vout, Dust_Vout";
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
}

void SDWrite() {
  String dataString = String(date_time) + ", " + String(cel) + ", " + String(e_NO2_Vout) + ", " + String(e_NO_Vout) + ", " + String(e_CO_Vout) + ", " + String(CO_Vout) + ", " + String(NO2_Vout) + ", " + String(O3_Vout) + ", " + String(dust_Vout);
  File logFile = SD.open("ARD1.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(dataString);
    logFile.close();
    printf_P(PSTR("%s, %s, %s, %s, %s, %s, %s, %s, %s\r\n"), date_time, cel, e_NO2_Vout, e_NO_Vout, e_CO_Vout, CO_Vout, NO2_Vout, O3_Vout, dust); 
    pinMode(SD_led, OUTPUT);
    digitalWrite(SD_led, HIGH);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
  
  delay(29000);
  digitalWrite(SD_led, LOW);
  delay(1000);
}

This code shows data on Serial Monitor but doesn't write on SD card except for header and some gibberish.
Here's a link to check the csv file. https://rapidshare.com/files/3321600708/ARD1.CSV

Please suggest where is the problem.

Thanks.

Best,
Z

This code shows data on Serial Monitor but doesn't write on SD card except for header and some gibberish.

This is probably because you have two totally different ways of writing to the SD card and the serial monitor.
What you want to do is to write out to the monitor EXACTLY the same data as you are writing to the SD card at exactly the same point in the code.
For example:-

void SDWrite() {
  String dataString = String(date_time) + ", " + String(cel) + ", " + String(e_NO2_Vout) + ", " + String(e_NO_Vout) + ", " + String(e_CO_Vout) + ", " + String(CO_Vout) + ", " + String(NO2_Vout) + ", " + String(O3_Vout) + ", " + String(dust_Vout);
  File logFile = SD.open("ARD1.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(dataString);
    logFile.close();
    Serial.println(dataString); // testing if the data string is correct
    printf_P(PSTR("%s, %s, %s, %s, %s, %s, %s, %s, %s\r\n"), date_time, cel, e_NO2_Vout, e_NO_Vout, e_CO_Vout, CO_Vout, NO2_Vout, O3_Vout, dust); 
    pinMode(SD_led, OUTPUT);
    digitalWrite(SD_led, HIGH);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
  
  delay(29000);
  digitalWrite(SD_led, LOW);
  delay(1000);
}

Why the very long delay at the end?

Thanks for reply, Mike.

I added Serial.println(dataString); to check and found that there is a problem in dataString. I am sure multiplexing is causing this issue since same code without multiplexing works perfectly.
I am not sure why dataString corrupts during the process. Please help me sort it out.

For writing to SD card I am using same way for both header and dataString. Please check in both functions I have used "logFile.println".
This

printf_P(PSTR("%s, %s, %s, %s, %s, %s, %s, %s, %s\r\n"), date_time, cel, e_NO2_Vout, e_NO_Vout, e_CO_Vout, CO_Vout, NO2_Vout, O3_Vout, dust);

is only a serial print command to print without consuming much RAM.

30 secs delay is sensor sampling period.

Best,
Z

I am sure multiplexing is causing this issue since same code without multiplexing works perfectly.

I can't see why that should be.

Take this line:-

String dataString = String(date_time) + ", " + String(cel) + ", " + String(e_NO2_Vout) + ", " + String(e_NO_Vout) + ", " + String(e_CO_Vout) + ", " + String(CO_Vout) + ", " + String(NO2_Vout) + ", " + String(O3_Vout) + ", " + String(dust_Vout);

and print out each variable separately and see if that shows up something wrong.

Thanks.
I now understand where is the problem.
Static buffer is overrunning.
With this code:

#include <SD.h>  //http://www.ladyada.net/products/microsd/
#include <Wire.h>
#include <SPI.h>
#include "floatToString.h"
#include "RTClib.h"
#include <avr/pgmspace.h>
#include <math.h>
int CS_pin = 10, SD_led = 8;
RTC_DS1307 RTC;
float COValue = 0, NO2Value = 0, O3Value = 0;
float e_NO2Value = 0, e_NOValue = 0, e_COValue = 0;
float celsius = 0;
int dust_Vout = 0, ledPower = 5, delayTime = 280, delayTime2 = 40;
char date_time[20];
char cel[10];
char e_NO2_Vout[10];
char e_NO_Vout[10];
char e_CO_Vout[10];
char dust[10];
char CO_Vout[10];
char NO2_Vout[10];
char O3_Vout[10];
int _outputserialchar( char c, FILE *t) {
	Serial.write( c );
	return 1;
} 
int pins [3] = {2, 3, 4};

void setup()
{
  for(int i = 2; i<6; i++) pinMode(i, OUTPUT);
  
  fdevopen( &_outputserialchar, 0); 
  Wire.begin();
  Serial.begin(9600);
  SPI.begin();
  Serial.println("Initializing Card");
  pinMode(10, OUTPUT);
  RTC.begin();
  headerW();
  
}

void loop()
{
  dustsensor();  //Dust sensor
  E_NO2(); //E_NO2 sensor
  E_CO(); //E_CO sensor
  E_NO(); //E_NO sensor
  MOS();
  temperature(); //Temp sensor
  FtoS();
  DnT();
  SDWrite(); 
  Serial.flush();
}

void temperature() {
  //Read Value of 4051 analog-in 5
  boolean five [3] = {HIGH, LOW, HIGH};
  for(int i=0; i<3; i++) digitalWrite(pins[i], five[i]);
  delay(1);
  int readInFive = analogRead(0);
  float kelvin = readInFive * 0.004882812 * 100;
  celsius = (kelvin - 273.15) - 5;
}

void dustsensor() {
  //Read Value of 4051 analog-in 1
  boolean one [3] = {HIGH, LOW, LOW};
  for(int i=0; i<3; i++) digitalWrite(pins[i], one[i]);
  delay(1);
  digitalWrite(ledPower,LOW);
  delayMicroseconds(delayTime);
  dust_Vout = analogRead(0);
  delayMicroseconds(delayTime2);
  digitalWrite(ledPower,HIGH);
}

void E_NO2() {
  //Read Value of 4051 analog-in 2
  boolean two [3] = {LOW, HIGH, LOW};
  for(int i=0; i<3; i++) digitalWrite(pins[i], two[i]);
  delay(1);
  int readInTwo= analogRead(0);
  e_NO2Value = readInTwo * 0.004882812;
}

void E_CO() {
//Read Value of 4051 analog-in 3
  boolean three [3] = {HIGH, HIGH, LOW};
  for(int i=0; i<3; i++) digitalWrite(pins[i], three[i]);
  delay(1);
  int readInThree= analogRead(0);
  e_COValue = readInThree * 0.004882812;
}

void E_NO() {
//Read Value of 4051 analog-in 4
  boolean four [3] = {LOW, LOW, HIGH};
  for(int i=0; i<3; i++) digitalWrite(pins[i], four[i]);
  delay(1);
  int readInFour= analogRead(0);
  e_NOValue = readInFour * 0.004882812;
}

void MOS() {
  NO2Value = analogRead(1) * 0.004882812;
  COValue = analogRead(2) * 0.004882812;
  O3Value = analogRead(3) * 0.004882812;
}

void FtoS() {
  floatToString(cel, celsius, 0);
  floatToString(dust, dust_Vout, 0);
  floatToString(e_NO2_Vout, e_NO2Value, 5);
  floatToString(e_CO_Vout, e_COValue, 5);
  floatToString(e_NO_Vout, e_NOValue, 5);
  floatToString(CO_Vout, COValue, 5);
  floatToString(NO2_Vout, NO2Value, 5);
  floatToString(O3_Vout, O3Value, 5);
}

void DnT() {
  DateTime now = RTC.now();
  sprintf_P(date_time,PSTR("%d/%d/%d %d:%d:%d"),now.day(),now.month(),now.year(),now.hour(),now.minute(),now.second());
}

void headerW() {
  if (!SD.begin(CS_pin))
  {
      Serial.println("Card Failure");
      return;
  }
  Serial.println("Card Ready");
  
  //Write Log File Header
  File logFile = SD.open("ARD1.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(", , , , , , , ,"); //Just a leading blank line, incase there was previous data
    String header = "Date_Time, Temp, e_NO2_Vout, e_NO_Vout, e_CO_Vout, CO_Vout, NO2_Vout, O3_Vout, Dust_Vout";
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
}

void SDWrite() {
  //String dataString = String(date_time) + ", " + String(cel) + ", " + String(e_NO2_Vout) + ", " + String(e_NO_Vout) + ", " + String(e_CO_Vout) + ", " + String(CO_Vout) + ", " + String(NO2_Vout) + ", " + String(O3_Vout) + ", " + String(dust_Vout);
  Serial.println(date_time);
  Serial.println(cel);
  Serial.println(e_NO2_Vout);
  Serial.println(e_NO_Vout);
  Serial.println(e_CO_Vout);
  Serial.println(CO_Vout);
  Serial.println(NO2_Vout);
  Serial.println(O3_Vout);
  Serial.println(dust_Vout);
  
  File logFile = SD.open("ARD1.csv", FILE_WRITE);
  if (logFile)
  {
    //logFile.println(dataString);
    logFile.close();
    //Serial.println(dataString); // testing if the data string is correct
   // printf_P(PSTR("%s, %s, %s, %s, %s, %s, %s, %s, %s\r\n"), date_time, cel, e_NO2_Vout, e_NO_Vout, e_CO_Vout, CO_Vout, NO2_Vout, O3_Vout, dust); 
    pinMode(SD_led, OUTPUT);
    digitalWrite(SD_led, HIGH);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
  
  delay(29000);
  digitalWrite(SD_led, LOW);
  delay(1000);
}

problem is almost solved! except for temperature.
Temperature values are received with minus sign (say -218, -210..). There is no problem with formula used. My other devices (mega board) are working fine with same formula but without multiplexing. How can I solve this issue now?

I could use dynamic memory allocation if I had all code in the main loop (like before) but since now they are defined as functions, dynamic would be not easier to define.

Is there a better way for memory management?

Thanks.

Best,
Z

Temperature sensor is connected on 5th pin of 4051.
Readings are not changing even though I change to different pin.
I am using LM35 LM35 data sheet, product information and support | TI.com.

Readings become 'normal' when I connect it to a separate ADC Arduino pin.

What is on the inputs you switch to. If nothing then that is your answer, the reading will not change.

Why put all the strings into one long string before writing it to the SD card? You can build up the record by writing the strings one at a time.

Grumpy_Mike:
What is on the inputs you switch to. If nothing then that is your answer, the reading will not change.

I am sorry I didn't understand your question. Please clarify.
4051 Circuit arrangement:
S0: Digital pin 2 on Arduino
S1: Digital pin 3 on Arduino
S2: Digital pin 4 on Arduino
Pin16: 5V Arduino Pin
Pin 6,7 and 8: GND Arduino
Pin 3: Arduino Pin A0 ADC
Pin 14(i.e. 4051 ADC Pin 1), Pin 15(i.e. 4051 ADC Pin 2), Pin 12(i.e. 4051 ADC Pin 3), Pin 1(i.e. 4051 ADC Pin 4), Pin 5(i.e. 4051 ADC Pin 5): My sensors are connected on them. Temperature sensor is on Pin 5.

Grumpy_Mike:
Why put all the strings into one long string before writing it to the SD card? You can build up the record by writing the strings one at a time.

Because I want all my sensor values to be in the same row for every measurement.
If I use two separate strings then values would be written differently.

Thanks.

Best,
Z

Because I want all my sensor values to be in the same row for every measurement.
If I use two separate strings then values would be written differently.

logFile.println(myString);

will put stuff on a new line but

logFile.print(myString);

Will not.

I am sorry I didn't understand your question. Please clarify.

You said:-

Readings are not changing even though I change to different pin.

So what exactly do you mean by this?

I mean changing temperature sensor output pin to a different 4051 ADC pin. Currently it is connected to pin5 but I see no difference in reading even though it is connected to Pin6 or 7 or any other. Of course when I change pin, I modify code.

Yes, my bad. logFile.print(dataString); will write in the same line.
I will check it later this evening.

Thanks.

Best,
Z

I mean changing temperature sensor output pin to a different 4051 ADC pin. Currently it is connected to pin5 but I see no difference in reading even though it is connected to Pin6 or 7 or any other. Of course when I change pin, I modify code.

Again I am still not sure what this means.
Is it:-

  1. The reading is no different on either pins 5, 6 or 7 and it is what I expect.
  2. The reading is no different on either pins 5, 6 or 7 and it shows no change when I increase the temperature of the probe.
  3. I only get a correct reading when I use pin 5, when I use pin 6 or 7 there is no change when I increase the temperature of the probe.

Hi Mike,
It's option 1 and 2.
Readings show -119, -120, 40, -200, so and so. These are random with 90% of negative values. Also, temperature sensor is not faulty at all. When I connect it directly to Arduino ADC pin, it gives correct temp value.

This sounds like something is floating or not connected.
You get random readings like this when an input is not connected to anything.

What happens when you connect a pot to those inputs? Can you see the readings follow the pot? If not check your wiring again.

Sorry for late reply.

Problem is solved after cleaning the circuit with acetone.
Probably some dirt were creating problem. I didn't clean properly after soldering.. my fault.
But I am wondering why this affected only temperature sensor...

Thanks.

Best,
Z