NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Tried adding led's to Temperature sensor project but now it reads hot with led's in place

December 25, 2009
by torydouglas
torydouglas's Avatar

Hi. I got my nerdkit today. After completing the temp sensor project and led project i wanted to try to have different led for the current state. For example green would be for good temperature range and red would be for too hot or to cold. So wrote some code and tried putting the leds in a couple different locations. Currently I have them on 15 and 16.

I created a new function

void checkTemperatureLight(double temp)
//enable pin for output for the red led and green led status lights
DDRB |= (1<<PB1) | (1<<PB2);

//test to turn on both lights
//PORTB |=(1<<PB1) | (1<<PB2);

if( (temp > 82.0) || (temp < 78.0))
    //turn on warning light (led) and turn off ok light
    PORTB |=(1<<PB1);
    PORTB &= ~(1<<PB2);
    //turn off warning light and turn on ok light (led)
    PORTB &= ~(1<<PB1);
    PORTB |= (1<<PB2);

When i try executing this i notice my temperature read out is a lot higher than i was earlier today with the other lab. If i put my finger on the sensor I can get it to around 99. Then as i pull out one led it drops about 10 degrees and then pull out the other and drops another 5 degrees.

So i am new to programming mcu's and electronics. Anyone know what is going on here and what i am doing wrong.

Thanks in advance.


December 25, 2009
by Rick_S
Rick_S's Avatar

Welcome to the forum! We hope to see you here often.

Without seeing your circuit, it sounds as if you are seeing a power fluctuation.

The temperature reading in the tempsensor program is determined using the ADC of the chip with VCC as a reference (external reference ADMUX=0 tied to VCC). The formula used for calculating temperature is based on the assumption you have a true 5V as VCC.

If your voltage to the chip drops below 5V, the formula becomes inaccurate and the temperature reported will be off. Depending on what you are using for power to the system and whether or not you are using current limiting resistors on your LED's, the supply voltage could be dropping causing the symptoms you are seeing.

If you are using battery power, try a fresh battery and see if the results change. If you haven't already used current limiting resistors on your LED's, try placing one in series between each LED and the micro-controller pin. A 1K resistor will often work fine.

If you are on battery power, you may want to look into an external power supply as spoken of in your guide. Batteries can pull down pretty quick at times and a good reliable external power source can be very helpful.


December 26, 2009
by BobaMosfet
BobaMosfet's Avatar

Even properly lighting the humble LED can pose necessary challenges. You must learn and understand the relationship of current and voltage. Your LED is a current device-- it is a Diode. This means that too much current through it can damage it or limit it's useful life. In order to protect it you must ALWAYS (Can't stress that enough- ALWAYS) put the appropriate resistor in series with it.

You must be aware of the fact that your power-supply will send as much current through your circuit as it can-- frying your circuit in the process if necessary to complete the path. Because, in reality, your entire project is viewed as nothing more than resistance (a controlled short circuit) to the power-source.

Ohm's and Watt's laws are your friend. Kirchhof and Thevenin come in later.

Your job, as a circuit designer, is to provide a controlled resistance to that current path, and derive some useful benefit in the process.

If you put a component on your board and see a voltage drop, that means you are drawing too much current. It is causing the regulator to collapse under the demand (it can't pass enough current at the regulated voltage to satisfy the demand). Add resistance to solve, and make sure you are connecting things correctly.

Just remember that your circuit presents both series AND parallel paths of resistance to the power-supply- each path must be designed correctly to prevent current from misbehaving and keep it's paths balanced.

December 26, 2009
by torydouglas
torydouglas's Avatar

Thanks Rick and BobaMosfet(love the name btw). I have put a 1000ohm resistor inline with both the led and now my voltage seems to be stable at 4.97v. I think you were both correct the led were drawing to much power and the voltage was dropping due to that. How would i know how much voltage an led can handle? Assuming i didn't buy it and have some spec sheet.

Thanks again for the help.

January 07, 2010
by BobaMosfet
BobaMosfet's Avatar


How much voltage will drop (aka, be LOST) across the LED.... that's easy-- use the diode tester function of your multimeter to check how much voltage it takes the meter to light the LED. It is a diode, after all. If it doesn't light in one direction, reverse the connections and try again-- the LED will pass current in only one direction.

IF you try both leads on the LED in both directions and it doesn't light-- it usually means your meter can't output sufficient voltage and current to drive the LED, so it's probably safe to assume you can drop 1.9 to 2.5 volts across the LED to get it to light.

If you don't have a diode tester that will work, you will have to find a datasheet for your type of LED (what is it made of, Gallium Arsenide, for example?) that will determine it's operating voltage range. Usually it changes based on color and material.

January 11, 2010
by ecornwell
ecornwell's Avatar

That's one thing I don't understand. I did a lot with Kirchhof and Thevenin in school but I don't really think I understood it properly. I love LED's and understand they need a current limiting resistor but I've never understood how to build or size them properly.

For instance, in the LED Array project, we don't use any of them. What if I want to put 2 in parallel? I'm assuming if the data sheet said for a 5v LED I would need 20ma current, I would need a ~250ohm resistor. But wouldn't there also be a voltage drop across the resistor? That's where I get confused.

Post a Reply

Please log in to post a reply.

Did you know that our USB NerdKit comes with everything you need to get started with microcontrollers? Learn more...