The Raspberry Pi Weather Station is constructed by connecting a temperature & humidity sensor and a servo motor to the Raspberry Pi to sense and indicate the temperature of its surrounding.
In this tutorial, we explain how to build a Weather Station with Raspberry Pi, a temperature & humidity sensor, a servo motor, and a Python program.
Connect the Temperature & Humidity Sensor to the corresponding GPIO pins on the Raspberry Pi. There are 3 pins on the Temperature & Humidity Sensor, namely, the VCC (Power Supply), Data (outputs temperature and humidity data), and ground. Connect these pins to the corresponding GPIO pins for 3V3 power, GPIO 4, and Ground on the Raspberry Pi.
The Raspberry Pi will be receiving temperature and humidity data from the Temperature & Humidity Sensor via GPIO 4.
Connect the Servo Motor to the corresponding GPIO pins on the Raspberry Pi. There are 3 pins on the Servo Motor, namely, the VCC (Power Supply), PWM Signal, and ground. Connect these pins to the corresponding GPIO pins for 3V3 power, GPIO 24, and Ground on the Raspberry Pi.
The Raspberry Pi will be modulating pulses to turn the servo via GPIO 24.
The wiring diagram below shows the connections between the Raspberry Pi, the Temperature & Humidity Sensor, and the Servo Motor.
Print the Scale and Indicator overlay. Cut the Scale overlay and paste it on a table or wall where you wish to display the temperature. Cut the Indicator overlay and paste it on the arm of the servo motor.
Â
Setting up the Software
Power on the Raspberry Pi. Run the terminal from the taskbar and type the following commands to install the CircuitPython DHT library:
The pins for the DHT22 Temperature & Humidity Sensor is initialized at GPIO pin 4
# set to use Broadcom GPIO numbers GPIO.setmode(GPIO.BCM)
# disable warnings GPIO.setwarnings(False)
# set servo pin as output GPIO.setup(servoPin, GPIO.OUT)
# initialize PWM on pin at 50Hz pwm = GPIO.PWM(servoPin, 50)
# start pwm with 0 duty cycle so it doesn't set any angles on start pwm.start(0)
The pins are initialized and are now ready to receive data from the Temperature & Humidity Sensor and to send data to the Servo Motor.
# create function so we can call this later def Set_Angle(angle): # calculate duty cycle from angle duty = angle / 18 + 2
# turn on servo pin GPIO.output(servoPin, True)
# set duty cycle to pin pwm.ChangeDutyCycle(duty)
# wait 1s for servo to move into position sleep(1)
# turn off servo pin GPIO.output(servoPin, False)
# set duty cycle to 0 to stop signal pwm.ChangeDutyCycle(0)
Create a Set Angle function so we can call this method in our code later. Instead of having to perform calculations every time we want to set the servo angle, we create a method so we can get all the calculations done by just calling the method.
# main loop while True: try: # Print the values to the serial port temperature_c = dhtDevice.temperature temperature_f = temperature_c * (9 / 5) + 32 humidity = dhtDevice.humidity print("Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format(temperature_f, temperature_c, humidity))
if temperature_c <= 29: Set_Angle(165) elif temperature_c <= 30: Set_Angle(135) elif temperature_c <= 31: Set_Angle(105) elif temperature_c <= 32: Set_Angle(75) elif temperature_c <= 33: Set_Angle(45) elif temperature_c <= 34: Set_Angle(15) except RuntimeError as error: # Errors happen fairly often, DHT's are hard to read, just keep going print(error.args[0]) continue
except Exception as error: dhtDevice.exit() raise error
sleep(1)
Read the sensor and set the servo angle accordingly. The DHT22 Temperature & Humidity Sensor will return an error once in a while – this is normal. Wait for a second before polling the sensor again to ensure data is refreshed.
# stop pwm on exit pwm.stop()
# release GPIOs on exit GPIO.cleanup()
Clear the PWM and release the GPIOs on program exit to free up resources.
Run the program, notice that the Raspberry Pi is reading and displaying the temperature and humidity of its surrounding, and printing it to the console.
Based on the temperature read from GPIO4, the Raspberry Pi sends a corresponding instruction to the servo motor to turn its arm.
*By completing this form you're signing up to receive our emails and can unsubscribe at any time.
Regular Price
$19.99
Sale Price
$19.99
Regular Price
$19.99
SaleSold Out
Unit Price
/per
Liquid error (snippets/product-form line 30): product form must be given a productLiquid error (snippets/product-form line 44): product form must be given a product