Sunny Sunday time-lapse

Today was a nice and sunny winter Sunday, with outside temperatures around -6°C.  From the weather camera capturing an image every minute, a time-lapse video covering approximately 12 hours is assembled. It shows the changing weather conditions plus a few cross country skiers enjoying themselves in the sunshine. It takes about 3.5 minutes to show. I think you also agree that the Raspberry PI weather camera does a pretty good job with the heater system keeping it in focus!

Time-lapse 25. Jan 2015

 

The video was created automatically by first generating a label in the bottom left corner of each image. The information about exposure and camera temperature sensors is taken from XML files generated by the on-board RPI software.

Second, 3 interpolated images between each pair of originals were created, effectively giving the impression of images taken every 15 seconds.  The XML generation, image interpolation and labelling software is home grown.

The final step was time-lapse video generation using ffmpeg.  A bash script running under Kubuntu orchestrates the whole thing,  but a very similar process can easily be achieved under e.g. Windows.

Mark2 heater board

In the Raspberry Pi Lens Heater post, the motivation for heating the RPI camera lens when operating in cold temperatures was given. A prototype version of a heater system was also presented, it has worked very well since early January. The images were significantly sharper when the lens is heated to around 15-20°C. Even well before that, the effect is significant.

Temperatures in January have been rather mild, around 0°C  (+/-5°C), but recently we had a day below -15°C  and the camera board temp sensor showed only +4°C at the lowest. Still, the star images and other details verified the heating had a good effect.

A friend showed interest in setting up a similar camera with heater, so that provided a chance to improve on the design. Clearly, the original “birds nest” implementation did leave something to be desired. My friend also wanted to accommodate 3 temperature sensors, one for the camera board (“CMOS”), one for camera house (“BODY”) and one for the outside (“OUT”). That meant an even bigger nest or something slightly smarter would be required.

After buying some parts from China via Ebay I came up with the rather obvious idea of eliminating the “nest” by assembling the components on a single board that would attach directly to the GPIO pins of the Raspberry PI. An informal sketch of the design follows:

image

The overall idea is based on a 4×6 cm prototype PCB board and a 2×13 Pin header soldered together in such a way that the board becomes a suitable add-on to the Raspberry PI.  The mini relay for switching the heater on/off is placed on the opposite end of the board relative to the pin header, along with its control circuitry. Between the two there is room for a “bus” for multiple DS18B20 temperature sensors. One is placed on the board, screw connectors are provided for the other sensors off the board. As the DS18B20 sensors are “1wire” devices, they can be identified in software via their unique serial numbers.

The board design thus allows both GPIO relay control + reading of 3 temperature sensors.

image

This looks like an improvement to me.  As designed, the board simply connects to the 28 GPIO pins of the Raspberry PI model B using the 2×13 Pin header. My friend is getting the Model B+ with more GPIO pins, but luckily the common pins of the two RPI models are the same, so the board should fit the B+ just fine.

image

A couple of quick tests showed that both the relay control and the temperature sensor worked just fine, so the board is fully operational and awaiting the Mark2 camera board heater element to be completed.

After completing the Mark2 heater board, I received some PCB Pin connector kits. They are possibly better alternatives than the screw connectors, since you do not risk getting the polarity wrong once the board has been properly made. Still, I am quite happy with the current Mark2 board!

Raspberry Pi Lens Heater

Happy New  Year!  Again it has taken some time since the last blog entry, due to Christmas activities taking priority. However, quite a few things have been happening with the Raspberry Pi weather camera in December.  Just over Christmas we had a cold snap, and I noticed that the camera images appeared degraded in the cold, they used to be much sharper before, didn’t they? I decided to check, here is a comparison for the same time of day, comparable weather, but different temperatures on 26. Dec (-15C) and 01. Jan (+3.8C).

værkamera_langsynt_i_kulda

You don’t have to be a rocket scientist to see that the image quality is degraded at lower ambient temperature. When comparing with older images from September 2014 when it was much warmer it is also clear that the relative sharpness of the 01.  Jan image is also degraded compared to the warmer September days.

