# Temperature Sensor with buzzer

**URL:** <https://forum.arduino.cc/t/temperature-sensor-with-buzzer/680007>\
**Category:** Programming\
**Created:** [October 26, 2020, 3:01am UTC](https://forum.arduino.cc/t/temperature-sensor-with-buzzer/680007 "2020-10-26T03:01:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknowniiix](https://avatars.discourse-cdn.com/v4/letter/u/dec6dc/32.png) [@unknowniiix](https://forum.arduino.cc/u/unknowniiix)\
**Post date:** [October 26, 2020, 3:01am UTC](https://forum.arduino.cc/t/temperature-sensor-with-buzzer/680007/1 "2020-10-26T03:01:54Z")

</div>

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?

 ![tmp.png](https://europe1.discourse-cdn.com/arduino/original/4X/d/5/c/d5c33a7d8bad5105d14095aa78aa305a6ca25084.png)

 ![tmp circuit.png](https://europe1.discourse-cdn.com/arduino/original/4X/3/1/5/31511793732aaa99924704d63070c937ff39aa4d.png)

 ![tmp.png](https://europe1.discourse-cdn.com/arduino/original/4X/d/5/c/d5c33a7d8bad5105d14095aa78aa305a6ca25084.png)

 ![tmp circuit.png](https://europe1.discourse-cdn.com/arduino/original/4X/3/1/5/31511793732aaa99924704d63070c937ff39aa4d.png)

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [October 26, 2020, 3:11am UTC](https://forum.arduino.cc/t/temperature-sensor-with-buzzer/680007/2 "2020-10-26T03:11:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![IoT\_hobbyist](https://avatars.discourse-cdn.com/v4/letter/i/e5b9ba/32.png) [@IoT\_hobbyist](https://forum.arduino.cc/u/IoT_hobbyist)\
**Post date:** [October 26, 2020, 7:02am UTC](https://forum.arduino.cc/t/temperature-sensor-with-buzzer/680007/3 "2020-10-26T07:02:18Z")

</div>

I think you need to change temp inside if to tempC

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [October 26, 2020, 9:57am UTC](https://forum.arduino.cc/t/temperature-sensor-with-buzzer/680007/4 "2020-10-26T09:57:37Z")

</div>

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](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... 🙂

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [October 26, 2020, 10:40am UTC](https://forum.arduino.cc/t/temperature-sensor-with-buzzer/680007/5 "2020-10-26T10:40:12Z")

</div>

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

```cpp
[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

```cpp
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.

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [October 26, 2020, 10:53am UTC](https://forum.arduino.cc/t/temperature-sensor-with-buzzer/680007/6 "2020-10-26T10:53:21Z")

</div>

This can produce an incorrect result:

```cpp
 tempC = (temp-0.5)*100;

```

This will reduce the chance of an incorrect result.

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

```

---

<div class="post-metadata">

**Author:** ![unknowniiix](https://avatars.discourse-cdn.com/v4/letter/u/dec6dc/32.png) [@unknowniiix](https://forum.arduino.cc/u/unknowniiix)\
**Post date:** [October 27, 2020, 12:26am UTC](https://forum.arduino.cc/t/temperature-sensor-with-buzzer/680007/7 "2020-10-27T00:26:37Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:49am UTC](https://forum.arduino.cc/t/temperature-sensor-with-buzzer/680007/8 "2021-05-06T00:49:10Z")

</div>


