Introduction: Focus the Pi High Quality Camera With Lego and a Servo

About: I love the design and ambition of vintage technology, and the usability and potential of new - my passion is bringing the two together.

With a slightly hacked Lego piece, a continuous servo and some Python code you can focus your Raspberry Pi High Quality Camera from anywhere in the world!

The Pi HQ camera is a fantastic piece of kit, but as I found while working on the recent Merlin Pi project, you do have to be careful to get the focus just right for the crispest results.

I wanted to find a way to focus it remotely, so that I could set up a wildlife camera in the garden and not have to go back & forth focusing it by hand.


Raspberry Pi High Quality Camera

3MP Wide Angle Lens

Raspberry Pi 3

Lego Compatible Continuous Rotation Servo

60 tooth Lego Turntable (part 18938)

Step 1: A Toothy Part

The first thing I needed was a Lego piece - a toothed Technic gear wide enough to fit over the camera lens. After minimal research I found part number 18938, which looked ideal, and could be ordered singly from eBay.

Although the piece already had a hole in the centre this was nowhere near large enough to fit over the lens, so I drilled it out using one of those Christmas tree (step drill) bits, enlarging the hole to 30mm. My favourite thing about the step drill is that it's easy to keep it in the centre, critical for this job.

I gingerly pushed the piece onto the lens and to my amazement it was a perfect friction fit, not too tight and not too loose. I'd planned to secure it using black Sugru, but didn't need to - and this saved me having to wait 24 hours for it to dry!

Now that the camera lens had its gear attached the next job was to find ways to move it, and before long I had rigged up a manual handle with a worm gear - not pretty but very satisfying. Next stop - automation!

Step 2: A Fitting Servo

I noticed these Lego compatible servos online a few weeks back and was struck by their cuteness! They come in normal, 270 degree and continuous versions, and I excitedly ordered the latter, the moment I had the gear fixed to the camera lens.

You can never tell from an online description how "compatible" these things will be with actual Lego blocks, but this turned out to be a perfect fit.

I added in a small gear from our "stock" and set about building blocks around the servo to make it mesh with the lens gear. In this respect I was incredibly lucky, I was able to fit the servo close to the lens and the gear teeth meshed together perfectly.

Step 3: Remote Control

After tidying up the Lego around the servo (an hour rummaging for tiny black tiles) I started pulling together the code that would control the servo mechanism.

I began with the scripts from my latest project, the Merlin Pi - this already included a user interface for setting the camera modes and capturing images, the only difference was that this time I'd be accessing the GUI remotely via VNC Viewer instead of on an inbuilt screen.

Next I used GUIzero to create another small menu, one that would let us control the servo via GPIO and therefore the focus of the camera. I designed the menu to be tall and thin so it'd be visible next to the camera preview window, letting you focus in real time. It included buttons to move the servo in both directions, and by large and small increments, to allow for fine tuning.

This all worked really well, so I set the scripts to both run automatically on startup and dumped the camera randomly in the garden for a test. The first trial took a while as I'd had the lens unscrewed, but I was soon able to focus the camera perfectly on a bird-feeder, via VNC on the computer in my office, very satisfying.

All of the code I used is available on GitHub , for both the "focus" and "capture" graphical menus.

Step 4: Fresh Focus

Being able to focus the camera remotely is a real game-changer for me, making it much easier to set up a camera trap around the garden, capturing top quality images and video.

It took me the best part of a day to get this up & running, but I think it could easily be done in under an hour if you have the parts handy and follow along with this Instructable. It's a great way to add flexibility to your camera project, doesn't use up many GPIO pins and is very cost effective, you can buy the continuous servo and the 60-tooth gear for under £10.

I can imagine this being a very useful addition to an existing pan/tilt arrangement, and especially if you're using the High Quality Camera as part of a robotics project, where a fixed or manual focus would be an issue.

The main thing for me was that this was lots of fun, just as any project combining Lego and the Raspberry Pi is quality time well spent as far as I'm concerned.

Thanks for reading and stay safe everyone.

My other Old Tech, New Spec projects are all on Instructables at

More details are on our website at and I'm on Twitter @OldTechNewSpec.