Focus Stacking with a Raspberry Pi – development modifications

    The rubber band drive has gone, replaced with some lego gearing. I had acquired various sized gear wheels (40, 24 and 16 teeth) and the one that worked best for my installation was the 40-16 arrangement shown here in the picture. I was able to adjust the position of the stepper motor to accommodate this new configuration by means of the adjustable tension arm. This works much better than the rubber band arrangement, and the movement of the stage on its rack is quite smooth and jerk free.

    The new gearwheel drive resplendent with miniature rubber duck. When I have built the dust cover, Mr Duck will sit on a spindle protruding through the cover so that I can see how the motor is behaving.
    The new gearwheel drive resplendent with miniature rubber duck. When I have built the dust cover, Mr Duck will sit on a spindle protruding through the cover so that I can see how the motor is behaving.

    I have also now installed the official rubber duck rotation indicator!

    The motor control software has a soft-start and soft-stop routine so that the rack speeds up and slows down gracefully when it has been motoring at full speed for a long traverse, say, to park the stage. It avoids an abrupt stop which is not ideal. (when incrementing between shots a much slower speed is used). I had noticed that the speeding up was quite gradual to start with and then seemed to make a big jump in speed at the end of the speed up sequence. The routine was running correctly and the motor was doing what it was told – namely to speed up on each 8-step sequence by a fixed amount represented by a reduction in sleep-time between individual motor steps of 0.001s starting at .060s until the target speed was reached at .002s.  After a lot of head scratching I realised that this simple approach was completely wrong as the last two increments effectively double the speed (from sleep time of .004 to .002) hence the jerk. A more sophisticated algorithm was required based on a reducing  percentage of the sleep-time at each 8-step sequence. This was easy to do and works much better.

    The flexible stalk lamps are going to be used to light the backdrop, and I have acquired some IKEA JANSJÖ LED lights for the main illumination of the specimen. These are very cheap (£10 each) and have a clamp at the base, and are mains powered. Just have to work out how to arrange them. I have also obtained some 0.5mm white plasticard sheet to build some light diffusers with – some experimentation is required to get this aspect right as it is quite critical to get even lighting.

    Camera remote control. You can also see the miniature beeper attached to the second relay channel on the Sainsmart relay.
    Camera remote control. You can also see the miniature beeper attached to the second relay channel on the Sainsmart relay.

    The remote camera shutter release  I bought on e-bay is now connected to the rig by means of a two-pin din plug and socket, and enables me to test fire the camera when everything is powered off, or if the GUI isn’t giving me the option.

    I have finally got a warning beeper wired in. The 3.5mm audio out put from the Pi requires an amplifier to drive anything at a reasonable volume so I had ruled that out as just an additional complication. MobaXterm could not provide a solution that would use the laptop audio system. In the end the easiest option was to drive a 5v beeper via the Sainsmart 2-channel relay board on which I had a spare channel doing nothing. The beeper can be seen in the picture hanging onto the relay. Volume control is by piercing a hole in the paper cover over the end of the beeper to protect it from damage during manufacture. Without the label it is too loud. With the label, way too quiet. A small hole just perfect -low tech but effective!!

    A simple program was added to turn on a green indicator LED when the Pi is booted up using the Cron job scheduler, so that I know it’s OK to connect the laptop. It sits in the home/pi directory and is called by a line at the end of the crontab  file:

    @reboot python/home/pi/ &

    The “&” at the end of the line calls for the command to be run in the background. So the green light stays on while other scripts run.

    Shall be ready to take some test shots soon.

    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.

    Focus Stacking with a Raspberry Pi – Assembling and Connecting the Peripherals

    There have been some advances and some setbacks – typical of an involved project.

    IR Transmitter
    IR Transmitter flexible stalk

    The LED USB stalk light arrived before Christmas and it was relatively straightforward to disassemble it and modify it to act as the housing for the camera shutter IR Trigger. I removed the USB plug which revealed the wiring; I then had to remove some of the steel flexicoil to expose sufficient wire to enable the wiring to be extended. This was not as easy as I thought it would be as the steel coil is very tough and quite thick. At the other end, the LED shroud came off easily enough, and the LED and its resistor were removed, being replaced with the IR transmitter, and the whole thing being put back together without difficulty. The stalk is attached to the ally chassis with a couple of p-clips I had lying around. It all tested out OK and makes a nice neat job.

    It was also time to figure out how to connect the stepper motor controller and the relay module. In both cases it was necessary to examine the PCB and to draw out the circuit tracks with the chip schematics and work out the overall unit schematic so that the connections could be made with confidence. I also researched the wiring on the internet but was there is very little reliable information, but lots of people making contradictory suggestions. In particular, it was not at all clear whether or not the jumpers on each module needed to be removed or left in place, nor how to use a separate 5v supply from the Pi. Drawing it all out helped to unravel the mystery.

    Having decided how to connect the stepper  motor controller, the next step was to try out the connection scheme without using the Pi, to avoid any Pi related disasters which would probably be expensive. I had various power supplies at my disposal, and once I had hooked up the motor control unit to the 5v supply it was possible to increment the stepper motor by poking 3.3v onto the input pins in sequence. You couldn’t see the motor stepping but you could feel it increment round, and after a number of pulses some rotation was visible. So there was a tick in the box for that!

    Next up was the 2-channel relay module to drive the IR transmitter to trigger the camera shutter – only one channel being used. In this case the wiring was simpler and clearer and there was a bit more information available. It should have been very simple, but absolutely nothing would make the relays operate. There are some LEDs that should light up and they remained dead to the world. I am thinking that the input opto-isolators on both channels are caput; these should be good for 3v to 15v so my attempts with 3v and 5v should have achieved something and done no damage. Anyway, the relay unit cost just £2.35 on e-bay and looks like a Chinese copy of a SainSmart 2-channel 5V relay module, so I have decided to buy the real thing which is quite a lot more expensive – probably also made in China but hopefully with better QA!  The replacement unit should arrive in a few days time.

    Control Panel
    Control Panel

    Next up, was to make the control panel that sits on the sledge chassis and contains some LED indicators, an emergency stop button that stops the current Pi routine, and a switch for the subject lights. Inside this box are the LED resistors and a few other bits and pieces. The finished article can be seen in the pictures. At one stage of the testing, right at the end when I was relying on the final ribbon cable for connection to the power supplies, nothing would work at all – this  after everything had been OK using individual jury rig connections for the power supplies. After some considerable time I found that there was a fault in one of the ribbon cable female connectors that slips onto a PCB pin – in this case the Ground connection which of course affected everything! On disassembling the faulty female connector it was possible to see that the insulation was crimped on as it should be, but there was no nice copper conductor emerging from the end – QED, no electrical connection. Soldering iron to the rescue and everything worked perfectly. I now check the continuity on each ribbon wire before using.

    Centimeter Scale
    Centimeter Scale

    I have also added a centimeter scale alongside the rack, with a pointer on the stage, so that the approximate position of the stage can be visually and consistently referenced. This may also be useful when calibrating the stepper motor to determine the number of steps per mm.

    So I am just waiting for the new 2-channel relay module and then I can finish testing. After that it will be time to connect everything up to the Pi.

    Once everything is tested and working correctly I will draw up a detailed circuit diagram and will post it on this site.

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