Creating Your Own Shield - User Libraries and Shields - Arduino Sketches: Tools and Techniques for Programming Wizardry (2015)

Arduino Sketches: Tools and Techniques for Programming Wizardry (2015)

Part IV. User Libraries and Shields

Chapter 25. Creating Your Own Shield

As you have seen throughout this book, Arduinos are powerful devices. With a large amount of input and output pins, they can perform advanced functions, but the real power comes with shields. Shields expand the functionality of an Arduino by adding electronic components or connectors. Hundreds of designs exist, adding Wi-Fi connectivity, Ethernet, LCD and TFT screens, more input and output, robotics, or simply prototyping.

Even if hundreds of shields exist, sometimes it is worth creating your own. Don't worry; this isn't magic. Some hobbyists are frightened of creating printed circuit boards, but new tools exist that make this simple. There are no expensive machines to buy and no messy chemicals to use. Even the software used to create these boards is free. If you can create a circuit on a breadboard, you can create a shield.

Creating a Shield

There are hundreds of boards available, either through Arduino, through Arduino-compatible vendors, or through hobbyists and makers. If so many shields are available, why would you want to create your own? Put simply, to have your own hardware the way you want it. A data-logging shield might miss a component that you want, or maybe that fancy input and output shield has a few components that you don't need. Also, the satisfaction of creating your own shield is indescribable. You'll see.

The Idea

It all starts with an idea. The idea is normally the project you have on your desk—a breadboard with dozens of wires linked to a few components. Large projects can have 100 or more wires connected to dozens of components. Although this works great for evaluation and development, it won't last long. Imagine that you have just finished a project for your house. You want to place a data-logging Arduino in the ceiling, hidden away from sight behind some plaster, or in a small hole in the wall. You have already thought of everything; a network cable has been run through the wall to this location, providing a network connection and power to the Arduino. All you have to do is to place the Arduino and the breadboard that contains your project: temperature sensors, humidity sensors, EEPROM data-logging, barometric sensors, and a few other components. You place the Arduino, you place the breadboard, and you connect everything. A job well done! You are about to take a break when you notice a wire on the floor. Where did it come from? It must be from a component when you installed the breadboard; but where? From which component? There are more than a dozen components, possibly 100 small wires, and even if the project works great, the breadboard is a mess. Finding out where the wire came from could be a huge task. Even worse, this device gives information about a single room; you still have to install the ones for the kitchen, bedroom, garage, and attic. Imagine snagging a wire on each. This could take hours, if not days.

Because each breadboard is identical, it is easy to create a shield. Because you created a breadboard design, it is easy to create an electronic schematic, which is explained in the section, “Your First Shield.” Having all the components on a shield instead of a breadboard makes the design much more resilient; no more wires to catch on clothes or nails. No more components ready to fall off. A nice, clear design, smaller than a breadboard, which can probably last years (if not decades). Even better, if you ever get that extension to your home finished, you already have the design for the shield, and you can add a sensor to the new room as soon as the work is done.

The Required Hardware

If you have the Arduino IDE set up on a computer, you already have all the hardware that you need. Back in the old days, you had to have transparent film, a UV lamp, ferric chloride, and a steady hand. When not created on a computer, connections and lanes were drawn by hand or by using stickers. This was printed or drawn onto a transparent film and then placed onto a photoresist copper-clad board. Exposing this to UV light removes the photoresist that was not protected by markings, revealing the copper. The board is then dipped into ferric chloride, a nasty chemical that can stain just about anything bright orange. When this was complete, the board needed to be cleaned, and the last thing to do was to drill holes where you were to place components.

This is still done frequently today, but companies have been created that do the work for you, resulting in a board that is professionally created with a much higher standard than anything you can make with home equipment. One of these companies is Fritzing.

The Required Software

Fritzing has been used throughout this book to create images of breadboards with the circuit connections. You can use this free, open-source application for a variety of things, from creating a breadboard connection diagram, to an electronic schematic, all the way to hardware design. Fritzing is available for Windows, Mac OS, and Linux and is available on the Fritzing website at http://fritzing.org/.

Fritzing comes with a large collection of components, including the standard resistor, LED, breadboard, most of the Arduino boards, and more advanced components such as the PCF8574, which is explained in the section, “Your First Shield.” Of course, there are hundreds of thousands of components available, and it isn't possible to list them all, so some companies and makers create user libraries of components. For example, Adafruit supplies a component library that Fritzing can use to help you use its components and breakout boards.

A Fritzing project contains several elements: the breadboard design, the electronics schematic, and the printed circuit board. This file can then be sent to the Fritzing website for production; you can order your shield directly through the application.

