Question
The following Python code for a raspberry pi, I have added a temparature sensor which works, however, I also added a sound buzzer and an
The following Python code for a raspberry pi, I have added a temparature sensor which works, however, I also added a sound buzzer and an ultra sonic ranger. I would like to add the buzzer and the ultra sonic ranger such that when i move an object closer to the sonic ranger and the temperature changes the buzzer buzzes. how can i achieve this. and the led to blink everytime the buzzer goes off
Guidelines for distance
Ultrasonic Ranger Distance Buzzer value
<20cm 0
<20cm 15
<15cm 25
<10 cm 50
#importing the necessary libraries import math from time import * from grovepi import *
#define a global variable to store teh digital port (D3) #************************************************* #Connet the temperature Sensor(DHT) # to a digital port(in this example) #we will use (D3) #*************************************************
temp_sensor3 = 3 #connect DHT11to D3 sound_buzzer5= 5 #connect the Sound Buzzer to D5 ultrasonic_ranger6=6 #connect the ultra sonic ranger on 6
myLED4= D4 # connect the LED to D4
#create a function to read temperature and humidity sensor data #*************************************************
#function: read_temp_sensor() #this function will capture sensor #data from DHT11 via GrovePi #**************************************************
def read_temp_sensor(): try: #create a vector of two elements to store #temperature and humidity values #->grovepi.dht is a method that returns 2, # values(first in temperature where as, # the second is humidity) #arg1: defines the port number #arg2: type of DHT sensor(0 for this kit) [temp,hum] = dht(temp_sensor3,0) #ensure that the temperature and humidity values are not empty or null if ((math.isnan(temp) == False) and (math.isnan(hum) == False) and (hum >= 0)): temperature = temp #copy temp value into variable temperature humidity =hum #copy hum value into variable humidity print("Temperature = %.2f Celcius\tHumidity = %.2f% %" % (temperature, humidity)) except KeyboardInterrupt: print("exiting...") except IOError as IOe: print("An error has occured. %" %IOe) except Exception as e: print("some error") #define the main of the program (loop indefinately to read sensor data unless there is an interrupt) #******************************************************* #main program #loop indefinitly and sleep for 1 sec #between every date capture #program terminates by pressing #Ctrl + c #*******************************************************
while (1): read_temp_sensor() #capture sensore data time.sleep(1) #delay the next reading by 1 second before next iteration
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started