Introduction: Auto-tracking Water Blaster
Rose-eating deer motivated me to build a target-tracking water blaster to help deter the voracious critters… This water blaster uses video based motion detection to aim a servo and trigger short bursts of water at the target. It shoots only after an acquired target is stationary for a few seconds (the delay can be adjusted in the code). I don’t care if the deer are just walking by but if they stop for a snack, sploosh!
Here is a video of me testing the water blaster:
The water blaster is a stand-alone box that can be remotely connected to (via wi-fi/VNC) from any computer on your network to monitor what it's doing. It takes a picture each time it's triggered so you can later see what was getting blasted.
I used a Raspberry Pi, NoIR cam, IR illuminator, standard linear servo, and a water valve to create this day/night, target tracking water blaster. The code is written in Python and borrows heavily from Adrian Rosebrock’s cv2 image processing code samples. You can see his write up at:
Since I’m going after relatively large, ground based targets (deer), my problem is somewhat simplified. I only require horizontal aiming so I can get away with using only one servo. Waiting for the deer to stand still helps me eliminate a lot of false triggers. This is my rev-0 attempt and I’ve found a few things I’d modify if I built another one. I’ve noted these things in the detailed write up that follows.
Step 1: The Code
The water blaster uses the Raspberry Pi 3 for processing. For capturing video, a NoIR Raspberry Pi cam is used along with an IR illuminator for night video. The OpenCV/cv2 Python package is used to capture and process image information and compute target coordinates. The pigpio library is used to control the gpio for stable servo operation. Using the regular RPi.GPIO package resulted in a shaky servo. NOTE: When using the pigpio library you need to run the pigpio daemon. Add this to your Pi’s /etc/rc.local startup file for the pigpio lib and the Raspberry Pi camera interface:
# Set up /dev/video0 to link to Raspberry Pi built in camera interface
# Start the pigpio daemon for the Raspberry Pi IO control library
See https://pypi.python.org/pypi/pigpi for more detail.
The source code is named: water_blaster.py and is attached below.
Disclaimer: I am new to Python coding so don’t treat it as any great model of Python coding style!
The basic algorithm is as follows:
- Grab an initial video reference frame. This will be used to compare against to detect motion.
- Grab another frame.
- Convert the frame to gray scale, size it, blur it.
- Compute the difference from the reference frame
- Filter out small differences, get coordinates of largest difference.
- Set a timer. If the target coordinate does not change for a few seconds, then take a picture of what we are about to shoot and trigger the water valve for a blast of water. Sweep the servo back and forth a few degrees for a “shotgun” blast.
- If we get three triggers too quickly, disable shooting, pause a bit, then update the reference frame as we may be shooting at a shadow or porch light that was just turned on…
- Every few minutes update the reference frame to account for low frequency changes (the sun rising/setting, overcast moving in, etc.)
I’m only using a horizontal aiming mechanism but many pan/tilt servo mounts are available on EBay and it would be easy to add another servo for controlling vertical aiming if you wanted more precise targeting.
I set up the Raspberry Pi to run as a VNC server, then connect to it via VNC from my laptop to start the program and monitor the video and logs. cd into the directory where you store water_blaster.py and run it by typing:
It will open a video monitor window, start a log file named "./log_[date]_[time], and create a sub-dir named "trigger_pictures" where jpg files (trigger_[date]_[time]) are stored for every shot taken.
Here are some notes on setting up VNC on your Raspberry Pi:
The first time I setup the Raspberry Pi, I used an external monitor/keyboard/mouse to set things up. There I enabled the VNC server at the RasPi config (Raspberry Logo / Preferences / Raspberry Pi Configuration/Interfaces/ Check VNC option). Afterwards, when it boots up, it enables you to connect to its :0 display via VNC client (w/ same credentials as the default user "pi").
In headless mode it defaults to a very small resolution display (as it does not detect any display), to force it to some bigger resolution, you add this to /boot/config.txt and restart:
# Use if you have display
# 1400x1050 w/ 60Hz
# 1356x768 w/ 60Hz
Here is some more info: https://www.raspberrypi.org/documentation/remote-...
Step 2: The Electronics
The water blaster electronics requirements are minimal using the Raspberry Pi 3 gpio to drive a servo, water valve and IR illuminator via discrete transistor buffers (built on a small proto board). A standard NoIR camera plugs directly into the Raspberry Pi.
The name of the schematic is: water_blaster_schematic.pdf and is attached below.
I used a 5v/2.5A dedicated supply for the Raspberry Pi and a 12v/1A supply for driving the IR illuminator and water valve. The 12v supply also drives a 5v regulator to supply power to the 5v servo. This was done to keep the “noisy” motor control power isolated from the Raspberry Pi 5v supply. The 12v/1A supply turned out to be right at its limit (actually slightly over once I added the fan). The code turns off the IR illuminator before powering the water valve relay to keep the current draw within range… It would be better if you used a 1.5A supply. Be sure to connect the ground terminals of all the power supplies together.
The camera module is a standard NoIR version that plugs into the Raspberry Pi directly. It’s a Raspberry Pi cam with the IR filter already removed enabling it to be used with an IR illuminator for taking night video.
The servo used is a standard sized 5v linear servo with 3-4 kg-cm of torque.
The IR illuminator was a low cost 48 led ring I found on EBay for about $4. It’s not super strong and can illuminate only out to about 15 feet. If you have extra budget, getting a stronger illuminator would be a good improvement.
I added a “debug-switch” to gpio23. The code checks the state of the switch and if pressed will disable the water valve relay for dry-fire testing. I thought I’d do more with that switch but didn’t end up actually using it at all. I’d remove it and the code that looks for it…
Step 3: Construction: Camera and IR Illuminator
I used a Harbor Freight plastic ammo box as an enclosure. Mainly I needed something water-resistant as a lot of water spray/runoff is inevitable. There are lots of holes/cutouts but they are covered by awnings, clear plastic, or are drilled under overhangs to shed water. In hind sight, I should have used a metal box with heatsinks internally attached to the high power components. By doing that I think I could have avoided adding the fan. The plastic box was too insulating and allowed the interior temperature to rise too much.
A small window was cut in the end for the camera to view out and the IR illuminator was mounted inside an old plastic lens case I had laying around.
Step 4: Construction: Water Piping
The water inlet is piped into a 12v water valve that is connected to a ¼” ID x 3/8” OD vinyl tube. That in turn is connected to a ¼” barbed tubing to ¾ slip fit PVC connector and glued to a ¾” PVC water cap with a 1/16” hole drilled for the water stream. I wanted to keep the water-valve relay out of the weather so it’s mounted inside the box. There is the danger that I could get a leak but I have drilled drain holes in the bottom of the box and mounted the electronics high up to minimize the chance of potential water damage to the electronics if that happens. A less aesthetically pleasing, but safer, plan would be to mount the valve on the exterior and run the 12v relay wires inside. The clear plastic disk over the servo was a convenient way to mount the hose-end and it keeps water from dripping down onto the servo. The fan was an afterthought as the box was warming up too much. I built a little awning over it to keep water from dripping in.
Step 5: Construction: Aiming Servo
A hole is cut into the top of the box and the aiming servo is mounted and sealed with silicon to keep water
Step 6: Construction: Mounting the Power-supplies, Fan, Raspberry Pi, and Proto-board
The two power supplies (5v and 12v) are wired to a single power cord exiting the side of the box. The Raspberry Pi and a proto board are mounted on the side of the box near the top. Notice the drain holes drilled in the bottom and the air vent holes drilled along the upper edge. The fan is mounted opposite the Raspberry Pi. There is no on/off switch as I don’t want to encourage shutting the Raspberry Pi off without a formal “sudo shutdown now” command (i.e. don’t want the power turned off too easily).
Step 7: Construction: the Proto Board
The proto board contains a 5v regulator, filter-cap, power transistors (that drive the servo and water valve), and a debug-switch.
Step 8: Construction: Raspberry Pi Camera
The Raspberry Pi cam connects directly to the Raspberry Pi via the ribbon cable and is mounted on the clear plastic plate covering the viewing cutout at the front of the box.
Step 9: Parts List
The project ended up costing about $120. The bulk of the cost of the project is the Raspberry Pi, camera, servo and power-supplies. I found most of the parts on EBay or Amazon and the plumbing parts at the local hardware store.
- Raspberry Pi 3 (Amazon) $38
- NoIR Camera (EBay) $30
- 5v Analog Servo (4kg-cm torque) (EBay) $10
- 5v/2.4A Wall Power Supply (EBay) $8
- 12v ½” Water Valve (EBay) $5
- Tubing, Pipe-Couplers (Osh) $5
- Plastic Ammo Box (Harbor Freight) $5
- 12v/1.5A Wall Power Supply (EBay) $5
- IR Illuminator (EBay ) $4
- Misc. Components (Resistors,Switches,Diode) $2
- CPU Fan (EBay) $2
- Proto Board, Standoffs, Screws (EBay) $2
- (2) Power Transistors (2n5296) (EBay) $1
- 5v Regulator (LM7805) (EBay) $1
- Clear Plastic 3/32” (Tap Plastics Misc. Bin) $1
- Power Cord (Osh) $1
Stores/sites where I bought items:
- Alice1101983 EBay Site: http://stores.ebay.com/TxHang-Electronic?_trksid=...
- 2bevoque EBay Site: http://stores.ebay.com/2bevogue
- Harbor Freight
- Orchard Supply Hardware
- Tap Plastics