Go Down

Topic: Multiple LM35 sesnors (Read 1 time) previous topic - next topic

Craig_Minca

Hi guys, bit of newby any advice would be greatly appreciated.
Im trying to run multiple LM35 sensors at once to turn on a cooling fan, but if i add one in parallel the temp becomes very inaccurate, if i add a sensor in a different port it then ignores the original. I have bought a heap of lm35 sensors and are hoping to use them in this project.  code as follows.

// Declare variables

float tempC;
int tempPin = 0;
int ledPin = 13;
int fan1 = 5;
void setup ()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(fan1, OUTPUT);
  }

void loop()
{
  tempC = analogRead(tempPin);
  tempC = (5.0*tempC*100.0)/1024.0;
  Serial.print((byte)tempC);
  if (tempC >25)
{
digitalWrite(ledPin, HIGH);
digitalWrite(fan1, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
digitalWrite(fan1, LOW);
}
delay(3000);

 
}

zoomx

I believe that this sensor cannot works in parallel with another one. You must use another analog pin. Take care of electric noise.

Craig_Minca

yes i have tried 2 different sensors on different  pins but it only reads of the new pin....... thanks for the tip on electrical noise.

Wawa

#3
Apr 12, 2016, 04:29 am Last Edit: Apr 12, 2016, 04:34 am by Wawa
Maybe this helps.
Just a hobby coder. Untested.
Leo..
Code: [Select]
float tempC1;
float tempC2;
float tempC3;
int tempPin1 = A0;
int tempPin2 = A1;
int tempPin3 = A2;
int ledPin = 13;
int fan1 = 5;

void setup ()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(fan1, OUTPUT);
}

void loop()
{
  tempC1 = analogRead(tempPin1);
  tempC1 = (5.0 * tempC * 100.0) / 1024.0;
  tempC2 = analogRead(tempPin2);
  tempC2 = (5.0 * tempC * 100.0) / 1024.0;
  tempC3 = analogRead(tempPin3);
  tempC3 = (5.0 * tempC * 100.0) / 1024.0;

  Serial.print(tempC1, 1); // one decimal place
  Serial.print("  ");
  Serial.print(tempC2, 1);
  Serial.print("  ");
  Serial.println(tempC3, 1);

  if (tempC1 > 25 || tempC2 > 25 || tempC2 > 25) // if 1 or 2 or 3 is >25C
  {
    digitalWrite(ledPin, HIGH);
    digitalWrite(fan1, HIGH);
  }
  else
  {
    digitalWrite(ledPin, LOW);
    digitalWrite(fan1, LOW);
  }
  delay(3000);
}

npkamen

How are you reading the second sensor?

Your code only has one set of variables and read functions for one sensor.

JimboZA

How are you reading the second sensor?

Your code only has one set of variables and read functions for one sensor.
I wondered that too, but I wasn't sure if that was OP's final code. But anyway, wawa's code at #3 a couple of hours ago takes care of that issue.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

npkamen

I wondered that too, but I wasn't sure if that was OP's final code. But anyway, wawa's code at #3 a couple of hours ago takes care of that issue.
Yes but the OP won't learn if they just get everything fed to them.  This is really a very straightforward problem which should have an obvious solution. 

Wawa

Guilty as charged.
There is a lot more to improve though.
An LM35 is a poor performer with simple code like this.
One degree C accuracy/stability at best.
Reading the sensors with 1.1volt Aref enabled, multiple reads, and/or some form of smoothing and calibration is needed to get accuracy and stability <= 0.1 degree C.
Leo..

outsider

Putting a 1k resistor between LM35 output and analog in pin + 0.1uF between analog in and GND, I was able to get 0.1 degree resolution and good stability with this sketch, maybe I'm lucky or its a fluke, but it works great. I don't have a second LM35 to try a multi sensor version.

Code: [Select]



// LM35 thermometer, no floats, no delays


byte numSamples = 8;

int8_t fudge = 90; // adjust for calibration

int kAref = 1090, // analog ref voltage * 1000
    kNumSamples = numSamples * 1000,
    fin = 2000,
    tempC,
    tempF;
    
uint32_t temp,
         start;

void setup()
{
  Serial.begin(9600);
  analogReference(INTERNAL);
  analogRead(0);
  for(int i = 0;i < numSamples;i++)
    temp += analogRead(0);
}
void loop()
{
  if(millis() - start > fin){
    start = millis();
    temp -= (temp / numSamples);
    temp += analogRead(0);
    tempC = temp * kAref / (kNumSamples + fudge);
    tempF = (tempC * 18 + 3200) / 10;
    Serial.print(analogRead(0));
    Serial.print("\t");
    Serial.print(temp);
    Serial.print("\t");
    prntTempC();
    prntTempF();
    Serial.println();
  }
}
    
void prntTempC(){
  Serial.print(tempC / 10);
  Serial.print(".");
  Serial.print(tempC % 10);
  Serial.print("\t");
}
void prntTempF(){  
  Serial.print(tempF / 10);
  Serial.print(".");
  Serial.print(tempF % 10);
  Serial.print("\t");
}  



gpsmikey

Make sure you understand the difference between "precision" and "resolution"  - "0.1 degree resolution and good stability" does not mean it was accurate to 0.1 degree - for that you have to work the calibration issue.  Good stability and resolution is a good start, but it needs to be calibrated for what you are asking.
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3

Craig_Minca

wow, thanks Wawa, ill try that. As far as reading the other sensors i added another pin for each senor and another tempc. Yes i am very much a newbie, and have recently re-kindled my love electronics and cant wait to get a handle on code writing. Thanks all for you inputs.

Go Up