Making Printed Circuit Boards (PCBs)

It turns out that it’s pretty easy (and relatively cheap) to design and order printed circuit boards (PCBs).

PCB wired up to a light box.

I’ve used Fritzing to design them. It’s simple to use with their PCB view window. They have a lot of parts available (such as the 3pin connectors), though I did have to get the fritzing part for the Raspberry Pi Pico off the Raspberry Pi website. Fritzing is very commonly used for circuit diagrams on the web, so it’s fairly easy to find parts you’re looking for. The current version (>1.0) requires a small donation, but the older version (0.9+) is free, and I’ve used both successfully.

The board above is used to connect a Raspberry Pi Pico to pairs of LEDs and touch sensors. In this case only one pair is in use to make a light box that can be turned on and off by touching the sides.

Light box in action.

There are a number of companies that will take the exported Gerber files and make you a PCB. In fact, there’s even an option within Fritzing to place an order. I’ve used PCBWay (this is my PCBWay referrer link that gets me a discount if you use it), which, as of this writing, costs about $5 for a batch of 5 PCBs (that’s the minimum order), plus about $10 for 15 day shipping to the US. The only change I made to their standard settings is to use the Lead-free solder.

Image of PCB.
One of my first PCBs. It makes it easier to connect a Raspberry Pi Pico to a series of touch sensor and LED pairs. I messed this one up by not using the official raspberrypi.com part and not realizing that the one I chose did not have the holes drilled all the way through the board.

I’d like to have my physics and/or computer science students design and make their own starting next year.

Bright and Shiny Things: Programming with LED’s

Teaching programming using the LED light strips is going much better than expected. I tried it with the 9th grade Algebra class during our weekly programming session using a set of coding lessons I put together. I went so well that though we started by having everyone (about 10 kids) share two LED strips, by the end of the year I had three students from that class build their own.

Student built LED strip.
Student’s LED strip on a sword. The battery can power it for at least 15 minutes.

The coding lessons are still a work in progress, but it has them learn the basics by running some of the test programs, then then explore sequences using for loops. There are a lot of directions to branch off after the for loops. I’ve had some of my Algebra II students make static patterns using linear and exponential functions, while a couple of the kids in my programming class used different functions to make dynamic lighting patterns; our hydroponic system (see here and here) now has a neat LED indicator that runs different sequences depending on if the pumps are running or not.

Some of the students who built their LED strips in the Makerspace posted about their projects: LED Thingy and LED Light Strip Project. The process (rpi-led-strip) is not too hard but required them to be able to do a little physical computing (with Raspberry Pi’s), use ssh and terminal commands (terminal instructions), and then run and write python programs.

Raspberry Pi that controls one of the LED strips from a student’s project.

Since the setup uses the same GitHub repository (rpi-led-strip) it’s also easy to update some of our existing projects like the Wall Anchor.

Wall anchor project.

I am amazed at how much the students have engaged with what are, ultimately, very simple systems (a Raspberry Pi and a strip of 20 lights), and I’m really excited to see where it takes us.

LED Light Strip with Pi Zero

Raspberry Pi Zero controlling a LED strip, with a hardwired clear button.

I wanted to set up a small (20 LEDs) light strip using a Raspberry Pi Zero, so students could learn how to remotely log in to a device, work with the Linux command line, run python programs, and get visible, real feedback on their progress.

Instructions and code are in the Github rpi-led-strip repository.

Web control for the LED strip.

The repository also has instructions and code for setting up a local server on the Pi so you can control the LED strip via a webpage. Students working on their own LED projects in the Makerspace will appreciate this.

The main idea here was to make the project as simple as possible. The web page is basic with minimal styling, so it should be easy to edit, but I do test out some of the newer HTML input elements, like the color picker. The README in the repository also includes instructions on how to, step by step, add components to the webpage to control the Pi: the “Blue” button is used as the example (it sets the entire strip to blue).

With only 20 LEDs you don’t need an external power supply so everything can be run through the Pi.

Adafruit’s CircuitPython NeoPixel library makes controlling the lights really easy. There are a few example programs in the rpi-led-strip/pyLED/ directory of the repository.

The full strip.

I’ve also included a physical button (it’s optional) that I’m using right now to just clear the LED strip. I may change it to just reboot the Pi, because I anticipate that things will get interesting when I have an entire class trying to connect to one or two devices. So far, I’ve had a small group of four students try this with some success.

Introduction to Pi’s (Raspberry Pi)

The family of Raspberry Pi’s are just really small computers. You can plug a monitor, keyboard, and mouse into one and it will not look too different from your desktop. They are small and cheap, but what makes them really useful is that they have little slots (called GPIO’s) that you can stick wires into that allow you to build circuits that can get information from sensors and control devices like LED lights or motors.

