Home Automation¶

Description¶
Home automation is a big part of “The Internet of Things”… Nowadays, all of our devices and appliances are connected to provide us with a seamless control over all aspects of our home and more. With home automation, you decide how a device should react, when it should react, and why it should react. You can automate tasks such as opening and closing a gate based off of your personal preferences thus providing convenience, control, money savings, and an overall smarter home. All of this can be easily achieved with the help of a single Raspberry Pi.
Learning Objectives¶
- Coding, electronics, and physical science through a hands-on application that can be directly related to real life situations.
- Motion detection using ultrasonic sensors
- The use of frequency, period and duty cycle of a signal to control the movement of a servo motor
- IoT: How devices communicate with each other in real life applications
- The implementation of the engineering design process
- Application of theoretical concepts to real life situations
Materials Needed¶
- Raspberry Pi with micro SD card and Raspbian
- Breadboard
- Jumper wires
- Ultrasonic Sensor: HC-SR04
- Servo Motor
- LED lights
- 4x 1.5V Batteries + Casing
- Glue gun and glue sticks
- Cutter blades or scissors
- Thin sturdy foam board (optional: used for the home design)
Setup and Functionality¶
For our home automation project, we want to automate a few things around the house. Let’s start with the gate! When someone arrives at the gate, the ultrasonic sensor detects that someone is close. Then, the gate opens and the garden lights turn on. Few seconds later, the gate closes and the main door opens and the house lights up.
Circuitry and Electronics:¶
- LED lights: we have 2 sets of led lights
- 6 LED lights in the garden connected to GPIO pin 13
- 2 LED lights inside home connected to GPIO pin 12
- Ultrasonic sensor: we 4 wires to connect to the RPi
- VCC -> 5V
- Trig -> Pin 10
- Echo -> Pin 8
- GND -> GND
- Servo Motors: Every motor has 3 wires
- First motor:
- Red wire -> (+) Battery
- Brown wire -> (-) Battery
- Orange/yellow wire -> Pin 11
- Second motor:
- Red wire -> (+) Battery
- Brown wire -> (-) Battery
- Orange/yellow wire -> Pin 16

Note
- Only 2 out of the 8 LEDs are shown in the figure.
- Only 1 servo motor is shown
- The other servo is connected similarly but to pin 16
Programming¶
# import the needed libraries
import RPi.GPIO as GPIO
import time
# setup all GPIO pins
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)
GPIO.setup(8,GPIO.IN)
GPIO.setup(10,GPIO.OUT)
# we initiate the servo motors with pin 7
pwm1=GPIO.PWM(11,50)
pwm2=GPIO.PWM(16,50)
# define a function to control the ultrasonic sensor
# that returns the distance
def reading():
GPIO.output(10,False)
time.sleep(0.3)
GPIO.output(10,True)
time.sleep(0.001)
GPIO.output(10,False)
while GPIO.input(8)==0:
signalOff=time.time()
while GPIO.input(8)==1:
signalOn=time.time()
timePassed=signalOn-signalOff
distance=timePassed*17000
return distance
GPIO.cleanup()
while True:
distance=reading() # get the distance from the ultrasonic sensor
print distance
if distance<10: # if the distance is less than 10
GPIO.output(13,True) # turn the garden light on (led light)
pwm2.start(7.5) # open the gate (servo motor)
time.sleep(10) # wait for 10 seconds
pwm2.ChangeDutyCycle(22.5) # close the gate (servo motor)
time.sleep(0.1)
pwm2.stop() # stop the gate servo motor
time.sleep(1)
pwm1.start(7.5) # open the front door (servo motor)
GPIO.output(12,True) # turn on the house lights (led lights)
time.sleep(5) # wait for 5 seconds
pwm1.ChangeDutyCycle(12.5) # close the front foor (servo motor)
time.sleep(1)
pwm1.stop() # stop the front door servo motor
GPIO.output(12,False) # turn off all the led lights
GPIO.output(13,False)
GPIO.cleanup() # reset all the GPIO pins
Science Concepts and Skills¶
Flex your brain! Did you ever consider the security of the house members? This home automation project does not deal with security. Anyone can enter this house! How can you make it more secure?