Sunday 28 February 2010

Arduino and TMP36 (temperature sensor)

Here is my first build of a temperature sensor and 7 segment LED connected to an Arduino Duemilanove, the display displays the first digit (10's) and pauses and then displays the second digit (units) pauses and then the decimal point to show end/start of temperature display.     

     

And here is the video:     





     
  

The full source code below

// Arduino - TMP36 and 7 segment LED
// By Chris Hawkins
// February 2010
// Temperature code from http://oomlout.com/TMP36/TMP36-Guide.pdf
//
// using bitRead help from connecting-a-7-segment-led-to-the-arduino-build-it/

// pins that 7 segment is connect to
int ledSegment[] = {
2,3,4,5,6,7,8,9};     

// Analog pin that TMP36 is connect to
int tempMonitor =0;     

// encode the on/off state of the LED segments for the characters
// '0' to '9' and 'DP' into the bits of the bytes
const byte numDef[11] = {
63, 6, 91, 79, 102, 109, 124, 7, 127, 103,128 };     

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

// Sets the pinMode for all LEDs
for (int x = 0; x<=7; x++)
{
pinMode (ledSegment[x],OUTPUT);
}     

// enable on by one each LED to test them.
for (int x =0;x<=7;x++){
digitalWrite (ledSegment[x],HIGH);
delay (50);     

}
// disable one by one to finish test of each LED
for (int x =0;x30 ){
setSegments( numDef[3] );
}
// check Temperature is over 20 and below 30
else if (temperature >=20 && temperature =10 && temperature =10){
temperature=temperature -10;
}     

// Print temperature to serial for use in processing
Serial.println(temperature);     

// Delay to display the 10's digit of the temperature
delay (1000);     

// now display unit of the temperature
for (int x = 0;x<=9;x++){
if (int(temperature) == x){
setSegments( numDef[x] );
}
}
// Delay to display the unit digit of the temperature
delay (1000);     

// Now display the decimal point (DP)
setSegments( numDef[10] );     

// delay to show DP
delay (2000);     

}     

void setSegments(byte segments)
{
// for each of the segments of the LED
for (int s = 0; s < 8; s++)
{
int bitVal = bitRead( segments, s ); // grab the bit
digitalWrite(s+2, bitVal); // set the segment
}
}     

float getVoltage(int pin){
return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}     


todos's : make code work easily with temperature greater than 30
Next project add two 7 segment LEDs and add shift registers

No comments:

Post a Comment