Learning Objectives
- Emphasis: Software
- Hardware: Temperature Sensor/RGB LED
- Software: Convert sensor readings into usable values
Materials
- Arduino Uno
- Breadboard and wires
- RGB LED (common cathode)
- 3-330 ohm resistors
- Temperature sensor (TMP36 or similar)
Reference Files
Breadboard Layout
- RGB LED: Connect the RGB LED the same way you hooked it up in Dim the Lights. If your LED is common cathode, the long leg connects to ground. The single leg separated by the long leg is red; the two to the other side, from inside to out, are green and blue. Each light leg connects through a 330-ohm resistor to a PWM pin - we used 9-11.
- Temperature Sensor: The temperature sensor we're using is hooked up similar to a potentiometer. The outer two legs go to power and ground, and the inner goes to an analog read pin (A0). However, unlike the potentiometer, this sensor won't work if you hook it up backwards. If you're looking at the flat side, the leftmost pin is power and the rightmost is ground.
- You're done! Plug your Arduino into the USB port on your computer.
Reference Schematic
Software
- Getting started: You know the drill: open a new sketch and initalize variables for your light pins and temperature sensor input.
- Store RGB values: We want the RGB values to change based on temperature. To do this, we're going to map a range of temperature values to a specific group of RGB colors. We chose 12 colors based on this color wheel and made three arrays to store the corresponding values for the different colored lights. Check the code on the left for reference, or feel free to come up with your own values.
- Setup: Set your LED pins to output and initalize the Serial port so you can print the current temperature.
- Read temperature value: Read in the analog value from the temperature sensor and store it in a variable. Create another variable to store the value we're mapping to our RGB color index.
- Convert temperature to color: The TMP36 sensor has a range of -40 to 125 C, but we're only going to operate at or around room temperature, about 22 C/72 F. Our mood light responds to changes from 12-34 C, or ~54-93 F. The TMP36 returns 500mV at 0 C and increases by 10mV for every 1 C. Therefore, our range corresponds to readings from 620-840 mV. To write the code, we need to convert our analog value into mV, single out a certain range, and map that to values from 0-11. Got it? Don't worry if you don't understand the math; you can always use the reference code. The first line converts our reading into millivolts. The if statements confine our readings to the range around room temperature. Then we convert our millivolts into a value from 0-11 and write the corresponding color value from our stored color array into each PWM pin. Voila, instant color!
- Print temperature value: We can also convert our reading into C/F and print that to the Serial monitor. For this, we're going to store our value in a float mapped from our 0-1023 range to 0-5 V. To convert to Celsius, we're going to use the fact that 500mV = 0 C and each 0.01 V = 1 C. Check the code for the resulting formula. To convert to Fahrenheit, use the normal formula: F = C * 9/5 + 32. Print the temperature in either system to the Serial monitor.
- Experiment: The most obvious change comes from tweaking the RBG values. Can you make your sensor more or less sensitive? What if you wanted to measure the room temperature and set off an alarm if it's too hot or cold? Consider printing the temperature to the LCD instead of just the Serial monitor.
Reference Code
<-- Previous: Random Drawing Robot
Next: Spinner -->