Arduino – Analog Temperature Sensor with LED Display

Description:

With this project we create an LED display to show users if a temperature reading is within certain limits. We use a Blue LED to signify that the temperature is Cold, Green LED to signify the temperature is within the right range, and a Red LED to show that the temperature is too hot. This project can be connected to a power adapter, or a USB battery and can run without needing to be connected to a computer.

For this project we will be adding to the code we used for the Arduino – Analog Temperature Sensor project. We will keep the Serial Monitor Output so that you can view the temperature that the sensor is reading, and then modify the conditional statements to set temperature thresholds that will work in the lab. (So if the room temperature is 75 then the Blue LED should be triggered if the reading iOS below 79, Green LED can be triggered between 79 and 85, and Red will turn on over 85)

You can put your finger on the analog temperature sensor in order to raise the temperature that the sensor reads.

Parts:

  • Arduino Uno
  • Breadboard
  • Analog Temperature Sensor
  • 3 LED’s
  • 3 220 Ohm Resistor
  • Wires

Code:

#define tempSensor A0

#define blueLed 8
#define greenLed 9
#define redLed 10

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

  pinMode(blueLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);
}

void loop()
{
  int reading = analogRead(tempSensor);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  float tempC = (voltage - 0.5) * 100 ;
  float tempF = (tempC * 9.0 / 5.0) + 32.0;

  Serial.print(reading); Serial.print(" reading - ");
  Serial.print(voltage); Serial.print(" volts  -  ");
  Serial.print(tempC); Serial.print(" degrees C  -  ");
  Serial.print(tempF); Serial.println(" degrees F");

  if (tempF <= 78) {
    digitalWrite(blueLed, HIGH);
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, LOW);
  } else if (tempF > 78 && tempF <= 84) {
    digitalWrite(blueLed, LOW);
    digitalWrite(greenLed, HIGH);
    digitalWrite(redLed, LOW);
  } else if (tempF > 84) {
    digitalWrite(blueLed, LOW);
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, HIGH);
  } else {
    digitalWrite(blueLed, HIGH);
    digitalWrite(greenLed, HIGH);
    digitalWrite(redLed, HIGH);
  }

}

Code with Comments:

  • Comments have been added for the LED Display, for comments about Basic Temperature Sensor Setup view the Arduino – Analog Temperature Sensor recipe
#define tempSensor A0

//define LED pins
#define blueLed 8
#define greenLed 9
#define redLed 10

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

//Set pinMode for LED pins to be OUTPUT
  pinMode(blueLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);
}

void loop()
{
  int reading = analogRead(tempSensor);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  float tempC = (voltage - 0.5) * 100 ;
  float tempF = (tempC * 9.0 / 5.0) + 32.0;

  Serial.print(reading); Serial.print(" reading - ");
  Serial.print(voltage); Serial.print(" volts  -  ");
  Serial.print(tempC); Serial.print(" degrees C  -  ");
  Serial.print(tempF); Serial.println(" degrees F");

//Set Conditional If/Else If/ Else Statements
//If tempF is 78 our below turn on Blue LED, and turn others off  
if (tempF <= 78) {
    digitalWrite(blueLed, HIGH);
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, LOW);
//If tempF is greater than 78 AND less than or equal to 84 turn on Green LED, and others off
  } else if (tempF > 78 && tempF <= 84) { 
    digitalWrite(blueLed, LOW);
    digitalWrite(greenLed, HIGH);
    digitalWrite(redLed, LOW);
//If tempF is greater than 84 turn on Red LED, and others off
  } else if (tempF > 84) {
    digitalWrite(blueLed, LOW);
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, HIGH);
//Final ELSE is used for troubleshooting if you get weird response.  Any tempF reading other than what has been defined before will turn All LED's on to show that there is a problem.
  } else {
    digitalWrite(blueLed, HIGH);
    digitalWrite(greenLed, HIGH);
    digitalWrite(redLed, HIGH);
  }

}