Drawer Open/Close LED Sensor with Raspberry Pi

After discovering how to run the Pi headless without an internet connection, I made a simple sensor to detect if one of my bureau drawers is opened or closed. Basically, an LED turns on if the drawer is closed, and turns off when the drawer is opened.

View the code here. I used a Model B Pi.

I wired the drawer like a pull-up resistor button setup, and made contacts with Meccano Erector set pieces and lots of electrical tape. I added an LED, and then wrote some Python 3 code to run headless on the Pi. The picture below shows the Model B Pi with the LED.

Drawer Log

This picture shows the contacts on the drawer. There’s thin piece of metal on the drawer, and the two contacts are connected to the red and black wires. When the drawer closes, the contacts both touch the metal strip, and the circuit becomes continuous.

side view of drawer wiring

Instructions For Running Headless on the Pi Without Wifi

Running headless mean having your Raspberry Pi run a program without having it hooked up to a monitor, keyboard, or mouse. Basically you just need to edit a certain system file called rc.local.

  1. Login to your Pi. Open up a terminal.
  2. Type cd /etc to change the current working directory (in Linux lingo, cd means ‘change directory’).
  3. To edit the rc.local text file, type sudo nano rc.localNavigate to the end of the file.
  4. Right before the exit0 line, add the line python3 /home/pi/code.py & where ‘/home/pi/code.py’ is the absolute location of the file you want to run, and the ampersand (&) means that you want to run the program in the background and let the Pi’s boot process continue. Note that you don’t need to add sudo because rc.local runs as root.
  5. The program will now run whenever you power the Pi, regardless if there is any peripherals attached. If you want to test it out, type sudo reboot in the terminal.
  6. Now you can showcase computing projects with your Pi without a monitor!
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s