Control the Door of the IoT House with Python

Example Description
Learn how to control the door of the IoT House with Python. Follow this step-by-step guide to control the servo motor and make the door open and close when you press the space key.

In this example, we will demonstrate how to control the door of the IoT House.

Circuit

Connect the servo motor to the Quarky Expansion Board servo pin 5.

Door Control

The door of the IoT House is controlled with a servo motor. You need to make the servo motor set to 0 angles before assembling the door. You can do it with the following code.

#Creating two objects called "quarky" and "expansion"
quarky = Quarky()

# The "expansion" object is now set to the "Expansion" class
expansion = Expansion()

# We are using the "moveservo" method from the "Expansion" class to make the servo motor 5 be set at 0-degree 
expansion.moveservo(5, 0)

Door Control Python Code

The following script makes the door closed by default and opens it for 1 second when the space key is pressed.

import time

sprite = Sprite('Tobi') # create a sprite object called 'Tobi'
quarky = Quarky() # create a Quarky object
expansion = Expansion() # create an Expansion object

expansion.moveservo(5,100); # move the servo on pin 5 to position 100

while True: # loop forever
	if sprite.iskeypressed("space"): # if the spacebar is pressed
		expansion.moveservo(5,0); # move the servo on pin 5 to position 0
		time.sleep(1) # wait for 1 second
		expansion.moveservo(5,100); # move the servo on pin 5 to position 100

Output

Press the space key to make the door open.

Table of Contents