Focus Stacking with a Raspberry Pi – Connecting the Pi to Hardware – Testing

There has been a lot of progress, and the build and testing are advancing.

Here you can see the finished wiring neatly finished with cable ties ( I hate untidy wiring!). The Sainsmart relay module in the foreground left.

The new 2-channel Sainsmart relay module arrived; it worked perfectly without any difficulties, so I must assume that the cheepo version was defective out of the box. With all the hardware now available the only thing left to do was to connect up the Pi. To reduce the chances of getting something seriously wrong I prepared a table of connections to the Pi GPIO header so I knew exactly which wire went where, with insulation colours, pin numbers and destinations all clearly defined. I have decided to adopt the header pin number rather than the BCM2835 nomenclature in the software GPIO code as this seems an easier to understand option; less likely to cause confusion when connecting up the header ribbon cable; and easier to double check between code and hardware wiring. Then it was a matter of connecting things up carefully and testing them one at a time in the order motor, relay module, and individual inputs and outputs.

Table of wiring
Table of wiring

Except that all did not go exactly according to plan!

Having connected up the motor control board to the Pi, it was necessary to move to a new version of my programme which replaced the code for the “virtual” motor with the code for the correct GPIO callouts for my stepper motor controller. When I tried to run the new version, the Pi helpfully reported back that the programme needed to run in root because GPIO had to be controlled from there. That was quite easy to do by running the programme with the prefix “sudo”. However when I did that, my remote terminal running MobaXterm threw up the message:

X11 connection rejected because of wrong authentication…..

So now the Laptop could not display the GUI. That had worked fine when running from the Pi home directory. It seemed like an impossible conflict of demands. Googling the problem just returned a small number of results which just added to my confusion with no solution that I could understand how to implement and why it would work; I don’t like implementing things that I don’t understand! Luckily, Jim came to the rescue and provided a very simple and easy to implement solution that did the trick immediately. All I had to do was add a line at the end of the file .profile that lives on the pi in /home/pi:    export XAUTHORITY=/home/pi/.Xauthority

The laptop connected to the Pi via a network cable and interfacing through MobaXterm.

A quick reboot and then we were off! Over the course of several days the various peripherals were connected and tested, and the Python 2.7  code modified here and there to introduce the GPIOs one by one and to fix the bugs that came with them that had not surfaced earlier.


Everything is now working more or less as intended. However, the stepper motor does seem to run quite slowly, even with the minimum time interval between pulses. I can implement either of two gearing options with different pulley sizes and I have swapped over to the step up ratio rather than the 1:1 ratio that it started with. The difference is that it now takes 450 motor steps per mm compared to the previous 1550.


Everything is now functional on the control panel apart from the top LED marked PI which requires a separate piece of software code

I have noticed what appears to be the effects of stiction in the gearing and the rack, which is causing a very slight jerky movement of the stage as the rubber band drive has enough give in it to allow this to happen. Jerky movement is not a problem in itself but unequal movement between successive shots would be a big problem. There is very little backlash in the gearing that came with the DVD rack and so I am fairly sure this is the result of the rubber band stretching; so I am probably going to have to replace the rubber band drive with a geared drive. Gears are on order.



Still lots to do:

  • Finish implementing the software – mostly minor cosmetic issues apart from error/exception handling which needs a lot more work.
  • Tidy up the programme software to get rid of redundant code.
  • Implement a routine to turn on an LED when the Pi is booted up; so that I know when it is ready to connect to the laptop.
  • Build the stage platform and individual detachable mounts.
  • Devise an alignment tool for setting the height of the specimen on its mount, so that it can be prepared elsewhere and will then align perfectly with the central axis of the lens when installed on the stage.
  • Figure out how to put in place and light suitable backdrop(s); ideally different backdrops for different effects for different types of specimen.
  • Draw some neat circuit diagrams so that in six months’ time I know what I did!
  • Work out a diffused lighting arrangement and make necessary diffusers; possibly ping-pong balls?
  • Fix the broken LED light – too much heat applied when soldering – oops.
  • Add a warning beeper.
  • Tidy up the manual remote camera shutter trigger which is hard wired. It enables me to trigger the shutter when setting up to take test shots without using the programme features and without accidentally jiggling the rig. I am looking for a nice (inexpensive) push button on a wire – the sort of thing that used to be used for advancing slides on a slide projector. Used to have one in fact, but couldn’t find it now that it might have had a new lease of life.
  • Replace rubber band drive with gear wheels? Then recalibrate the rig.
  • Build a dust cover for the working parts
  • Find some specimens
  • Test, test, test!
shoot seq
Screen shot of the GUI for controlling the actual shoot sequence. The stage advances from the parked position to the first shot position and pauses so that I can check everything is good to go. If OK then a click on the “TAKE SEQUENCE” button and it shoots the rest of the sequence. Counters show progress. The GUI uses Tkinter for its implementation. Code is written in Python 2.7.
dry-run screen
Screen shot of the GUI for checking the set up. This is a for checking the set up after all the parameters have been chosen and before shooting the sequence.

What’s it all about?

Here are my jottings about my photographic projects and activities. I have been working on a focus stacking macro photography rig. There are quite a few posts about that. In addition I write about other photographic activities as and when!


Category Specific RSS


Warning: A non-numeric value encountered in /home/public/mhp_blog/wp-content/plugins/ultimate-social-media-icons/libs/sfsi_widget.php on line 238