Project 9.02 Doing Something Based on Temperature

In this project, you'll learn how to create a dynamic temperature-responsive display using the MC Trainer!
In this project we will use the temperature data to change the color of the Neopixels based on the temperature. The warmer the temperature to more red the Neopixels will appear. The colder the temperature sensor the more blue the Neopixels will appear.

Project Code:

				
					//////////////////////////////////////////////////////
// 9.02 - Doing Something Based on Temperature

#include <Adafruit_NeoPixel.h>

byte dataPin = 10;     
byte numberOfPixels = 2;  
byte brightness = 10;     

byte redValue = 0;      
byte greenValue = 0;
byte blueValue = 0;     

byte tempSensorPin = A3;

Adafruit_NeoPixel pixels(numberOfPixels, dataPin, NEO_GRB + NEO_KHZ800);
void setup() {
  pinMode(tempSensorPin, INPUT);

  pixels.begin();
  pixels.setBrightness(brightness);

  Serial.begin(9600);
}

void loop() {

  float currentTemperature = ReadTemperature();

  Serial.print("The temp in C is: "); Serial.println(currentTemperature);
  Serial.print("The temp in F is: "); Serial.println(CtoF(currentTemperature));

  UpdatePixelColorBasedOnTemp(CtoF(currentTemperature));
  delay(1000);

}

/*
   This function takes a temperature in degrees F and updates the neopixels to reflect the temperature.
   The hotter the temperature, the more red the neopixels.
   The colder the temperature, the more blue the neopixels.
*/
void UpdatePixelColorBasedOnTemp(float temperature) {
 
  int blueValue = map(temperature, 70, 80, 255, 0);
  blueValue = constrain(blueValue, 0, 255);
  
  int redValue = map(temperature, 70, 80, 0, 255);
  redValue = constrain(redValue, 0, 255);

  Serial.print("Blue: "); Serial.println(blueValue);
  Serial.print("Red: "); Serial.println(redValue);

  pixels.setPixelColor(0, pixels.Color(redValue, 0, blueValue));
  pixels.setPixelColor(1, pixels.Color(redValue, 0, blueValue));
  pixels.show();
}

/*
   This function reads a temperature in degrees C from the MCP9700AT-E/TT and returns it.
*/
float ReadTemperature() {
  int currentTempReading = analogRead(tempSensorPin);

  float currentTempVoltage = currentTempReading * (5.0 / 1024.0); 
  float temperature = (currentTempVoltage - .5) /  .01;         

  return (temperature);
}

/*
   This function converts a temperature passed in degrees C to degrees F and returns it.
*/
float CtoF(float temp) {
  return ((temp * 1.8) + 32);
}
//////////////////////////////////////////////////////
				
			

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

This project is fairly simple thanks to the functions we wrote in the last project. They are simply copied and pasted into this one.

The first thing that we need to do is include the Neopixel library.

				
					#include <Adafruit_NeoPixel.h>
				
			

Next, we declare some standard variables and include the constructor for the Neopixel library.

				
					byte dataPin = 10;       
byte numberOfPixels = 2;  
byte brightness = 10;     

byte redValue = 0;      
byte greenValue = 0;
byte blueValue = 0;     

byte tempSensorPin = A3;

Adafruit_NeoPixel pixels(numberOfPixels, dataPin, NEO_GRB + NEO_KHZ800);
				
			

After that in the setup() function, we set the pinMode of the analog pin connected to the temperature sensor, initialize the Neopixels, and setup Serial communication.

				
					void setup() {
  pinMode(tempSensorPin, INPUT);

  pixels.begin();
  pixels.setBrightness(brightness);

  Serial.begin(9600);
}
				
			

The loop() function starts just like the last project did. First, we get the current temperature.

				
					void loop() {

  float currentTemperature = ReadTemperature();
				
			

Next, the temperature is printed out in Celsius and Fahrenheit.

				
					Serial.print("The temp in C is: "); Serial.println(currentTemperature);
Serial.print("The temp in F is: "); Serial.println(CtoF(currentTemperature));
				
			

Let’s take a look at the next function in the loop(), “UpdatePixelColorBasedOnTemp”. From the function description we know that this function takes a temperature in degrees Fahrenheit and updates the Neopixels to reflect that temperature. This is why the temperature is passed to the function with the CtoF function.

				
					UpdatePixelColorBasedOnTemp(CtoF(currentTemperature));
				
			

Inside the function we can see what is really happening. First the blue value is mapped to the temperature range of 70 – 80 degrees F.

The parameters in this implementation of the map function are a bit different than usual. You’ll see that the higher end of the output range is mapped to the lower end of the temperature range, and vice versa. This means that when the temperature is 70 degrees the output will be 255 and when the temperature is 80 degrees the output will be 0. This is an easy way to invert the output range to go from high to low. We need this because as the temperature gets lower, we want a larger blue value.

				
					int blueValue = map(temperature, 70, 80, 255, 0);
				
			

Then the constrain function is used to ensure we stay in the proper output range.

				
					blueValue = constrain(blueValue, 0, 255);
				
			

Next, we will use the map function normally to map the temperature range to the red output range. The constrain function is used to keep the values in the proper range.

				
					int redValue = map(temperature, 70, 80, 0, 255);
redValue = constrain(redValue, 0, 255);
				
			

Then these values are printed out onto the Serial port.

				
					Serial.print("Blue: "); Serial.println(blueValue);
Serial.print("Red: "); Serial.println(redValue);
				
			

After that the Neopixels are loaded and updated.

				
					  pixels.setPixelColor(0, pixels.Color(redValue, 0, blueValue));
  pixels.setPixelColor(1, pixels.Color(redValue, 0, blueValue));
  pixels.show();
}
				
			

Going back to the loop() funciton, after the UpdatePixelColorBasedOnTemp function, a small delay is implemented to keep the Serial port from flooding and the loop() is ended.

				
					  delay(1000);

}
				
			

Learn More In Our Free Instructional Guidebook

This comprehensive guidebook for the 1st Maker Space Microcontroller Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of microcontrollers, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on learning, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Project 1.00 Blink

In this project, you’ll learn how to blink an LED!

Project 1.01 Blink x2

In this project, you’ll learn how to blink more than one LED!

Bri Wagner

Education Solution Specialist

Bri Wagner brings a bachelor’s degree in Elementary Education and nine years of classroom teaching experience to her role at 1st Maker Space. A former Teacher of the Year Award recipient, Bri is passionate about creating engaging, hands-on learning experiences that inspire student success. She understands the impact that innovative instruction and meaningful classroom resources can have on student achievement and is dedicated to partnering with educators to bring high-quality STEM solutions into classrooms. Bri is committed to empowering teachers and helping students develop the creativity, confidence, and problem-solving skills they need to thrive.