Today, I will teach you how to make a “digital microscope” using Raspberry Pi.
The project has been around since 2019, but that doesn’t stop us from learning.

The reason for this small project is that the author wanted a digital microscope to inspect his circuit boards and 3D printed parts.
He hoped that the microscope could have good display effects at a reasonable price, so he used a Raspberry Pi Zero W paired with a cheap mobile macro lens to create this project.

The effect is as follows
Considering the price factor, the video quality is still very good, especially in the center of the image.
Although the edges look a bit blurry now, and the depth of field is very shallow, this can be improved by using a better lens.
The microscope can be used to inspect your electronic components or to closely observe your 3D printed works, discovering some details.

It can also be used to inspect some tools,


or small gadgets in life.

It feels like going back to chemistry class in middle school.
Materials Used
-
1×Raspberry Pi Zero W -
1×Camera Module V1.3 -
1×12mm rod (approximately 140mm long) -
1×5mm rod (approximately 42mm long) -
1×20x macro lens
Production Steps
The author used some aluminum parts here, but the overall part of the microscope can be 3D printed, and the relevant model files can be downloaded at the end of the article.
Base
Those who have used a microscope should know that for stability reasons, the base of the microscope is generally quite heavy, so we can also add some steel plates or similar things on it.
Take a 12mm steel rod and clamp it on the base with M4 positioning screws.
The main body of the microscope is clamped on the steel rod with M4 screws. To enable the microscope lens to move up and down, the author used a gear rack design.
A 5mm shaft is needed, and aluminum was chosen here as it is easier to process.
This knob can also be 3D printed and glued to the shaft. (Files can also be obtained at the end of the article)
The gear has a hole that needs to be threaded first.
Then use an M3 positioning screw to clamp it.
The assembly of the base is done, and next is the microscope lens.
Microscope Lens
The six holes in the main body and the back support need to be threaded with M3 screws.
Then place the gasket on top and install the Raspberry Pi Zero W.
Camera Module
This module is version 1.3, with a flexible cable that can be directly installed on the Pi.
Then screw the lens into the camera mount.
This part can also be easily modified according to other cameras or lenses you choose.
It is not recommended to push the camera sensor all the way in, as this will make it difficult to remove later.
The lens used in the project is a 20x macro lens, which comes with a white clip.
There are many similar lenses available online, and you can find a suitable one, just remember to modify the dimensions in the 3D printed parts.
Finally, add a button to the two lowest pins of the Raspberry Pi and insert an SD card, preferably with the Raspbian operating system already installed.
Then we insert the microscope lens into the base, and the assembly part is complete.
Code Section
To obtain video signals immediately after startup, we need to add some code.
Here we assume that the Raspberry Pi has already installed the Raspbian system and has been connected via ssh.
If you are unsure how to do this, you can search for tutorials online.
First, we need to activate the camera, so:
sudo raspi-config
Press Enter, then go into the interface options, select “Camera”, and press YES
.
The specific operation is as follows:
Interfacing Options
—— Camera
—— Yes
Don’t rush to select restart yet; we will reboot later after finishing.
Next, we create a script to automatically start the camera, so enter sudo nano camera.sh
and paste the following code.
#!/bin/bash
#-sa 100 turns up the saturation to maximum, adjust if necessary
raspivid -t 0 -w 1920 -h 1080 -fps 30 -sa 100
Press Ctrl+O to save, Ctrl+X to exit.
Then we add another script that allows us to safely shut down the Raspberry Pi. Enter sudo nano shutdown.py
, and similarly, just paste the code, save, and exit.
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def shutdown(channel):
os.system("sudo shutdown -h now")
GPIO.add_event_detect(21, GPIO.FALLING, callback=shutdown, bouncetime=1000)
while 1:
time.sleep(1)
Enter sudo nano /etc/rc.local
and paste the code, which will run our two scripts immediately after startup.
Add before exit 0
:
sudo python3 /home/pi/shutdown.py &
sh /home/pi/camera.sh &
Save and exit, and it’s done.
Enter sudo reboot
to restart the Raspberry Pi, and after a few seconds, you will see the video image.
Ensure that the shutdown button works properly; a few seconds after you press it, the green indicator light should go out.
Alright, a simple digital microscope is complete! I hope it can help with your daily creations!
Original project English link: https://www.youtube.com/watch?v=jzcHGjFiR0o
Project author: Brauns CNC
This translation was first published in DF Maker Community
Hardware Arsenal
Click to learn more👆
If you have anything to say or corrections to the article translation, feel free to leave amessage!
3D printing files and code involved in the project can be downloaded by replying “Raspberry Pi Microscope” in the public account!
Previous project review
Community Arduino Exciting Project Review
This Raspberry Pi case is quite appealing!
Creating a high-tech OLED display
How to hold an electronic music concert for LED Miss?
Make a small four-legged robot dog based on ESP32
Paying tribute to Zhi Hui Jun? Making a transparent watch themed on astronauts!
Spring is here, and you need a retro-style Raspberry Pi camera!
Click to read👆