Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Learn Electronics with Raspberry Pi: Physical Computing with Circuits, Sensors, Outputs, and Projects
Learn Electronics with Raspberry Pi: Physical Computing with Circuits, Sensors, Outputs, and Projects
Learn Electronics with Raspberry Pi: Physical Computing with Circuits, Sensors, Outputs, and Projects
Ebook574 pages5 hours

Learn Electronics with Raspberry Pi: Physical Computing with Circuits, Sensors, Outputs, and Projects

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Updated for the recent Raspberry Pi boards, including the Raspberry Pi 4, this new edition offers an all new digital logic circuits project, explaining the theory behind how digital electronics work while creating a new project for measuring temperature.

Raspberry Pi is everywhere, it’s inexpensive, and it's a wonderful tool for teaching about electronics and programming. This book demonstrates how to make a variety of cool projects using the Pi with programming languages like Scratch and Python, with no experience necessary. You'll see how the Pi works, how to work with Raspbian Linux on the Pi, and how to design and create electronic circuits. 

You'll create projects like an arcade game, disco lights, and infrared transmitter, and an LCD display. You'll also learn how to control Minecraft's Steve with a joystick and how to build a Minecraft house with a Pi, and even how to control a LEGO train with a Pi. And, you'll build your own robot, including how to solder and even design a printed circuit board.

Learning electronics can be tremendous fun — your first flashing LED circuit is a reason to celebrate! But where do you go from there, and how can you move into more challenging projects without spending a lot of money on proprietary kits? Learn Electronics with Raspberry Pi shows you how to, and a lot more. 

What You'll Learn

  • Design and build electronic circuits
  • Make fun projects like an arcade game, a robot, and a Minecraft controller
  • Program the Pi with Scratch and Python
Who This Book Is For
Makers, students, and teachers who want to learn about electronics and programming with the fun and low-cost Raspberry Pi.
LanguageEnglish
PublisherApress
Release dateOct 29, 2020
ISBN9781484263488
Learn Electronics with Raspberry Pi: Physical Computing with Circuits, Sensors, Outputs, and Projects

Related to Learn Electronics with Raspberry Pi

Related ebooks

Related articles

