(5) LM35dz temp sensors to analog pin(s) - uno sketch

Hey everyone, I have a simple project that utilizes 5 LM35dz sensors / arduino uno r3 and a breadboard. I have all 5 set up on the breadboard w/ each center pin to analog pins 0-4 & 5v and grounds connected. I am using the following sketch to read the first lm53 on analog pin 0 and I can see the temp properly in the serial monitor just fine.

Now I want to read the other 4 sensors but need guidance on the sketch as I am unsure how to proceed. I have read that there needs to be a delay between readings to give the next sensor time to reflect properly.


int analogPin = 0;
int readValue = 0;
float temperature = 0;
float temperatureF = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
readValue = analogRead(analogPin);
//Serial.println(readValue);
temperature = (readValue * 0.0049);
temperature = temperature * 100;
temperatureF = (temperature * 1.8) + 32;
Serial.print("Int: ");
Serial.print(temperatureF);
Serial.println("F");
delay(1000);

}


thanks in advance for your patience and support

So I have updated the above sketch to include the other 4 sensors and things SEEM to be OK. Using the serial monitor I see temps from all 5 sensors that appear to be believable according to a digital thermometer I have next to my breadboard.

here is my sketch, please let me know if I am on the right track.

int analogPin0 = 0;
int readValue0 = 0; 
float temperature0 = 0; 
float temperature0F = 0;

int analogPin1 = 1;
int readValue1 = 1;
float temperature1 = 1; 
float temperature1F = 1;

int analogPin2 = 2;
int readValue2 = 2; 
float temperature2 = 2; 
float temperature2F = 2;

int analogPin3 = 3;
int readValue3 = 3; 
float temperature3 = 3;
float temperature3F = 3;

int analogPin4 = 4;
int readValue4 = 4; 
float temperature4 = 4;
float temperature4F = 4;

void setup() { 
Serial.begin(9600); 
}

void loop() { 
  readValue0 = analogRead(analogPin0);
  //Serial.println(readValue0);
  temperature0 = (readValue0 * 0.0049); 
  temperature0 = temperature0 * 100; 
  temperature0F = (temperature0 * 1.8) + 32;

  readValue1 = analogRead(analogPin1);
  //Serial.println(readValue1);
  temperature1 = (readValue1 * 0.0049); 
  temperature1 = temperature1 * 100; 
  temperature1F = (temperature1 * 1.8) + 32;
  
  readValue2 = analogRead(analogPin2);
  //Serial.println(readValue2);
  temperature2 = (readValue2 * 0.0049); 
  temperature2 = temperature2 * 100; 
  temperature2F = (temperature2 * 1.8) + 32;
  
  readValue3 = analogRead(analogPin3);
  //Serial.println(readValue3);
  temperature3 = (readValue3 * 0.0049); 
  temperature3 = temperature3 * 100; 
  temperature3F = (temperature3 * 1.8) + 32;
  
  readValue4 = analogRead(analogPin4);
  //Serial.println(readValue4);
  temperature4 = (readValue4 * 0.0049); 
  temperature4 = temperature4 * 100; 
  temperature4F = (temperature4 * 1.8) + 32;
  
  Serial.print("(Int): "); 
  Serial.print(temperature0F);
  
  delay(1000); 
  
  Serial.print(" (Ext): "); 
  Serial.print(temperature1F);
 
  delay(1000); 
  
  Serial.print(" (In): "); 
  Serial.print(temperature2F);
 
  delay(1000); 
  
  Serial.print(" (Out): "); 
  Serial.print(temperature3F);
 
  delay(1000); 
  
  Serial.print(" (Box)  : "); 
  Serial.print(temperature4F);
 
  delay(1000); 
}

ScreenHunter_02 Feb. 12 15.04.png

Now improve the code by using an array to hold the sensor pin numbers, a for loop to read the sensors in turn and a function to do the calculations. Your code will be about half the length or less.

Thanks for the tip(s) however that sounds a bit over my head. Any links you might know of for me to read up on would be a big help.

Here is your program modified to use a for loop and arrays. It compiles and produces output but I don't have any sensors to test it with so please test it and let me have some feedback.

const byte inPins[] = {0, 1, 2, 3, 4};
int readValue[5];
float temperature[5];
float temperatureF[5];

void setup() 
{ 
  Serial.begin(9600); 
}

void loop() 
{ 
for (int sensor = 0; sensor < 5; sensor++)  //read each sensor in turn 
{
  readValue[sensor] = analogRead(inPins[sensor]);  //put the values in an array
  
  temperature[sensor] = (readValue[sensor] * 0.49); 
  temperatureF[sensor] = (temperature[sensor] * 1.8) + 32;
}

  Serial.print("(Int): "); 
  Serial.print(temperatureF[0]);

  delay(1000); 

  Serial.print(" (Ext): "); 
  Serial.print(temperatureF[1]);

  delay(1000); 

  Serial.print(" (In): "); 
  Serial.print(temperatureF[2]);

  delay(1000); 

  Serial.print(" (Out): "); 
  Serial.print(temperatureF[3]);

  delay(1000); 

  Serial.print(" (Box)  : "); 
  Serial.println(temperatureF[4]);

  delay(1000); 
}

