How to Create a Motion Controlled Lamp with Raspberry Pi

How to Create a Motion Controlled Lamp with Raspberry Pi

raspberry pi motion control lamp

raspberry pi motion control lamp

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.

Get IoT Essential Guide & Enter to Win a Free Sensor Kit!

Recipe for This Project


  • Computer: Lenovo T420 Windows 10
  • SD Card: Sandisk DHCD 8GB
  • Microprocessor: Raspberry Pi Model B Revision 2.0 (512MB)
  • Expansion Board: GrovePi+ v3.0
  • Relay Switch: Grove Relay v1.2
  • Sensor: Grove Ultrasonic Sensor v2.0
  • WiFi Adapter: Edimax EW-7811Un 150Mbps Wireless 802.11 b/g/n nano USB Adapter
  • Monitor: Generic VGA Monitor with VGA Cable
  • VGA Adapter: Generic HDMI Input to VGA Adapter Converter For PC
  • Power Supply: Generic Powered USB 2 4-port HUB
  • Cables: USB Type-A / Micro-B; and USB Type-A / Type-B
  • Keyboard: Generic USB Keyboard
  • Mouse: Generic USB Mouse
  • WiFi Router: Generic WiFi Router
  • Plug: Straight Blade Extension Cord
  • Light: Generic Lamp <= 100 watts
  • Box: CAT5 Surface Mount Jack


  • SD Formatter v4
  • Dexter Industries Modified “jessie”
  • Win32 Disk Imager 0.9
  • Python 2.7.9


  • Wire Cutters
  • Cable Ties
  • Screw Driver Set
  • Voltage Meter

From Idea to Deployment

There are many steps in the development of a project from concept to deployment.

raspberry pi motion control lamp

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

raspberry pi motion control lamp

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 “” Disk Image and uncompress it to your hard drive.

2) Format an 8GB SD Card. I used SD Formatter v4.

raspberry pi motion control lamp

3) Write the downloaded Disk Image to the SD Card. I used Win32 Disk Imager.

raspberry pi motion control lamp

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.

raspberry pi motion control lamp

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.

raspberry pi motion control lamp

4) Select GrovePi from the drop down menu and Update in this order:

i) Hardware Firmware

ii) Raspbian

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/

4) COPY this code into the empty file window:System will ask to reboot upon completion:

raspberry pi motion control lamp

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

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/

raspberry pi motion control lamp

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.raspberry pi motion control lamp


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.

Get IoT Essential Guide & Enter to Win a Free Sensor Kit!