The Raspberry Pi is an inexpensive (US$40 or less) small single-board computer, like the Arduino. However it is a considerably more powerful, and more complex, computer. The Pi is available in several models, runs the Linux operating system, which allows it to run multiple programs simultaneously, and has much more memory than an Arduino. In fact, it could be used as a somewhat cut-down desktop computer if you wanted, although it does have some limitations in that regard.
But in the context of a model railroad, it’s capable of doing very sophisticated processing. It could, for example, be used as a dedicated computer to run JMRI, or in applications where an Arduino just isn’t powerful enough. It takes more work to set one up, and it has fewer control pins, so it’s not going to replace the Arduino for many uses. But it’s a much better tool for more complex applications.
This page contains basic notes about the Pi, and is the top of a set of pages with additional detail that will fill out as I investigate how I might apply it.
There have been several generations of the Pi since it was introduced in 2012. Originally there were two models in each generation, a simplified “A” model and a more complete “B” model. After the A+ (2nd generation) only the B have been produced, although the A+ remains available. After the B and B+ the 2B was a significant upgrade, and the one I first started using. In early 2016 the model 3B was introduced, with a faster CPU and built-in WiFi. The faster CPU is somewhat limited by heat though, and actually using it full-out for extended periods requires at least the addition of heat-sinks and may require a cooling fan, depending on local conditions. However, if it overheats it just slows down, so that’s generally not a big problem. Small heat-sinks are available, both separately and with some cases.
The higher-end models have all sold for US$40 list in recent years, but you can sometimes find discounts. The A+ model is cheaper (US$25), if you can find it, but also considerably limited by its single USB connector.
The Pi can be connected to an external monitor (via HDMI or composite video), and USB keyboard and mouse, or it can be managed “headless” (no monitor or keyboard/mouse) via the network using SSH, the Secure Shell protocol. You can also set up VNC for remote graphical access. However a monitor/keyboard/mouse is required to initially configure a memory card for SSH.
Note: it also supports analog video out as composite video to PAL or NTSC standards on a 3.5 mm jack also used as an audio out jack.
The Pi 2 B has a 900 MHz quad-core 32-bit CPU, 1 GB of RAM (some of which is used for the graphics display) and is said to be six times the speed of the B+. It has built-in Ethernet and a four-port USB hub. By default the hub is limited to providing 600 mA (subject to having a large enough power supply), but this can be increased to 1.2 Amps with a change to the kernel boot configuration (/boot/config.txt). The 3 B ups this to a 1,200 MHz 64-bit CPU, but also recommends use of a 2.5A power supply (due to the CPU and built-in WiFi).
The A+ model has a 700 MHz single-core CPU, 256 MB of RAM (shared with display), and only a single USB connector (which can be used for a Wi-Fi network adapter if no keyboard is used).
In November of 2015 the Pi Zero was released. This is a tiny Pi with capabilities similar to the A+, but without the network, camera or display connectors (it does have a miniHDMI connector and a miniUSB connector). It supports a GPIO connector, but you’d have to solder one on; it doesn’t come with it. It sells for just US$5, so it could be very interesting for some uses, although I haven’t come up with one yet. Nine months after release they’re more readily available, but I still haven’t thought of a use for one that I couldn’t do better with an Arduino.
Unlike desktop or laptop computers the Pi does not have a battery-operated time-of-day clock that keeps track of the time when it is powered off. If connected to a network it can synchronize its clock when powered up, although this may take some time. And the current software will reset the clock immediately after a boot to the time of the last shutdown (older versions reset to 1970). Otherwise, if you care you’ll need to enter the date and time manually using the “date” command, or have a network connection. Under some conditions it can take more than an hour for the clock to sync up with a network connection, but it will happen eventually, and usually it only takes a few minutes.
The Pi uses a flash memory card for storage (more on this below). Except on the original A/B models this is a Micro SD card. The minimum size is 4GB, but 8GB or more is recommended to allow space for additional files. This contains the operating system, any installed programs, plus any user files. Several different operating systems can be used, although the majority of add-on programs (some of which are required to use accessories such as touch screens) are for the Raspbian version of Linux (derived from Debian Linux). Raspbian has several releases: “Wheezy” has been the stable release for several years, and supports most accessories and third-party programs. Jessie is the new stable release, available since September 2015 for the Pi, although Debian Jessie came out earlier.
The Pi uses HDMI or composite video over a special cable to connect to an external monitor, or it can be set up to operate “headless”, configured and managed though a network connection.
Three expansion connectors are provided: the multi-pin GPIO (General Purpose I/O) connector and ribbon-cable connectors for the CSI (Camera Serial Interface) for a camera and a second one for a display (such as the touch-panel). Add-on cards called HATs (Hardware Attached on Top), similar to Arduino shields, can be attached via the GPIO connector. The CSI can be used to attach a simple camera capable of still and video recording. There are two official cameras available today, the normal one and an infra-red one for special applications (such as night vision with an IR light). The GPIO pins include support for serial, SPI and I2C, which can be used to connect other devices (such as Arduinos), although some care is required there due to voltage level differences.
The “B” versions of the Pi support Ethernet. Both A and B can also use Wi-Fi if a compatible USB adapter is connected, but some additional configuration will be required. Connection to the Internet via either Ethernet or Wi-Fi is required to update software and to download and install new applications. An Internet connection is also important to keep the Pi’s time-of-day clock synchronized, as the Pi has no built-in clock and will reset its time if power is lost and there is no network connection (it does reset to a fairly recent time, so it won’t be far off unless it’s powered down for a long time).
The GPIO header for the A+, B+ and 2B has 40 pins. The original A & B used a 26-pin GPIO, and some hardware may be designed for that.
The GPIO signal level is 3.3V and the Pi WILL BE DAMAGED by connection to a 5V source, so use caution when hooking things up.
Power output on a GPIO pin is very limited. The 3.3V pin itself can apparently provide 50 mA to power external devices, but that’s more than an individual pin can support (the 5V pin is not limited, and can provide up to the available power, hundreds of mA).
Per this source (originally from here), the limit per pin is apparently 16 mA, well below that of an Arduino, but even worse the aggregate of all pins is apparently limited to the 50 mA of the 3.3V pin (and any used directly from that pin is also counted) so the typical load per pin needs to be even lower than 16 mA, around 3 mA per pin if all are in use. This affects the kinds of output circuits that can be used with the GPIO pins. For example, when driving LEDs directly use larger resistors to reduce current, and in most cases you’ll need to drive external amplification circuitry (e.g., a transistor) rather than driving a device directly.
Note: the above analysis is based in part on the voltage regulator being the limiting factor. Future Pi’s could have different specifications. Also, these “limits” can be somewhat exceeded without immediate damage, but at the cost of shortening the operational life of the Pi’s processor (which is not replaceable) or other components. One poster on the above-linked discussion stated that the maximum limit of a pin was around 30 mA, but as noted this is probably operating outside the “safe” zone.
GPIO pins are also much more sensitive to damage than an Arduino. The best role for a Pi is not direct wiring of electrical circuits, but direct connection of specialized sensors and similar devices, which can include Arduinos. That’s not the only way to use them, but it plays to their strengths.
The Pi is NOT a replacement for an Arduino, and using Arduino circuit designs with a Pi without modification is almost certain to result in damage to the Pi.
Power and Interfacing
The Pi must be powered by a 5V DC adapter, rated for at least 800 mA (NOT recommended for a 2B), and a 1.2A to 2.5A supply is recommended, depending on what additional accessories will be powered. The 2B can provide a limited amount of power to USB peripherals as well as to attached HAT cards from this supply.
The Pi itself uses only a few hundred mA, the rest is needed for USB and other attached devices. The Raspberry Pi folks don’t define a specific amperage for the 2B, but recommend at least a 1.8 Amp power supply when the USB is configured to provide 600 mA (its default), so this implies 1.2 mA may be used by the PI and any devices connected the GPIO or CSI (camera) connectors. A 2.4 A power supply would thus be required to support the full 1.2A USB capability. However they also say that a 1.2 mA supply will work, presumably if there are no/minimal peripheral needs.
Note: the Pi power page linked above shows the 3B only using ~400 mA, but they still recommend a larger power supply than the 2B needed, so I expect this number is a “typical” rather than a maximum requirement (i.e., it may be power use without the radio and with the clock speed reduced to its minimum).
When used with the new touchpanel screen, the recommendation is to use at least a 2.0 Amp supply (presumably more for the 3B, but I haven’t seen a number for that). More could be needed if you were also using USB beyond a simple keyboard/mouse or WiFi device.
With a model 2B and no connected devices other than a display (HDMI), a USB keyboard/mouse, and wired Ethernet I saw power use peak at 430 mA during boot, falling to a steady-state of 290 mA once the CPU was idle. With the USB keyboard/mouse removed power dropped to 250 mA (all numbers @ 5.1V). Power use is higher when writing to the flash card. I couldn’t saturate this, but with a C program writing unbuffered lines as quickly as possible (the write LED was still not blinking very often) current peaked at 379 mA, but the meter may have been averaging, and true peaks could have been higher. Power use did not change if the GUI was active or not.
Left overnight, with the monitor in screen-saver and the USB disconnected, power use dropped to an average of 240 mA @ 5.15V, or effectively 250 mA @ 5V, so a headless Pi with no peripherals uses a relatively modest amount of power. That’s not very meaningful for model railroad applications, since we’re unlikely to need to run one off a battery, but it is useful to know. Note that very little I/O was being done, and my meter didn’t record peak power, only average. Since power at boot is higher, there is some additional peak power needed when heavily reading from / writing to a card.
This suggests that a 1.0 A supply might be adequate for a headless Pi 2B with no connected peripherals other than a wired Ethernet, and even the minimal 800 mA supply mentioned could work, however see the comments below about the risk of underpowering a Pi.
With a touch-panel connected (and USB keyboard/mouse) instead of the HDMI display, current peaked at 890 mA during boot and GUI start-up, and settled down to 680 - 690 mA @ 5.07V with the CPU largely idle and the GUI in use. This implies that the touch-panel requires about 400 mA (possibly more if it was rapidly updating or the touch sensor was in use). In general though, most power use on an LCD comes from the backlight, rather than the screen itself (and touchpanels need very little), so this is probably a good number for it.
I used a high-quality 5.1V power supply in these tests. Many 5V power supplies produce less than 5.0 volts under load, and current will rise if voltage drops (to maintain constant wattage). So planning for a safety margin is important if you don’t know the exact characteristics of your power supply.
For the above testing I used a USB In-Line Voltage and Current Meter (Adafruit’s USB Voltage Meter with OLED Display) with a 5.1V, 2.0A power supply. I’m not certain of its accuracy, but it reports wattage to three decimal places and voltage to two, and I used those numbers to get current to the nearest mA rather than using the current meter, which is only reported in tens of mA (+/- 10 mA may be the limit of its accuracy, but that’s not certain).
Note: the Pi will draw more power if overclocked. It’s unclear if the recommended numbers include the “normal” overclocking range of 1,000 MHz for the 2B or not. More power may be needed if you overclock. I haven’t tested that because I have no reason to overclock. The 2B has plenty of performance for my needs as-is.
If devices attempt to draw more power than is available, voltage will drop. This can result in the memory card becoming corrupted (writes to SD cards are very sensitive to voltage) requiring the card to be reformatted and restored from a backup (you did make a backup, right?). A 2.0 Amp power supply is not expensive (US$8 is typical) and will provide a safety margin in normal (1.8 A) use: invest in one. Even the new 2.5A supplies can be had relatively inexpensively (Adafruit has a 2.4A supply at the same price as the old 2.0 Amp, US$8).
The Pi operates at 3.3V, unlike the Arduino which normally operates at 5V (there are some 3.3V models of Arduino). This means that you cannot directly connect the pins from a Pi to those of a normal (5V) Arduino. A “level shifter” circuit is required to convert between the two voltages.
The current versions of the Pi use a MicroSD card. These generally come with an adapter shaped like a normal SD card, so they can be put in SD card readers. The memory card contains the operating system, any applications, and all associated data files. If you move the card to a new Pi, the new Pi will be identical to the old one (assuming it’s the same hardware model); nothing is stored in the hardware of the Pi itself. If you duplicate a card to a new card, you will have two Pi’s with the same configuration. This is useful for quickly setting up a new Pi, as you only need to change the hostname of the copy (assuming it uses dynamic addresses) and you will have everything else set up exactly as you like it.
You can buy memory cards pre-configured with NOOBS (the system that allows you to load different operating systems) or with a specific OS (like Raspbian), or you can download the image for either kind of card and burn it to a blank card that has been formatted as FAT32 (on a Mac, that’s “MS-DOS FAT” with “Master Boot Record” in Disk Utility).
You can create and backup these cards (with some limitations) using a Windows, Macintosh or Linux computer. Search online for instructions.
Note: the built-in SD card readers on Macs are known to have some problems; best to buy a USB-connected SD card reader for this.
The memory card should be 8 GB to 32 GB in size (there are some ways to use larger ones, but you need to do more work to set them up), and SDHC (rather than SDXC) is typical, although elinux.org reports that some SDXC cards are known to work. However, throughput is capped well below the limits of even SDHC, so the more expensive cards provide no performance benefit. Although Class 10 cards are common now, their speed advantages don’t really apply to typical Pi use (memory cards are optimized for sequential I/O of large files, which is what’s needed for cameras, but computers use random access and small files most of the time).
There is some performance benefit to faster cards, and a real penalty to cheaper ones. One set of tests showed random-access read speeds on a Pi to vary based on the card. Most cards, including “fast” ones, were all about the same speed, but a few were about 50% faster. Note that “class 10” cards normally weren’t significantly faster than lesser ones. Really cheap no-name “class 4” cards could be close to half the speed of typical ones. (I can’t find the link for this discussion thread now). For most things, random-access read is going to be most important, as it affects loading software and reading configuration data, the two most-common storage activities for most uses.
Memory card sizes aren’t identical, and a backup restored to a slightly smaller card won’t work (you’ll get a kernel panic on boot). I’ve seen comments that making the card partition end just 200 bytes smaller than the card will avoid this, but my experience with 16GB cards is that even that can fail. Use 14GB or so (you’ll need to hunt up instructions on using gparted or a similar utility to resize partitions, as the Pi can only resize to maximum size). Once a partition has been resized, don’t use “expand partition” in the raspi-config utility (or the equivalent in the GUI) to maximize partition size.
The current OS image (Jessie) fits easily on even an 8 GB card with a fair amount of room. However 16 GB cards will provide room to grow (or to use NOOBS with multiple OSs), and prices are dropping. However, don’t cheap-out on memory cards. There are a lot of low-quality ones, and even counterfeits, available online, and if they work at all, their reliability is questionable.
I use 16 GB Class 10 Sandisk SDHC cards sold for use with Android phones (“and similar devices”). Prices for these vary a lot, often changing from week to week with “sales”. I’ve seen them sell for US$10, more common are “sale” prices of US$15. Just don’t buy them when they’re selling for US$30. I’ve used several, loaded with the standard Raspbian Jessie image from Raspberry Pi with no problems. Per the report mentioned above these are probably “typical” speed (it wasn’t clear which class 10 Sandisk card the report was mentioning, but it showed up with a “good but close to typical” speed).
I also keep a “base” configuration image created from a memory card (as a backup) with all the normal settings and applications I prefer, and when I set up a new Pi I create a memory card by loading that onto it, and then customizing it for whatever I need that’s special on that Pi. Then I make a backup of the customized image so I’ll have it if that card fails.
Memory cards contain two (or more) partitions, each containing files. You can’t back one up just by copying individual files. You can back up the whole card to an image (.img) file, and then load that onto another card to restore it. This needs to use a fairly low-level “raw” copy method, but for at least some computers there are utilities available to make this easier (I use dd on a Mac, which is not one of the “easier” utilities, so I don’t know much about the others). There’s more about this on my Pi Setup page.
Note: as most of the online documentation will tell you, using dd on a Mac or linux can easily erase your computer’s own disk if you typo the command, so if you use it, use it carefully.
Copying a whole memory card is not a fast process. Larger cards can easily take more than a half hour. It’s a good thing to do at the end of a day, so you can walk away from the computer while it does the copy.
A good memory card won’t soon fail, but failure is inevitable over the long term. I haven’t had a Pi card fail yet, but I have had high-quality SD cards fail in cameras after a couple of years. As mentioned above, you may have problems loading onto the “same size” card if you haven’t resized your partition to leave some slack. One safety net is to reload onto a larger card (e.g., a 16 GB image onto a 32GB card) and then resize that to less than the original card size and save the new, smaller, image for restoration onto a “same size as original” card.
If you have a linux computer, you can resize the used portion of the card (before you back the card up) to avoid the card size variation problem. Do this once, just after you first create the new card. There are many ways to do this, but the best is probably a utility called “gparted”.
If you don’t have a linux computer, you can download a bootable image with gparted installed and burn that to a CD (or probably flash if your computer supports booting from it). I use this on an older Mac with a CD drive to resize my images.
You will need the following to get started:
- Raspberry Pi model B (2B or 3B), US$40
- MicroSD memory card with installed software (or a blank card and download it), US$10-15
- 1A or greater 5V via MicroUSB (or direct to GPIO pins), 2.5A is recommended, US$8
- HDMI or composite monitor and associated cable, varies
- USB Keyboard and Mouse (a hub is also needed for an A model), varies
- A notebook for keeping a record of what you’ve changed and why is a REALLY GOOD IDEA.
If you already have a monitor, keyboard and mouse you can use for initial setup, you can do this for well under US$75. Once you have the Pi working as you like, it may not require the monitor, keyboard and mouse (depending on what you use it for).
The software can be downloaded as an image and installed on a blank memory card as mentioned above.
Once you have one, dive right in and play with it. If you mess up, at worst you’ll need to get a new memory card (or reload a new image on the one you have) and start over. Keep notes, and eventually you’ll have a feel for both how to work with it, and what options are important to you, and a record that lets you set up a fresh card the way you like it.
One security note: Change the password of the “pi” username on the Pi from “raspberry” to something else with at least 8 letters/numbers/symbols you can remember, and don’t turn on SSH until/unless you are going to use it (and then search for recommendations on using it securely). An online Pi with the standard password and SSH is wide-open for anyone on the internet to access (well, sort of, more about this on my Pi Setup page), and there are automated systems (“robots”) searching for ssh-connected computers that they can take over to do nefarious things. Even if you change the password, those may be able to guess it, but you’ll at least have made it less likely.
For more about initial configuration of a Pi, see my Pi Setup page.
A Raspberry Pi is a circuit board, with exposed chips and traces sensitive to static electricity, and many tiny components that can break off if bumped. It is more delicate than the average thing around a model railroad. A case to protect it is a good idea, but you probably want one that leaves the GPIO pins and the two ribbon-cable connectors accessible. My favorite used to be the Pimoroni Pibow Coupé, which fully encloses the Pi while leaving everything accessible. However, when the model 3 came out this was discontinued and replaced with the Pibow 3 Coupé, which has changes to make it compatible with that model, including cutouts for the model 3 heat sinks. While still a good model, and compatible with the older model 2 units, the low profile leaves little room for air to circulate internally, and given the higher heat output of the 3, that’s a bit of a concern. (note: Adafruit is presently, mid-2016, selling the new case under the old name, still US$12).
So my new preferred case is a similar one with more space, the C4 Labs’ Zebra Black Ice. This is harder to find, and note that like the Pibow an earlier model was not Pi 3 compatible, so be careful when buying. It’s also more expensive, going for US$15. They make a number of similar cases in other colors, but not all are 3-compatible as yet. The benefit of this case is that more space inside, coupled with the cutouts for the GPIO pins, camera and touchpanel cables, provide both internal air circulation and venting. It also comes with three heat-sinks, which help offset the cost if you planned to use those.
Assembly of these cases (Pibow or Zebra) requires a bit of manual dexterity and a small screwdriver (and once I needed a file as the hole for one screw cap on the Zebra was a bit too small), but generally isn’t too hard. The smooth acrylic should work well for attaching velcro when mounting the Pi to the layout. And the Zebra even has screw cutouts, but be very careful using those as a screw could short circuits on the board.
There are lots of other cases out there, so if your needs differ from mine, you can probably find something else you like.
The following accessories may be useful.
Adafruit Pi T-Cobbler Plus (US$8)
For development, this is much easier to work with than the EzConnect HAT. It lets you connect the Pi’s GPIO pins to a break-out board, where you can easily access all of them for trying things out. And they’re all nicely labeled for you (figuring out which pin is which is often rather hard when working directly with the pins). Note: this is the version for the Pi 2 and + models, which are all the current hardware.
Alchemy Power Pi-EzConnect HAT, US$20
This board provides screw-terminal connections for the GPIO pins, suitable for a permanent installation on a layout. It also includes a small prototyping area, where pull-up/pull-down resistors or other simple circuits could be soldered in place. As it mounts to the GPIO, this is only suitable for newer Pi’s with a 40-pin GPIO connector.
Pi Foundation Touch Display, US$80
A variety of touch screens are available, but the Pi Foundation touch screen is one of the best. It provides a 7-inch color 800x480 touch-sensitive screen, using a HAT board and a short cable to connect to the Pi. There are screw-mount holes on the back allowing the Pi to be mounted to it. It can be powered from the Pi, or from a separate power supply. The mounting is compatible with the A+, B+ and 2B, but it is electrically compatible with the older A and B boards also. The only negative, and it’s relatively minor, is that the angle of view is less than that of a typical monitor, so you need to use it where you can look nearly straight-on (it’s +/- 70°, so it’s not too bad, but typical monitors can be twice that). I’ve played with this for setting up JMRI control panels, see my Pi Touchscreen page.
The Pi Camera Module V2, US$30
This is a somewhat limited fixed-focus camera similar to those found in smartphones. The V2 model replaces the original 5 megapixel camera sensor with an 8 megapixel one, but is otherwise the same (apparently the old sensor became unavailable so they had to pick another). The default focus is “1 meter to infinity”, but there are descriptions online (see here, and elsewhere) of how to adjust the focus for closer use or to do the same with a macro lens, which would enable it to be used on a layout. Those probably still apply to the new sensor, although some adjustments may be needed. I plan to experiment with one for “subway station” use eventually.