Get started with programming the Raspberry Pi
Tuesday, May 09 2017 01:08 GMT
Posted by: MembraneTags:programmingraspberry pi
This Raspberry Pi, having been connected to power and an HDMI display, stands ready to accept commands.
In a previous post, we discussed the concept of electronic devices that act as appliances, with the word "appliance" used to describe our requirement that the device should do its job effectively while maximizing user convenience. Under that guideline, a perfect appliance is one that always does what its user expects and never causes surprises. A simple device such as a toaster has few expectations and rarely finds a way to violate them, but a general purpose machine such as our old friend, the Raspberry Pi, has many possible functions and is therefore more likely to do the unexpected. Luckily for us, however, it's possible to get a Raspberry Pi closer to appliance-like behavior through software. If we take control over the methods by which such a device is able to receive commands, we can, through careful programming, ensure that those commands are always handled in a manner that best reflects the user's intent. Today, we'll look at the first step in programming the Raspberry Pi, which is the same first step we see in any programming project: build and install our target hardware to prepare an environment for development.
From zero to Pi
To control a Raspberry Pi, we must first obtain one, along with the required accessories.
- Raspberry Pi: The main board for the device, with the latest and most capable model being the Raspberry Pi 3 Model B. I've bought these things from several different vendors, but have most often ordered from one of the official distributors: Newark / Element 14. Cost: US $35
- microSD card: The Raspberry Pi has no onboard storage, and needs a microSD card to hold its operating system and other persistent data. Our intended operating system, Raspbian, takes about 4GB of space, so any microSD card of 8GB or larger will do the job. Most of my cards have come from Amazon, which always has a good variety for sale. Cost: US $10-$30
- Micro-USB power adapter: The Raspberry Pi needs power to run, which it expects to receive over a Micro-USB port. As with many things, we can buy what we need here from Amazon. A wall charger lets us plug into a power outlet, while a USB hub with cables can power one device with others along for the ride as well. Cost: US $5-$20
- Raspberry Pi case: When running outside the lab, it's a good idea to put our Raspberry Pi board in a protective case. There's an official case from the Raspberry Pi foundation, but many fine alternatives can be purchased from places such as Pimoroni and Adafruit. The best case depends on the exact use we intend for our Raspberry Pi; for example, some cases are specifically designed to accommodate the Raspberry Pi Camera Module. Cost: US $5-$15
The operating system
Our Raspberry Pi hardware, once connected, is technically functioning but remains a useless brick without software to control it. To provide access to primary system functions, the operating system we've chosen to install is Raspbian Lite, a distribution of the well-known Linux system.
Etcher handles our disk imaging job in just a few easy steps: choose the image file, choose the target drive, and confirm.
To install the Raspberry Pi's operating system, we download the latest Raspbian Lite disk image and write it to the microSD card. The Raspberry Pi documentation has a page named Installing operating system images covering this procedure, but we can summarize it as:
- Find a PC or Mac with an SD card reader. If your computer has no SD card reader, that need not be problem. USB readers are available on (where else?) Amazon.
- Download Etcher, the recommended program for burning images, and install it on your computer.
- Use Etcher to burn the desired image (again: Raspbian Lite) to the microSD card. Note that Windows PCs trigger a confirmation warning for this operation, which we can accept to allow the write to proceed.
A new beginning
The login prompt after a fresh installation and successful boot. By default, entering "pi" followed by "raspberry" here gets us in.
Our newly created Raspberry Pi device is full of possibilities for useful applications. For example:
- By launching a browser such as Chromium, we can show a website on the Raspberry Pi's display. With a small amount of programming, we can set up a device that autonomously cycles through a set of sites, thereby creating an informational kiosk.
- Using the omxplayer command, we can play a video on the Raspberry Pi's display. If we program the Raspberry Pi to accept such commands over a network, we can establish control over a whole set of them at once, thereby creating a display array.
- If our Raspberry Pi device is equipped with the Camera Module, the raspistill command causes an image to be captured. If we program the Raspberry Pi to accept such commands over a network, we can remotely view the device's sightline.