This tutorial will take you through the steps of building an automatic lamp with motion control.
This project will be built using a Raspberry Pi Model B with the GrovePi expansion board from Dexter Industries along with their Ultrasonic sensor and Relay module. We’ll be using a custom Raspbian disk image for this project, running on the latest “jessie” build.
The goal of this tutorial is to demonstrate a working prototype and show you the steps you need to test and revise this design in order to refine and deploy to fully execute your idea.
There are many steps in the development of a project from concept to deployment.
STEP I – Concept
When I come home I have a foyer that’s pretty dark. I often have my hands full and I want the lamp in the room to automatically turn on when I enter. Motion control is the perfect solution.
STEP II – Design
You can use a simple python program to test for a specific distance variable from an Ultrasonic sensor and control a high voltage relay when the sensor variable is triggered. Plug a lamp into the relay and it will turn on when someone enters the room.
STEP III – Prototype
This prototype uses the GrovePi expansion board from Dexter Industries to manage the RaspberryPi GPIO pins. I like using this expansion board because of its plug & play design and the modified Raspian disk image from Dexter Industries that has a whole host of troubleshooting tools, which comes in handy during the testing phase.
A. Setting up the Disk Image (PC)
1) Download the “04.25_Dexter_Industries_jessie.zip” Disk Image and uncompress it to your hard drive.
2) Format an 8GB SD Card. I used SD Formatter v4.
3) Write the downloaded Disk Image to the SD Card. I used Win32 Disk Imager.
B. Assembling the the RapberryPi and GrovePi GPIO Expansion board
1) The GrovePi lines up with the 28 GPIO pins on the RaspberryPi Model B. It’s a snug fit but with gentle pressure it fits (see video).
C. Start up the RaspberryPi & Update
1) RaspberryPi boots directly into the Dexter Industries GUI.
2) Click on the WiFi Setup icon to connect to your local router and the internet.
3) Click on the DI Software Update icon to launch.
4) Select GrovePi from the drop down menu and Update in this order:
i) Hardware Firmware
iii) Dexter Software
5) System will ask to reboot upon completion
D. Setup Python Program to run relay
1) Open File /home/pi/Desktop/GrovePi/Software/Python/test_script
2) GOTO [Edit] >[Select All] AND [Delete]
3) GOTO [File] >[Save As] /home/pi/Desktop/Documents/Range_Relay.py
4) COPY this code into the empty file window:System will ask to reboot upon completion:
5) [File] [Save]
E. Connect Grove Relay & Ultrasonic Sensor
1) Connect the Ultrasonic Ranger to D4 connector on GrovePI Expansion Board
2) Connect the Grove Relay to the D2 connector on the GrovePi Expansion Board
3) [Run] [Run Module] in the Range_Relay.py
4) Test sensor and make sure Relay LED turns on when there is motion in front of the Ultrasonic Ranger.
F. Wire up the High Voltage side of the relay and test
1) Using a twin blade extension cord cut the ‘Hot’ wire in the middle of the cord and gentle peel it back being careful not to cut into the insulation for the ‘Neutral’ wire (see video).
G. Setup RaspberryPi to be headless and automatically run the relay program
1) GOTO [Menu] >[Other] >[LXTerminal]
2) ENTER at [pi@dex:~ $] prompt:
sudo crontab -e.
3) If the file doesn’t exist, then create it and add the following line (this is where you should have saved the Python program):
@reboot sudo python /home/pi/Documents/Range_Relay.py
4) When the RaspberryPi boots the program should load
5) Go test your motion control creation on your favorite lamp
STEP II – Testing
1) I’ve hooked the Raspberry Pi up to my hallway lamp to test it in the field.
This was a fun project without too many moving parts. I’m excited to be able to expand the programming of this motion control lamp and put in a socket condition that will allow a connected smartphone or tablet to also control the lamp. I hope you enjoyed this tutorial and please send me us any modifications you’ve made to this project.