Wednesday, 4 February 2015

Triggering the camera with the Raspberry Pi

This page is a continuation from Focus stacking - connecting the wires.

The next stage in my project is to set up software called gPhoto2 to allow me to trigger the camera from the python programme that I have already been using on the Raspberry Pi. This will mean that the python programme on the pi can advance the scanner arm, then tell the camera to take a photo, then advance the arm, then take a photo, and so on, even up to hundreds of photos.

This part was very complicated because we were installing packages from source, so I had to phone a friend.

The pi runs Raspbian, which is a kind of linux operating system, and I control it entirely from the command line by ssh-ing through from my laptop. Installing packages on linux is normally fairly easy, but installing from sources is harder. Several of the packages that we had to install turned out to need other packages installed first, so we had to keep going back and installing those and then trying again. We got there in the end though.

We had to install three main things. libgphoto2, gphoto2, and the python bindings to allow us to trigger the camera from inside a python programme.

I'm going to describe it in some detail in case it is useful to others who are trying to do the same thing.

Here is what we did:

First to check that libgphoto2 was already installed:
Type: dpkg -l | grep gphot
ii  libgphoto2-2:armhf             2.4.14-2        armhf        gphoto2 digital camera library
ii  libgphoto2-port0:armhf       2.4.14-2        armhf        gphoto2 digital camera port library
This means that we already have libgphoto2. 

Then to install the programme called pip that would allow us to install python bindings:
Type: sudo apt-get install python-pip

Then to install gphoto2:
Type: sudo apt-get install gphoto2

Then to use pip to install the python bindings in gphoto2
Type: sudo pip install gphoto2
This did not work because of missing dependencies. 

Next we used wget to install libgphoto2.
Type: wget

Rename the libgphoto2 file to make it end in tar.bz2.
Type: mv libgphoto2-2.5.7.tar.bz2\?r\=http\\&ts\=1423043960\&use_mirror\=kent libgphoto2.tar.bz2

Uncompress the tar file.
Type:  tar xjf libgphoto2.tar.bz2

Remove any previous installs of libgphoto2 as they are not working with pip.
Type: sudo apt-get remove 'libgphoto2*'

Install the libgphoto2 package.
Type: cd lib[Tab]
Type: ./configure
This did not work because we did not have libtool.

Install libtool.
Type: sudo apt-get install libltdl-dev
Type: sudo apt-get install libtool

Instal libgphoto2:
Type: ./configure
Type: make
Type: sudo make install

Try again to install the python bindings in gphoto2.
Type: sudo pip install gphoto2
This failed because of a missing file.

Type: dpkg -l | grep libpython
Type:  sudo apt-get install swig
Type:  sudo pip install gphoto2
This failed because of a missing file.

 Type: sudo apt-get install python2.7-dev

Try again to install the python bindings in gphoto2.
Type: sudo pip install gphoto2

Then it was all finished. All of the packages have been installed that will enable me to trigger the camera from the python programme that is already advancing the scanner arm. The next thing to do it to learn how to write the commands in the python programme. That will be for another day now. 

As an advance taster, this page explains how to trigger the camera from a python programme using the gphoto2 commands.

This project eventually took wings and now has a website of its own. To see more about it please go to

Sunday, 1 February 2015

Focus stacking - connecting the wires

This is a follow-on from "Setting up focus stacking".

This page describes how I made the following connections:

laptop --> Raspberry Pi --> chip --> stepper motor. 

Sites and books that I learned from:

The motor is a unipolar stepper motor, with five wires. The final setup is shown below.

Connecting the wires in possibly the right order...

The closer photo below shows how I have connected the components via a breadboard.

The power for the motor comes from the mains cord that originally powered the scanner. I cut the plug off that had previously plugged into the scanner, and I stripped the wires to fit them into the breadboard (Labelled + and - in the photo.). The plug steps the electricity supply down to 15V so the bare wires are not dangerous.

In the middle of the breadboard is the black ULN2803 chip that the raspberry pi will be driving to control the stepper motor.

At the back of the photo is the raspberry pi with 5 wires plugged into its GPIO (output) ports. These wires lead to the breadboard to feed signal into the chip. The signal comes out of the other side of the chip into the other set of five wires that are attached to the motor.

The hard part was to know in which order the coloured wires in each set should go. Here's what I did:

The Raspberry Pi Cookbook explained what to do with the wires that plugged into the pi. That was easy. There are 13 pairs of pins and you can see which colour of wire went onto each pin here. The yellow wire is directly behind the brown wire. So far so good.

