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);
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.
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.
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.
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.
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.
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).
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.
#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);
}
}
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.