Fritzing has an easy-to-use interface. When you open the application, you are presented with the main screen. On the top, you see four buttons that correspond to four activities. By default, you will be on the Welcome screen. Next, the Breadboard view allows you to create projects with a virtual breadboard, using visual components that resemble what you have used until now. Next, the Schematic view can create an electronic schematic from the Breadboard view. Finally, the PCB view allows you to create a printed circuit board from the Breadboard and Schematic views.

To the right are two views: Parts and Inspector. Parts is where you can find electronic components like resistors and wires, but also breadboards, Arduino, and breakout boards. Anything that you want to place is present there. The Inspector panel is used to change component characteristics; you can change the value of components, for example, change the resistance of a resistor.

Your First Shield

Throughout this book, you have used libraries and shields created by other people. Now you are ready to take that a step further and create your own shield! This chapter describes the steps necessary to design and create your own shield. To use that shield, you need to create a software library, which is discussed in the next chapter.

So, what kind of shield can be made? It would be easier to ask what kind of shield cannot be made; there are so many different shield designs that it is impossible to list them all. For this chapter, you create an I/O shield, increasing the capacity of the Arduino by another 16 pins. Why would you want to create more I/O? Doesn't the Arduino have enough I/O? I've seen projects where the makers would clearly say that no, even the Arduino Mega 2560 does not have enough input and output, and more is always welcome. It would be even better if the shield could use the I2C protocol, therefore using up few pins.

There are many I2C-compatible components on the market. The component that you use for this project is the PCF8574AP. This component is an 8-bit I/O expander, capable of adding eight input and output pins to an I2C bus. The Arduino already has a built-in I2C bus, so no other components are required.

The first thing to do when using a new component is to download the datasheet. This device is created by NXP, and their website contains a link to datasheets. This specific datasheet is available at http://www.nxp.com/documents/data_sheet/PCF8574_PCF8574A.pdf. Here is an extract from that datasheet.

The devices consist of eight quasi-bidirectional ports, 100 kHz I2C-bus interface, three hardware address inputs, and interrupt output operating between 2.5 V and 6 V. The quasi-bidirectional port can be independently assigned as an input to monitor interrupt status or keypads, or as an output to activate indicator devices such as LEDs. System master can read from the input port or write to the output port through a single register.

A single paragraph tells you a lot about the component. First is the I2C speed, 100 kHz, something that the Arduino can use. The I2C has three address inputs, meaning that 3 bits of the address can be set, allowing several components to be used at the same time, or simply to configure the address on a heavily populated I2C bus.

Another important detail; inputs and outputs can function between 2.5 V and 6 V. Arduinos use two voltages: 3.3 V and 5 V. This shield will be compatible with both types of Arduino, without the need to add voltage-shifting hardware.

Next, it talks about quasi-bidirectional ports. What exactly is a quasi-bidirectional port? An input port is one that can read the voltage on a pin. An output port is one that can set a voltage on a pin. In theory, a bidirectional port is one that can do both at the same time: set output voltage and read input voltage. The problem arises when, for example, the output is set to a logical one, 5 volts, and the input is a logical zero, ground. In this configuration, that would result in a pin set too high to be in direct contact with the ground, resulting in a short circuit, cutting power to the board and potentially damaging the component and the board. Quasi-bidirectional solves this and allows the component to work in this fashion. Quasi-bidirectional pins can sink a rather large amount of current (tens of milli-amps, more than enough for an LED) but can source only a small amount of current (sometimes tens of micro-amps). In the case of a short circuit, the device simply limits the current, as if a large resistor was placed in the circuit. The advantage is, of course, ease of use. There is no need to set a pin to be specifically input or output, but the disadvantage is that this pin cannot be used to power all components; it will not deliver enough power to turn on an LED. So why does the datasheet talk about output devices such as LEDs? Well, they can still be used, but they should ground to the device, turning the LED on when the output is a logical zero or be used with a transistor that requires much less current to activate than an LED. That part will be left to the end user; your job is to create the shield that will contain the components and connectors.

Step 1: The Breadboard

Breadboards are an excellent way to test circuits and ideas. It is extremely easy to add wires, to change connections, and to duplicate part of the circuit if required. Most projects start as an experiment on a breadboard, even the most professional Arduino applications.

To create a simple circuit, you can use a breadboard to create a design almost immediately. There is, however, a slight difference between this breadboard design and the designs that you have been using. In previous designs, the output of a component was simply left as it is; to use that output, you need to place a wire in one of the breadboard connectors. When designing a shield, you should always use the type of connector that will be on the final design. There is a good reason for this; one that you will see in the section “The PCB.”

