Alarm system project help

Hi guys I am currently working on this project where if the temperature detected by a temperature sensor is above a certain value, it will sound a piezo.
Im very new to arduino and this is my first project so please help.

this is the code im having problems with:

void setup()
Serial.begin(9600); //Start the serial connection with the computer
//to view the result open the serial monitor

void loop() // run over and over again
//getting the voltage reading from the temperature sensor
int reading = analogRead(sensorPin);

// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 5.0;
voltage /= 1024.0;

// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
Serial.print(temperatureC); Serial.println(" degrees C");

delay(1000); //waiting a second
if (??? > 30)


the variable that i want to put in the ??? is the temperature detected but im unsure what to write. Please help :slight_smile:

p.s so its like if temperature > 30 then it will sound the piezo.

Hi, first thing is to put your sketch between the hash marks to that it is more easily readable.

Second, can you sound the piezo ?

Look at the example code "button" instead of turning on an LED. or, in addition to turning on an LED, turn on the piezo.

If you want to set it to run for 5 seconds, then add a delay and then turn it off.

Learn how the IF statement for the button works and the answer should present itself.

by the way, a subject line that is informative about what you are looking for helps us in many ways. it helps those of us looking at the topics and helps us find an old topic.

dave-in-nj: Hi, first thing is to put your sketch between the hash marks to that it is more easily readable.

Let me clarify that.

When you post your code here, highlight it and use the "[ # ]" icon to put it in "code" tags so that it comes up properly formatted in a "code" window. It makes it much easier to deal with, and you can go back and do that on your previous post by using the "modify" option.

Or do this: In the Arduino IDE click Edit then click Copy for Forum and Paste in reply.


It would look something like

if (temperatureC > 30.0)  // Note the 30.0 is now float also

Does your code / hardware work just writing to the serial monitor?