Table of Contents

About

In the following tutorial a low-cost unbranded (or rebranded) USB microscope is used along with a Raspberry Pi Zero and a monitor to create a large display suitable for working with electronics.

The Microscope

These microscopes are so generic that they appear on the market with various brands advertising from $50$ to $1600$ times magnification, optionally connectable to Android, and all models have pretty much the same electronics inside. These microscopes are based on an UVC-type webcam with a lens inside that slides across two struts in order to zoom in or zoom out. Perhaps the main feature of these microscopes is that they are extremely low cost.

The stand that is provided with the microscope is completely unusable for the purpose of electronics work such that a telescopic microphone arm is better suited for holding the microscope.

Finally, the worst part is that compared to similar microscopes with built-in screens, these "bullet"-like microscopes only provide an USB connector such that a monitor is needed and a computer to be able to view the image.

Technical Details

Connecting the microscope to Linux, results in the microscope being recognized as an UVC USB webcam and all the handling is done by the uvcvideo kernel driver. The device created will be placed at /dev/video0 or /dev/videoN, depending on whether there are other V4L devices connected to the computer.

Issuing:

v4l2-ctl --list-formats-ext

yields:

ioctl: VIDIOC_ENUM_FMT
        Type: Video Capture

        [0]: 'YUYV' (YUYV 4:2:2)
                Size: Discrete 640x480
                        Interval: Discrete 0.040s (25.000 fps)
                        Interval: Discrete 0.040s (25.000 fps)
                Size: Discrete 160x120
                        Interval: Discrete 0.040s (25.000 fps)
                Size: Discrete 176x144
                        Interval: Discrete 0.040s (25.000 fps)
                Size: Discrete 320x240
                        Interval: Discrete 0.040s (25.000 fps)
                Size: Discrete 352x288
                        Interval: Discrete 0.040s (25.000 fps)
                Size: Discrete 640x480
                        Interval: Discrete 0.040s (25.000 fps)
                        Interval: Discrete 0.040s (25.000 fps)

such that the microscope advertises resolutions from 160x120 up to 640x480.

Realistically speaking however, 640x480 will be very slow and will respond slowly on Linux if you move the microscope around. It seems that the sweet spot for such microscope is the 320x240 resolution that results in a constant, consistent and workable video feed.

The microscope comes with a stand, a button to snap pictures (recognized as an input device on Linux) and another button that increases the light projected by the LEDs. Realistically speaking, the light provided by the microscope is insufficient and a better lighting source will be needed.

Similarly, to protect the objective, the microscope has a transparent plastic casing - this should really just be used to protect the microscope when it is not used, otherwise the plastic gets dirty easily and will just block and diminish the picture quality.

The stand that comes with the microscope does not give credit to the microscope itself: it is too small, too light and the microscope kips over and does not allow a lot of movement; certainly not as much as the telescopic microphone arm does.

Fortunately enough, the microscope is good enough such that it can be elevated up to a quarter of a meter above the board using the telescopic microphone arm and thereby provides ample room to be able to work underneath the microscope. This makes the microscope quite a workable tool for solder work and repairs where small components need to be magnified.

Setting up the Raspberry Pi

A Raspberry Pi Zero has been chosen for this task since only the video feed and perhaps some data acquisition can be used from the GPIO ports. Fortunately, the microscope is recognized flawlessly by the Raspberry Pi Zero. Since the solution described in this article aims to be minimal, it is best to just use the Linux framebuffer directly to display the picture rather than installing the desktop version of RaspiOS. That being said, the lite version of RaspiOS is installed, along with ffmpeg:

apt-get install ffmpeg

The Raspberry is set up such that it can be configured and reconfigured remotely via the local network and all the TTYs are disabled such that they do not mess up the screen when ffmpeg displays the image:

for i in {1..8}; do systemctl mask getty@tty${i}.service; done

Next ffmpeg is used to render the image from the microscope to the framebuffer:

ffmpeg -f v4l2 -video_size 320x240 -i /dev/video0 -pix_fmt bgra -f fbdev /dev/fb0

If everything went well the image from the microscope should appear on top of the console and should be responsive enough to allow the item placed under the microscope to be moved while the image is still updated.

Obviously, the problem is that a 320x240 image will look very small on the Raspberry Pi output and will appear in a corner on the screen. Unfortunately the 320x240 resolution cannot be changed such that one attempt would be to scale the image up to the Raspberry Pi resolution:

ffmpeg -f v4l2 -video_size 320x240 -i /dev/video0 -pix_fmt bgra -vf scale=1024x768 -f fbdev /dev/fb0

where 1024x768 is the framebuffer resolution.

This does not work either and ffmpeg starts churning the CPU in order to scale the image up to the resolution passed to ffmpeg.

The final solution is to scale down the framebuffer to 320x240 - instead of scaling up, we scale down. This can be accomplished by first issuing:

raspi-config

going to the display settings and selecting the smallest resolution available (640x480). Next, the file /boot/config.txt is loaded and the framebuffer is resized forcibly to 320x240:

framebuffer_width=320
framebuffer_height=240

When the Raspberry reboots, the framebuffer should be scaled down but the screen will be filled such that the picture displayed from the microscope will appear to be large.

Bringing it Together

If not done previously, the TTYs can be disabled with:

for i in {1..8}; do systemctl mask getty@tty${i}.service; done

in order to prevent Linux from spawning login shells and clutter up the screen.

In order to start the video capture right after the Raspberry Pi boots, a systemd file is placed at /etc/systemd/system/microscope.service with the following contents:

[Unit]
Description=Microscope
After=network.target

[Service]
ExecStart=/usr/bin/ffmpeg -hide_banner -loglevel quiet -f v4l2 -video_size 320x240 -i /dev/video0 -pix_fmt bgra -f fbdev /dev/fb0
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=microscope
User=root
Group=root

[Install]
WantedBy=multi-user.target

After a reboot, ffmpeg will start grabbing from the microscope and display the image onto the screen.

Results

The following is a close-up image of a PCB with the microscope placed $20cm$ above the objective and goes to show that the microscope delivers a great image and is suitable as a tool. The image shows some trace damage on the PCB around two of the throughholes on the PCB.

For a micro-cost DIY microscope solution, this is great! :)

Further Work

The large screen has been removed at a later time and replaced with a cheap small composite screen leading to a more advanced project accomplishing the same objective.