This design requires two PCF8574AP integrated circuits, one 16-pin or two 8-pin headers, and optionally, additional headers to specify the I2C addresses. Remember, the PCF8574AP needs to be configured by either pulling pins high or low to define the address. This can either be done “hard” by physically wiring the pins on the shield, or “soft” by placing jumpers on the board. For this example, they will be hard-wired. You can add headers and jumpers as an exercise. The pin layout is shown in Figure 25.1.

image

Figure 25.1 PCF8574AP pin layout

Open up Fritzing and enter the Breadboard view. By default, a new project already has a breadboard placed in the center of the view. Go to the Parts panel and search for the PCF8574AP chip by entering the text “PCF8574” next to the magnifying glass. The result will be displayed below. To place a component, you can drag the component from the Component view directly onto the breadboard. Place the two PCF8574AP chips and headers on the breadboard. Connect +5 V and GND pins of the Arduino to the power rails of the breadboard, and then wire power to the integrated circuits.

Pin 16 is VDD, supply voltage, and pin 8 is VSS, supply ground. This is a common layout for integrated circuits.

Next, connect the I2C bus wires. Remember, pin A4 is for SDA, and A5 is for SCL. Connect these two pins to the breadboard. Figure 25.2 shows my layout.

image

Figure 25.2 Power and I2C connected (Image created with Fritzing)

Next, set the addresses of the two integrated circuits. Pins 1, 2, and 3 are used for the address. For this example, device 0 (on the left) will use 000b (all low), and device 1 (on the right) will use b001; that is, A0 and A1 will be low, and A2 will be high. This can still be achieved without the breadboard view becoming too complicated by using both the top and lower power rails, as shown in Figure 25.3.

image

Figure 25.3 I2C address set (Image created with Fritzing)

The breadboard view is still visible and not too complicated. That is about to change. The last thing to do is to connect the two headers, each one requiring eight wires. The view will be extremely complicated, but don't worry; you will see that there is a better way of looking at your circuit when this is done.

Connect all eight input/output pins of the two devices: P0 to P7 to each header. My breadboard looks like the one illustrated in Figure 25.4. Note that I made the wires a little clearer on the right but not on the left. With the I2C wires in the way, it isn't easy to make something that is elegant. It might be possible by spending a lot of time, but remember that breadboard schematics are all about getting things done and not about understanding the electronics behind a design.

image

Figure 25.4 Breadboard final layout (Image created with Fritzing)

You now have a working breadboard design, but how do you turn this into a shield? You can do this directly, but just before, it is time to look at the schematic.

Step 2: The Schematic

Reading breadboard designs isn't easy. The bottom left pin on both integrated circuits are pulled low, but is this pin the ground? Is it an address? It is difficult to know without extensive knowledge of the integrated circuit itself, and there are tens of thousands of designs. To understand what a circuit does, you have to look at the schematic.

Fritzing also has a schematic view that updates automatically. To view the schematic view, click the Schematic tab. (You are currently on the Breadboard tab.) Figure 25.5 shows my schematic.

image

Figure 25.5 Default schematic view (Image created with Fritzing)

Whoa. What is this? This is an engineering nightmare, completely unreadable. There are dotted lines going from pin to pin, crossing over each other. This is actually a valid layout; each connection has been made, but it still needs to be sorted out. This takes some time but is a useful part of any project.

In the schematic view, your job is to re-create the connections in a way that is easily readable by others (and by yourself). When you mouse over a component, it is “selected” by a gray background. Right-clicking this component opens a menu allowing you to perform certain actions. The most useful are the rotate and mirror actions. By left-clicking and dragging, you can move the components around. Attempt to move the components in a way that creates the least amount of crossing lines. You won't make it perfect, so don't worry if a few do cross over; this will be sorted when creating the PCB. In my view, I have moved the components in a way that looks better, and I have also started to make some connections between the Arduino and the two integrated circuits, as shown in Figure 25.6.

image

Figure 25.6 Beginning schematic work (Image created with Fritzing)

A connection is already made between the different pins according to what was done on the breadboard, and now your job is to make a solid, visible line between the different pins. To do this, Fritzing helps you out. Place your cursor above one of the pins, and it changes to blue. Click and hold down the mouse, and you can start to create a wire. Fritzing also highlights pins that need to be connected in red, making it easier to know what pins need to be connected.

