Thursday, 7 February 2013

Hammer vs Screw Driver

What's better for your project, a Raspberry Pi or an Arduino? Linux or a microcontroller? I'm not an expert by far, but some people have been asking me this question and I've been asking it myself for a couple of projects, so here's a short answer from my perspective:

A Raspberry Pi (or other general purpose computer) is good for doing multiple things at a time and interacting with people, especially people at a distance, even multiple people at the same time. It can also interact with hardware attached to it and switch things on and off, make some measurements, etc. There are no guarantees about how quickly everything will happen and response will slow down as you ask it to do more things. That's fine if you don't care if your sprinkler goes on right now, or just really soon. For things that are really time critical, your Pi may not be watching at just the right moment and might miss something. (WiringPi incorporates interrupts on the Pi.) (Latency data below.) Without additional hardware your Pi will only read on/off (digital) signals so you can tell the difference between dark and light, but not between a sunny day and a cloudy day (analog).

An Arduino (or other microcontroller) runs only one program at a time, so your program will always be in control and will only miss something if your program ignored it for too long. If it's really important to catch it, you can set one of the hardware interrupts to run a function whenever something happens. This will let you update whatever you're trying to control much faster than 100 times a second. It directly supports both analog and digital signals to measure things like how bright it is outside.

If your project needs to talk to people and be network friendly, include a Raspberry Pi.

If your project needs to read analog data and react to inputs right away, 100 times a second, include a microcontroller.

If it needs to do both, then maybe you need both and there is certainly room for both in most boxes and budgets. There are lots of different ways to interface them so that the Pi can concentrate on UI, communications and data storage while the microcontroller concentrates on making everything happen at exactly the right time. If space or budget are really tight you may be able to cover both with a single unit, but it may be more difficult. Other details to consider include:

Memory: A general purpose computer usually has more volatile memory (e.g. 512MB RAM on the Pi) where you can store programs, or data that change often and have them vanish when you turn off the power. A microcontroller usually has less volatile memory (e.g. 2KB on the UNO) combined with some non-volatile memory where you can store programs that won't vanish when you turn off the power.

Booting: A general purpose computer has to load programs into volatile memory and initialize them before anything useful happens. (This can take about a minute for a Pi from and SD card.) A microcontroller already has a program in memory and starts doing useful things almost immediately. (a few seconds on an UNO with the bootloader, or less.)

It's important to pick the right tools for the job. I found these hemostats from Pakistan at Princess Auto for a few bucks each. Very useful for fine work and clamping! Lousy for driving screws or nails. The little board getting the power leads soldered on is a Teensy 3.0 microcontroller with processing power similar to a Raspberry Pi and much faster than an Arduino UNO.



Pinging from one pi to another over wireless on the same wifi network shows it can be quite fast, except when it's not. If latency of less than 1/4 second is important, you may want another solution.


64 bytes from rwspi.local (192.168.2.24): icmp_req=1421 ttl=64 time=225 ms
64 bytes from rwspi.local (192.168.2.24): icmp_req=1422 ttl=64 time=28.2 ms
64 bytes from rwspi.local (192.168.2.24): icmp_req=1423 ttl=64 time=223 ms
64 bytes from rwspi.local (192.168.2.24): icmp_req=1424 ttl=64 time=25.1 ms
64 bytes from rwspi.local (192.168.2.24): icmp_req=1425 ttl=64 time=12.9 ms
64 bytes from rwspi.local (192.168.2.24): icmp_req=1426 ttl=64 time=203 ms

Pinging google.com is reliably faster over a much longer path:

PING google.com (74.125.226.66) 56(84) bytes of data.
64 bytes from yyz06s07-in-f2.1e100.net (74.125.226.66): icmp_req=1 ttl=54 time=18.8 ms
64 bytes from yyz06s07-in-f2.1e100.net (74.125.226.66): icmp_req=2 ttl=54 time=15.1 ms
64 bytes from yyz06s07-in-f2.1e100.net (74.125.226.66): icmp_req=3 ttl=54 time=20.5 ms
64 bytes from yyz06s07-in-f2.1e100.net (74.125.226.66): icmp_req=4 ttl=54 time=14.0 ms