I looked around to see if this was a known problem, and I did find a report of a very similar problem by someone in Germany (?), where problems with focus were correlated with cool temperatures.  More looking around landed me at a page with a lot of technical specifications for the Raspberry Pi. There is an interesting quote there that says “The threaded focus adjustment is set to infinity at the factory. Changing the focus from infinity to something closer requires that you turn the threaded lens cell counter-clockwise, moving the lens further away from the imaging sensor”  .

In other words, the Pi camera lens has fixed focus unless you start messing with it.  It is a so called Extended Depth Of Field (EDOF) lens, which essentially is using some clever optical and internal processing tricks to reach a pretty decent, sharp image without the user having to perform any focusing at all. Note that it is not an auto-focus system, the lens is fixed at all times. So it is a decent and user friendly compromise if you operate the camera within the design specifications.

Based on this, I suspected that there was an assumption of optimal temperature built into the PI camera EDOF system, for example an assumption room temperature (say, +20C).  My take would then be that low winter temperatures causes the lens assembly to contract/deform in such a way that the lens is effectively brought slightly closer to the imaging sensor, resulting in a “beyond infinity” focus setting, and thus rather blurred images in the cold.

This is bad news if you are using the PI camera for outdoor imaging at low temperatures, but is it possible to do something about it? Of course, one might consider changing the lens focus position by rotating the lens, but this is not practical in this case, considering the tiny lens and doing it in the cold.

However, heating the camera board to near room temperature is perhaps more feasible? From amateur astronomy we know dew heaters are made by coupling a number of resistors in parallel and sending a small amount of current through them, generating something like 2W in total to heat the optical surfaces and thus avoid dew.

image

We could perhaps make a similar system using just a few resistors and generate something like 0.5W to heat just the camera board?  The general idea is illustrated at left, using 3×150 Ohm resistors in parallel. If you apply 5V to this setup, it will generate 0.5 Watt. By placing the resistors close to the lens assembly, then much of the generated heat will be transferred to lens.  By also measuring the temperature close to the lens one can determine how long/much the lens should be heated and optionally turn it on/off automatically as required. To make this work,  one needs to insulate the resistors so they don’t lose the generated heat too fast. I decided to embed the resistors in melted plastic, as I had some hobby plastic that could be used. I made a simple form, put the resistor assembly in it, poured plastic over it and melted the plastic with a heat gun.  After cooling and adjustments I had a basic lens heater element,  shown below. The heat from the resistors will not dissipate as easily and will probably also cause a slightly more uniform heating around the lens.

image

I could have put a small DS18B20 temperature sensor into the melted plastic, but I was unsure about whether it would survive.  So instead I cut a trace for it after the plastic had cooled and glued in place between the red markings in the image below left. Before that I had soldered suitable wires to it. At the same time, holes were drilled to match the existing holes in the PI camera board. Here one needs to be accurate with the separation and placement of the holes.  In the end I used M2 machine screws and a piece of plastic on the back side of the camera board with similar holes to hold the assembly in place. The purpose of the plastic on the back is to insulate both electrically and also temperature-wise.

 image

The DS18B20 temperature sensors are quite “friendly”, as you can connect many such sensors to the same wire/GPIO pin, they are so called “1-wire” sensors, although you also need wires for current. Once connected to the GPIO pins, the measurements turn up as small text files you can read.  The method I use for reading the temperature sensors is described here and  especially here . Since the weather camera images are scheduled using crontab, the driver for using 1-wire  sensors must be loaded at system start up, there is a page describing how to do that here.

Assembly

In a previous post, I discussed how to control a relay, and now this could come into use.  Quite possibly, one does not want the heater to be on at all times. We have very variable temperatures during the winter, and when the sun is high in the summer it can get rather warm. So a system for control the applied heat is required. If a separate power supply is used and the socket is accessible, one may do it manually. But in this case, the socket is not conveniently located and I eventually want 100% automatic temperature control.

A simple solution to this is to use the relay board,  allowing the power to the lens heater to be controlled from the PI itself, even when the heater is powered from a separate power supply. Then it also becomes possible to automate the heater control, by evaluating the temperature sensor embedded in the heater. Typically, one may want to turn on the heater if the temperature drops below +10C and turn it off when it exceeds +20C, or something of that nature. Such a thing is possible to do from software,  using the relay.