This draws a straight line between the two pins, possibly crossing over other wires or even components. Don't worry. You can add bendpoints by clicking the wire that was created. Try to keep wires horizontal and vertical to make the schematic easier to read. If you need to move a bendpoint, select the bendpoint by hovering over it with your mouse, and then drag and drop the bendpoint to the new location. To delete a bendpoint, hover over and then right click it, and select the Remove Bendpoint option from the menu. Moving a component automatically moves the first part of the wire up until the first bendpoint.

After 10 minutes, this is what I created, illustrated in Figure 25.7. It is much clearer than the first version and can be shown to other makers to share ideas or to ask for advice if needed.

image

Figure 25.7 Final schematic (Image created with Fritzing)

Although you can create a shield only from a breadboard example, creating a schematic does help. Did you notice those INT pins on the integrated circuits? The PCF8574AP can “warn” a device through an interrupt that one of the inputs has a changed state. On a breadboard, this was impossible to notice, but on a schematic, it is clearly visible. It might be a good idea to connect these to the Arduino in a next version. For now, it is time to create the shield.

Step 3: The PCB

The most rewarding part of creating a shield is designing the PCB, the Printed Circuit Board. It is also the most complicated part, but it isn't overly difficult, and Fritzing helps you a lot.

Designing a PCB is all about the physical world; in the schematic view, it doesn't matter if the connectors go on the left side or on the right side. It is more a question of preference, and if I put the connectors on the left, it was mainly because that is what Fritzing started with. For the PCB, it is different. The Arduino headers, for example, are placed in a specific position and cannot be moved. Fortunately, this is just one of the many ways in which Fritzing helps you.

When opening the PCB view, you are presented with a black screen with individual components placed on the screen. Again, there are dotted lines connecting the different pins and components. In the middle of the screen, Fritzing has placed a shield layout. By default, it will be for the Arduino Uno, but this can be changed. Fritzing can help you create shields for almost all Arduino types. To select a board, click the board on the screen, and select the board type in the Inspector on the bottom right side, and select the type. For this example, you create a shield for the Arduino Uno without the need for the ISCP headers.

This particular shield design already has the correct header placement, so you do not have to place those. However, you have to place the integrated circuits and the two headers. This is why you had to use headers for the breadboard view so that the component is visible. If you had used wires only to connect other devices, the header would not have been added.

Place the headers on the left in a line close together but not too close. Next, place the integrated circuits on the board, somewhere where the dotted lines don't cross over too often. Remember to use the rotate function to place the components in the best possible location.

Printed circuit boards have one or several copper plates on their sides or inside. Basic printed circuit boards have one copper side, known as the single layer. More advanced circuit boards have copper on both sides and are known as dual-layered. The mainboard inside your personal computer can sometimes have up to a dozen “layers” of copper and are extremely advanced. Instead of the wires on your breadboard, there will be copper “lanes” going from one component to the other. Fritzing can provide double-layer printed circuit boards, meaning that there can be connections on both sides.

Contrary to the schematic view, wires cannot cross each other on the same side. If you cannot go around a wire, you can go underneath or above. This makes routing slightly difficult, but luckily, Fritzing has yet another tool to help you, known as autoroute. Autoroute attempts to create wires between the components and normally does a good job but does need a little bit of tweaking.

My solution is available at http://www.wiley.com/go/arduinosketches. Have a look, and compare with your own.

The final step to making your shield is to send it for fabrication. This is done automatically by clicking the Fabricate button on the bottom-right side. After selecting the amount of printed circuit boards, the design is sent to the Fritzing Fab.

Fritzing checks your design, but only for major problems: short circuits, design problems, or missing connections. After a few days, you will receive a professionally made printed circuit board, ready to go! Place it on your Arduino, and prepare yourself for another adventure; after you have created a shield, you have to create the software for the shield. You do this in the next chapter.

Creating Arduino shields is an excellent way of learning electronics but can also be a source of income. Several companies sell Arduino shields but also individuals shields on dedicated electronics sites. Arduino shields work, of course, on Arduinos, but not only. Several boards exist with Arduino-compatible connectors, even if they are not Arduinos and are not programmed by the Arduino IDE. One example is Atmel's SAMA5D3 evaluation board. Atmel supplies most of the microcontrollers on Arduino boards but also creates advanced processors for professional designs. The SAMA5D3 is one example of a processor that can run a full Linux or Android system, but with Arduino shields.

Summary

In this chapter you have seen just how easy it is to create your own shield using Fritzing, an open-source application to create schematics and help you create professional quality shields. You have created your own project and developed a solution to increase the input/output of your Arduino beyond its initial design point. However, to use your shield, you will require software to control the components, something that will be presented in the next chapter. You will see how to create your own library using the Arduino IDE, and how to package it to distribute to other people and projects.