Orange Unit: A Person-Centered Launch
4D: Coding Electronics
Exercise: Connecting a Breadboard to a Raspberry Pi
In the previous two chapters of this Orange Unit session, 4B: Meet the Microcomputer and 4C: Getting Started with the Raspberry Pi, we began familiarizing ourselves with the application of computer building blocks specifically to the Raspberry Pi microcomputer. After meeting the hardware, we went on to set up a Raspberry Pi-based Linux operating system, an organic (that is, self-luminous) light-emitting diode (OLED), and a bootup script to display some essential current data on the functioning of the Raspberry Pi. The result looked something like this:
But in doing so, we’ve also reserved access to six of the General Purpose Input Output (GPIO) pins, including the only two 5-volt power pins.
Above, you can see two different displays of the pin layout formats commonly used with the Raspberry Pi GPIO[1].
Look through these and note that some, like #10, #9, and #11 just below the middle 3.3V pin, can be used for multiple purposes, such as digital inputs and outputs, or as a serial peripheral interface (SPI) connector with Master Out/Slave In, Master In/Slave Out, and Master Clock pins.
The use of the terms “master” and “slave” is rightly discomforting for many. As noted in the tween zine Beyond Dark Matter, “The master/slave relationship has been used for centuries in technology, often to explain situations where one master process or component controls a slave process or component” (p. 31, Beyond Dark Matter).
Efforts are made periodically to move away from this embedded framing of the master/slave relationship within core aspects of all our electronics. This blockquote seeks to unpack further the hidden sociopolitical and economic realities that remain encoded within our sociotechnical technologies and systems.
While there are many other pins for us to use than the ones covered by the PiOLED attached directly to the GPIO, this layout has proved less than optimal for later exercises, in which the Raspberry Pi is used with the breadboard electronics. This is particularly the case when a shared Raspberry Pi is connected and disconnected with different breadboards as each person tests out their own prototype[2].
For the next exercise, we’ll remove the PiOLED from the Raspberry Pi GPIO, and instead connect a 40-pin ribbon cable to the 2×20 GPIO of the Raspberry Pi. This can then be attached to a Cobbler or T-Cobbler on a full-sized breadboard. As we’ve used columns 1-30 for past exercises in the Orange Unit, we’ll now attach the T-Cobbler to columns 44-63 of the breadboard.
If your IP address changes moving forward, you may need to disconnect the ribbon cable from the GPIO pins of the Raspberry Pi and reconnect the PiOLED temporarily. Alternatively, you can reconnect the Raspberry Pi to a keyboard, mouse, and monitor.
Connecting the Raspberry Pi and Breadboard
While the Fritzing diagrams in the first half of the Orange Unit were illustrated using a half-sized breadboard, these could be (and hopefully were) done using a full-sized breadboard, which would have been demonstrated with a Fritzing image as below. The unused space at the right of the breadboard is just what is needed for this part of the exercise.
- Disconnect the USB-to-TTL cable and associated jumper wires from the breadboard.
- Insert an Adafruit T-Cobbler Plus into columns 44-63 of the breadboard. Leave at least two rows accessible above and below the T-Cobbler.
The T-Cobbler was chosen to ensure that the ribbon cable does not cover the breadboard holes and the GPIO pin labels associated with the top and bottom halves of each column. However, this results in an extended layout of full-sized breadboard, ribbon cable, and breadboard. For some, the Cobbler Plus may be the preferred alternative, keeping the full-sized length and instead adding the ribbon cable and Raspberry Pi to the height of the layout.
- Use male-to-male jumper wires to provide 3.3 volts to the upper red rail, ground to the upper blue rail, 5 volts to the lower red rail, and ground to the lower blue rail.
- The 3.3-volt GPIO pin immediately above the 5-volt GPIO pin below was chosen instead of the 3.3-volt GPIO pin in the middle of the T-Cobbler to keep the jumper wires to the right when working on later wiring. That said, both 3.3-volt pins and any of the ground pins can be used for this step.
- Rapid prototyping begins with quick testing of circuits, with jumper wire placement of secondary concern. But wires can sometimes get in the way, and so in your layout of the base system for future exercises, spending a bit of time now to strategically create a platform for further work may be of value.
- From here, you can insert a 40-pin ribbon cable between the breadboard and the Raspberry Pi.
- A common practice for ribbon cables is to use a white non-conductive shell over the conductive metal wire that connects to pin one and black non-conductive shells for the remainder of the wires. For the Raspberry Pi GPIO, pin 1 is the 3.3V pin, which is now the upper half of column 63. A bump-out on the end-frame of the ribbon cable is also often used, and an associated gap is provided on the T-Cobbler to further help assure pin 1 is appropriately aligned.
- Removing the ribbon cable does take some force. Holding it by the plastic frame or as close to it as possible while steadily pulling it from the T-Cobbler or up from the Raspberry Pi will help maintain the connections of the 40 wires with their metal connectors inside this frame.
- The GPIO pins tend to bend a bit if the ribbon cable frame is leaned slightly while being removed from the Raspberry Pi. When possible, consider leaving the ribbon cable attached to the Raspberry Pi and instead removing it from the T-Cobbler as needed.
Try Something New!
The slide switch can continue to be used as a make-or-break switch to turn on and off 5 volts of current to the shared anode leg of the RGB LED. This would then provide a mechanical override of any selections made by code moving forward. However, given that the Raspberry Pi provides both 5-volt and downregulated 3.3-volt power options, the slide switch can also be used to mechanically select between voltages, helping to control the brightness of the lights by running a jumper wire from the 3.3-volt rail to the left throw leg of the switch, as shown below.
Schematically, the RGB LED, resistor, and switches within the breadboard circuitry remain the same. However, now they receive 5V and 3.3V power and ground through the Raspberry Pi by way of the T-Cobbler.
From here, consider using resistors to further control the lower level of brightness for that left side of the switch to match your preference for a dim setting.
Try Something New!
NOTE: Testing over two semesters and extended conversations with others who have used the PiOLED across the country have indicated that there is significant risk of improperly wiring a breadboard to the PiOLED, thereby resulting in short-circuiting of the PiOLED. This burned-out PiOLED circuit cannot be repaired. Therefore, this has been removed as a normal exercise and instead has been moved to a Try Something New activity. Use caution in proceeding in this selected optional task.
- To connect the PiOLED to the Raspberry Pi GPIO pins, you now need to run 6 male-to-male jumper wires from the breadboard to the PiOLED.
- There is an Adafruit flower icon on the upper left side of the PiOLED. This points to the hole associated with 5-volt power.
- The other two holes in the top row are a second 5-volt hole for power and a hole for ground, in that order.
- The hole just below the 5-volt power hole is the 3.3-volt power hole.
- The other two holes in the bottom row are the holes for SDA (serial data) and SCL (serial clock) communications, respectively.
- Pin 1 of the PiOLED is the lower left hole in the drawing below. It is connected to a 3.3-volt source on the breadboard.
- Here it is connected to Raspberry Pi GPIO pin 1, which is the upper rows of column 63 of the breadboard. This design choice was made to help align the remainder of the pins of the PiOLED with the appropriate connections on the breadboard.
- Using an orange-colored jumper wire for the 3.3-volt connection, red jumper wires for the 5-volt connections, and a black jumper wire for the ground connection may help identify the connection points in the future.
- Once you’ve reached a successful point of “yes,” consider using some electrical tape at the plastic frames of the jumper wires to keep these wires in their assigned layout at the point where they connect to the breadboard, and again at the PiOLED if you will only occasionally be using the PiOLED attachment.
Key Takeaways
The breadboard is a common way to do rapid prototyping. Many different electronic circuits may be tested in rapid succession. It’s also possible to keep multiple breadboards with different versions of an electronic circuit on hand to take with you in testing by a diverse range of communities. While it’s sometimes possible to also bring multiple Raspberry Pi’s, this layout is meant to provide a means by which different breadboards can be attached and detached from a given Raspberry Pi in support of testing of multiple breadboard prototypes.
Digital Switches
So far, we’ve been working with mechanical switches. We’ve used the slide switch to move the flow of current to the anode leg of the RGB LED both between on and off states and between 5-volt and 3.3-volt power sources. We’ve also used the momentary “on” push button switch to temporarily complete the flow of current to ground, thereby turning on the associated red, green, or blue cathode legs of the RGB LED. Like light switches in our house, these mechanical switches can serve a valuable function in our daily lives.
But sometimes the use of a mechanical switch may prove inadequate for a task. Perhaps you still make use of the mechanical switch for regular use of a light, but need a safety switch to override a mechanical on/off switch to quickly turn on the light when a fire alarm sounds. Or maybe access to the mechanical switch is difficult and you’d like to move to a digital control through a phone app.
Digital Input/Output Pins vs. Power and Ground Pins
Before moving on, it’s important that we look a bit closer at the labels on the GPIO pins:
In particular, compare those whose labels start with “GPIO #” to those labeled as “Ground.” We’ve specifically used labeled “Ground” for the blue grounding rails on the breadboard. Those labeled “GPIO #,” on the other hand, or not designed as a ground source, but are instead general-purpose input/output pins. Digital (IO) are used on microcontroller and microcomputer GPIO pins for binary on or off logic. In code, this is sometimes written as binary High and Low or as digital True or False. The current used for digital IO is relatively small and can include negative as well as positive voltage. This can be important when pushing a speaker cone out with a DC signal that varies between 0 and some positive voltage before pulling the speaker cone back with a DC signal that varies between 0 and some negative voltage. The general-purpose digital IO pins provide a perfect match to the momentary and transistor switches.
As the GPIO digital IO pins do move from positive voltage to relative zero voltage, we could just send our digital outputs straight to the 470 Ω resistors connected to each of the three cathode legs. But as this is not true ground but a relative zero voltage, it is possible there could be a slight glow of color on an LED, even though the digitally switched circuit is an open circuit. The relative difference between power and ground is insufficient to assure the LED is off in keeping with the decision tree within the code.
Digital switches come in multiple classes and types. To address the misuse of the digital IO ports as a ground source instead of its intended digital High or Low role, we will use as digital switches transistors—solid-state electronic devices used as part of much of our daily life electronics, which are designed specifically for tasks like this. We’ll place these between each of the three cathode legs of the RGB LED and their assigned GPIO ports.
More on Transistors
Starting in the 1960s, a new term was heard in relation to radios, calculators, and televisions. I remember the commercial that inspired our family to get its first “transistor radio.” I also remember going to the five-and-dime or drug store to purchase a vacuum tube until we finally got that transistor television. Transistors replace those tubes by providing a semiconductor device having at least three legs: a source current terminal; a drain current terminal; and a gate terminal, which controls the output current.
At its most basic level, the transistor serves as a switch. While our momentary switch circuits above required a physical act, the pushing of a button or the sliding of a switch, a transistor switch uses an electrical input to determine the flow of current. Further, transistors are designed such that the switching voltage is less than or equal to the voltage of the current flowing through the transistor. The 3.3- or 5-volt output from the Raspberry Pi GPIO pins can be used to turn on and off not only a 5-volt LED, but also a 40-volt DC motor! In this way the transistor can also serve as an amplifier. “In fact the initial tests of the first transistor involved hooking a speaking to its output and hearing that it was louder than the input. Every modern piece of audio circuitry has transistors at its heart amplifying signals.” (Dave Astels, “Transistors 101”: https://learn.adafruit.com/transistors-101/basic-operation)
Types of Transistors
are built using two different types of semiconductor material. Unlike a jumper wire that is always conductive, a is a substance that can conduct electricity under some conditions, but not under others. An n-type semiconductor is mostly made of negative electrons. A p-type semiconductor is mostly made of positive charge carriers.
When a transistor uses both n-type and p-type semiconductor material, and only those two types of semiconductor material, it is called a bipolar transistor. The bipolar transistors are three-terminal devices with the standard Emitter, Base, Collector (EBC) pinout. The emitter and collector on either end of the transistor are of the same type of semiconductor, while the center base is of the other type of semiconductor. In this way, the base provides a junction between the collector and emitter. When a biasing voltage is applied to the base terminal, the junction breaks down, and current can flow from the collector to the emitter.
As we’ve been doing with the momentary switches of our parallel red, green, and blue RGB LED circuits, we can use three bipolar transistors as switches to determine the state of each circuit. To do this, we need to decide between the two different configurations of a basic bipolar transistor.
- The NPN bipolar transistor has a collector and emitter made of N-type negative electrons and a center base made of P-type positive electrons. For our LED circuits to have current flow from the cathode leg of an LED to the collector and emitter of the transistor and thereby to ground, voltage is sent to the base leg of this type of transistor to turn on its gate.
- The PNP bipolar transistor has a collector and emitter made of P-type positive electrons and a center base made of N-type negative electrons. For our LED circuits to have current flow from the cathode leg of an LED to the collector and emitter of the transistor and thereby to ground, ground is sent to the base leg of this type of transistor to turn on its gate.
Below is a Fritzing drawing and associated schematic in which a momentary switch is used to provide ground to the PNP transistor base and 5 volts to the NPN transistor base. Let’s now use this information as our starting baseline for designing and prototyping our programmable RGB LED switched circuitry.
Exercise: Control LEDs with Python Code
With the GPIO pins on a Raspberry Pi readily accessible via a Cobbler or T-Cobbler on the breadboard and with a rudimentary understanding of PNP and NPN bipolar transistors, we’re ready to rewire the electronics to use Python code to input data from the momentary switches and to output data to transistor switches connected to the three legs of the RGB LED.
For this exercise, we’ll use GPIO #5, #6, and #12 for the momentary switches, and GPIO #26, #19, and #13 for the base of the transistor switches to be integrated into programming code. The grouping of these pins was selected to roughly match electronics with GPIO pin groupings. But other digital IO pins with a “GPIO #” hash label at their start could have also been used. What works for one may not be ideal for others, which is where alpha and beta testing, along with open channels for feedback after implementation, come in. Feedback from past students doing these exercises has proved essential in improving functionality for diverse audiences.
Part 1: Steps to Rewire the Circuits
Here’s the configuration of our current circuits (without sequin LEDs) as a reminder of where we’re starting:
And here’s the Fritzing image we’re working towards:
Instead of using the pressing of momentary switches to directly complete the flow of current from power to ground by way of an associated leg of the RGB LED, a Python program will now continuously collect digital inputs from each momentary switch to determine if they are pressed or released. The code will also continuously send output to the base of each transistor to inform them of the state of the associated momentary switch, thereby electronically switching the red, green, and blue LEDs between on and off.
- Repurpose the three male-to-male jumper wires connecting the three momentary switches to the associated red, green, and blue cathode legs of the RGB LED. Connect the right legs of each momentary switch with its associated GPIO digital input pin.
- Move the jumper wire connected to column 19 to instead connect to the upper rows of column 49, which is associated with GPIO #5. Leave the wire connected to column 3, bringing together the “red” push button with GPIO #5.
- Move the jumper wire connected to column 21 to instead connect to the upper rows of column 48 which is associated with GPIO #6. Leave the wire connected to column 9, bringing together the “green” push button with GPIO #6.
- Move the jumper wire connected to column 23 to instead connect to the lower rows of column 48, which is associated with GPIO #12. Leave the wire connected to column 15, bringing together the “blue” push button with GPIO #12.
- Be sure to leave the male/male jumper wires connecting the left legs of the momentary switches with the blue ground rail on the breadboard.
- Add three PN2907A PNP bipolar transistors to the breadboard, connecting the collector legs to the associated red, green, and blue cathode legs of the RGB LED and the emitter legs to ground.
- These should be placed in the upper rows of the breadboard, with the flat face of each transistor facing the lower rows. In so doing, the emitter leg will be on the left side of the transistors and the collector leg on the right side of the transistors.
- Place one transistor in the upper rows of columns 25, 26, and 27. Connect column 25 to the blue ground rail. Connect column 27 to the upper rows of column 19.
- Place a second transistor in the upper rows of columns 29, 30, and 31. Connect column 29 to the blue ground rail. Connect column 31 to the upper rows of column 21.
- Place a third transistor in the upper rows of columns 33, 34, and 35. Connect column 33 to the blue ground rail. Connect column 35 to the upper rows of column 23.
- Finally, connect the base of each transistor with its associated GPIO digital output pin.
- Connect the transistor base found in column 26 with the upper rows of column 45, which is GPIO #26.
- Connect the transistor base found in column 30 with the upper rows of column 46, which is GPIO #19.
- Connect the transistor base found in column 34 with the upper rows of column 47, which is GPIO #13.
In practice, when doing any of these breadboard exercises, your implementation may look a bit different than the Fritzing image to adjust for the actual components at hand. The key is to achieve the designated associations that we’ll next make use of within a Python program.
Part 2: Steps to Run the Code
For Part 2, we’ll now be working in the Raspberry Pi OS. While this can all be done from a terminal window (the standard Linux command-line Interface, or CLI), for most this is best done through a graphical user interface (GUI) using a monitor, keyboard, and mouse. These can either be directly connected to a Raspberry Pi, or as demonstrated in Orange Unit 4C, by using a RealVNC Viewer on your personal computer.
- In the web browser on the Raspberry Pi, enter:
https://go.illinois.edu/switchLED - Download the file “switchLED.py” to the Raspberry Pi. You do NOT need to log in or sign up to successfully open and download the Python code.
- Click on the “File Manager” icon in the Raspberry Pi Desktop Menu. Move switchLED.py from the Downloads folder to the /home/pi/Code/Python folder.
- Double click on the “switchLED.py” file. By default, files that end with .py will be opened with Thonny, a popular Python integrated development environment (IDE) that comes pre-installed on the Raspberry OS Desktop.
- Click on the Run icon in Thonny. This is a common method within integrated development environments to test out new code. As such, you will see in the “Shell” window at the bottom the same display that would be shown if this code were being executed in a terminal window in practice moving forward.
- Click on the Stop icon in Thonny, and then close the Thonny window.
- Click on the “Terminal” icon in the Raspberry Pi Desktop Menu. In terminal, type:
cd /home/pi/Code/Python
python switchLED.py
You’ll now fully see the text provided by the switchLED.py code, with instructions for using the programmable switch. It also provides instructions to hold down the <CTRL> key and press the “c” key to exit this program.
Key Takeaways
Take a few minutes to go back to Thonny and skim through the code, especially noting:
- The three groupings of components associated with the red, green, and blue parallel circuits.
- The outputs are designated by the associated LED that will be lit. The inputs are designated by the associated momentary switch that is mechanically pressed and released.
- Your Raspberry Pi GPIO # should match the listed board.D number, being assigned within the digitalio.DigitalInOut library object definitions. For instance, the right leg of the “red” momentary switch should be connected to GPIO #5 to match board.D5 being assigned to switchR.
- At the bottom of the code, you’ll find the line “while True:” which starts a Python forever loop. The next three lines will continuously be run to update ledR.value to match the current switchR.value.
- When a momentary switch is pressed, the digital input has a flow to ground. This sets the current to ground, which is identified as a binary zero. And as setup within the digitalio library, this is identified as a digital False.
- When the ledR.value is next updated with the switchR.value, it will now be set to digital False/binary zero as well.
- When the digital False/binary zero output reaches the base of the red transistor, the red rail’s 5-volt current, which flowed through the shared anode leg to the red cathode leg and through the 470 Ω resistor to the collector leg of the transistor, can now flow to the emitter leg of the transistor and then to the blue ground rail. The red circuit is closed, and the light comes on!
In previous sessions of the Orange Unit, circuits were constructed using electronic components without code.
Looking at the new programmable code schematically, we can see three parallel momentary switches and three parallel LEDs as before. But each momentary switch is no longer directly, physically in series with its associated LED. Circuits are instead dynamically constructed and completed through code. This schematic shows us only the electronics of our new system. Code is not shown in schematics, as it can be readily changed to do different acts with the same physical structures. We are now truly opening the literal and conceptual hood of technology in a way that hopefully begins to demystify not just this example, but also more of your electronic components used as part of your daily life.
As mentioned at the start of this activity, we chose to use the Raspberry Pi GPIO #5, #6, and #12 for the momentary switches, and GPIO #26, #19, and #13 for the LEDs to be integrated into programming code. In this case, the Raspberry Pi’s GPIO and associated electronics and embedded code together create one, more complex circuit. Code, in this case using the Python programming language, is used to determine the activities based on the state of the momentary switches and the design decisions controlling the state of the LEDs.
In what ways do you find the following true? False? As we move from circuits made exclusively of physical electronic parts to ones that also include programming code, greater opportunities for innovation-in-use become available.
Wrap Up
We’ve gone from basic LED circuits in parallel using different levels of resistance and different voltages, to LEDs in series controlled by momentary switches, to integrated circuits, to code-based circuits. We have also taken our first dips into the Raspberry Pi microcomputer pond to see how these electronic parts, along with a range of other electronics, come together to create large, complex systems like computers.
A wide range of individual electronic components can be brought together using power and ground sources and conductors to build individual circuits, like the red, green, and blue LED circuits we created using the RGB LED with common anode. Using a printed circuit board, electronic components can be put together to create an integrated circuit, like the Sequin LED integrated circuit. Integrated circuits and additional electronic components can be further put together with code to create limited purpose microcontrollers like the Circuit Playground Express we’ll use in the Blue Unit, and general-purpose microcomputers like the Raspberry Pi.
We’ve begun to discover that software can open opportunities for further innovation-in-use, as is the case with different character maps for one keyboard key matrix. And in the character map example, we’ve also seen how software can at the same time restrict innovation-in-use, as is the case when an operating system or other software overrides a selected character map with its own standards for keyboard character associations. The configurations of our sociotechnical devices are multi-tiered hardware, software, and social constructs that are far from neutral. These constructs can come into conflict as data and information regarding past design choices are lost or hidden from viewing by current innovators. They shape current work, even as the current work seeks to further shape these sociotechnical devices.
As mentioned in a video segment at the beginning of this session, my history with computers goes back to the mid-1970s in junior high, and my work with general electronics even longer, as I grew up in my family’s sawmill. In this next video, I share my memories of electronics and programming during the late 1980s and early 1990s, as I worked on my research as a PhD student at Rutgers. You may find some similarities to the work we’ve just done!
Comprehension Check
- The website https://pinout.xyz/ provides a useful guide on the Raspberry Pi GPIO pins. They also provide a simple program you can run from the terminal window on your Raspberry Pi where you would type: pinout ↵
- During beta-testing 2021-2022, the PiOLED was left connected and a half-sized breadboard was used. Male-to-female jumper wires were used to connect the appropriate pins on the Raspberry Pi GPIO with the associated holes on the breadboard. This remains an option for those with ongoing access of the same Raspberry Pi and for whom consistent access of data from the PiOLED and/or the smaller half-sized breadboard has proved of importance. ↵
The Inter-Integrated-Circuit serial communications bus.
Unlike UART and SPI which provide send and receive communications to one or two attached devices, respectively, I2C is a bus and can work with device addresses to do serial send and receive communications with multiple devices.
Specific to electronics, a digital signal is a representation of a physical quantity expressed as a series of the digits 0 and 1, that is, binary. The range of decimal numbers representing physical quantities in our work, for instance 12 seconds, is converted to a binary equivalent--in this case, 1100. In this illustration, the dots along the analog sine wave are data points collected to create the digital representation of the physical quantities.
Input devices let users control the computer (e.g., keyboard, mouse, touch-sensitive devices). Output devices inform the user of what is happening (e.g., monitor, printer).
Small electronic switches allowing control of electrical flow within a circuit without using programming code.
A substance that can conduct electricity under some conditions, but not under others. Many electrical components are built using such semi-conductive materials, including diodes, sensors, and integrated circuits.