Adapting a relay board inside the camera housing was not part of the original weather camera design. The PI was simply placed on a  85mmx123mm aluminium plate that slides into the tracks in the inside of the camera housing. To fit the relay, I found a piece of unused plastic that could serve as a relay board holder. Adapting the relay board required a couple of holes to be drilled, and checking that the final assembly still fit inside the housing.

image

There are probably far more elegant ways of connecting it all than what is shown below, it is a bit of a “bird’s nest” :-) But it shows where the temperature sensors are and also that the wires are connected to a 28 pin pin-header on the far side, so nothing is soldered directly to the GPIO Pins of the PI (Model B in this instance). If required, the whole thing can easily be disassembled.

image

In the image above, all wiring is done, except for the power to the heater and power to the PI itself.  Notice that he PI camera board + lens heater assembly is placed on the front side of the plate holding it. This is different from before, and places the camera closer to the front glass.

The PI is powered via a micro USB cable (black in the image below), and the lens heater is connected to the red/black power cable via the relay. The whole thing then slides inside the housing, using the tracks on the inside walls.

image

A likely improvement and simplification if I was to make it again, would be to integrate the plate the PI sits on with the actual holder of the camera/heater as one piece.  Another likely improvement would be to create a “breakout board” for both the relay and temperature sensors. That would eliminate the need for much of the messy wiring. So the current solution should be considered a prototype.

Below is the new front of the camera, now a combination of the old dew fix and the new lens heater. The lens heater in this configuration should also help to prevent dew, since the back side of the glass is now heated.

image

Initial test results

Once assembled, I was eager to test it. First step was to check that the camera still worked, and that it was possible to read both temperature sensors. The 1-wire sensors kept their promise and both showed data. Each sensor has a unique serial number, but you cannot say which is which by looking at the serial number, you have to observe the behaviour.  After some simple experimentation, I was able to determine the “body” sensor and the “cmos” sensor  and their respective serial numbers.

Then the real test began, by applying current to the lens heater. To switch the relay on/off, I used the C code found in the article Raspberry Pi – Driving a Relay using GPIO, i.e. the same method as in Raspberry Pi – Controlling a Relay.

After switching on the heater current, the “cmos” temperature sensor started to report higher values. Success! During the initial testing, the outside temperature was about +2C, and before heating began the cmos temperature sensor reported just over 5C. After switching on the 5V heater current, the temperature increased gradually over 45-60 minutes until it stabilised around +17C. This was pretty good!  If more power is needed, it is possible to run the heater at 6V or higher, one just needs to check that the power rating of each resistor is not exceeded, we don’t want anything to catch fire!

How about image quality? This is the best part, the sharpness is dramatically improved. At the time of writing, it is dark. But I will write a new post tomorrow,  comparing daylight images to previous images at the similar ambient temperature conditions.

To conclude, the heater works and it has the desired image quality effect!

fighting dew

I have not updated the blog for a while, because I have been working abroad (Singapore). During that time, the weather camera developed dew problems after a long period of massive rain.  The camera house is not 100% water proof, and the variation on outside air humidity causes dew to settle on the inside walls. Another problem was the big front glass that was exposed to the cold environment, it became the coldest “wall” as seen from inside, and therefore the most likely place for dew to settle, precisely where you don’t want it. The result is seen below, lots of dew causing fogging of the image!

image

The camera chip & lens cannot be seen in that image, but it is in the centre between the fake LED components (the camera house is from a fake surveillance camera). The fake LEDs site on a dummy PCB that is also part of the problem: it make internal ventilation problematic, increasing the dew problems.

As a first step in fixing the problem, the camera was opened and all the fake LEDs were removed. After all, their only “purpose” was to cause strange reflections of the Sun. Next, 4 big holes were drilled in the fake PCB, so that air could be freely ventilated between the space between the camera and front glass, and the rest of the camera house, where the Raspberry PI generates some heat. 

