Have a spare Raspberry Pi lying around? Well, we did, and what better day to use it than Pi Day! The idea behind this came from the fact that working in an office, there are always food thieves. Now, to combat those thieves you could do a number of things, but we decided to create a portable electronic safe for our pie, controlled by our Pi! Just a little bit of perception coming at you.
The electronics involved are minimal. We used a Raspberry Pi 3 Model A+ as the brains of the project. There is a hall effect sensor connected to GPIO17 (pin 11) on the Pi. The sensor has a logic output with an internal pull-up resistor setting it high. When a magnetic field is present the sensor’s output is pulled low, sending the signal to the Pi.
The low signal received by the Pi from the hall effect sensor then triggers a solid state relay on GPIO21 (pin40) of the Pi, to send a 12VDC to trigger a solenoid with a 12VDC coil. When the solenoid is triggered, the pin pulls back into the solenoid thus unlocking the case.
Now, you might be saying, “Anyone could open the case with a magnet then, how is that a safe?” Well, the enclosure we selected is totally sealed and non-transparent. This gives us the ability to hide the sensor so well, that we even forgot where it was at one point. (If you’re wondering if we looked silly dragging a magnet all over a LARGE black case, the answer is YES.) Another perk to this, if someone figures out where the sensor is located, you can move the sensor, similar to changing a password or combination.
To power all of this we used a dual AC to DC power supply with 5VDC out to run the Pi and 12VDC out to run the solenoid. This was the easiest way to power this project and for our purposes, it was ok that it required an outlet. Now, if you want to make this wireless, you can use a 12VDC battery pack with a 5VDC regulator to power the Pi. One thing to keep in mind is that if the battery dies, the solenoid will not trigger and thus the case will stay locked indefinitely. If you go the battery pack route, make sure you place them on the outside of the case or place a charging connector that can be accessed from the outside.
We added neopixels to the inner bottom portion of the case for a sort of “glowing” effect like you see on movies when a treasure chest is opened. When doing this it was discovered that if the Pi was powered from the USB port, there was a significant voltage drop while the neopixels were lit so we had to directly wire the pi to the power supply.
All of this is programmed with Python in Raspbian OS. The code is very straightforward. It has been embedded below for your use.
Overall the project isn’t super complex but was an extremely fun project that provided for an awesome video. There are a lot of modifications that you could make to this project. For example, the hall effect sensor could easily be switched out for RFID, a keypad, fingerprint scanner, etc. If you create this project, post it on social and use #MakeWithDigikey so we can see your versions! Happy Pi Day!
Pi Day 2019
Using Solid State Relay 255-2680-ND and hall effect sensor AH3582-P-BDI-ND.
Pull solenoid 1528-1191-ND is controlled by the SSR. This has a 12VDC coil,
Raspberry pi is 3.3v logic. SSR allows switching of the 12VDC supply to control
SSR Pin 1 is LED input, pin 2 is LED GND. 1.5v up to 50mA. Use a 50ish ohm resistor.
Pins 3&4 of SSR switch, with normally open operation.
Hall effect sensor can accept 3-28Vin. Pin 1 Vin, 2 GND, 3 Output. Internal
pullup, output is low when magnet approaches.
GPIO21 (physical pin 40) SSR output
GPIO17 (physical pin 11) Hall effect input.
GPIO18, (physical pin 12) Neopixel Data pin
import RPi.GPIO as GPIO
pixels = neopixel.NeoPixel(board.D18,150) # 150 is the number of pixels on your strip. Modify as needed.
GPIO.setup(21, GPIO.OUT, initial=GPIO.LOW) #SSR Output
GPIO.setup(17, GPIO.IN) #Input from hall effect sensor
lightsOn = 0 #Variable to control state of lights
while 1: #run this code forever
print (GPIO.input(17)) # Print status of hall effect sensor to shell.
while (GPIO.input(17)== 0): # While the hall effect sensor output is low (magnet close to sensor)
GPIO.output(21, 1) # Activate SSR/Solenoid
lightsOn = not lightsOn
pixels.fill((255, 200, 0)) # Golden "treasure chest" look. Values are RGB.
pixels.fill((0, 0, 0)) # turn lights off if lightsOn variable is = 0
time.sleep(1.5) # time to hold latch open
print (GPIO.input(17)) # Continue printing status