RpiGPIO

— DRAFT —

Last Edit: BK 2022-04-29

Status Update - See embedded comments in Edit window

What: Proposed workshop to introduce Raspberry Pi and the GPIO hardware interface

When: TBD (Very hard to find Raspberry Pi boards at the time of this edit (2022-04-06) Additionally Davis Public Lib prefers that we do this late June or early July since they’re gearing up for Juneteenth 6/5)

Brief Overview

The workshop will help guide new Pi / GPIO users to set up a headless (no monitor/keyboard) and using Python/CircuitPython, connect a simple device (TBD) on one of the hardware busses (I2C, SPI, or UART).

(Editors: Please suggest data source, boards or sensors)

Required Materials

Participants should bring:
- Raspberry Pi with soldered GPIO header. The header will be needed for GPIO connections and possibly for set up (any of: Zero/Zero W/Zero 2 W/1/2/3/4) DMS to potentially provide units for sale - Case for the Pi or at least tape on the bottom side to prevent shorting - 5 volt Power Supply with the correct cable for your Pi – Laptop USB or 1 Amp phone charger is fine for Pi Zero, 2 Amp for Pi 3, 2.5 Amp (5.25 volt) for Pi 4 - SD/uSD card (bring 2 or 3!) anything from 2 GB and up, for the OS (DMS to potentially provide units for sale)
- Laptop computer with any of: Window, Mac OS, Linux - Your home network SSID (name) and Password so that you can connect at home

MacOS - Has Bonjour, install Pi Imager
Windows - Install Bonjour printer util, install Pi Imager
Linux - Install Bonjour support (sudo apt-get install avahi-daemon), install Pi Imager
Chrome OS - Pi Imager not available in Play Store, but can use the dd utility and do a manual config of the boot partition which is not covered here)

What’s the Point here anyway?

Python and Circuitpython provide an easy access to working with hardware on a Raspberry Pi. Pis are cheap (relatively) use a known OS (Linux). In many cases the Pi would be used as a remote sensor that doesn’t need a dedicated monitor and keyboard and can be run “headless”. The Pi can store data locally and/or send data across the network using a Python implementation of Linux sockets. Cheap(-ish) minimal displays can also be attached to a Pi for immediate data display..

Steps

(Editors: I need to test that this works on the Library PUBLIC wifi)

Set up OS on card using Pi Imager

Install for yourself beforehand to save time.

Windows/Mac OS - https://www.raspberrypi.com/software/

Linux - $ sudo apt install rpi-imager

  • Enable Advanced options - Click the “Gear” icon

  • Set hostname to something unique. You will connect via SSH as username@hostname.local

  • Enable SSH

  • Set username and password

  • (Set everything else too)

  • Configure wifi (SSID PUBLIC for use at Library) wifi country US,

  • Set locale (Time zone: America/Los_Angeles, Keyboard layout US)

  • Flash OS on card using Pi Imager

RPi Imager wants a password for the WiFi connection. Maybe need to go into the config file and add something like this to /etc/wpa_supplicant/wpa_supplicant.conf

network={

    ssid="PUBLIC"

    key_mgmt=NONE

}

And while you’re at it, add a network section for your home network.

network={

    ssid="HomeNetwordName"

    psk="HomeNetworkPassword"

}

  • [Optional] Edit config.txt to allow a shutdown “button” by shorting pins 5 & 6 (Gnd) for s few seconds. To shut down with the same button, just add the line dtoverlay=gpio-shutdown to /boot/config.txt on your Pi. /boot is a FAT partition and should be writeable by any Mac/Windows/Linux machine.

  • [Optional] To force HDMI output in case you need to attach a monitor to the Pi, you can add (or uncomment) this to the config file: hdmi_force_hotplug=1

  • This sets up the ability for a pushbutton switch to shut it down using GPIO pin 17 and ground: dtoverlay=gpio-shutdown,gpio_pin=17,active_low=1,gpio_pull=up,debounce=1000

(RPi Imager doesn’t like an empty password for the wifi connection. At least 8 chars are required. Need to test that any string will work. Actual instruction the wpa_supplicant.conf file should be “key_mgmt=NONE”)

Enable Bonjour support on your laptop

  • Windows - install Bonjour support (printer util) and reboot

  • Mac - already installed

  • Linux - sudo apt-get install avahi-daemon

Fire Up the Pi

Insert card into Pi and power up

Wait a couple of minutes and login to your Pi ($ ssh username@hostname.local)

[Optional] Set up Public Key Access to the Pi

(On local, not the Pi)

$ ssh-keygen

(no password)

$ cat ~/.ssh/id_rse.pub | ssh pi@hostname ‘mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys’

You should now be able to log in to your Pi without the password

Update OS on the Pi

  • $ sudo apt update
  • $ sudo apt upgrade -y

[Optional] Do you want to test the speed of the uSD card?

$ sudo apt-get hdparm

$ sudo hdparm -tT /dev/mmcblk0

** Set up your home network ** If you haven’t already, you should probably add your home network configuration. Add this to /etc/wpa_supplicant/wpa_supplicant.conf:

network={

ssid=“”

psk=“”

}

Does everyone understand?:

Command line instructions in the form of:

“$ {raspi os command}” where ‘$’ indicate the system prompt

ex: “$ sudo shutdown -r now”

and “>>> python expression” where ‘>>>’ indicate the Python REPL prompt

“>>> import time”

[Optional] Set up a Virtual Environment

(TBD)

[Optional] Install RClone to backup files to a cloud server such as Google Drive, Dropbox, etc.

[Optional] Install VS Code on local machine with the “Remote SSH” extension to connect to the Pi

Install Blinka

(Following instructions on the Blinka page, link below)

Instructions on Adafruit aren’t clear here but your library installs will be in the form of this:

$ pip3 install adafruit-circuitpython-pm25

Test in the REPL by typing:

import {library_name}

from adafruit_pm25.i2c import PM25.I2C

Resources

  • Portable Python Projects A good introduction to Pi setup and use in home automation projects.

  • Pi Imager - Use to install Raspberry Pi OS on the SD card.

  • Using Advanced Features of Pi Imager - Introduction to useful advanced features of the Pi Imager.

  • Tips for connecting to a headless Pi (https://www.raspberrypi.com/documentation/computers/remote-access.html#remote-access)

  • Power Off Button https://greiginsydney.com/pi-shutdown-button/

  • Useful but obscure RPi OS commands to explore:

compgen - shows you available commands

pinout - prints a graphic showing GPIO pins