TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
    • Book
    • Downloading
  • Python
    • Learn Python >
      • Python Modules
    • PyGame Zero
    • Python Programs >
      • Higher or Lower
      • Magic Calculator
      • Password Checker
      • Python Pit
    • What's News App
    • Pixels to Cells
    • Python Mosaics
    • Python OCR
    • L-1-AM
    • Web Scraping >
      • Scraping Trains
    • Weather App
    • Snakes and Windows
    • Python Web Server >
      • Flask
    • Python Picks
  • Raspberry Pi
    • All About the Pi
    • Getting Started
    • Remote Desktop and VNC
    • Static IP Address
    • Sonic Pi >
      • 3.14
    • Twitter Feed >
      • Tweepy
    • Android & Pi >
      • Advanced Apps
      • Odds
    • A.I on the the Pi
    • CRON
    • Pick Your Own
  • Pi Hardware
    • Pi HATS >
      • Sense Hat Hacks
      • AstroPi HAT
      • Unicorn-HAT >
        • Unicorn Alphabet Disco
        • Uni Codes / Programs
      • Skywriter
      • Piano HAT
    • STS Pi
    • Pi Camera >
      • Pi-Cam, Python & Email >
        • Time Lapse
      • Pi Noir
    • Pipsta >
      • Flask, Input & Printers
    • Raspberry Pi Power >
      • Energenie IR power
    • Pibrella
    • Distance Sensor
    • LCD Screen
    • Pi-Tooth
    • Robot Arm
    • PiGlow
    • PiFM
    • Accelerometer
    • PiFace >
      • Installing PiFace >
        • Python Commands
  • Pi-Hacks
    • Drone Hacks
    • Pi Glue Gun Hack
    • Blinkt!
    • Sonic Pixels
    • R2D2
    • Get to the chopper
    • Astro Bird
    • Twitter Translator
    • Hacking a Robot
    • Nature_Box >
      • Best Nature Photos
    • Wearable Tech >
      • Project New York
      • P.N.Y Part 2 Health
      • P.N.Y Part 3 Games
      • P.N.Y Part 4 Translation
    • Dino-Tweet
    • Other Links
  • Pi-Hacks 2
    • The Joker
    • Hologram Machine
    • Google Vision: Camera Tell
    • Yoda Tweets
    • Pi Phone
    • Darth Beats
    • Twitter Keyword Finder
    • Crimbo Lights Hack
    • Xmas Elf
    • Halloween 2016
    • Halloween Hack 2015
    • Socrative Zombie
    • Voice Translation
    • The Blue-Who Finder
    • GPIO, Twitter
    • Pi Chat Bot >
      • Dictionary Definitions
    • PiGlow & Email
    • Pibrella Alarm System
    • SMS with Python >
      • Spooking a Mobile
  • Pi-Hacks 3
    • David Bowie
    • Lamp Prank >
      • TEST
    • Yoda FM
    • Retro Player
    • LED Pixel Art
    • TARDIS
    • Battleships
    • LED Board
    • Night Vision
    • Enviro+ Weather
  • Minecraft
    • Minecraft API
    • Minecraft Sweeper
    • PiGlove: Minecraft Power Up
    • Minecraft Photo-booth
    • Rendering Pixels
    • Speed Cube
    • Lucky Dip
  • Computing
    • Why Computing?
    • Can You Compute
    • micro:bit
    • Coding Resources
    • Learn to Code >
      • Coding with iPads
      • Apps Creation Tools
      • sKratchInn
      • Sound Editing
    • Cheat Sheets
    • Theory
    • HOUR OF CODING
    • BEBRAS Computing Challange
    • Computer Facts
    • Free Software and Links
  • Contact Me

What is it?


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.

 Night Vision


Picture

My completed Night Vision combines an IR camera (infrared, which enables you to take photos and videos in the dark) the Pimoroni Hyperpixel4 touch screen 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.

Building a Night Vision Device


Features



​A note on screen rotation

  1. See in the dark (2m to 5m)
  2. 4inch LCD touch screen
  3. GUI controls
  4. 10 button control with various preview times
  5. Handy titling handle
  6. Swivel head so camera can be moved to the optimum viewing angle.
  7. Capture real time video saved to the camera
  8. Capture images and stills

Software Install


​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/

At the time of building this project (August 2019) I was having issues with the HyperPixel4 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/

Using GUI Zero


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) 

Shutdown the Night Vision


​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


The GUI consists of the following 10 buttons and features:
  1. A 10 second preview
  2. A 30 second preview
  3. A 60 second preview
  4. 5 minute preview
  5. 10 minute preview
  6. 15 minute preview
  7. Take a photo
  8. Capture a 10s video clip
  9. Turn on the preview (no time limit)
  10. 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

FAILING (First Attempt in Learning)


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.

​FAIL 1: 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
FAIL 2: ​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!     
Picture

Picture
Picture
FAIL 3: 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. 

FAIL 4: 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!’.  I settled for the latter, so the 10 second button does not work, but if anyone would like to gift me a HyperPixel4 I will replace the cracked one. 

Click to download the program code


Picture

Copyright 2020 TeCoEd @dan_aldred