Reviews for Learn Electronics with Raspberry Pi

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Learn Electronics with Raspberry Pi - Stewart Watkiss

    © Stewart Watkiss 2020

    S. WatkissLearn Electronics with Raspberry Pihttps://doi.org/10.1007/978-1-4842-6348-8_1

    1. Getting Started with Electronic Circuits

    Stewart Watkiss¹ 

    (1)

    Redditch, UK

    Most of this book is going to involve connecting circuits to a Raspberry Pi, but before we plug anything into the Raspberry Pi, you will need a basic understanding of electronic circuits. This is going to be a gentle introduction, so if you already know how to build your own simple circuits and would like to jump straight in to connecting into the Raspberry Pi, then feel free to jump to Chapter 2.

    An electronic circuit combines individual electronic components to perform a specific function. This could be as simple as a light circuit in a torch that turns on when the on switch is pressed or incredibly complex such as the circuitry inside a laptop computer. Electronic circuits are all built around the same principles.

    The most basic principle is the concept that an electronic circuit must make a complete physical circuit. For a circuit including a battery, there must be a complete path starting from the positive (+) side of the battery, through any components (such as a switch and buzzer), and then back to the negative (–) side of the battery. This is shown in the circuit in Figure 1-1.

    ../images/417997_2_En_1_Chapter/417997_2_En_1_Fig1_HTML.jpg

    Figure 1-1

    Switch and buzzer circuit

    This is a simple circuit connected using crocodile clip leads. The circuit has a buzzer and a switch which can turn the buzzer on and off. When the switch is closed, the contacts inside the switch come together completing the circuit, which allows current to flow around the circuit making the buzzer sound. When the switch is open, there is a gap between the connections inside the switch preventing the current flow, causing the buzzer to stop.

    Obviously, this is a very basic circuit, but it’s also the basis of almost all the circuits we make. We will replace the mechanical switch with an electronic component and use different sensors to turn the switch on and off. We will also use different outputs including LEDs and motors.

    Voltage, Current, and Resistance

    I’m going to keep the theory as simple as possible, but these are some terms that we are going to refer to throughout the book, which will be explained in this chapter. Understanding how the circuit works and the math involved is going to be important by the time you get to the stage where you are designing your own circuit. I have avoided putting too much math into the projects, but there are some examples where we need to perform some calculations.

    The voltage is the difference in energy between two terminals or points in a circuit. It is measured in volts indicated by a letter V. If you had a 9V PP3 battery (such as the one used in the buzzer circuit in Figure 1-1) then the battery has a difference of 9 volts between its positive and negative terminals. We would consider the negative terminal to be at 0 volts and the positive terminal at 9 volts. Although the battery is designed for 9V, the actual voltage may vary depending upon how much charge is in the battery and what load is connected to it.

    The current is the flow of electric charge around a circuit which is measured in amperes. This is normally abbreviated to amps and is indicated by a letter A. The current is related to the voltage as the higher the voltage of the power supply, the more current can flow through the circuit, although it also depends upon what other components are in the circuit. Using conventional electric current, we say that the current flows from the more positive to the negative terminal. In the electronic circuits we create, most currents will be small and so will normally be measured in milliamps, mA, where 1mA = 0.001A.

    The electrical resistance is a measure of how difficult it is for the current to flow around a circuit. It is measured in ohms, which is represented by the Greek omega character Ω. There is resistance in all components of a circuit, but we can normally disregard the small amount of resistance in a good conductor such as a wire. The resistors we will be using normally range from around 200 ohms to several thousand ohms (kΩ).

    Note

    This book uses conventional current where the electrical current is considered to flow from positive to negative terminals. This is based on an incorrect assumption from early electrical theory. We now know that current is a flow of electrons from the negative to the positive terminal, but for circuit design, conventional current is used.

    Ohm’s Law

    When creating advanced circuits, some of the math can get quite complicated; fortunately, we don’t need to do many calculations for most of the circuits in this book. There are still some basic calculations that we will need to do. In particular, for some of the circuits, we will need to work out a suitable resistor size to ensure that the current cannot damage any components while ensuring that there is sufficient current to allow the circuit to work.

    To do this, we use a single formula, which is almost certainly the most important formula used in electronics. It’s also one of the simplest. This relationship was discovered by German scientist Georg Ohm and is known as Ohm’s law.

    The basic formula is

    I = V / R

    As you may expect, V represents voltage and R represents resistance, but I is not so obvious. I is used to indicate current based on the phrase intensité de courant based on research by French scientist André-Marie Ampère.

    This formula says that to find the current through a circuit, divide the voltage by the resistance. This can be rearranged to find the voltage using the formula

    V = I x R

    To calculate the required resistor size, you can use

    R = V / I

    An easy way to remember this is using the Ohm’s law triangle shown in Figure 1-2.

    ../images/417997_2_En_1_Chapter/417997_2_En_1_Fig2_HTML.jpg

    Figure 1-2

    Ohm’s law triangle

    To use the triangle, hide the value you want to calculate and read remaining entries. To find the voltage, hide the letter V, leaving I and R. Multiply the current and resistance to find the voltage. To find the required resistor size, hide the letter R, which leaves V above I. Divide the voltage by the current to get the required resistor size.

    Electrical Safety

    Electricity can be dangerous. All the projects in this book are designed to work at low voltages up to 12V, and as long as an appropriate safe power supply (such as a wall wart or plug-in low-voltage power supply) is used, there is no risk of electrocution. The same does not apply to the high voltage present in the mains electricity supply.

    In fact, it’s not the voltage that’s dangerous but the amount of current that can flow through the body. Electric fences used for farm animals give a shock of several thousand volts, but while they give a nasty shock, they are considered safe for use near people as they limit to short bursts of very low current (although should still be avoided particularly by children or those with heart conditions). The mains electricity to your house is between about 100V and 250V (depending upon which country you live in) and is very dangerous as it can supply enough current to be fatal. As a rule, to avoid any risk of electrocution, I recommend only working with circuits designed for 24V or less unless you are 100% sure you know what you are doing.

    It’s not only electrocution that poses a risk though. Even at much lower voltages, too much current can create a lot of heat and potentially start a fire. This is particularly important when using low-voltage (12V) electrical lighting or car batteries, which can provide very high currents in the event of a short circuit. I recommend only using power supplies with short circuit and overcurrent protection and consider adding a fuse (this is explained later in the disco light project).

    Caution

    Do not try and connect any of these circuits to the mains electricity in your home, except using the appropriate power supply adapter.

    Analog vs. Digital

    The world we live in is varied. If we take sound as an example, we may use various words to describe the amount of sound something is making from saying that someone is very quiet or that the MP3 player is very loud or even that a pneumatic drill is being deafening. We don’t normally know or care about the actual values of the sound (measured in decibels), but we do know if we want it to be loader or quieter. A computer does not understand these terms. It only deals in actual values. In fact, at a most basic level, it only thinks of things being on and off, but it can compare against different levels to interpret this as a more accurate value.

    An analog circuit is one that can interpret any number of variations of the input. Perhaps one of the few remaining purely analog circuits you will find at home today is a simple amplifier built into a set of speakers. Here, as you turn the volume control, the volume changes smoothly, increasing the volume compared to the input signal. Compare this to a modern TV where you press the volume button on the remote control and the volume moves up a fixed amount, say, between 1 and 40.

    Most electronic circuits are now digital, and in fact, most include a microprocessor, either a full computer such as a Raspberry Pi or a more basic microcontroller such as the ATmega microcontrollers used in the Arduino. The real world continues to be analog so there is often an analog sensor or output and a conversion between analog and digital and vice versa.

    Breadboard

    Many of the circuits in this book will be built on a solderless breadboard, sometimes called a plugboard. A breadboard is a good way of creating temporary circuits to allow testing prior to committing with solder. They consist of a plastic board with a matrix of holes. The holes are then connected in small rows so that components plugged into the same section are connected together.

    Breadboards are very easy to use and don’t damage the components, so it’s easy to change the circuit and experiment with different components. If you don’t want the circuit anymore, then the components can be removed, and both the breadboard and the components can be used again for another circuit. Integrated circuits (ICs) can also be inserted and wired to other components. To connect wires to a breadboard, you should use solid core wire or special jumper wires which have a solid end that can be plugged into the breadboard. The alternative type of wire is known as multi-stranded wire, which is more flexible and so more popular with soldered circuits but doesn’t plug into the board properly.

    Breadboards are available in a variety of different sizes from small ones with 170 holes to large boards with multiple breadboards mounted onto a single panel. You can also connect multiple boards together which slot together. Unfortunately, there is no standard for how the boards slot together, so this may only work if using the same manufacturer. A selection of different breadboards is shown in Figure 1-3.

    ../images/417997_2_En_1_Chapter/417997_2_En_1_Fig3_HTML.jpg

    Figure 1-3

    A selection of different breadboards

    Each size of breadboard has a set of circumstances where it works best, from the smallest which can be included in a small box to the large one which is great for larger circuits. Some breadboards include connectors that are useful for plugging banana plugs from an external power supply.

    For most of the circuits in this book, half-size breadboard is an ideal size. It’s about the same size as the Raspberry Pi and is a good compromise between the space taken up and the amount of space for connecting circuits. An example half-size breadboard layout is shown in Figure 1-4.

    ../images/417997_2_En_1_Chapter/417997_2_En_1_Fig4_HTML.jpg

    Figure 1-4

    A half-size breadboard

    The main central area consists of columns numbered 1–30 and rows from a to l. Each of the columns is connected, with a break down the center. So, for column 1, positions a to f are connected and then positions g to l. There are then two rows at the top and bottom of the breadboard, which depending upon the manufacturer may be included or as an optional extra. These rows are normally used for the main power rails with the blue row used as ground (0V) and the red row used for the positive rail. Also note that on this example the red line covers 12 holes with there being a break in the line between the next 12. This indicates that there is also a break in the track at that point, so if using a single supply voltage, you may want to use a short wire to connect these together. This very much depends upon manufacturer, so you should check the ones that you have. It’s frustrating trying to understand why your circuit isn’t working and then finding out it’s because your breadboard has a gap in the power rail.

    You may also notice that some breadboards have a slightly different number of pins (many have only ten rows between a and j) and are numbered in a different direction. The actual positioning doesn’t matter as long as the same pins are connected.

    A useful addition is a mounting plate that allows a Raspberry Pi and a half-size breadboard next to each other. An example is shown in Figure 1-5. The mounting plate makes it easier to wire the Raspberry Pi and breadboard together as it means the wires are less likely to fall out. You could even make your own using an appropriately sized piece of plastic or thin wood.

    ../images/417997_2_En_1_Chapter/417997_2_En_1_Fig5_HTML.jpg

    Figure 1-5

    Raspberry Pi and a breadboard mounted together

    You will also need a way to connect between the Raspberry Pi and the breadboard. The Raspberry Pi has a male connector for the GPIO connector (explained in the next chapter), so a female connector is required. These are available as individual jumper wires which go from male to female. A selection is shown in Figure 1-6.

    ../images/417997_2_En_1_Chapter/417997_2_En_1_Fig6_HTML.jpg

    Figure 1-6

    Jumper wires. Male to male and male to female

    One disadvantage of using a breadboard is that wires or parts can be accidentally pulled out, so they are rarely used for a permanent circuit. Later, we will look at making more permanent circuits that can last much longer.

    A First Breadboard Circuit

    Our first circuit is a stand-alone circuit to get used to reinforce the explanation of needing a complete circuit and for a first practice in using a breadboard. The breadboard layout is shown in Figure 1-7. This diagram is created using Fritzing, which is covered in Chapter 13.

    ../images/417997_2_En_1_Chapter/417997_2_En_1_Fig7_HTML.jpg

    Figure 1-7

    Simple LED circuit

    Starting from the left, there is a 9V PP3 battery. These can be connected using a push-on battery connector with leads. These normally have stranded wire which as I said earlier don’t work so well with a breadboard. Some have the ends of the wire coated which should connect to the breadboard, or if you are not able to connect it to the breadboard directly, then wires with crocodile clips can be used instead.

    The next component is a miniature push-button switch. This should be a single-pole, single-throw type often known by its initials SPST. This means that there is a single switch inside, and that switch can change between two states (in this case, on and off). This is a push-to-make switch, which means that when the button is pressed, the switch contacts are connected.

    Only two connections are needed for an SPST switch, but typically, they have four as shown in Figure 1-8. Each pair is interconnected on the left and right of the switch, respectively.

    ../images/417997_2_En_1_Chapter/417997_2_En_1_Fig8_HTML.jpg

    Figure 1-8

    SPST push-to-make switch

    As you can see in Figure 1-8, pins 1 and 2 are connected as are pins 3 and 4. Each side is connected when the button is pressed. In this circuit, we are connecting the positive supply to pin 1 and then taking the output from pin 3.

    The next component is a resistor, in this case, a 470Ω, which is indicated by the different colored strips around the body, which are yellow, violet, brown, and then gold. The resistor is used to reduce the amount of current that can flow through the LED which would otherwise damage the LED.

    The final component is a light-emitting diode known by the initials LED. This must be connected a specific way around. The anode connects toward the positive end of the supply (connecting to the resistor) and the cathode to the ground connection and on to the negative end of the battery.

    You can tell which end is the anode (the positive terminal) as it normally has a longer lead. Failing that, there is normally a flat area on the plastic casing, which indicates the cathode (negative terminal). If all else fails, then this is a simple circuit where it would be safe to temporarily connect it either way around, and if it doesn’t work, then try the other way around. This is one of the advantages of using the breadboard.

    Once you have connected the components and the wires, pressing the button should cause the LED to light and releasing the button will turn it off again.

    Calculating the Resistor Value

    Earlier, I said that the resistor value was 470 Ω, but I did not explain how that value is worked out.

    To calculate the resistor, you first need to know the current you want through the LED. This information is usually available from the supplier or from a datasheet. In this case, we are looking at a current of around 15mA to light the LED. We also need to know the voltage dropped across the LED which is typically around 2V for a red LED.

    Once we know these, we know that there will be 7V across the resistor (9V from the battery minus 2V across the LED) and that we want to limit the current to around 15mA.

    Using Ohm’s law, the resistance = V ÷ I, which works out at 7 ÷ 0.015 = 467 Ω.

    The nearest value of resistor is 470 Ω.

    Static Sensitive Devices

    You may already know that you can create static electricity by rubbing a balloon through your hair or using a comb to pick up bits of paper. You can also create static electricity by walking on a carpeted floor. While harmless to us, that same static electricity can cause permanent damage to some electronic components. Often static sensitive components will be supplied in special bags like the one shown in Figure 1-9, but that is not always the case.

    ../images/417997_2_En_1_Chapter/417997_2_En_1_Fig9_HTML.jpg

    Figure 1-9

    Warning symbol for a static sensitive component

    It is worth getting an anti-static wrist strap and connecting to a suitable grounding plug. A wrist strap and grounding plug is shown in Figure 1-10. If you don’t have a wrist strip, then you can discharge static electricity by touching a metal object that is connected to an electrical ground connection (also known as earth); this could be a grounded radiator or the outside case of mains electrical equipment. You should only touch the outside of electrical equipment and never open mains electrical equipment in search for an earth connection.

    ../images/417997_2_En_1_Chapter/417997_2_En_1_Fig10_HTML.jpg

    Figure 1-10

    Anti-static wrist strap

    More Circuits

    In this chapter, we have looked at what makes a circuit and the importance of connecting a complete circuit which goes from the positive end of the power supply all the way back to the negative side.

    We have briefly looked at Ohm’s law which will come in useful later and then built our first stand-alone circuit using a breadboard and a few basic components.

    To experiment further, you could try increasing the size of the resistor and watch the effect it has on the brightness of the LED or try swapping the LED and resistor with a buzzer (use the same voltage buzzer as the battery) for a noisy circuit instead.

    In the next chapter, we will explore the Raspberry Pi and the GPIO ports that allow us to add electronic circuits to the Raspberry PI.

    © Stewart Watkiss 2020

    S. WatkissLearn Electronics with Raspberry Pihttps://doi.org/10.1007/978-1-4842-6348-8_2

    2. All About Raspberry Pi

    Stewart Watkiss¹ 

    (1)

    Redditch, UK

    It’s time to pull out the Raspberry Pi and get started. This chapter looks at the Raspberry Pi from both the hardware and software points of view. This chapter concentrates on the GPIO ports and the programming tools that will be useful for later chapters. It goes beyond the essentials, as a wider understanding of Linux will help with future projects involving the Raspberry Pi.

    Introduction to the Raspberry Pi

    The Raspberry Pi is a low-cost computer initially designed as a tool for learning computer programming. It also makes a great platform for hobbyists looking for a computer to use with electronic projects. Although the price is obviously appealing, arguably more important are the two rows of pins that can be used to connect electronics to the Raspberry Pi. These pins are collectively known as the GPIO pins and provide a way to interface the computer to homemade electronic circuits that wasn’t available before. As a result, there are just as many adult makers who want a computer that can be used to create hardware projects.

    There are now several different versions of the Raspberry Pi. The original Raspberry Pi (often referred to as Raspberry Pi 1) included 26 GPIO pins (17 of which can be used for inputs and outputs), but the newer increased the number of pins to 40 (28 of which can be used for inputs and outputs). The extra pins allow for more complex circuits and additional add-on boards known as HATs (Hardware Attached on Top). There is another option, which is the Raspberry Pi compute module. The compute module is designed to be incorporated directly into commercial circuits and is beyond the scope of this book.

    The photo in Figure 2-1 shows two of the main versions of the Raspberry Pi on sale at the time of writing. These are the Raspberry Pi Zero W and the Raspberry Pi 4.

    ../images/417997_2_En_2_Chapter/417997_2_En_2_Fig1_HTML.jpg

    Figure 2-1

    A Raspberry Pi 4 and a Raspberry Pi Zero W

    In Figure 2-1, the GPIO connector starts in the top left of each of the computers. As you can see, the default Pi Zero (left) does not include the GPIO header pins, which are factory soldered on the other versions. There is a version with these soldered on (Raspberry Pi Zero WH). Alternatively, you can solder on your own headers or wires directly to the board. More details on learning to solder are in Chapter 12.

    Which Raspberry Pi to Use?

    The original Raspberry Pi 1 is now very old and has less GPIO pins. It can still be used for interfacing with electronics; however, many of the projects in this book are based on a 40-pin Raspberry Pi. So, I suggest looking at either a Raspberry Pi 2 or Pi Zero as a minimum. The Raspberry Pi 3 and later and the Raspberry Pi Zero W include wireless networking which is easier than having to add an external adapter.

    If you want a powerful Raspberry Pi that can run a full desktop, then I suggest a Raspberry Pi 4 with at least 2GB of memory, which is the standard model at the time of writing.

    If you are working on a project that has limited space or that is battery powered, then you may want to consider the Pi Zero W instead. The Pi Zero W is particularly useful for low-power projects with an embedded computer, such as a simple robot. You will not get the same performance from the Pi Zero W (which is based on the same processor as the Raspberry Pi 1), but its size, low power consumption, and low cost are hard to beat.

    Raspberry Pi GPIO Ports

    As mentioned previously, the GPIO ports on the Raspberry Pi are a real game changer. They make it easier to connect simple electronic circuits to the Raspberry Pi providing a way to communicate with the physical world.

    GPIO stands for general-purpose input/output. It is a common term used to refer to ports on a processor that can be used either as an input or an output. The GPIO pins on the Raspberry Pi are connected directly to the GPIO ports on the processor. The processor runs at 3.3V, and as such, the GPIO ports are designed for 3.3V. This is less than the 5V commonly used by some electronic circuits, and a 5V connected as an input to one of the GPIO pins could damage the Raspberry Pi. The output of the GPIO is only able to provide a current of up to 16mA for each pin and maximum of 50mA across multiple pins.

    Caution

    The GPIO ports do not include any built-in protection. Connecting a port to an input that is above 3.3V or drawing too much current from an output can permanently damage the Raspberry Pi.

    The GPIO pins are on a two-row male header along one side of the Raspberry PI. Most of the GPIO pins can be used for normal input/output as well as some having alternative functions such as I²C and PWM. The recent versions of the Raspberry Pi have 40 pins. Of the 40 pins, 26 can be used for input/output ports, 2 provide a 5V power supply, 2 are connected to the 3.3V power supply, 8 are connected to ground, and 2 are for board ID recognition.

    The Raspberry Pi 1 had only 26 pins, some of which changed during a revision. A summary of the main pin allocations for the 40-pin versions of the Raspberry Pi is shown in Figure 2-2; for the pin details of the other Raspberry Pi revisions, see Appendix D.

    ../images/417997_2_En_2_Chapter/417997_2_En_2_Fig2_HTML.jpg

    Figure 2-2

    GPIO pin layouts for the 40-pin Raspberry Pi

    The GPIO connector is in one corner of the Raspberry Pi. If you position the Raspberry Pi with the GPIO connector in the top left as shown in Figure 2-1, then the pins are numbered with 1 starting at the bottom left of the connector and then the odd numbers across the bottom and even numbers across the top. The last pin is pin 40 in the top right of the connector. This is shown in Figure 2-2, which shows the GPIO number and alternative function for many of the pins.

    The GPIO ports can be referred to by different references. The

    Enjoying the preview?
    Page 1 of 1