image

Measurements have shown that the temperature inside the camera house is generally about 6 degrees C higher than the outside air.  This looks like an improvement, but the front glass is still much to large and exposed to the environment. How about masking off the part of the glass that the camera isn’t looking through? I did some indoor tests with a piece of cardboard to determine the size of the area that needs to be clear of obstructions, and the result is shown below. Most of the glass can be covered without obstructing the view from the Raspberry Pi camera lens.

image

The thinking is to cover and insulate the the glass plate on the outside so the limited heat generated inside the camera house has a smaller surface to “work on”, i.e. just the inside of the unobstructed circular glass area exposed to the environment, right in front of the lens. 

image

The final result was as show above: A piece of Plexiglas with the same size and shape as the cardboard plate (slightly bigger hole) with a 1cm thick foam plate as insulation towards the glass. The assembly is then attached to the front using water proof tape.

The resulting camera performance can be observed on the weather station page .

Weather camera database

At the top of this page, you may have seen the weather station link, showing up-to-date temperature, pressure and more. The page updates automatically every 10 minutes, 24/7. The weather station was installed 31. Jan 2010 taking readings every 10 minutes. Beginning 04 Jan 2012, the frequency of readings was increased to every 5 minutes, and it has been doing that since then, almost without any pause.  31 January 2015 will mark the 5th anniversary of the weather station and the associated web page, pumping out graphs like this:

image

On 03 Sep 2014, my new Raspberry PI based weather camera became operational, it has been capturing images every minute 24/7 since then, adding visual context to the other data. It is easier to understand the causes of the temperature fluctuations when you see what the weather actually looked like at the time.  The weather camera construction and setup will be subject of later post(s) here.

The database for the weather station has collected only  24MB of data since 2010, but capturing images is something completely different, even though the weather camera is set up to save images at half resolution and as JPEG with high compression. Keeping all those images as individual files in the file system, eventually causes trouble. It is also cumbersome to review old images that way.  Therefore, I have started developing a database & viewer for the images and other data produced by the weather camera. Eventually, this will be merged with the weather station data, but first we need to handle the substantial volume of image data coming from the camera.

As a first test of some ideas, I created a prototype database and viewer application to see how responsive it would be. I took all images from October 2014 – more than 44 000 individual images! – and put them all in the same database, resulting in a file size of 2.95GB. Here is a small glimpse of the responsiveness, which I find quite adequate.

Testing the weather camera database

Even though this appears to be working fine, I would like to find ways of compressing the data more, without compromising on the final image quality, so there will be more tests before I settle down on the “final system”. I am thinking of presenting temperature or other graphs alongside the images, to give better overview. The image database is based on SQLite, but I have added my own C++ layer on top of it, which makes it more suitable for object orientation. This layer may become open source at some stage, if it turns out the way I want it.

The next video below, isn’t really about the database as such, but it illustrates with better image quality what the weather camera can do. Of course, producing such time lapse videos from the database is a possible goal.

Weather camera time lapse 04. September 2014

If you are interested in any of this, and want to know more detail, tell me about it!

Raspberry Pi – Controlling a Relay

I have no background in electronics, but the Raspberry Pi has given me an incentive to learn. Some time ago I made a Raspberry PI weather camera (it will be subject of a separate post here at some later stage) which has now operated continuously for over two months in very mixed weather. Sometimes, the camera develops dew on its front cover window, it may be inside or outside or both.

imageCould I do something to fix that, for example using a window heating circuit controlled from the Raspberry Pi  camera?  This post is exploring how such an external circuit could be controlled.

First, one might be tempted to think the GPIO pins could provide enough power to drive such a circuit, but that is a sure way to short circuit and terminate the PI, so don’t even think of trying that!

When looking for answers, I found an extremely instructive post on the subject, Raspberry Pi – Driving a Relay using GPIO . It provided a solution: The GPIO pins cannot drive a heater directly, but they can be used to control a relay – which works as the on/off switch of another circuit isolated from the PI, i.e. using its own power supply.  I recommend reading that original post carefully, this post should be regarded as mere ‘replication’ as done by a novice. A circuit diagram was also provided, but how does it actually work, would I be able to use it in practice?  I set out to try and understand.

