Introduction: Camera Sound Trigger
In this project, I am using an Adafruit Perma-Proto Pi HAT with a Raspberry Pi 2 and a sound sensor to build a mechanism for triggering a camera by sound.
Step 1: Parts Needed
- Adafruit Perma-Proto Pi Hat
- Raspberry Pi 2
- 1 x LM393 Sound Sensor
- 1 x NPN Transistor (2N2222A)
- 1 x 1K Ohm Resistor
- 2 x 3.5mm stereo male plug solder connector
- 2 x 3.5mm panel mount jack sockets
- Wired remote trigger for the specific make and model of your camera
Step 2: Wire the Sound Sensor to 3.5mm Stereo Male Plug
The sound sensor I used is actually advertised for use with an Arduino microcontroller and run at 5V. Nevertheless, it works fine with a Raspberry Pi operating at 3.3V. Whatever you do, do not connect it to a 5V source when using it with a Raspberry Pi. You will damage your Pi.
Take some jumper wires with female ends and push fit them to the sound sensor. The sound sensor has three pins, 5V (Power), OUT (Output) and GND (Ground). I have used three different colours of wire, red for 5V, green for OUT and black for GND.
The other ends of the wires need to be soldered to the 3.5mm male plug. Cut the connectors off the ends and expose bare wire. The male plug has three tabs under its casing. Before soldering, make sure you feed the three wires through the plug casing. Solder one wire to each tab. It doesn't matter which wire you solder to which tab at this point. You just need to make sure the wires match up correctly when soldering the plus socket later.
To finish off, I laser cut a case for the sensor out of 3mm plywood.
Step 3: Wiring Diagram
The pinholes used on the Adafruit Perma-Proto Pi Hat are 3V, GND, #20 and #21.
I use pinhole #20 for input from the sound sensor and pinhole #21 to trigger the camera.
Wire the sound sensor to the Pi Hat as follows :-
- 5V pin to any 3V pinhole (Red line)
- OUT pin to pinhole #20 (Green line)
- GND pin to any GND pinhole (Black line)
Wire Pi Hat to the camera as follows :-
- Pinhole #21 to the 1K ohm resistor
- 1K ohm resistor to the Base pin of the NPN transistor (Green line)
- Collector pin of the NPN transistor to one of the wires of camera trigger lead (Red line)
- Emitter pin of the NPN transistor to the other wire of the camera trigger lead (Black line)
The transistor is the switch that triggers the camera shutter. When the transistor is activated via the Base pin (green line from pinhole #21), it connects the Collector (red line) to the Emitter (black line). This shorts the camera trigger wires thus firing the camera shutter.
The camera remote lead I used has 3 wires in it, white, red and yellow. Shorting red and yellow makes the camera focus but does not fire the shutter. Shorting red and white makes the camera focus and fires the shutter. So the only wire I needed to use were red and white. You will need to work out which wires to use for your particular camera.
Step 4: Adafruit Perma-Proto Pi Hat
Solder the wires, transistor and resistor to the Pi Hat following the wiring diagram in the previous step. I am using a 3.5mm stereo plug and socket combination to connect both the sound sensor and the camera. I plan to add different sensors to this project in the future and be able to connect different makes and models of camera. Each sensor and camera lead would have a 3.5mm plug attached and so can be easily swapped in and out.
When soldering the socket for the sensor plug, you need to make sure the wires match up. To do this, I used the continuity function on my multi-meter to work out which tab on the socket joined to which wire from the sensor.
Do the same for the camera trigger lead. Cut the actual button trigger hand piece off as you do not need this. Expose the wires and solder to the plug. Solder wires to the socket remembering to use your multi-meter to work out the correct tab to use.
To finish, I made another case to house the Raspberry Pi, the Pi Hat and the two 3.5mm sockets.
Step 5: Raspbian and Python 3
I am running the standard Raspbian build on the Pi 2 which comes with Python 3 by default. I have to run in super user mode so to fire up the Idle 3 (the Python editor), open up a terminal and enter sudo idle 3.
Enter the following Python program :-
import time<br>import RPi.GPIO as GPIO ## Import GPIO library
GPIO.setmode(GPIO.BOARD) GPIO.setup(38,GPIO.IN) ## Set board pin 38 to IN (Pi HAT pin #20) GPIO.setup(40,GPIO.OUT) ## Set board pin 40 to OUT (Pi Hat pin #21)
GPIO.output(40,False) ## Output default to off outputPinOn=False
while True: if GPIO.input(38)==False: ## If sound detected if not outputPinOn: GPIO.output(40,True) outputPinOn=True time.sleep(0.2); else: if outputPinOn: GPIO.output(40,False) outputPinOn=False
Note: The sound sensor am using is HIGH when no there is no sound and outputs LOW when a sound is detected. This is a little counter-intuitive I think. The implication is that in our code, we need to test if the input pin is FALSE to check if a sound has been detected.
Step 6: Testing Setup
Here I have the sensor plugged into the Input socket (though you can't see it in the photo!) and the camera plugged into the Output socket. I pre-focus the camera and then switch it to Manual Focus mode. I launch Idle 3 and run the Python code. The equipment is now set for sound activated photography.
Test 1 - Pool Ball
The first test is a pool ball bouncing off the table. The sound of the ball hitting the table triggers the camera shutter.
Test 2 - Coin and water
The second test was to capture splashing water. The sound of splashing water is too quiet to trigger the camera so I used a shallow plate of water. It is the sound of the coin hitting the plate that triggers the camera shutter.
Step 7: Result 1 - Pool Ball
There is an ever so slight delay between the noise of the ball hitting the table and the camera shutter firing. The photos are therefore capturing the pool ball as it bounces off the table. I have since found out the delay is actually at the camera itself. My ageing camera is slow to react to the trigger signal. You may find better results with a newer camera.