electronics: Embedded devices
Mike Bedford delves into the world of embedded computing, and discovers that a small amount of hardware can certainly go a long way.
Mike Bedford delves into the world of embedded computing, and discovers that a small amount of hardware can certainly go a long way.
Single board computers (SBCS), typically creditcard sized or smaller, are now a familiar part of the computing scene, thanks in no small part to the Raspberry Pi family of products. The Pi has spawned lookalikes from several companies, which have also attracted their supporters, but perhaps less well-known are those SBCS intended exclusively for embedded applications. Our aim here is to introduce this category of SBCS, and to illustrate how you could put them to work using a starter kit that you can pick up for just a few pounds.
To start, though, just in case this is a new concept to you let’s describe what we mean by an embedded application. This is one that works in the background in such a way that the user might not even be aware that they’re using a computer, in the broadest sense of the word. So, for example, household appliances such as microwave ovens or washing machines are generally controlled by embedded software.
If you’ve delved into the Raspberry Pi, you’ll no doubte be aware that it has several GPIO (General Purpose Input Output) pins. These can be used to interface to external components such as pushbuttons, LEDS, motors, sensors and such like, so they could be used to control a robot or a whole range of other realworld devices. In this sense the Pi could be considered as a platform for embedded computing, but it’s quite different from those SBCS that are intended exclusively for embedded use.
The Pi is a general-purpose SBC; in fact many people use it to learn to code, and completely ignore its interfacing capabilities. This dual nature has one important consequence: Raspberry Pi computers run an operating system, commonly some flavour of Linux. By way of contrast, dedicated embedded SBCS generally don’t run an operating system.
The lack of an operating system might seem to be a serious drawback, but for embedded applications it’s an asset. If your use of an SBC is just to learn about embedded computing, the Pi approach works well, but if you want to put your SBC to work in a real application, an operating system is an overhead you can do without. Perhaps the major drawback with an operating system is the time it takes to boot. We can imagine that a car wouldn’t be too popular if you had to wait 30 seconds
for Ubuntu to start up before you could drive off. It also has implications in the amount of hardware you need. Many embedded applications will work perfectly well with a much less powerful processor than one you’d need to run an operating system. It’s not just the processor – commonly an embedded application won’t need any graphics hardware, and often it won’t need a host of interfaces such as Wi-fi or Ethernet. Not only does this have cost benefits, it also reduces the power requirement which is a major advantage for batterypowered equipment.
Introducing Arduino
Rarely will you find an off-the-shelf SBC inside a drone, a TV remote control unit or a food processor. Instead, in order to minimise the cost of these sorts of highvolume products and to reduce size, the computing hardware will usually be incorporated into a customdesigned circuit board, alongside all the other electronic circuitry. For low-volume DIY applications, though, and especially for learning about embedded computing without being hampered by an operating system, a dedicated embedded SBC makes a lot of sense. This is where Arduino boards come to the fore.
Raspberry Pis and similar SBCS tend to be built around an Arm processor of some type. By way of contrast, many of the Arduino boards are based on
the Atmega328 from Microchip. There’s a lot more than just a name that separates them, though, as a few figures will reveal. The processors on Pi-type boards have a 32-bit of 64-bit architecture, they generally have two, four or eight cores, and they’re usually clocked somewhere between 1GHZ and 1.8GHZ. The Atmega328 has an 8-bit architecture, a single core, and the clock speed is often just 16MHZ.
The difference in memory and storage is just as stark. Instead of capacities measured in hundreds of megabytes or gigabytes, Arduino boards will commonly have 32KB of flash storage for the program, and user storage of a few kilobytes of RAM and a similar amount of non-volatile EEPROM. For embedded applications with no need to support an operating system, you might be surprised at how much you can do with such an apparently modest specification.
Finally, before moving onto something a bit more practical, we need to spell out what we mean when we talk about an Arduino board. Arduino was developed at the Ivrea Interaction Design Institute in Italy for educational use. Official Arduino boards – of which there are several – are available at www.arduino.cc. The design is open source and several companies have produced compatible designs. These should not be called Arduino boards, because the name has been registered, although third parties can refer to them as Arduino-compatible: several use names ending in ‘duino’ or similar to make the point.
Generally, third-party designs are cheaper than those sold by Arduino. There are also boards on the market that appear identical to Arduino products, but use the Arduino name and logo illegally – these should be avoided. Oh, and just a quick word on what we mean by the phrase ‘Arduino-compatible’: the bottom line is that different manufacturers might use it to mean different things, so be sure to read up carefully if compatibility is important. At the software level, it means that it will work with the Arduino IDE – that’s the Integrated Development Environment that you’ll use on a separate PC to develop code before uploading it to an Arduino. Most compatibles offer this level of support.
At a hardware level, the most common understanding of compatibility is that the I/O pins are compatible so that the board can be used with Arduino shields: add-on boards which are roughly equivalent to HATS in the world of Raspberry Pis. Most so-called compatibles also offer this level of compatibility, but there have been reports of a few that don’t.
The Arduino IDE
Because Arduino-type boards don’t run an operating system, nor are they easily able to interface to a keyboard, mouse and display, software is developed on a separate PC and is then downloaded to the Arduino. The easiest way to do that is to use the Arduino IDE which is available for all major operating systems, but note that it runs under Java, so you’ll have to install that if it’s not already present on your PC. As with many IDES, the one for Arduino provides all the tools necessary for code development. In particular, it includes a text editor in which you’ll create and modify the source code, a compiler and linker, and a means of downloading the compiled code to the Arduino board. Because all third-party Arduino-compatible boards
note that there’s a forum where you can enlist community support. We should also point out that the first tutorial in the starter kit describes the installation of the IDE so, if you have problems, take a look at the PDF document that appears on the kit’s CD in the folder for your language.
Elegoo UNO
We’re now going to be showing you how to get some hands-on experience of embedded computing using the Elegoo UNO Basic Starter Kit. Elegoo sells directly from its website (www.elegoo.com) in US dollars, but it also sells via Amazon in several countries at much more competitive prices. At the time of writing, the Basic Starter Kit costs just £13.99 in the UK and $14.99 in the USA, from Amazon.
As the name suggests, the kit includes an SBC that’s compatible with the Arduino UNO R3. We won’t list the specification of that board; suffice to say that it’s adequate for all the projects documented in the kit, and a whole lot more. The kit also includes a USB cable to connect the UNO to a PC, and a mini-cd containing a tutorial document and the code for all the projects, although they’re also available online. The rest of the kit comprises several electronic components with the means of connecting them together and to the SBC.
First is the breadboard on which you’ll mount the various electronic components and make electrical connections between them. There are male-male patch leads for making connections between components on the breadboard, and for connecting the components on the breadboard to the input/output pins on the UNO, and there are also a few male-female patch leads. Turning to the components themselves, there are LEDS of various colours, pushbuttons, a buzzer, a photo detector and a tilt switch. There are also several components that are needed for interfacing, mostly resistors, including a 74HC595 IC – a serial-to-parallel converter that’s used in some of the projects.
To see the UNO in action, just attach it to your PC using the USB cable supplied. Be careful not to put the board down on an electrically conducting surface, because this could cause shorts to the solder joints on the bottom of the UNO. As soon as it’s connected to a PC, you’ll notice that the green power LED illuminates and an orange LED starts flashing. The UNO is supplied with a simple program already flashed into memory, and it’s this code which is responsible for flashing the LED, and serves to prove that the board is working properly. Now it’s time to start up the IDE, which we assume you’ve already installed; this won’t stop the LED from flashing. You’re now ready to take your first steps in embedded programming.
Programming in an UNO
First we’re going to see how to upload some code to the UNO and move on to modify that code. This mirrors the second lesson on the Elegoo UNO Basic Starter Kit CD, the first one being the installation of the IDE. This is an excellent place to start since it doesn’t require you to wire any external components to the UNO’S I/O pins.
Before doing anything, it’s necessary to tell the IDE which Arduino board you’re using, although it’ll probably default to the UNO. To check, and to change it if necessary, go to Tools > Board. We’re not going to create any new code to start, nor use one supplied in the starter kit, but instead we’ll use some sample code supplied with the IDE. Open this at File > Examples > 01.Basic > Blink and it’ll appear in the IDE’S editing window. In Arduino-speak, the code is called a sketch.
The language is usually described as very similar to C/ C++ with libraries for working with Arduino boards. Whatever you want to call it, though, so long as you have had some experience of coding, you’ll soon get to grips with it. In fact, even if you’re new to programming, with a bit of guidance in the form of online tutorials you should get the hang of it without too much trouble.
The purpose of this code is to flash the onboard orange LED, so it’s pretty much the same as the code that was already flashed into the UNO. Even so, to check that everything’s working, click the round icon with the right-pointing arrow in the toolbar. Watch in the blue status bar at the bottom and you should see ‘Compiling sketch…’ followed by ‘Uploading…’ and then ‘Done uploading’. You might also have noticed that another couple of orange onboard LEDS flash briefly, indicating the transfer of data between the PC and the Arduino. Once you know the IDE is working, we’ll change the behaviour of that flashing LED.
Towards the bottom of the sketch you’ll find a line of code that turns the LED on, followed by one that waits for 1,000 milliseconds (one second), then one that turns it off again, and finally one that waits another second. These four instructions are inside a loop, so they execute forever. An easy change is to alter the lengths of the two waits from 1,000 to 500 milliseconds, so edit the sketch accordingly and upload the new code. After the other two LEDS have flashed, you’ll notice that the orange onboard LED will start to flash more quickly. If you want to save your altered code, do it under a new name because the examples are read-only files.
Grip the hardware
We’re not going to go through all the tutorials in the starter kit documentation because that would be reinventing the wheel. The descriptions provided are excellent, giving both instructions and explanations, and you’d be well advised to go through all of them in sequence. Our aim here is just to whet your appetite in the hope and expectation that you’ll get hold of this starter kit and, in so doing, get to grips with embedded computing. To do this, we’re going to highlight just one tutorial, Lesson 5, which is a slightly expanded version of a classic in embedded computing. This enables an LED to be turned on with a pushbutton and turned off with another, so it illustrates the use of a digital input and a digital output. You can think of it as the equivalent of the ‘Hello World’ program, which is the first program many people write when learning to code.
In this example, we’re going to use some hardware that’s external to the UNO, which is the very essence of embedded computing. We’re going to be mounting and connecting together a few electronic components on the piece of breadboard that forms part of the starter kit. Breadboard would never be used for a real-world piece of equipment because printed circuit boards are so much more compact and reliable. For learning, or for trying out ideas, though, they’re ideal. Breadboards have an array of holes into which you can push the leads of components, and behind those holes are electrical connections so components can be wired together without soldering. This means that you don’t have to buy a soldering iron, and components can be reused.
To understand the internal connections inside the breadboard, hold it with the red and blue lines running horizontally at the top and bottom. Behind the scenes, all the holes in the top row connect together, as do those in the second row, the bottom row and the row one up from the bottom – these are intended for the power supplies. The holes in the remainder of the breadboard are connected in vertical columns, but with a horizontal gap where you can see the gutter.
Now, add the various components to the breadboard as shown in the starter kit manual. You’ll notice that, when you push a lead into a hole it will be ‘grabbed’, thereby holding it physically and connecting it to other holes internally. It doesn’t matter if you use exactly the same holes as shown in the documentation so long as the components are positioned correctly with respect to each other. Next, load the code for Lesson 5 into the IDE from the starter kit’s CD.
If everything’s gone to plan, you’ll find that the lefthand pushbutton turns the LED on and the right-hand one turns it off. If it doesn’t work initially, try wiggling the component leads on the breadboard because it’s all too easy make a poor connection when you add the add components to the board. Finally, before we leave you to work through the other lessons, how about trying your hand at coding by modifying the sketch?