This project is part of a bigger project of mine, and it displays the ambient temperature on a set of 2 Common Cathode 7-Segment LED displays. The temperature sensor is the LM35 IC, which outputs voltage calibrated to degrees centigrade.
The LM35 is connected to the analog input of the Arduino board. The code running on Arduino then averages 10 temperature readings to reduce jitter, and then outputs the 2 digit integer temperature value to the 7-Segment display. To achieve the latter, it uses a set of 2 shift registers (IC 74HC595). This is a technique that minimizes the number of output pins required by Arduino to drive the display. You can read about it in detail here:
http://www.arduino.cc/en/Tutorial/ShiftOut
Code
The following code shows how to read the analog input from the LM35 and use shiftOut() to send data to IC 74HC595. There is also an optional countDown() method here which runs down from 99 to 0, for testing.
/*
* TempDisplay
* by MV - http://electro-nut.blogspot.com/
*
* Displaying ambient temeprature on a 2 x Common Cathode 7-Segment Displays
* Using 74HC595 shift register and the shiftOut() built in function
* Using LM35 to sense temperature.
*
* REFERENCES
*
* http://www.arduino.cc/en/Tutorial/ShiftOut
*
*/
// set this to 1 to run 99-0 countdown
#define TEST_MODE 0
int val = 0; // variable to store the value coming from the sensor
int lm35Pin = 0;
int DEBUG = 1;
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
// 7-segment digits 0-9
// 74HC595-->7-Segment pin Mapping:
// {Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7} --> {DP, a, b, c, d, e, f, g}
byte digits[] = {
B01111110, B00110000, B01101101, B01111001, B00110011, B01011011,
B01011111, B01110000, B01111111, B01111011
};
// set up
void setup() {
if(DEBUG) {
Serial.begin(9600);
}
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
// Main loop
void loop() {
#ifdef _TEST_MODE
countDown();
#else
// read the value from LM35.
// read 10 values for averaging.
val = 0;
for(int i = 0; i < 10; i++) {
val += analogRead(lm35Pin);
delay(500);
}
// convert to temp:
// temp value is in 0-1023 range
// LM35 outputs 10mV/degree C. ie, 1 Volt => 100 degrees C
// So Temp = (avg_val/1023)*5 Volts * 100 degrees/Volt
float temp = val*50.0/1023.0;
int tempInt = (int)temp;
displayNum(tempInt);
// serial output for debugging
if(DEBUG) {
Serial.println(temp);
}
#endif
}
// Display 2 digit number
void displayNum(int num)
{
// get digit 0
int dig0 = num % 10;
// get digit 1
int dig1 = (num/10) % 10;
// shift out digits
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digits[dig1]);
shiftOut(dataPin, clockPin, MSBFIRST, digits[dig0]);
digitalWrite(latchPin, HIGH);
}
// test function to count down from 99 to 0
void countDown()
{
// display numbers
for(int i = 0; i < 100; i++) {
displayNum(100-i);
delay(500);
}
}
Conclusion
There is not much original here, the Arduino page on ShiftOut pretty much explains everything.
As you can see above, the temperature does change.(But don't try this and burn down your IC or worse, your house!)
References
For details on 74HC595 and shiftOut:
http://www.arduino.cc/en/Tutorial/ShiftOut
For details on LM35:
http://www.facstaff.bucknell.edu/mastascu/elessonshtml/Sensors/TempLM35.html
[...CLICK HERE to read the full post...]