Temperature Sensor with buzzer

This is the code i use

float temp;
float tempC = 0;
float tempF = 0;
int buzzer = 7;
void setup()
{
pinMode(A0, INPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
}
void loop()
{
temp = analogRead(A0);
temp =((temp*5)/1024);
tempC = (temp-0.5)100;
tempF = ((tempC
9)/5 + 32);
Serial.print("Temperature = ");
Serial.print(tempC);
Serial.print(" C , ");
Serial.print(tempF);
Serial.println(" F ");
if (temp <=22)
{
noTone(buzzer);
delay(1000);
}
if (temp >= 18 && temp <= 22)
{
noTone(buzzer);
delay(1000);
}
if (temp > 23)
{
tone(7, 1000);
delay(200);
}
}

I can simulate it with no errors but i have a problem
if the temperature is greather than 23 there no sound coming from buzzer how to solve it?

You're displaying tempC and tempF, but your buzzer code relies on temp. An issue there I suspect.

I think you need to change temp inside if to tempC

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

As was written you are comparing apples (floats) to oranges (integers) is what you do

[color=#222222] if (temp > 23)[/color][color=#222222][/color]
[color=#222222]  {[/color][color=#222222][/color]
[color=#222222]    tone(7, 1000);[/color][color=#222222][/color]
[color=#222222]    delay(200);[/color][color=#222222][/color]
[color=#222222]  }

Is what I'd do

if (tempC > 23.0f)
  {tone(7, 1000);
   delay(200);}

See the difference? You are using the raw AD value (temp), I am using the calculated C value.

Also when using floats and you use a literal to compare a float value against, I found it is best to fully describe your literal.

23 describes a literal integer, 23.0f describes a float literal. A 23 integer is not really equal to a 23.0f float as some resolution is lost in the implicit conversion.

This can produce an incorrect result:

 tempC = (temp-0.5)*100;

This will reduce the chance of an incorrect result.

tempC = (temp-0.5f)*100.0f;

wildbill:
You're displaying tempC and tempF, but your buzzer code relies on temp. An issue there I suspect.

thank you sir for clarifying this

And thank you for your all replies i manage to fix it , it finally make sound .
I really appreciate all your replies
Thank you very much