Membrane SoftwareTechnology development studio
Get started with programming the Raspberry Pi
Tuesday, May 09 2017 01:08 GMT
Posted by: MembraneTags:programmingraspberry pi
Raspberry 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
All in all, we can gather the components for our Raspberry Pi appliance by spending about US $60, although we also have to provide things like monitors, keyboards, and mice if needed. For help connecting the Raspberry Pi, we can refer to a brief instructional video from the Raspberry Pi Foundation.
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 window
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.
When the microSD card is ready, simply plug it into the Raspberry Pi and power it on with a monitor and keyboard attached. After a brief post-install and reboot sequence, we arrive at the login prompt. Here, we can use the default username pi and the default password raspberry to access the command prompt on our freshly installed general-purpose computing system.
A new beginning
Raspberry Pi
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.
Each of these projects could easily span multiple posts on its own, and some require a specific Raspberry Pi setup as well as custom programming. We'll delve into details for some of them in future articles. For now, though, we're out of time and must stop at having simply prepared to program. This is something I'd indeed consider a victory unto itself, but if you're interested in charging ahead I can recommend a few pages for continued reading: Linux commands for a basic list of commands from the Raspberry Pi Documentation, 42 of the Most Useful Raspberry Pi Commands for a set of more advanced commands, and Learning the Shell for a introduction to the Linux shell, which offers one possible path to programming and otherwise manipulating these commands.
 
What did you think of this article? Leave a comment or send us a note. Your feedback is appreciated!