Block I Illinois Library Illinois Open Publishing Network

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

We’ve slowly been working towards creating a digital Little Free Library extension to the wonderful analog Little Free Libraries we find in many neighborhoods. Microcontrollers like the Circuit Playground Express provide a range of sensors and indicators that can be very useful to communicate back and forth with Little Free Library patrons. Microcomputers like the Raspberry Pi, on the other hand, serve as an e-library stacks, library catalog, chat room, and much more. Together, these can be hidden away in a toolbox and run with an extension cord or solar panel and battery pack. For now, our goal is to bring the Circuit Playground Express and Raspberry Pi together. We will set up the Circuit Playground Express to communicate through serial connection with the Raspberry Pi, and the Raspberry Pi to play MP3 audio recordings through its audio jack. In this exercise, we will begin to explore the possible relationships between a single-purpose computer, such as the Circuit Playground Express, and a multi-purpose computer, such as the Raspberry Pi.

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:

  1. Download the Toolbox Trumpet Counterstory UART Prototype for the Circuit Playground Express:
  2. 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.
  3. Connect the Circuit Playground Express to your laptop via the microUSB to USB cable and flash the new UF2 to your Circuit Playground Express.
  4. 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.
  5. 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]

A diagram of the Circuit Playground Express to Raspberry Pi UART connection.

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)
  • 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:

Take a glance at the code, but most importantly right click in the window to download the program as (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

The macOS terminal window is connected to a Raspberry Pi via ssh connection. The file is open in the nano text editor.

  • 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,, 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/

The program runs within a macOS terminal window.

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.
While the Python program runs, CTRL-C is entered in the macOS terminal window to quit.

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 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.

A macOS terminal window is connected to a Raspberry Pi via ssh connection. The terminal shows the execution of a Python script, followed by the logout command.

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.

  1. In what ways might the toolbox trumpet and Raspberry Pi counterstories serve as a resource advancing agency? A capability?
  2. 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?
  3. 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?

  1. Daniel Oberhaus, “‘Master/Slave’ Terminology Was Removed from Python Programming Language,” Vice, September 13, 2018.
  2. Fritzing breadboard graphics are licensed under CC-BY-SA 3.0.


Icon for the Creative Commons Attribution-ShareAlike 4.0 International License

A Person-Centered Guide to Demystifying Technology Copyright © 2020 by Copyright © 2020 Martin Wolske. Copyright “Ideating and Iterating Code: Scratch Example” © 2020 Betty Bayer and Stephanie Shallcross. Copyright “Introducing the Unix Command Line” © 2020 Martin Wolske, Dinesh Rathi, Henry Grob, and Vandana Singh. Copyright “Security and Privacy” © 2020 Sara Rasmussen. is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book