In order to fully understand the code you need to read up on arrays and for loops. Incidentally, once I moved the sensor reading into the for loop the need for a function to do the calculations was reduced so I did it in line.

You could further tidy the program by using a for loop to output the temperatures using an array to hold the text to be printed with each one. As it is the delay of 1 second between each temperature display is not necessary but I left it in because that is how you had programmed it.

Incidentally, your names for the variables made the conversion to using arrays easy because the clue was in the names with numeric suffixes.

Brilliant! Thank you so much UKHeliBob! I don't fully understand what you did but now I have it to study and reference for future use.

Basically it is simple, but then everything is when you know how to do it, of course.

The sensor pin numbers are in an array and can be referred to as inPins[0], inPins[1], inPins[2], inPins[3] and inPins[4]. The same with the temperature variables. With that in place you can use a for loop to cycle through the pin numbers and read the sensors. Note that the array references run from 0 to 4 but the pin numbers do not have to be sequential if you do not want/need them to be.

Using arrays and a for loop allows the same code to be used 5 times, the trick being to refer to the pin numbers and variables using the current value of the for loop variable which goes from 0 to 4. This is a very common technique and once understood you will find it very useful.

UKHeliBob, now that i have my sensors straightened out I would like to calibrate them to match each other while I have them side by side on the same breadboard. I will eventually relocate each sensor to it's new location.

(Int): 65.52 (Ext): 65.52 (In): 67.28 (Out): 67.28 (Box): 69.04

The average of all 5 senors is 66.92

is this possible?

You could certainly add a 'fudge factor' to the calculations by using (yet) another array to hold the FF to be applied to each sensor.

The display could then be made to display the same temperature for each sensor but that would only be the case at one, or at best a small range of temperatures, and it is probable that none of them would actually be correct anyway.

Having said that, I would have expected the outputs to be more consistent than those you quote.

It dawned on me after I posted my last comments that I have 10 sensors in my stash so I have replaced 2 and now have a closer set of temps. +/- .75 degrees which is acceptable for my project.

I have attached my Seeedstudio SD card shield v3.1 which has passed the Cardinfo sketch from the Examples using a 16gig micro sd card and have my 5 sensors connected thru the shield.

Now to get the sensor data to log to the sd card. Any tips?

iNteloper:
It dawned on me after I posted my last comments that I have 10 sensors in my stash so I have replaced 2 and now have a closer set of temps. +/- .75 degrees which is acceptable for my project.

But how do you know any of them are right? Just because they're close....

Fair question. I have a digital temp gauge next to my breadboard and the 5 sensors. after replacing a couple of sensors all 6 temps (including my digital temp gauge) are within .75 degrees of one another and for my project that is acceptable as I am trying to document relative temperature change over the coarse of a day(s).

let me have some feedback.

Use Tools + Auto Format!

PaulS:

let me have some feedback.

Use Tools + Auto Format!

Guilty as charged. Although I have to say that I don't like what Auto Format does to array declarations, but in this case it was forgetfulness on my part.

UKHeliBob, would you be so kind as to have a look at the sketch you helped modified? I took the next step in adding my SeeedStudio SD Card Shield to my Uno however after updating the sketch it is NOT logging any data.

![](http://www.epcr-co.com/Uploaded Pics/Uno_SD Card_ Sensors.JPG)

![](http://www.epcr-co.com/Uploaded Pics/serial_monitor.png)

#include <SD.h>

const int chipSelect = 10;

const byte inPins[] = {
  0, 1, 2, 3, 4};
int readValue[5];
float temperature[5];
float temperatureF[5];

void setup() 
{ 
  Serial.begin(9600); 

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop() 
{ 
  // make a string for assembling the data to log:
  String dataString = "";
  for (int sensor = 0; sensor < 5; sensor++)  //read each sensor in turn 
  {
    readValue[sensor] = analogRead(inPins[sensor]);  //put the values in an array

    temperature[sensor] = (readValue[sensor] * 0.49); 
    temperatureF[sensor] = (temperature[sensor] * 1.8) + 32;

    File dataFile = SD.open("datalog.txt", FILE_WRITE);

    // if the file is available, write to it:
    if (dataFile) {
      dataFile.println(dataString);
      dataFile.close();
      // print to the serial port too:
      Serial.println(dataString);

    }

    Serial.print(" (Int): "); 
    Serial.print(temperatureF[0]);
    Serial.print(" (Ext): "); 
    Serial.print(temperatureF[1]);
    Serial.print(" (In): "); 
    Serial.print(temperatureF[2]);
    Serial.print(" (Out): "); 
    Serial.print(temperatureF[3]);
    Serial.print(" (Box): "); 
    Serial.println(temperatureF[4]);

    delay(10000); 
  }
}

I know I have missed something simple.

You are doing far to much in the for loop that reads the sensors. Take all of the datafile stuff out of it. Collect the data in the loop then write it afterwards.

What is the purpose of the dataString variable ?

I know I have missed something simple.

Yep. You forgot to delete this:

  String dataString = "";