Blue Unit: Computational Tinkering
4B: Raspberry Pi Counterstory Little Free Library
Background Knowledge Probe
- Which of your daily-use mechanical tools, if any, have attached digital electronic components? Think about this broadly, including your car or kitchen appliances, for example.
- Which of these digital electronics are microcomputers? Microcontrollers? A combination of the two?
- In what ways is the design and use of the digital component prioritized over the mechanical? The mechanical over the digital?
- In what ways are your daily-use tools, mechanical and digital, prioritized over the person? In what ways are your daily-use tools first and foremost person-centered?
Technical Overview
Data Communications Between Devices
The Raspberry Pi, like many microcomputers and microcontrollers, provides pins to which you can attach transmit and receive wires to connect with personal computers. In this way, even if there is no network or keyboard, mouse, and monitor to provide access to one of these devices, a simple USB to serial cable can be used to configure, diagnose, or perform regular tasks on it. Many operating systems, such as the Raspberry Pi OS, also provide a control window to output and input text from and to the device through this serial interface.
A note on the technical terms Master and Slave:
- Historically, the technologists who developed these data communication tools used the term Master to indicate the controlling device and Slave to indicate devices being controlled by that device. I follow the lead of Python developers who, moving forward, are using the terms Parent and Worker, respectively. These changes within Python coding were made in November 2018, after heated discussion launched by a change request submitted in September 2018 by Victor Stinner, a developer with the software company Red Hat.[1]
- Other programming languages have implemented similar changes. Drupal uses the term replica to replace slave and primary to replace master. As another example, Django has moved to follower and leader.
- The terms still largely used within engineering and computer science remain slave and master. In discussions regarding these change requests, many argued that these common terms are used to refer to the operation of technology independent of, or at the direction of, another technology with no reference to human slavery. They argue that because there is no common replacement that could easily be made given the existing labels on electronics and in code back many decades, it is important to stay with the stock terminology.
- As you read through this document and look at abbreviations listed on your electronics and in code, note that sometimes the listed abbreviations — for instance MISO, below — do not align with written descriptions in this text — Parent In/Worker Out.
Communications between devices takes many shapes and forms using a range of protocols. Three of the most commonly used are:
- I2C (Inter-Integrated Circuit and pronounced I-squared-C) was developed in the early 1980s and is widely used to connect lower-speed peripheral to microprocessors and microcomputers using the bus topology (think of a physical bus you ride to travel with others between locations). It is designed to work over relatively short distances, and allows multiple “worker” integrated circuits to communicate with one or more “parent” integrated circuits. I2C only requires two wires and can support up to 1008 worker devices. It can also support multiple parent devices, allowing for more than one parent to communicate with all devices on the bus.
- SPI (Serial Peripheral Interface) is another widely used bus topology protocol used to send data between peripherals and microcontrollers/microcomputers. What is unique about SPI is that it uses a serial clock in addition to a MOSI (parent out, worker in) data line and a MISO (parent in, worker out) data line. The serial clock’s oscillating signal allows for data communications to occur synchronously. As with I2C, multiple workers can connect to a single parent. To accomplish this, a fourth line, SS or Worker Select, alerts a particular worker that it should wake up and send and/or receive data, as well as to detect that multiple workers are present.
- UART (Universal Asynchronous Receive Transmit) interfaces: UART is actually a microprocessor with a package of integrated circuits on a , along with program logic, and is used to attach serial devices to a computer. This allows the computer and attached devices to “talk” and exchange data. There is a range of USB (Universal Serial Bus) TTL (Transistor Transistor Logic) serial cables used to provide connections between USB and serial UART devices. For our purposes, UART is the simplest and a very effective strategy for basic work in the console window of the Raspberry Pi using a personal computer.
Exercise: The Circuit Playground Express Meets the Raspberry Pi
In the first three technical session chapters, we’ve explored MakeCode and configured our Circuit Playground Express microcontroller, eventually working to create our own functions. In this session, we’ll bring together the microcontroller and the Raspberry Pi microcomputer to allow us to use the “trumpet valves” to play selected counterstories saved on the Raspberry Pi. To do this, we’ll use the UART serial communications pins found on the Circuit Playground Express and the UART Python extension on the Raspberry Pi to transmit valve press sensor data from the Circuit Playground Express to the Raspberry Pi, and to then assess this data to choose which of the available MP3 counterstories to play using the audio output of the Raspberry Pi.
We’ll accomplish this in three steps:
STEP 1: Set up the Raspberry Pi to store and play counterstories using the OMXPlayer. This may have already been done previously as it is an exercise within the Orange Unit.
STEP 2: Set up your Circuit Playground Express. The Toolbox Trumpet will now be expanded to use components of the MakeCode Serial UART Communications Extension to transfer capacitive touch and momentary switch data to the Raspberry Pi to play counterstories based on sensor inputs.
STEP 3: Use the Toolbox Trumpet to play short counterstories (no momentary switches pressed), introductions to the counterstories (left switch pressed) and extended counterstories (right switch pressed). A simple Python-based program has been included to test out the Circuit Playground Express, the USB to TTL serial cable, and the Raspberry Pi as a media center.
Step 1: Transfer MP3 Files to the Raspberry Pi
The first step is to set up a folder called cstories in the home directory of your Raspberry Pi, containing your counterstory MP3 files. You may have completed this step as part of the Orange Unit. If not, jump to Coding Electronics. In the steps below, we’ll continue working with this folder.
Step 2: Set Up Your Circuit Playground Express
On your laptop, make the following update to the Circuit Playground Express so that it can work with the Raspberry Pi to play counterstories:
- Download the Toolbox Trumpet Counterstory UART Prototype for the Circuit Playground Express:
circuitplayground-ToolboxTrumpet4Octave-WithUART.uf2
- Review the new conditional that has been added to the MakeCode, building from the previous Toolbox Trumpet 4 Octave exercise. In this case, we now do a serial write of data whenever the capacitive touch sensor count is greater than zero. This is possible because within the Advanced section, we’ve taken a serial redirect Configuration block and added that to the On Start function.
- Connect the Circuit Playground Express to your laptop via the microUSB to USB cable and flash the new UF2 to your Circuit Playground Express.
- Note you now need to move the off/on switch on the Circuit Playground Express to on to hear the audio playback of the trumpet. Turn this on and double check that you can still play the notes.
- Disconnect the microUSB to USB cable.
Step 3: Use the Toolbox Trumpet to Play Your MP3 Files
Now let’s connect up your Raspberry Pi to your Circuit Playground Express using a USB to TTL cable and some male to alligator clips. The diagram below assumes power is being provided to the Circuit Playground Express using the microUSB to USB cable.
Alternative power option: You can provide power to the Circuit Playground Express directly from the Raspberry Pi via the USB port by connecting the red TTL wire to the VOUT on the Circuit Playground Express using a fourth male to alligator clip wire. In this way, one power source directly attached to the Raspberry Pi is all that is needed to run the whole digital Little Free Library![2]
Use three or four male to alligator clip wires to connect the TTL side of a TTL/USB Serial Cable to the Circuit Playground Express microcontroller:
- Black wire to ground (GND)
- Red wire to power (VOUT) (ONLY IF ALTERNATIVE POWER FROM Pi IS USED)
- White wire to transmit (TX)
- Green wire to receive (RX)
Plug the USB side of the second serial cable to one of the four USB ports on the Raspberry Pi. Any one of these should work OK. You should see the Circuit Playground Express green power light go on. If not, a few tests can include:
- Confirm there is power on the Raspberry Pi and that it is not in the boot-up phase.
- Check to see if the cable USB is fully pressed into the USB port of the Raspberry Pi.
- Try using a different USB port on the Raspberry Pi.
- Confirm the GND and VOUT pins are properly connected to the red and black wires on the TTL/USB serial cable.
From a web browser, enter: https://uofi.box.com/s/a93h58laobjwb69b8fipyznrjvgcc5mp
Take a glance at the code, but most importantly right click in the window to download the program as cstories.py
(NOT as cstories.txt as automatically listed). Be sure to save it to your cstories folder on the Raspberry Pi. You can do this several ways:
- Directly from the Raspberry Pi’s Graphical User Interface, using a web browser on the Raspberry Pi.
- By downloading it to your laptop’s cstories folder, and then transferring the file using the USB to TTL serial console connection.
- By downloading it to your laptop’s cstories folder, and then transferring the file using SSH/SCP (secure shell and secure copy) in a terminal (Mac) or PowerShell (Windows 10) window. For this approach, first identify the IP address of your Raspberry Pi. Jump to Network Troubleshooting for instructions. Visit Introducing the Unix Command Line for details on using SSH/SCP.
If you have more than one cstory with intro, regular, extended, and end notes, open the text editor nano to edit the Python code with the following command. Change the variable “numStories” from 1 to the number of cstories now on the Raspberry Pi.
pi@raspberrypi:~/cstories $ nano cstories.py
- Use the up, down, left, and right arrows on your keyboard to navigate to the row that starts “numStories.” Navigate to the number 1 and change it to the number of stories you have transferred to /home/pi/cstories.
- To save, hold down the Control or CTRL key, seen as ^ at the bottom of the window, and hit the letter O key. To keep the same name,
cstories.py
, just hit enter when it shows that name. - To exit nano, hit Control X (e.g., ^X).
Test it all out. In the terminal window in which you have logged in on the Raspberry Pi, type the following, then touch the different Toolbox Trumpet valves to see if you can hear a story be read to you:
pi@raspberrypi:~/cstories $ python3 ~/cstories/cstories.py
To exit the Python command output, provide a keyboard interrupt by holding down the Control (that is, CTRL or ^) key, and then hitting the ‘c’ key.
FAIL FORWARD TIP: Sometimes the connection of the USB/TTL Serial Cable from the Circuit Playground Express to the Raspberry Pi may have changed. In the terminal window on the Raspberry Pi, type:
pi@raspberrypi:~ $ ls /dev/ttyUSB*
Note the result. If nothing is listed, then the USB/TTL Serial Cable may not be connected. Otherwise, compare the returned value(s) with the one listed in the “uart” variable within the cstories.py
code. The best way to do this is using nano. If the value returned from the ‘ls’ command is different from that listed in “uart = serial.Serial” then change the number accordingly in the Python code, save, and retest.
From Here
If you’ll be jumping in and out of your Raspberry Pi periodically, feel encouraged to leave it powered on, just as you would any server. Close out of your ssh connection to the Raspberry Pi by hitting ^D (that is, holding down the Control key and hitting the ‘d’ key), or by typing logout
. Either way, you should see the following window display at the end, making it possible for you to SSH back in via Terminal or PuTTY whenever you need to.
Wrap Up
The primary objective of the Blue Unit is to bring together our microcontroller and microcomputer systems as a means of sharing data between devices, thereby creating a basic physical network. That is, we’ve worked to create a digital networked information system. Together, we can advance a range of technical skills in addition to a number of cognitive and socio-emotional skills to help your community of practice use a growth mindset to build these multi-layered systems.
Comprehension Check
Now would be a great time to revisit the lesson plan listed in “Valued, Inclusive Information and Computing Technology Experiences.” Spend some extended time with your Professional Journal Reflections this week, especially responding to the probes at the end of that session. As you reflect, bring our activities to date into conversation with the capability approach and inclusive computational thinking frameworks brought forward in this session.
- In what ways might the toolbox trumpet and Raspberry Pi counterstories serve as a resource advancing agency? A capability?
- In what ways might the toolbox trumpet and Raspberry Pi counterstories serve as an information system problematically disrupting valued beings and doings of individuals and communities?
- In what ways might the toolbox trumpet and Raspberry Pi counterstories be designed for designers, that is, for the innovators-in-use who adapt this to their own functionings? In what ways might it have been designed differently leading to this point?
- Daniel Oberhaus, “‘Master/Slave’ Terminology Was Removed from Python Programming Language,” Vice, September 13, 2018. https://www.vice.com/en_us/article/8x7akv/masterslave-terminology-was-removed-from-python-programming-language. ↵
- Fritzing breadboard graphics are licensed under CC-BY-SA 3.0. ↵
Integrated circuits (ICs) are semiconductor wafers which contain a collection of tiny resistors, capacitors, and transistors. These can then be built to serve a wide range of electronic functions. In practice, larger sized electronic components used to build circuits are first tested using materials like breadboards for rapid prototyping. They are then redesigned to be built into integrated circuits and optimized for regular, more standardized use. At times, a mix of electronic components along with integrated circuits are themselves used on breadboards to do further rapid prototyping of yet larger circuitry. The 5-Key Capacitive Touch Sensor included in the kit for this book contains a mix of integrated circuits. Examples of integrated circuits include: processors, memory, controllers.
A printed circuit board is a board base made of fiberglass or glass-reinforced plastic with one or more layers of copper or other conductive traces placed on one or both sides of the board base. If you've ever tie dyed a shirt, you've got a good starting point for considering the printing process of a circuit board. Individual electronic components and integrated circuits are then soldered onto specific trace paths to build complex electronics. A small printed circuit board was used to build the 5-Key Capacitive Touch Sensor used in our kit.