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.

Project Help and Ideas » Add buzzer to tempsensor project

March 09, 2010
by dcarter777
dcarter777's Avatar

Can anyone help me out with understanding how to set up the tempsensor project so that the piezo-electric buzzer goes off at a certain temperature?

I new to micro controller programming and the C language but used to pretty good at Fortran90 (not finding that very useful in the real world). Here is what I'm thinking I need to figure out how to do.

1) add an if statement in the code so that when the temp setpoint is reached one of the I/O pins on the micro controller goes from 0 to 5 volts to turn the buzzer on.

2) Figure out which register to set within the if statement to control the I/O pin I select. From the datasheet, pin PD7 seems like a good one to try to control with the if statement.

Basically I'm stuck trying to figure out how to flip the PD7 pin from low to high and back again with the if statement. I can probably figure out where to put the if statement and the syntax but knowing which register to trigger to control pin PD7 is beyond me right now.

What I'm shooting for is to be able to squeeze the temp sensor to get the temp to come up off of ambient and then have the buzzer go off at around 80F. Then if I let go of the sensor and it cools below 80F the sensor goes off.

Thanks in advance for any help.

PS. Thanks so much to the Nerdkits team for making micro controllers so accessible!

March 10, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi dcarter777,

Turning digital pin on and off is easy on our little microcontrollers. The NerdKits guide goes into it in more detail in the LED Blink project which comes right after the temperature sensor project. Lets say you wanted to use PB5 (using PB5 instead of PD7 because PD7 is used by the LCD).

I would first set PB5 to be an output pin on the DDRB register:

DDRB |= (1<<PB5);

Now I can set the pin high by setting the right bit on the PORTB register.

PORTB |= (1<<PB5);

Note I am using the |= operator here to set a bit on a register without affecting the other bits, you should take a little time to realize why that works. In order to set the pin low you merely need to set the bit on the PORTB register back to 0 (i'm going to let you figure out how to that though).

On another note, simply setting 5V across a piezzo buzzer will not make it buzz. A piezzo element merely deflects a little when you put a voltage across it. You need to rapidly change this voltage across the buzzer to make the element vibrate back and forth and create a sound. This could be as easy as switching the voltage every time you take a reading and realize that the temperature is still above your threshold, or you can use the built in PWM modules on the chip to drive the buzzer (which is a bit harder, but a great learning experience).

I think I've given you enough to think about for now. Take a stab at your project and let us know how you get along!


March 10, 2010
by dcarter777
dcarter777's Avatar

Thanks Humberto! Your explanation was just enough to get me there without totally handing it to me. With your hints, after poking around in the micro controller data sheet and the article on C programming at I got it working on my first try. Very exciting for me!

Now I'm going to work towards replacing the tempsensor signal with a tap on a deep cycle 12V battery (voltage varies from ~12V_DC to ~15.5 V_DC) and the buzzer with a relay to disconnect a 5 amp solar panel based on battery voltage. This will be a crude charge controller for simple solar system. I would like to work up to a system that uses shunts and counts amp hours drawn out of the battery then allows the panel to put in about 105% of those amp hours back during the charge cycle.

Thanks again!

March 11, 2010
by Rick_S
Rick_S's Avatar

Keep in mind, the voltage going into your ADC on the chip cannot be 12 volts. You will most likely damage your chip. To test the 12 volts, you will need to create a voltage divider of sorts to ensure that the voltage going to the ADC pin on your chip does not exceed the 5 volts your chip runs at. Didn't know if you were aware of this or not, but in case you weren't, I thought I'd give you a heads up.


Post a Reply

Please log in to post a reply.

Did you know that NerdKits has been featured in the MIT Undergraduate Research Journal? Learn more...