How to connect a microscope to your computer with a raspberry pi for cheap

You have a cool microscope and you are enjoying it but you’d like to

  • capture images and video of what you are seeing
  • look at your samples for a longer period of time without eye strain
  • be in a more relaxed position for long microscopy sessions

For all these reasons you want be able to connect your microscope to your computer so you can see enjoy images and smooth video.

Here’s how to do it.

  1. Things you’ll need
  2. Preparing the Pi
  3. Getting everything connected
  4. Viewing the camera feed

Things you’ll need

  1. If you don’t already have one … a microscope.

We recommend getting a trinocular microscope. A bit more expensive but well worth it. For this writeup we used the SW380T. A popular medium end consumer microscope.

SW380T
SW380T
  1. A Raspberry Pi. $10

If you don’t already have one you should get the Raspberry Pi Zero W. This is the small and wireless version. At the time of this writing there are supply issues with Adafruit. So if they still don’t have one - check Amazon. You’ll need a micro SD card as well.

Pi Zero
Pi Zero
  1. A Raspberry Pi 12 MP Camera. $50
Pi Camera
Pi Camera
  1. A reduction lens. $54

This will help ensure you see more of the image coming from the objective lens.

Reducer
Reducer
  1. A Raspberry PI Zero Camera Cable. $5

This is to connect your Pi to the Camera. If you have a regular PI - you wont need this.

Pi Ribbon
Pi Ribbon
  1. A Raspberry Pi Case with Heat Sink - $10

You don’t strictly need this but you do need a case for your Pi to keep it from accidentally shorting on a metal body microscope if thats what you have. This specific one has a few more things than you need but it does have a heatsink is nice to have for longer viewing sessions and it has a micro USB cable with switch which is nice to permanently mount on your final setup.

Pi Case
Pi Case

The Pi related parts should cost about $130 dollars.

Programming the Pi

  1. Install the basic Pi OS on the micro SD card. Use the Raspberry Pi Imager to do this. https://www.raspberrypi.com/software/

  2. Plug the SD card into the Micro SD card.

  3. Connect the Pi to your computer with a micro SD cable.

  4. Connect to the Pi using SSH. Use these instructions if you are not familiar

  5. Get your Pi connected to your WiFi. Here is how you do that.

  6. In the home folder /home/pi create a file called stream.py

Paste in code found in section 4.10 from here

This is the web server that will pull the data from the camera and make it available on your local home wireless network.

We changed the line of code with 640x480 to:

with picamera.PiCamera( resolution='1920x1080', framerate=24) as camera:

This gives us a bigger image which is what we want. If you have a monitor with lower resolution adjust this value as necessary.

  1. Add the following line to /etc/rc.local

sudo -u pi python3 /home/pi/stream.py &

This will make the python server start automatically when the PI boots up.

And thats it.

Getting everything connected

now comes the fun part. Getting it all connected. We’ve hacked together our mount with painters tape but you can go wild and make an aesthetically pleasing setup.

The SW380T comes with all the requisite parts to do this.

  1. Setting up the stack
  • connect the adapter, which comes with the SW380T, to the trinocular mount.
  • screw the reducer into the camera
  • connect the ribbon to the camera
  • slide the reducer into the adapter

If you don’t have a SW380T look how you can connect cameras to their microscope. You should be able to mix the instructions together.

  1. Connect the Pi to the camera and mount it
  • Mount the Pi in an appropriate location.
  • Connect the other end of the ribbon to the Pi
  • Connect the usb extender that came with the case to the Pi. This will be used for power only since we’ll be streaming the video feed over wifi.
  1. Viewing the camera feed
  • Look up the Pi’s IP address.

When your PI boots up it will usually be given an IP address by your home router. You can look up the specific IP address. For example with NetGear routers you will look for Attached Devices. Then in the device list look for raspberrypi

The IP address you see associated with the Pi will be the address of the Pi’s web server.

Every time you reboot the Pi the IP address may change but it will usually stay the same. If it does change, which you’ll discover when you cant connect to your pi, simply look it up again on your router software.

  • Connect to the Pi using your browser (Chrome is recommended)

If the IP address of your Pi is 192.168.1.25 then go to http://192.168.1.25.
IMPORTANT: Make sure it is http and not https

And you’re done!

When you are done with a microscopy session simply power off your Pi and Microscope and put it away.