Arduino – They got it mostly right!

In classic Fast Show style – this week I have been mostly programming Arduino.

After the demise of my RaspberryPi/Gertduino weather station project I decided to go fully Arduino.   I’d bought 2 Uno clones from ebay and never did much with them, so time to put them to use!

Next I bought the ethernet shield.   About £10 has bought be an ‘internet of things’ building platform.   The plan was to have the weather station based in the garage reporting via IP to the Linux server in the house.   That way the outboard station was all flash based with no local storage.   I’ve used Atmel AVRs a LOT and know they are pretty robust.   No more worries about SD cards eating themselves!

All the code for the gerduino (which handled the time critical and analogue elements of the weather monitoring) was all written in vanilla C.   As I’m used to.   Lots of use of interrupts, printf (yes, I’m lazy) and some bit bashing too.

As I wanted to use the ethernet shield I could either convert to the Arduino build env or try and mate the Arduino libs with vanilla C.   A quick look around showed that using the C++ libs with vanilla C was like trying to drink the water of life.   Many have tried, few have come out of the experience with their sanity intact.

So time to get friendly with the way the Arduino IDE works.   The setup and loop is very neat, you can get something working in very few lines of code.   Even the ethernet libs were pretty simple.   Take up acres of flash, but pretty simple! 🙂

Stumbling blocks… I use the 1ms(ish) interrupt for things other than just counting milliseconds.   I don’t want to put time critical stuff in the loop section as the timing in there is uncertain.   I don’t want to use timers 1 or 2 as they are used to control the solar thermal experiment. (details later, it needs a new pump)   So cue a bit of core code hacking to add a function pointer to the end of the timer0 overflow.
static volatile voidFuncPtr intMsFunc = 0;

// volatile static voidFuncPtr twiIntFunc;

void attachMsInterrupt(void (*userFunc)(void)) {
intMsFunc = userFunc;
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
// copy these to local variables so they can be stored in registers
// (volatile variables must be read from memory on every access)
unsigned long m = timer0_millis;
unsigned char f = timer0_fract;

if (f >= FRACT_MAX) {
m += 1;

timer0_fract = f;
timer0_millis = m;


So I’m pretty much on track.   Didn’t bother to use the library ADC functions as I oversample to get 12 bits.   The cheap chinese wind direction I’m using sensor is a bit crude and some of the direction values are too close together for accurate detection in 10 bits.

The onewire lib worked first time.   EEprom, serial, TCP sockets etc.. all seemed to fall into place.   They’ve  all done a good job.   TWI was a bit funky as the examples are a bit lacking.   Its also interrupt based which is something I’ve not done on the atmega.   And of course it takes the address shifted down by 1… they don’t really mention that bit 🙂

So what about gotchas… the serial writes!   Don’t use them in interrupts, they don’t work 🙂   To stop the serial writes from blocking they use a software buffer and interrupts.   Makes sense, anyone coming to this new won’t realise that sending strings at 9600 baud takes a really long time and it will mess up your carefully calculated delays.   But of course when in an ISR in atmel world all the interrupts are turned off!

Also, just like with printf you use up huge amounts of SRAM with the format strings.   Remember to put F() around your text!   Saves hundreds of bytes.   I always use rom_printf.   An inline that puts the format string in PROGMEM.

Just to make life complex I found arduino-netboot.   A small bootloader that uses BOOTP and tftp to update an ethernet equipped Arduino.   Luckily I have a JTAGIceMkII to hand so could re-program the bootloader.   That was the easy bit… turns out my 3com switch blocks BOOTP when spanning tree is turned on… that took me a while to figure out…

It needs a binary file for the tftp.   Not easy getting these from the Arduino IDE.   Simple way is to find the temp dir and avr-objcopy them yourself.   Yuk!   I wrote a bat file that creates the .hex and a .bin and copies it to the tftp dir.   Hacked platform.txt to make it call my bat file instead of avr-objcopy.   Would have been nice if they’d used makefiles… ho hum.

So now I have a fully Arduino based weather station based in the garage which boots off the network and reports back to WeeWx over TCP.   Lets see how reliable this incarnation of my ‘made from spare parts’ weather station is.

Raspberry Pi vs BeagleBone Black

Ahh, the age old question of which is best.   I’ve seen many a forum thread discussing this very topic.   And as ever many of the threads descend into total carnage!

So what has prompted me asking this?   Well my Raspberry Pi powered weather station has died and doesn’t want to play any more.   We had a power cut on Thursday morning and that was the final nail in the coffin for the SD card file system.   It needs a total rebuild!   And of course I didn’t take an image of it when it was in a working state.

Its not dead dead.   I can still mount it and read the data off.   And I’ll admit to a small portion of blame in this as I didn’t fully sanitise the amount of writing to the SD card.

Its not like I used a cheap SD card either.   An Integral Ultima.   But no SD card is really designed for lots of small write cycles.   They are designed for cameras or MP3 players.   It does make booting a Pi really easy.   Burn an image to the SD card, plug it in and away you go!   Very simple.

So there are 2 basic issues with the Pi that I’m not overly happy with.   1) the simple fact SD cards are not overly reliable and 2) the socket leaves a lot to be desired.   I’ve had to wiggle the SD card a few times to get it to boot.    (and 3) the lack of good IO on the Pi!   another story…)