This is a quick introduction about how to set one up. You’ll find lots of great tutorials on the internet. This one is specific to my needs: it’s an introduction to the Pi’s for students who are new to them; I’m setting it up with a web server so we can control the devices through a webpage; and I’m setting it up so you can control the Pi “headlessly”, which means you don’t need the keyboard, mouse, etc..

Installing the Operating System

Downloading the OS

Download: The operating system files can be downloaded from the Raspberry Pi website. We’re going to use the Raspbian Desktop version with the recommended software.

Your typical computer has a built in hard drive that stores the data you save, the programs/apps you install, and the operating system (OS) that runs it all. When you start the computer the first thing it does is read the files that make up the operating system from the hard drive and set them up in the active, processing memory (RAM). Then when you interact with the computer (type on the keyboard, click the mouse etc.) you’re interacting with the operating system: you tell the operating system what to do, like start up a web browser (Firefox, Chrome, Safari, Explorer, Opera etc.), and it does it. And when your apps want to do something, like save a file, they have to ask the operating system to do it.

On the Raspberry Pi the data for the operating system is not stored on a built in hard drive, but on an SD card (or microSD), which means that you’re going to have to install the operating system yourself to get your Pi running. You can find the operating system at the Raspberry Pi website’s download page.

Installing

As of this writing, I’ve been using balenaEtcher to install the operating system on the SD Card.

balenaEtcher is free and pretty easy to use. Hopefully, your computer has an SD card port, if not you’re going to have to find an adapter. Just plug your SD card into your computer and run Etcher, it will ask you to:

  • Select Image: Which is the Raspbian file you downloaded
  • Select Drive: Which should default to the SD card you plugged in (check the size of the drive to make sure)
  • Flash: Which writes the Operating System files to the SD card, making sure everything is in the right place.

You may see some warnings pop up about Unrecognized Files Systems or similar. You can just close those windows.

When the flashing is done, don’t take the SD card out of your computer (or put it back in if you have) just quite yet. We’re going to set it up so the Pi can automatically connect to the WiFi, which will make it easier to talk to.

Setting Up WiFi

You’re going to have to edit some files on the SD card to give the Pi the information about the WiFi situation so that it can automatically connect. This is most useful if you’re not going to plug in a keyboard and monitor and just want to control the Pi from your computer (more on how to do this later). If you do want to go the keyboard and mouse route, you can just plug the SD card into the Pi, power it up, and set up the WiFi like you would normally do on your laptop.

To edit the files I use Atom on Windows or TextEdit which is built in on Mac. These programs should allow you to easily save files as plain text, without any of the fancy styling that will create errors when the Pi operating system tries to get the information from the files.

WiFi

Create a new file called: “wpa_supplicant.conf” (based on these notes) containing:

ctrl_interface=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
 ssid="networkID"
 psk="password"
}

But you have to change:

  • networkID to the name of the WiFi network you’re trying to connect to
  • password to the password for the network

If you need to connect to multiple networks (home and school for example) you can add another network command on a new line after the first one:


network={
 ssid="otherNetwork"
 psk="otherPassword"
}

Save this file to the boot directory of the SD card.

ssh

ssh allows you to remotely connect to your Pi’s operating system. This means that you can use your laptop to control the Pi (however you’ll be using command line commands).

Create an empty file named “ssh” and save it to the boot directory of your SD card.

USB connection

You should be able to find your Pi on the network (I use an app on my phone called Fing) and ssh in. However, to do most of the setup, especially if the Pi has trouble connecting to the WiFi (or you can’t find it on the network), you’ll probably want to set up your pi so you can plug it into your computer’s USB port and control it from the computer. Based on the notes from Adafruit, do this:

Open the file “config.txt” which is in the SD card’s boot directory, and add this as the last line in the file:

dtoverlay=dwc2

Save the file then:

Open the file “cmdline.txt”, find the word “rootwait” and, after it, insert the phrase:

 modules-load=dwc2,g_ether

You should end up with something that looks like “…=yes rootwait modules-load=dwc2,g_ether quiet…”:

Connecting to your Pi

To talk to your Pi’s Operating System you should be able to connect your Pi’s USB port to your computer’s or connect over WiFi. Either way you’ll need to use an ‘ssh’ program.

  • Windows: I use putty. Install the program and run it. Then you’ll need to enter:
    • Host Name: raspberrypi.local
    • Password: raspberry
  • Mac: I use the built-in Terminal (In your Applications->Utilities folder). Type in the command (don’t type in the “>”):
    • > ssh raspberrypi.local
    • Use the password: raspberry

If you go the WiFi route, you’ll need to find your Pi’s IP address and use that as the Host Name.

Update and Upgrade

Once you’re ssh’d in, and are connected the internet, you can update and upgrade the operating system. Type in the commands (without the “>”).

> sudo apt-get update
> sudo apt-get upgrade

