Now that I have my Arduino as well (though no display as yet), last night I put together a little test sketch just to see what the LM35 on its own provides.
The sketch is a simple loop that takes a reading, converts it to temp, checks against min and max values, and then prints the results out to the serial monitor. However - my first attempt resulted in 20.0C over and over again. I put in some extra print lines to troubleshoout the raw value coming from the A0 pin, and found it to be showing slightly different values each time as I would have expected, but after a little research I found the problem... I was using the simpler map() function to translate my analog value between 0 and 1023 into a temperature between 0 and 110C, rather than messy mathematics.
float celsius = map(pinVal,0,1023,0,110);
It turns out that the map() function ONLY produces INT values, so the small variations I was seeing on the analog pin, were not big enough to generate a whole degree of difference.
I reverted back to the mathematical approach, and started to get much better results, showing different readings varying by tenths of a degree (though adjacent readings separated by just 5 seconds could sometimes be half a degree different - so I question the accuracy of the sensor when in the open - perhaps it needs to be in an enclosure to protect it from drafts, etc.
float celsius = (pinVal/1024.0) * 110.0;
Now, with a steady stream of data arriving through the serial console and rapidly scrolling off the top, I thought it might be nice to see how long the sketch has been running, and initially just included the millis count (oh - by the way, I have discovered that the millis count doesn't reset every 9 hours or so as I originally thought - apparently that figure was a throwback to an old version from around 2007!! The current millis counter will run a little short of 50 DAYS before wrapping around to 0 again).
Then I figured it would be nice to show the elapsed time in an hh:mm:ss format, so set about converting it, like this...
Take the elapsed millis and divide it by the number of millis in an hour, to get an (int) hrs value
Subtract (hrs times the number of millis in an hour) from elapsed millis
Take the modified elapsed millis and divide it by the number of millis in a minute,
Subtract (mins times the number of millis in a minute) from elapsed millis
Take modified elapsed millis and divide by 1000 to get an (int) secs value
Finally, display each of the elements on the console.
I found that there is seemingly no easy way in Arduino code to do something like
print(hrs + ":" + min + ":" + secs)
so the simplest solution looks like being a whole bunch of Serial.print(); statements in my sketch. It ain't pretty - but at least it is easy to follow.
So finally, here is my little test sketch...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | int LM35Out = 0; // Analogue pin 0 float min = 100; float max = 0; int counter = 0; void setup() { // put your setup code here, to run once: Serial.begin(9600); analogReference(INTERNAL); // use 1.1V reference to get greater granularity on analogRead } void loop() { // Get reading and increment counter int pinVal = analogRead(LM35Out); counter++; // Convert analog value to temp (don't use MAP as it calculates int values only) // float celsius = map(pinVal,0,1023,0,110); float celsius = (pinVal/1024.0) * 110.0; // Check and set max and min values if (celsius > max) max=celsius; if (celsius < min) min=celsius; // Keep note of elapsed time unsigned long elapsed = millis(); // Convert elapsed millis into hh:mm:ss int hrs = elapsed/3600000; elapsed = elapsed - (hrs*3600000); int mins = elapsed/60000; elapsed = elapsed - (mins*60000); int secs = elapsed/1000; // Print results to serial monitor Serial.print("Reading: "); Serial.println(counter); Serial.print("Elapsed time = "); if(hrs<10) Serial.print("0"); Serial.print(hrs); Serial.print(":"); if(mins<10) Serial.print("0"); Serial.print(mins); Serial.print(":"); if(secs<10) Serial.print("0"); Serial.println(secs); Serial.print("Temp is now "); Serial.print(celsius,1); Serial.println("C"); Serial.print("Max was "); Serial.print(max,1); Serial.println("C"); Serial.print("Min was "); Serial.print(min,1); Serial.println("C"); Serial.println(); // Wait 5 seconds between readings delay(5000); } |

No comments:
Post a Comment