I’m probably in the minority here as I’m expecting the Pi to run 24/7 in a not exactly kind environment.   The garage does get a bit damp.

The Beaglebone black does have the advantage of an on-board eMMC NAND flash.   And those have proper wear levelling.   As to how much better it will be compared to an SD card?   I will have to try.

The Beaglebone does also have the advantage of lots more IO.   Downside is its 3v3 rather than 5v and all my weather station IO has been based around Arduino type hardware so is all 5v.   *sigh*   This is something the hobbyist is going to have to deal with more and more.   5v is old skool and most electronics is 3v3 at most, if not 2v5 or below.   Would have been neat for the beagleboners to put some 5v tolerant IO on the board, even if it was just enough to replicate the IO of the Pi.

But I think the weather station is going to go fully Arduino. (I’m not using the IDE.   I live and breathe Atmel AVR so coding direct to the hardware is easier for me)   I’m going to try the ethernet shield as its cheap and maybe a Yun.   Costly compared to a Pi though.   Plan would be to make the weather station pipe data direct to my home sever (which runs RAID discs and has a UPS) and log the data there.   No more local storage.   Probably safest.

13A plugs and sockets

Found this in the IET mag.   Quite a good website (if a little badly layed out) about those little plastic socket protectors.

Makes me pretty grumpy that something like this only has to pass safety testing as a toy.   I think the BS 13A plug is by far the nicest and safest out there and probably the only plug in the world that will always sit ‘pins up’ in order to inflict maximum damage to your foot from those machined brass edges.

15A through one of those bent metal US plugs?   I’m amazed they don’t have more issues.   115V might be safer but you get awfully used to toasters and kettles running at UK speeds.   Making toast in the USA is a slow process.

Compact fluorescent bulbs

Oh how I hate these things!   They have to be the most evil of all cheap and nasty non-fake consumer electronic products out there!   I happily accept that the humble tungsten light bulb is far from efficient and that we really do need something better, but a mercury filled bulb stuck to cheap plastic housing stuffed with the worst designed electronics known to man is not the way.

Having tried CFLs in the early years and been frustrated by the glacial warm-up periods and annoyed by the gentle degradation of their light output I’d decided to ignore them.   I’d gone over to using the halogen GLS replacements as they offered a saving without the problems of the early CFLs.   Slight downside to the halogen GLS is if you knocked them when they were on they do like to instantly die.   No good for a bedside lamp it turns out, but that is another story.

So about 18 months ago I decided to have another look at CFLs.   Energy prices were going up and after managing to smack the hall light twice in quick succession while putting on my coat I was getting a bit miffed with swapping out the halogen GLS bulbs.

I had a look around and bought a Kosnic Quick Start spiral 18W CFL.   Well it did what it said on the box, star-up was much improved compared to 5+ years ago.   Oh good I thought, save myself a good few W/hrs a day, no more dead bulbs if I happen to knock the lampshade and no waiting for it to give enough light to walk down the stairs in the middle of the night.

Cut to 18 months in the future… the CFL is starting to flicker when I turned it on and its taking its time to get to full brightness.   Ah heck, they still can’t make them last!   I know putting them in enclosed fixtures will kill them but this lampshade has an open top so that can’t be the reason.   8000 hours?   Not even close!   I think one of my IET mags has an article about new lighting regs that give minimum lifetimes.

Well it only lasted about a week after that.   Flick the switch, darkness remains.   And this is what I found when I took it out of the holder….


Wow, that has properly overheated!   Brown plastic is never a good sign.   This is why you should not buy cheap knock-offs, they don’t use V0 rated plastic.   Anyone see the fake dyson bladeless heater that self-immolated?

Upon opening the CFL the cause of the failure was apparent, 2 of the lead wires had come off the base of the bulb.   They had been nice enough to shroud them in fibreglass sleeves but it didn’t help longevity.


The PCB looks like its FR-2.   No manufacturer or safety markings.   I’m sure we have to put UL references on all our PCBs to meed CE..   Hmm..

So where do I start on this utter POS I have in my hand.   There is a PCB reference for ‘Fuse’.   Well its not there!   The mains inlet bypasses it.   Oh brilliant!


The 4 output wires going to the tube are just twisted to stub wires soldered to the PCB.



Ewwww!   Now admittedly I did something similar once but I was 8, I was using some bell wire I’d found and the end result was my first electric shock.

So what is the future?   Not CFL I hope!   I now have to take this dead bulb to the dump for disposal as its hazardous.   I assume it is RoHS compliant.   It has the symbol (along with CE, however this passed any testing is beyond me!) but its still got mercury in it and either way comes under WEEE.

I have some LED bulbs that I’ve been trying in various places.   Not those nasty ones with 50 or so 5mm white LEDs.   Proper ones with BIG chip LEDs.   Not cheap 🙁   So far so good but they don’t really go much above 60W equiv.   Colour temp is pretty good too.   We will have to see about lifetimes too.   I’d love to dissect one of them but only after they are dead.