Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Babs  Ogunbanwo
Published © GPL3+

PocketBeagle Automatic Light Switch

Create an automatic light switch using a simple servo, PocketBeagle board, and Adafruit PIR motion sensor!

BeginnerFull instructions provided3 hours1,825
PocketBeagle Automatic Light Switch

Things used in this project

Hardware components

Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1
Female Header 20 Position 2 Row (0.1")
Female Header 20 Position 2 Row (0.1")
×2
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Hitec HS-422 Servo Motor
×1
MicroSD card
×1
PocketBeagle
BeagleBoard.org PocketBeagle
×1

Story

Read more

Schematics

Servo and Motion Sensor Connectivity

This is an example diagram for connecting the servo and PIR motion sensor to the pocketbeagle on a breadboard.

Code

Servo Control Code Using PIR Motion Sensor

Python
Open Cloud 9 (192.168.7.2:3000) on a web browser and add this python file to a directory.
# Import Libraries
import Adafruit_BBIO.PWM as PWM
import Adafruit_BBIO.GPIO as GPIO
import time
# Initial Variables
servo = "P2_1"
pir_value = 0
PIR_PIN = "P2_4" # Pin number connected to PIR sensor output wire.
# Calibrate servo
PWM.start(servo,50,60)
time.sleep (1.0)
PWM.stop(servo)
PWM.cleanup()


# Get PIR Output
# Setup digital input for PIR sensor:
direction = GPIO.setup(PIR_PIN, GPIO.IN)
# Get PIR output from GPIO port
pir = GPIO.input(PIR_PIN)

# Main loop that will run forever:
old_value = pir_value
while True:
    pir_value = GPIO.input(PIR_PIN)
    print("pir_value = {0}".format(pir_value))
    
    if pir_value:
        # PIR is detecting movement!
        # Check if this is the first time movement was
        # detected and print a message
        if not old_value:
        # Rotate the servo CCW 180 degrees
            PWM.start(servo,0,60)
            time.sleep (1.0)
            PWM.stop(servo)
            PWM.cleanup()
            print('Motion detected!')
            
    else:
        # PIR is not detecting movement. Turn off LED.
        # Again check if this is the first time movement
        # stopped and print a message.
        if old_value:
        # Rotate the servo CW 180 degrees
            PWM.start(servo,50,60)
            time.sleep (1.0)
            PWM.stop(servo)
            PWM.cleanup()   
            print('No motion detected')
    old_value = pir_value
    time.sleep(5.0)
    # Press CTRL + C to end program

Credits

Babs  Ogunbanwo

Babs Ogunbanwo

1 project • 0 followers
Rice University Student

Comments