Questions And Answers

More Tutorials

Raspberry Adding an Alarm Function

In Project 1, we developed the function of displaying the temperature and humidity value on the LCD. We can now also further develop the functions of monitoring and alerting by adding a buzzer.

Hardware Connection

In this project, we will use the Grove - Passive Buzzer as the alarm device. Connect the buzzer to D16.

Write a Program

Expanding from Project 1, we only need to add code to judge the temperature and humidity and control the buzzer for the alarm behaviour. To do this, we need to use the logical operator. Logical operators can connect two or more simple statements to form more complex statements. We often use them in all kinds of conditional judgment statements. Here, we need to use Boolean “or". When the temperature “temp" is greater than 30 or the humidity “humid" is less than 30,
we would like the buzzer to give an alarm; otherwise, do nothing. To achieve this, we will use the “if...else..." conditional statement. To make the buzzer sound, recall that we can use the freq() function and duty_u16() function to define the tone and volume, and use “buzzer.duty_u16(0)" to turn off the buzzer.

1 if temp >30 or humid <30: 
2 buzzer.freq(1000) 
3 buzzer.duty_u16(1000) 
4 else: 
5 buzzer.duty_u16(0)#close

The complete program is as follows:

1 from lcd1602 import LCD1602 
2 from dht11 import * 
3 from machine import I2C, Pin, ADC, PWM 
4 from utime import sleep
6 i2c = 12C(1, scl=Pin(7), sda=Pin(6), freq=400000) 
7 d = LCD1602(i2c, 2, 16) 
8 d.display() 
9 dht2 = DHT (18) 
10 buzzer = PWM(Pin(16))
11 in
12 while True:
13 temp, humid = dht2.readTempHumid()#temp:
14 humid: 
15 sleep(1) 
16 d.clear() 
17 d.setCursor(0,0) 
18 d.print("Temp: "+str(temp)) 
19 d.setCursor(0,1) 
20 d.print("Humid:"+str(humid)) 
21 sleep(1)
22 if temp >30 or humid <30:
23 buzzer.freq(1000)
24 buzzer.duty_u16(1000) 
25 else:
26 buzzer.duty_u16(0)#close

Use a USB cable to connect Pico to the computer and click the “run" button to save the program to any location. Hold the temperature and humidity sensor with your hand, and you should see the changing values of temperature and humidity on the LCD. Give it a try. Will the buzzer sound an alarm?


In this page (written and validated by ) you learned about Raspberry Adding an Alarm Function . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry Intelligent Fan.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.