Blog Stats

Visualizing temperature as color using an RGB led, a LM35 sensor and Arduino

I decided this weekend to work on a mini-project, since it came to my mind during the last week.

I found that my Arduino starter kit actually had an LM35 temperature sensor, it’s just that I noticed that quite late. The LM35 is a sensor that varies its ouput voltage proportionally to the temperature and it’s specially designed for the Celsius scale. The limits of what it can sense is from -55°C to 150°C. You can read more about it if you want from its datasheet.

When I set out to make this mini-project work, I thought it would be really simple to program it. And simple it was, until I had a couple of *facepalm moments.

I wanted to make the LED shine from blue to green, to yellow then red as the temperatures increases. I believe there are probably better methods of accomplishing this, but my logic told me to this:

the blue led will be at maximum brightness if the temperatures drops bellow 0°C, and will gradually decrease in intensity from 0°C to 45°C.

the green led will increase its brightness from 15°C to 35°C and then will gradually decrease until 75°C

the red led will increase its brightness from 45°C to 90°C, and will stay lit after 90°C

That was apparently simple until I opened the Arduino IDE. First of all, I used some code I found on this blog. I was mainly interested on the voltage-to-celsius conversion part, and I build upon that.

The problem was that I had no idea how to correctly decrease the intensity. At first, I instinctively implemented something like the function f(x)=1/x, but that obviously didn’t work. I then figured out that I should use something like f(x)=a-x, where a is a constant defined by me. Using the blue pin as an example, “a” would have been 25. So at 0°C I would have f(o)=25-0=25. At 24°C f(24)=25-24=1. But then that obviously needs to be converted to a value between 0 and 255, because that’s how PWM works on Arduino.

But then somebody suggested I use the map function with reversed values here. That was a glorious *facepalm moment. It was that simple! All I had to do was: map(tempC, 0, 25, 255, 0);

It’s not a perfect code, it’s not the perfect solution, but it works. Here’s the code:

Hi , that’s fantastic … I have the same idea for a project that i have to do on college but i have problem with the schematic-circuit .. i don’t know how to do it … that arduino is unknown for me … do you mind of giving me your schematic ? if there is a problem it’s fine …. thanks

I’d happily post a schematic, but I’ve done the project quite a while ago, and as you can guess, I don’t have it assembled anymore. It’s quite easy if you follow the pin initializations at the start of the code. Most of what is not connected to those pins goes to ground. Don’t forget about resistors for the LED! Here’s a quick guide on how to hook up your temperature sensor to Arduino: http://learn.adafruit.com/tmp36-temperature-sensor

I would love to, but this is project has been disassembled long time ago, and my materials are at my home. Besides the code, you just have to use 3 resistors for each LED anode pin( the positive pins, the ground just goes to GND on Arduino). The temp sensor is easy to connect as well, you just insert its output into the first analog socket, the ground to GND and the positive to +5V.

Hey I really enjoyed this post
This code was a conciderable help to me as far as understanding the code and how to build upon it
i made some changes for it so my computers tempature would have color as well but i edited it to have purple as the lowest colors temp
i also had the code take a average of 50 results to smooth out the transition aswell
here is my final products code and i hope this helps someone like it helped me

#define aref_voltage 3.3
const int numReadings = 50;

int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average

I'm curious, inquisitive and like to ask questions about the universe, about nature, about life.
Nothing I play ever feels like Half-Life, that's why I'm passionate about Valve's games.
I also do programming from time to time, and I like the concept of IoT.