Raspberry Pi Night Vision Camera
As a child I spent many hours playing ‘Splinter Cell.' I never enjoyed the classic ‘shoot em up’ style game so SC was perfect. A stealth game with a focus on creeping around in the shadows, where light and sound were used as tools to complete or hinder various missions. The iconic image from the game is the night vision goggles that Sam Fisher wears. I have always been fascinated by the idea that you can see in the dark and this formed the foundation of my idea to build a portable hand-held night vision piece of equipment.
My completed night vision combines an IR camera (infrared, which enables you to take photos and videos in the dark) the Pimoroni HyperPixel 4.0 touchscreen and a Raspberry Pi Zero W. I bought an old handheld camera flash from a car boot for 50p and this became the housing for the hardware. The Pi Zero is the perfect size as it sits inside the flash casing and still has enough room to support the HyperPixel screen and the original parts of the flash. The flash also has a tilting handle which means that you can angle the night vision and it is portable.
- See in the dark (2m to 5m)
- 4-inch LCD touchscreen
- GUI controls
- 10-button control with various preview times
- Handy titling handle
- Swivel head so camera can be moved to the optimum viewing angle
- Capture real-time video saved to the camera
- Capture images and stills
At the time of building this project (August 2019) I was having issues with the HyperPixel 4.0 screen. The screen needs to be rotated which is easy to do by editing the config.txt file. However, I found that the ‘touch’ accuracy was off by about 1 and 2 cms. I tried the new Buster OS but this still did not correct the issue. In the end I used an old version of the Raspbian OS which I downloaded from here: http://downloads.raspberrypi.org/raspbian/images/
The Raspbian OS contains all the required software to use the camera straight away once it is connected up. The only other required install is the guizero, which is simple,
In the Terminal type,
sudo pip3 install guizero
More details about guizero, installing it and how to use it can be found here: https://lawsie.github.io/guizero/
Most of the program code uses the PiCamera Python library to trigger the IR camera, to either, take a photo, record a video or display the preview. It is the preview that is used to display what is being seen in the dark and by extending the preview length you can specify the amount of time that the night vision is turned in for.
The program code consist of creating a function to control the camera preview, for example, display the preview for 10 seconds.
def short_preview(): print("10 second preview") camera.start_preview() sleep(10) camera.stop_preview()
The guizero is used to program a button which is displayed on the HyperPixel screen. When it is pressed it calls the function and runs it.
P_10 = PushButton(app, text="10s", width = "fill", height = "fill", align = "right", command=short_preview)
The program also makes use of gpiozero to combine the ‘shutdown’ function with the guizero ‘options’ feature, where you can create and display a window that provides the user with a choice. In this project the user can select to ‘shutdown’ or ‘cancel’ the shutdown of the night vision.
def shutdown(): #check_call(['sudo', 'poweroff']) close_down = app.yesno("SHUTDOWN", "Are you sure?") if close_down == True: app.warn("Ready", "Shutting down") sleep(3) check_call(['sudo', 'poweroff']) else: app.info("Shutdown", "Shutdown aborted")
The GUI consists of the following 10 buttons and features:
- A 10 second preview
- A 30 second preview
- A 60 second preview
- 5 minute preview
- 10 minute preview
- 15 minute preview
- Take a photo
- Capture a 10s video clip
- Turn on the preview (no time limit)
- Close the night vision GUI and shut down
I changed the colour of the buttons to black and green as the original grey was too bright in the dark and distracted from the ‘stealth’ feel that I was trying to emulate
One of the elements that I really enjoy when making a project are the problems that I encounter. The failings where you plan something but then you find you either need to do something different or the method you chose just will not work. Then you go on the hunt to find solutions. (Thank you to all who provided ideas, support and weblinks, @biglesp, @OldTechNewSpec, @Mr_MartinParker, @MolloyLaurence) Below is a quick rundown of some of the headaches that kept me awake thinking at night and how I solved them.
Fitting the hardware into the flash casing was a pain and involved lots of dremeling, is that even a word? The smell of burning plastic was not pleasant and at times I was concerned about the fumes. Then I had to ensure that the original ‘plastic flash surround’ would still fit into the casing. It didn’t, so I had to Dremel out holes for the USB cables and the IR camera ribbon. This was stressful as the surround was weak, so if I cracked or split it, the project was over! In the end I dremeled the plastic away over a week, just a little bit each day
Mounting the camera and IR sensors went well, I used some Sugra to hold the two IR LEDs in place, drilling two holes in the plastic which they slotted through. All was going well until I ran the program and realised that the camera image displayed back on the HyperPixel was upside down. Luckily at this point I was using elastic bands to hold the case and hardware together, so I could flip it around. BUT, the IR LEDs were secured in and breaking the Sugra might damage or split the plastic. As a stroke of fate, the IR LEDs are mounted into holders and guess what, you can pop the LEDs out of the holder. So, I popped them out and then rotated the camera unit and then popped it back into place!
The night vision is stand-alone so I needed it to be portable and bootable, with minimum interaction from the user to get it running. The obvious solution was to use a crontab, but this did not load the GUI. One idea was to write a script that ran on boot up that triggered the Python program to run after 60 seconds. However, this did not work either, the script worked with a simple ‘hello world’ program, but the GUI would still not load. In the end I opted for a desktop icon which when you click it. The Python program runs and the GUI loads.
Once I had tested every part of the project, I needed to fit it all together and then secure the screws back into place. As I did this. I had the screen flat on the desk and pushed a little too hard. The corner of the screen cracked!! The whole project was working and perfect so I was left with the conundrum of either replacing the screen and risking all the inner hardware not fitting back in. Or, just ‘make do and mend!’.