I, For One, Welcome Our New Raspberry Pi Overlords
%#@&$! Raspberry Pi! I’ve been a Raspberry Pi hater since they first appeared on the electronics hacking scene a few years ago. I had no strong reason for disliking the Pi, but something about it just bugged me. It was too cute and trendy. It felt like a new kid forcing its way into the clubhouse of ATMegas and PICs and Propellers, trampling everything with well-meaning but misplaced enthusiasm. The media portrayal of the Pi bugged me too. It was constantly compared with the Arduino, but the Pi isn’t even really the same class of device. It’s a full-on desktop computer, with a Linux operating system, USB mouse and keyboard, ethernet, and HDMI video. I thought it would make as much sense to write articles comparing Arduino to the MacBook Air.
Part of my dislike for the Raspberry Pi was also a grumpy old man conviction that it was just too easy. “Back in my day,” I’d say, “we didn’t have your fancy operating systems and scripting languages and network adapters. If we wanted to code on an embedded microcontroller, we had to use bootloaders and cross-compilers and bit shifters and registers named UCSR1A. Now get off my lawn!”
You can probably guess where this is going: I finally gave in to the march of progress, and built some experiments with a Raspberry Pi. And despite my initial reticence, I have to say that Raspberry Pi tastes pretty good!
A First Taste of Raspberry Pi
My first Pi project was an electronic symphony orchestra, derived from an article in Make Magazine. I connected a few external pushbuttons on a breadboard, and wrote a Pi program to play various instrument sounds when the buttons are pushed. I also created an on-screen GUI showing what instruments are currently playing, and you can click on an instrument’s picture to make it play through the UI. Pretty neat! Maybe it could evolve into some kind of Pi-based jam box.
So how is Raspberry Pi development similar to working with a PIC or ATMega (or the ATMega-based Arduino), and how is it different? What kinds of projects are best suited to each platform?
Both the Pi and the Arduino are self-contained computing boards, about the size of a deck of playing cards, with a price around $30. Both have a bunch of general-purpose I/O pins that can be connected to external buttons, LEDs, sensors, LCD screens, motors, etc. Manipulating those I/O pins from software is easy on either platform:
GPIO.setup(10, GPIO.OUT) # configure pin 10 as an output
while True:
GPIO.output(10, True) # LED on
time.sleep(1)
GPIO.output(10, False) # LED off
time.sleep(1)
Arduino LED blinking, in C
pinMode(10, OUTPUT); // configure pin 10 as an output
while (true)
{
digitalWrite(10, HIGH); // LED on
delay(1000);
digitalWrite(10, LOW); // LED off
delay(1000);
}
Looks similar enough, although the preferred programming language for Raspberry Pi development is Python, rather than C. It’s certainly possible to write Pi programs in C, but you’ll be swimming against the current, as virtually every common Pi library and example project is Python based.
While these code snippets appear similar, look beyond the LED blink example and you’ll discover that developing for the Raspberry Pi is a completely different experience from the Arduino. With the Arduino, you write your program using an editor that runs on a Mac or PC. Then you push a button, and your code is sent over a cable to the connected Arduino, where it runs. Contrast this with the Raspberry Pi, where you can write your program using a graphical editor running on the Pi itself, in a full-blown Linux operating system. You can hook the Pi to an HDMI monitor, or view its virtual display using remote desktop software.
The significance of having a tiny but full-featured computer may not be clear from an LED example, but how about playing a sound when a button is pushed?
GPIO.setup(10, IN) # configure pin 10 as an input
trumpet = pygame.mixer.Sound("trumpet.wav") # load the sound file
while True:
if GPIO.input(10) == True: # is the button pressed?
trumpet.play() # you're Louis Armstrong!
Arduino Sound Trigger
// buy a wave shield?
How about logging an 8-bit digital sensor value to a web server every 60 seconds?
for i in range(8):
GPIO.setup(10+i, IN) # configure pins 10-17 as an inputs
while True:
time.sleep(60)
sensor = 0
for i in range(8): # build a byte from the 8 input bits
sensor = sensor * 2
if GPIO.input(10+i) == True:
sensor += 1
url = "http://api.mylogserver.com/logdata.php&value=" + str(sensor) # log it using HTTP GET
response = urllib.urlopen(url).read()
Arduino Sensor to Web Logger
// umm...
How about any kind of physical computing project that can benefit from easy access to sound, video, USB, a file system, or internet? Send a tweet when your toast is ready. Stream data from an SD memory card to a GPS chip. Use a mouse to control a robot. All these things could probably be done with a traditional microcontroller like an Arduino too, but would need extra hardware and complicated software. The Raspberry Pi makes this kind of work easy – almost too easy. And I didn’t even mention the huge advantage in RAM space and CPU speed that it has over traditional microcontrollers. It’s not hard to see why the Raspberry Pi has become so popular. Yes the Pi is overkill for many simple projects, but if it’s no bigger nor more expensive than the alternative, why not?
Arduino – It’s Not Dead Yet
For all the advantages of the Pi, there are still some situations where a good old Arduino or bare ATmega or PIC makes more sense. If your project has time-critical behaviors or requires specialized low-level hardware functions then you’re probably better off with an Arduino. An Arduino program is the only thing running on that hardware, so you can rely on fast, deterministic timing of I/O events. If you need to set pin 10 high exactly 20 microseconds after pin 9 goes low, you can do it on the Arduino, but the vagaries of the Pi OS’s task scheduling prevents this kind of precision. Likewise if you want something like super-fast pin change interrupts, a hardware watchdog, or advanced use of serial protocols like I2C, the Pi isn’t the best choice.
If you need a device that’s “instant on”, then you’ll be disappointed with the Raspberry Pi. An Arduino based project can be up and running within milliseconds after the power is turned on, but my Pi takes about 40 seconds to boot. Then it dumps me to a login prompt, and finally I have to run my program manually from the command line. You can edit a config file to skip the login and auto-start a specific program after boot-up, but it’s a little cumbersome and you’ll still be waiting 40 seconds.
Need analog inputs? Too bad, because the Pi doesn’t have any. You can use external solutions like this 8-way A-to-D converter with SPI interface, but reading an analog sensor is definitely more hassle on a Raspberry Pi than an Arduino.
What about battery-based projects? In sleep mode, the power consumption of an ATmega microcontroller can be tiny – in the microamps range. My Backwoods Logger ran off a single 3 volt coin cell battery for two years! In comparison, the Raspberry Pi is a huge power hog.
If these issues are important for your project, then stick with a traditional microcontroller. But if not, give the Raspberry Pi a try. I think you’ll be as surprised how easy it is to build something exciting!
3.1415926535
Now it’s your turn. What kind of microcontroller do you use for your projects? What are the Raspberry Pi’s biggest strengths and weaknesses? What does the future hold for embedded computing boards like these?
Read 5 comments and join the conversation5 Comments so far
Leave a reply. For customer support issues, please use the Customer Support link instead of writing comments.
Whenever I’m working on a project, I reach for an ATMega controller by default. I keep a small supply of ATMega644 and ATMega328 controllers in my parts bin. I do have a pair of Raspberry Pis, but I don’t use these for a lot of embedded stuff.
There are two problems with the Pi that you didn’t mention:
1) It’s a real computer (you sort of mentioned this) and with that comes all of the software updating, backing up, and management that a real computer requires.
2) The Pi is a power hog. If you’re doing anything battery powered, the Pi is probably the last thing you want to use.
My current project actually will use both types of controllers. I’ve got an idea to build a bunch of sensors (temperature, humidity, light, etc) and place them in and around my house. The sensors will probably be ATMega328s with an XBee radio, but the base (handling data gathering and user interface) will most likely be a Pi – this is the best of both worlds.
@MisterSnuggles Sorry to post something not related to the article, but check out http://jeelabs.org/2013/02/01/dive-into-jeenodes/ I would think this is pretty close to what you’re doing with a Pi as the base station and various wireless sensors. I know building something yourself is half the fun, but it’s always good if someone does the hard parts for you.
I fell in love with the Teensy 3.1 – It’s somewhere between a Pi and a *duino. You can either use the Arduino software tools, or go low level and code in GNU C/ASM. The project I did was similar to your floppy emu. I ported sd2iec to the teensy and used the extra I/O lines to expose a parallel port so I could read from an old Epson Zip-Drive on my C=128.
http://www.pjrc.com/teensy/teensy31.html
http://www.c64-wiki.com/index.php/SD2IEC
I think this is a good comparison of the Arduino with the Pi. I’ve used the Arduino personally, but I’ve also seen Pi projects at the Bristol Hackspace. One of them uses the Pi’s audio playback capability to good effect, but needs a big battery pack to keep it running (plus a charger and switch-over circuit). For anything involving networking, I’d go for the Pi any day. But I’ve also been exploring the ARM processor in the form of microcontrollers; there’s the NXP range (LPCxxxx) and the ST Microelectronics STM32 range, amongst others. I have an LPC2119 with 16k RAM and similar I/O to an AVR (only four analog channels, though). There are even two LPC ARM chips available in DIP packages (LPC810 and LPC1114). The STM32 range includes chips with hardware floating-point, and peripherals such as a dual 12-bit DAC. But the Arduino beats them all for ease of getting started!
@Mark I think it’s worth mentioning that you can also program *duino boards in GNU C. I often use them as little ATMEGA dev boards for my own projects along with AVRlibc