Starting at the relay end, the switch in the relay opens and closes as the relay coil, an electromagnet, is powered. It turns out that the Raspberry Pi 5V GPIO pin can provide enough power to activate this coil, but we need a way to turn the power on/off so the relay can be opened or closed from software running on the PI. For this purpose, a transistor is ideal, as it may be used as yet another on/off switch, only at lower power. image By combining the relay coil and the transistor circuit one should be able to control the external circuit.

A transistor has 3 pins or “legs”, and there are 2 kinds of transistors, NPN and PNP types, we are here using the NPN type. You may want to look up the difference.

The current that goes through the relay coil goes to the collector  pin of the transistor and exits via the emitter pin, on the condition that the base pin is powered. The emitter pin is grounded.

Even less power, just a couple of milli-Ampere at 3.3V is required to “turn on” the transistor . Using U=R*I, U=3.3V, R=1.2KOhm we find that it corresponds to about 2.75 mA of power from the GPIO pin to the transistor base pin. Turning this GPIO pin on/off will control the transistor switch, and therefore also the relay.

We are almost done, but one component is missing: the diode labelled D1 above. The diode is there to protect from power spikes when switching off the relay, when the coil magnetic field collapses. The diode dissipates the power spike and makes sure the PI survives another on/off cycle!

Ok, that’s all talk, how about the real thing?  First, some components are needed, and for this Ebay is quite nice. I got most components from Asia with free shipping. For the relay, it is important that the coil circuit is 5V, I used a SRD-S-105D relay. The transistor must be NPN type, and the post above mentions the BC337 NPN transistor. Before receiving them, I found that I had a couple of BC547 transistors in a drawer, and it turns out they can both be used for this purpose.  The diode I used was 1N4004.

With all the parts collected, you just solder on, right?  Well, some do…. I had a strip board with holes connected in groups of 3, and I found it quite hard to imagine how to layout the circuit on the board without making a big mess.  imageTherefore, I got the idea of placing the strip board on my flatbed scanner, copper down, so that I could create an image to draw on.  I used Photoshop layers to experiment with layouts. I am sure much more suitable software exists for this purpose, but it worked for me.

I created the drawing at left this way. It is a little funny, because you are looking at the copper side and the components are really supposed to be on the opposite side, as in the photo further down. The black rectangle corresponds to the relay, and inside it you find the 5 relay legs as black dots.  The thing that almost looks like a grey resistor inside the relay rectangle is supposed to represent the relay coil, and the red line is the switch itself. The green show connections between strips.  The other symbols are perhaps more understandable. Observe that the diode and transistor legs must be soldered correctly, look up the data sheets! I am not suggesting you duplicate this way of working, it is highly non-standard, it simply shows how I was able to layout the components. Once again, remember that you are looking at the components from “under” the board (copper side), the components are really on the other side, as in the photo below-left.

image

I had to drill an extra hole for the 5th centre pin of the relay, as it hit right between 4 holes. I am no soldering expert, but the end result seemed quite ok to me.  But looks is one thing, more important is: Would it work?

Testing

In the image below, the +5V pin on the relay board is connected to the +5V GPIO pin (pin 2) using the black jumper cable.  The blue jumper is connected to GND (pin 6) and the green is connected to pin 11 (GPIO 17). The photo was taken before the green contact was added to the relay board.

image

As for testing the controlled circuit, I connected an external power supply to one of the green contact sockets on the relay board, and coupled a LED in series on a breadboard. The test was performed using the C program available from the article mentioned in the beginning of this post. Here’s an animated GIF showing that the relay did in fact respond as required. Success :-)

DSCN2905_crop

Raspberry PI model A+

Today this blog debuts with some very modest technical content. The Raspberry PI single board Linux computer has taken the world by storm, and it also fascinates this author.  The plan is to share some PI experiences and perhaps even inspire.  What is then more fitting than opening this series by mentioning a brand new member of the Raspberry PI family? Today, it was announced that the PI family got a new baby weighing only 23 grams, i.e. the smallest of the main boards. The baby’s name is Model A+.