The “sudo” means you’re giving yourself permission to run commands that could potentially mess up your system. The program you’re running is called “apt-get” which connects to the internet repositories with the latest updates and upgrades to your operating system and programs, and then downloads and installs them. The options “update” and “upgrade” specifically tells the “apt-get” program what you want it to do. Downloading and upgrading may take a while.

Enable Interfaces

You’ll also want to check that the interfaces to the GPIO pins are enabled, so you can build circuits and control them. Notes on this are here.

First check that your tools are installed and updated with the commands:

> sudo pip3 install --upgrade setuptools
> sudo apt-get install -y python-smbus
> sudo apt-get install -y i2c-tools

Then Activate the Interfaces. You’ll run the command “raspi-config” and then use your keyboard to tab through the windows to activate the I2C and SPI interfaces. These are just two different ways for the Pi to talk to the devices you plug into it.

> sudo raspi-config
---- Interfacing Options
-------- I2C
------------ Yes
---- Interfacing Options
-------- SPI
------------ Yes

To get this all up an running you need to reboot the Pi:

> sudo reboot now

For the OLED displays

To control the little OLED displays we have, install the adafruit-blinka, and OLED libraries:

> sudo pip3 install adafruit-blinka
> sudo pip3 install adafruit-circuitpython-ssd1306

Tornado Server

The tornado server allows us to create webpages on the Pi that we can connect to over WiFi that can be used to control devices connected to the Pi. Install tornado using:

> sudo pip3 install tornado

Now restart everything and we can get to work.

> sudo reboot now

Headless Pi Zero

Pi Zero‘s are even cheaper versions of the Raspberry Pi. The price you pay is that they’re harder to connect to since all the ports are small (micro-USB’s and mini-HDMI’s), so you need adapters to connect to keyboards, mice, and monitors. However, if you have the wireless version (Pi Zero W) you can set it up to automatically connect to the WiFi network, and work on it through there using the command line. This is a brief summary of how to do this (it’s called a “headless” setup) based on Taron Foxworth’s instructions. It should work for the full Raspberry Pi as well.

Set up the Operating System

You can install the Raspbian Stretch (or Raspbian Lite which does not include the desktop GUI that you will not use) on a SD card (I used 8 or 16 Gb cards).

  • Download Raspbian.
  • SD Card Formatter to format the SD Card. It’s pretty quick, just follow the instructions.
  • Etcher: to install the operating system on the SD Card

Set up automatic connection to WiFi

You may have to remove and reinsert the SD Card to get it to show up on the file system, but once you have you can set it up to automatically connect to WiFi by:

  • Go into the /boot partition (it usually shows up as the base of the SD Card on your file manager) and create a file named “ssh”.
  • Create a text file called “wpa_supplicant.conf” in the /boot folder, and put the following into it, assuming that the wifi router you’re connecting to is called “myWifi” and the password is “myPassword”.
  • country=US
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    
    network={
     ssid="myWifi"
     scan_ssid=1
     psk="myPassword"
     key_mgmt=WPA-PSK
    }
    

Now put the SD Card into the Pi and plug in the power.

Find the IP address

First you have to find the Pi on the local network. On Windows I use Cygwin to get something that looks and acts a bit like a unix terminal (on Mac you can use Terminal, or any shell window on Linux).

To find the ip address of your local network use:

on Windows:

> ipconfig

look for the “IPv4 Address”.

Mac (and Linux?)

> ifconfig

look under “en1:”

Connecting

To connect you use ssh on the command line (Mac or Linux) or something like Putty

Static IP

Without a static IP address it is possible that the Pi’s IP address will change occasionally. I followed the instructions on Circuit Basics and MODMYPI, but basically you have to:

  • Identify your network information:
    • Find your Gateway:
    • > route -ne
      
    • Find your Domain Server:
    • > cat /etc/resolv.conf
      
  • Add the information to the end of your /etc/dhcpcd.conf file (here the wlan0 means you’re doing this to the wireless interface (WiFi) and the static ip is 10.0.0.99, the static router the gateway ip you found above, and the domain_name_servers takes the domain server ip):
  • interface wlan0
    static ip_address=10.0.0.99
    static routers=10.0.0.1
    static domain_name_servers=75.75.75.75
    
  • Now reboot and you should be able to ssh or sftp into the new static IP.

A Web Server

Adding a webserver (apache with php) is pretty easy as well, just run the commands (from the Raspberry Pi Foundation)

Apache web server:

sudo apt-get install apache2 -y

PHP for server-side scripting

sudo apt-get install php libapache2-mod-php -y

Now your can find the webpage by going to the ip address (e.g. http://10.0.0.1).

The actual file that you’re seeing is located on the Pi at:

/var/www/html/index.html

You will probably need to change the ownership of the file in order to edit it by running:

> sudo chown pi: index.html