Multispectral Imaging with Raspberry Pi

Last weekend I went to a meetup which was a bit like a hackathon. The title was ‘Multispectral Imaging with Raspberry Pi’. In a lot of ways I am not a fan of the Raspberry Pi – I feel that it was hyped as a great way to get kids into programming, but in reality most kids have access to full Windows PCs which they will be more familiar with and also have much more user friendly programming IDEs, tutorials etc. What interested me was the multispectral imaging, so I went along not sure what to expect.

They introduced the concept of the Normalised Difference Vegetation Index (NDVI). This is a value indicates the health of plant life and calculate by measuring the light reflected by a plant. It is defined as:

{\displaystyle {\mbox{NDVI}}={\frac {({\mbox{NIR}}-{\mbox{Red}})}{({\mbox{NIR}}+{\mbox{Red}})}}}

Where NIR is the Near Infrared light reflected by the plant and Red is the red light reflected by the plant.

In order to capture these two values, we used two raspberry pi cameras connected to two raspberry pis. In one of our cameras there was an infrared filter fitted which stops IR from reaching the sensor, however in the other this had been removed and a blue filter put in front. This meant that the red channel of the first camera captured only red light, while the red channel of the second camera captured only IR. Using the two pis and two cameras we could capture both images simultaneously.


The camera with two sensors and two pis

The device operated from a normal usb power bank and connected to the wifi. This meant that we could open a secure shell to each of the raspberry pis from my laptop. In order to synchronise the two photos a wire was connector from the GPIO of one pi (the master) to the GPIO of the other pi (the slave). Therefore, we could connect to the master via SSH and tell it to take a photo. The master would then send a pulse on the GPIO to the slave which was waiting. Both devices would then take a photo simultaneously. We could then copy both photos back to my laptop via SCP.

The final step was to take the two photos and, for each pixel, calculate the NDVI. This value could then be used to create a greyscale image output.


The NDVI photo of a local plant

You can see that the plant stands out as a very white object. Interestingly you can also see other, non-plant objects that stand out. Therefore this cannot be indiscriminately used to find healthy plants, but merely used to distinguish healthy plants from unhealthy plants once the property of ‘being a plant’ has been ensured.

Admittedly, I never tested it on an unhealthy plant simply because none were present and it seems inhumane (inplante?) to purposely make a plant ill.

The pictures are clearly not perfectly aligned and this is because the two images are taken from adjacent cameras. This could be improved by using a single sensor and making the filter removable, however the photos would then not be perfectly aligned.


The Atmel’s Universal Serial Interface (USI) sucks for I2C

Quick Intro to I2C

Along with USART and SPI, I2C is definitely the most common interface used by a microcontroller to communicate with peripherals. In order to implement an I2C bus all you need is two open-collector collector pins, one for the SCL (clock) line and one for the SDA (data) line.  It has to be open-collector because there are times during the protocol when two devices drive the clock line at the same time which can lead to a short circuit if one device drives it high and one drives it low. This way, the bus lines are high by default due to the pull up resistors –  if a device wants a line to go low, it just shorts it to ground via an internal transistor. There is no path from VCC to GND that does not contain a high-valued resistor.

Continue reading “The Atmel’s Universal Serial Interface (USI) sucks for I2C”

Project: Nixie Clock (upgrade) – Accuracy

Throughout this project, I have been saying that the oscillator I am using, the DS32kHz, is accurate to 7.5 parts per million, or 4 minutes per year. Having run the clock continuously for about 3 weeks now I would expect the clock to have drifted by approximately 14 seconds. However, measuring the clock against the clock on my phone I have found that it has drifted by approximately only two seconds.

Continue reading “Project: Nixie Clock (upgrade) – Accuracy”

Mini Project: Guitar Amplifier (old!)

Playing guitar and electronics have been two of my favourite things for a long time now. When I was about 14, I combined these two for the first time and built a pretty simply 32W amplifier. While the design is super simple, it actually has a really nice clean tone and does not distort the sound at all. It’s capable of diving an 8 ohm or 4 ohm load. Today I decided to give it a bit of a clean and check if it still worked.

Continue reading “Mini Project: Guitar Amplifier (old!)”

Project: Nixie Clock (upgrade) – Final Code

The clock is controlled by an ATTiny87 which has three main jobs:

  1. Counting the pulses from the Maxim DS32kHz
  2. Controlling the display via the shift registers (read more)
  3. Interacting with the user via the reed switches to produce a user interface

Each of these jobs will be discussed separately below as well as the main code to bring it all together in a power efficient way. Full code can be found at my GitHub.

Continue reading “Project: Nixie Clock (upgrade) – Final Code”