I have only read about it, but it is clear that it is an improvement on the original model A. It is significantly smaller, uses less power (20-25% less than the original model A), has more GPIO pins (40), uses a Micro SD card and is cheaper! Other than that, it is in many ways similar to the Model A with 256MB RAM. There is also an audio port and a slot for the PI camera board, plus a HDMI socket and a single USB port.  This means you need a powered USB hub if you need to connect more than one USB device. The computer can run Raspbian  linux just like the other models.

Although the A+ resembles the model A, it is also closely related to the Model B+, the mounting holes are the same distances from each other, and the model A+ could be said to be virtually identical to the model B+, minus the Ethernet port, minus 3 USB ports and minus half the RAM. Given the form factor, this model is probably targeting dedicated embedded projects such as small robots or other applications that don’t need to be networked (it can be networked via a USB wireless dongle).

Clearly, it is imagination that limits what people can do with these things, and best of all: It is a great way of learning new stuff. I have already learned things about electronics I didn’t know earlier, and it is a great feeling to observe yourself getting a little bit wiser every day. Let us see what imaginative people can do with this baby!

This modest post will be followed up with examples from using the Model B, and things it and its cousins can do. Stay tuned!

To my father

Today is the birthday of my late father Carsten Arnholm Sr., he would have turned 85 years today. He was a caring grandfather to our children and came to visit us very frequently for more than 20 years. He also travelled with us several times abroad, and always had something interesting to say. He was someone who never stopped learning and we could sometimes sit and talk about history, technology, politics or anything else for hours.

carsten_sr

Although he was educated as a lawyer, and spent his professional time in the Ministry of Finance working with oil taxation laws, his passion in life was photography.  He was born in 1929 and tried photography at a very early stage, and it never stopped fascinating him. He went through all the phases of technology from simple cameras in the 1940s, to more elaborate laboratory techniques in the 1950s and 1960s.

His great tragedy in life was the loss of his sister, father and mother over 18 months in 1947-1948, when he himself was only 17 years old. You don’t go through such an experience without scars, but he managed to get a good education and his passion for photography quite naturally became even deeper. He became very active in photo competitions world wide and won many prizes. He also became very active in local clubs such as Oslo Kamera Klubb where I saw him enjoy himself in the late 1970s.

Although never being formally educated in science or technology, he never had any problems following technological trends. Spending many long nights in the darkroom in the 50’s, 60’s and 70’s gave him a deep insight into what constitutes a good photography and how to process an image. Sometimes he used very elaborate lithographic techniques with marvellous results. The image below was made in the 1970s without any digital techniques. The raw material is an image of an elderly portuguese woman, which he turned into his famous “Ektepar” (“The Couple”). It shows his humorous side!

02_Ektepar When people complained about “image manipulation”, he rarely agreed. He claimed all images are heavily processed in one way or another, “raw” presentations really do not exist.

When digital photography arrived, he was thrilled. At first he gave me long lessons about the superiority of negative film over the first digital cameras, but he really embraced digital image processing from day one.  He scanned films into digital files and spent much time exploring Photoshop. He became such an expert of Photoshop that when he was more than 80 years old, he routinely gave courses to people less than half his own age.

Later, the films were abandoned in favour of the top-of-the-line digital cameras. He had fully converted from simple box cameras, black and white film and a chemical lab, into someone who naturally spoke of bit depths, unsharp masks and many other things.  Although his techniques were superb and detailed, he also had an eye for what is a good image. He travelled with friends to both Antarctica and Svalbard in the far north. Arctic photography of natural life became one of his specialties.  Some of his portraits and other images can still be enjoyed at his personal website Carsten J. Arnholm . Have a look yourself.

In 2011, it became clear that he suffered from cancer. I cannot quite comprehend how he at once accepted his situation, and never ever complained although he insisted on receiving the treatment that doctors could give. His struggle lasted for 18 months, during which time he even found ways of doing photography from bed.

On the first day of 2013 he passed away quietly.

Thank you for being who you were. Happy birthday from your son.

My corner of the universe…