The other end of the pi wires plugged into the breadboard like this. Four at the top end and the last one at the bottom end of the chip.

The inside of the chip looks a lot this like diagram that I have copied from, and which he got from the manufacturer. We are using a different chip, but it is very similar. To get the wires in the right order we need to understand what is going on in here.

What is going on in this maze of wires? 

As I understand it, this is how the business works:

The box on the left is the pi. The box in the middle is the chip. The web diagram on the right is the motor. It's the magnets in the motor that really matter.

The motor has four magnets in it. They are connected in series in pairs. Two are on a circuit with a yellow wire at one end and an orange wire at the other end. Two are on a circuit that has a brown wire at one end and a black wire at the other end. If both ends of the circuit are at 15V then the electricity does not flow and the magnets to not do anything. If one end of the circuit is at 15V and the other end is at 0V then the electricity flows and the magnets start to work. When they start to work, they move the motor, just one little step, and then they stop again. That is why it is a stepper motor.

The thing that we need to do is to make the magnets work, one after another, so that they pull round the circle. If they do this then the motor will work its way round over and over again and draw the scanner arm along. If we activate the magnets in the wrong order, then either nothing will happen, or the motor will go backwards then forwards, or it might just go one step and then stop.

But how does the pi control this?

In the normal state, all the pins on the left side of the chip are at 0V and all the pins on the right side of the chip are at 15V. Both ends of each magnet circuit are attached to the right side of the chip. This means that both ends of each magnet circuit are at 15V and the magnets are doing nothing.

When we tell the pi to act, it sends a signal down one of its wires. This switches the left pin on the chip to 15V. It triggers the NOT gate in the chip, which flicks the pin on the other side to 0V. Now, one of the magnet circuits has 15V at one end and 0V at the other end. There is a fifth (red) pin in the middle of the  motor that is also 15V so the circuit is actually 0V at the chip and 15V in the middle. Electricity starts to flow, and just one of the the magnets starts to pull the motor round.

We will tell the pi only to send a signal down one wire at a time, so only one NOT gate will be triggered, and only one magnet will pull. If we can trigger the magnets in the correct sequence, then they will pull one after another, and the motor will work its way round a complete circuit.

The photo above focused on the breadboard end of the pi wires. This photo below focuses on the breadboard end of the wires that lead to the stepper motor.

Once I had put the wires in like this, I had to figure out which order to trigger the gates in, so as to make the magnets work in the right order. I had no idea of the order until I did some experiments. Below are two tests that I found useful.


1) Test that the motor works. 

I held the two power wires against the red and one other colour of wire coming from the motor. The motor made a clicking noise, confirming that it was working.

2) Check the order of the pins

I wrote an experimental programme that told the pi to trigger the wires in pairs. The programme tested all possible pair combinations, and printed on the computer screen a list of which pair was being tested, as it happened. If the motor made a loud clunk, then I knew we had triggered the wires in the right order. If it made a quiet clunk then it meant the pairing had not worked well together.

Here's what the programme did: Programme rationale.

The code of the programme is here: Programme code
(It is adapted from the suggested programme in the Raspberry Pi Cookbook.)

To run a programme like this, I used WinSCP to put the programme on the pi. I used putty on my laptop to ssh to the pi. The programme was in my home directory and I had to type the following to run it:

> chmod +x test.txt (to change the permission so the programme can run.)
> sudo ./test.txt (to run the programme.)

When I ran this test, the scanner went clunk clunk clunk, and I could just see the cogs moving a bit. However, about every third clunk was louder than the others, and it was clear that these louder clunks were the good pairings. Conveniently they were the pairings 0-1. 1-2, 2-3, and that meant the fourth good pairing must be 3-1. Success!

What's next?

I re-wrote the programme to run these good pairings in the correct sequence, and the scanner arm smoothly worked its way along, with no backwards steps. Victory!

This is the new programme: Programme code
To reverse the arm I used this programme: Programme code

Now I have a programme that makes the scanner arm potter along the scanner in the direction I want and at a speed that I specify. Next I have to install a programme on the pi that will allow it to trigger the camera as well. Once I've done that, the system will be able to move the sample one tiny step towards the camera, then trigger the camera to take a shot, then move again. Once that's working, we are well on the way.

The next step of this project is continued on this blog page.

This project eventually took wings and now has a website of its own. To see more about it please go to