Close

Humidity and Servo Functions

A project log for Raspberry Pi Weather App

Using the Raspberry Pi to get weather from the internet

vaneh-boghosianVaneh Boghosian 05/21/2018 at 22:300 Comments

5/21

Since the code to move the servo motor is lengthy, I created a function to move the servo by simply calling move_servo(num). The num is the amount of degrees the servo will move by.

I decided to try to move the servo based on the humidity percentage outside. If the humidity was less than 50%, the servo would move to 3 degrees (num=3) indicating a low humidity. If the humidity was greater than 50 but less than 70, the servo would move to 7.5 degrees (num=7.5) indicating a normal humidity. If the humidity was greater than 70 but less than 120 (random high number), the servo would move to 11 degrees (num=11) indicating a high humidity.

def move_servo(num):             #function to move servo             
    p = GPIO.PWM(19,50)           #19 is the pin 
    p.start(2.5)                             #Starting at 0 
    time.sleep(0.5)
    p.ChangeDutyCycle(num)    #Changing degree based on humidity      
    time.sleep(0.3)
    p.stop() 
    time.sleep(0.3)
    p = GPIO.PWM(19,50)          #After calling p.stop() must put this information again
    p.start(num)                          #Starting at the degree we left off at we will begin to                                                     go back to zero
    time.sleep(0.3)
    p.ChangeDutyCycle(2.5)      #Going back to 0
    p.stop() 
    time.sleep(20)

Discussions