From 2c1bba5a518a0907fe7d23f00501284d4e1bb356 Mon Sep 17 00:00:00 2001 From: Joe Foley Date: Tue, 30 Jun 2026 18:00:28 +0000 Subject: [PATCH 01/25] discussion on the IMU --- imu.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 imu.md diff --git a/imu.md b/imu.md new file mode 100644 index 0000000..74efd54 --- /dev/null +++ b/imu.md @@ -0,0 +1,13 @@ +# The Intertial Measurement Unit + +As of 2025, we started using an AliExpress version of https://wolles-elektronikkiste.de/en/icm-20948-9-axis-sensor-part-i + +This has an unmarked jumper on the back. Joe believes that it shorts the VDDIO and VDD pins in order to allow it to be used with 1.8V devices. + +Marcel has tested that the pads connect to both of those pins. + +The IMU runs on 1.8V, but our Pi has GPIO voltages of 3.3V. Marcel reports that students in 2025 had no problem using it as long as AD0 and NCS were connected to VDD. +According to the specification, the maximum pin voltage is VDDIO+0.5, which would put it at 2.3, a whole volt below 3.3. On his board, he connected the pins to 3.3V though a 10K resistor to protect it from overcurrent. + +For I2C at least, it appears that we are saved by the fact that it is open-drain. Each device communicates to the master by pulling lines low, which should work with both voltages. + From b43d1d67983705406f30be1706a5b476e6354bc2 Mon Sep 17 00:00:00 2001 From: Joseph Timothy Foley Date: Thu, 2 Jul 2026 16:13:20 +0000 Subject: [PATCH 02/25] added GPIO pinout document link --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 42174c0..1d4160a 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Scripts and documentation for setting up a Raspberry Pi (Zero W 1 or 2) for use - [Starting on Raspberry Pi Zero](https://reykjavik.instructure.com/courses/6589/pages/starting-on-raspberry-pi-zero-w) ## References and Links +- [Sparkfun GPIO pinout](https://cdn.sparkfun.com/assets/learn_tutorials/6/7/6/PiZero_1.pdf) - [Guide to creating ssh keys](https://pimylifeup.com/raspberry-pi-ssh-keys/) Of note, this guide is a little dated as it suggests using RSA keys and many people are moving toward EDCSA keys. - [Raspberry Pi Connect](https://connect.raspberrypi.com) - [Setting up Raspberry Pi Connect](https://www.raspberrypi.com/documentation/services/connect.html) From aa83f5c957f3754b8bf9d8fad81edadcec628c3f Mon Sep 17 00:00:00 2001 From: "Joseph T. Foley" Date: Thu, 2 Jul 2026 16:56:44 +0000 Subject: [PATCH 03/25] got an LED to blink on GPIO18 --- lgpio/blink.py | 19 +++++++++++++++++++ lgpio/pwm-test.py | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100755 lgpio/blink.py create mode 100755 lgpio/pwm-test.py diff --git a/lgpio/blink.py b/lgpio/blink.py new file mode 100755 index 0000000..7c6b782 --- /dev/null +++ b/lgpio/blink.py @@ -0,0 +1,19 @@ +#!/usr/bin/python +# From https://raspberry.tips/en/raspberrypi-tutorials/raspberry-pi-gpio-python +import lgpio +import time + +# Open GPIO chip (0 = /dev/gpiochip0, on Pi 5 gpiochip4 also possible) +h = lgpio.gpiochip_open(0) + +try: + lgpio.gpio_claim_output(h, 18) # GPIO18 (pin 12) as output (MOTA1) + + for _ in range(5): + lgpio.gpio_write(h, 18, 1) # HIGH + time.sleep(0.5) + lgpio.gpio_write(h, 18, 0) # LOW + time.sleep(0.5) + +finally: + lgpio.gpiochip_close(h) # Always close! diff --git a/lgpio/pwm-test.py b/lgpio/pwm-test.py new file mode 100755 index 0000000..e44f005 --- /dev/null +++ b/lgpio/pwm-test.py @@ -0,0 +1,25 @@ +#!/usr/bin/python +# From https://raspberry.tips/en/raspberrypi-tutorials/raspberry-pi-gpio-python +import lgpio +import time + +# Open GPIO chip (0 = /dev/gpiochip0, on Pi 5 gpiochip4 also possible) +h = lgpio.gpiochip_open(0) + +try: + lgpio.gpio_claim_output(h, 16) # GPIO16 as output (MOTA2) + lgpio.gpio_write(h, 16, 0) # LOW + + # Hardware PWM on GPIO18 (pin 12) must be PWM-capable + # Frequency: 1000 Hz, duty cycle: 50% + lgpio.tx_pwm(h, 18, 1000, 50) + time.sleep(2) + + # Reduce duty cycle to 20% + lgpio.tx_pwm(h, 18, 1000, 20) + time.sleep(2) + + lgpio.tx_pwm(h, 18, 0, 0) # Stop PWM + +finally: + lgpio.gpiochip_close(h) # Always close! From 8dac7f365648f878b6dddbc51193cb824394ba1b Mon Sep 17 00:00:00 2001 From: "Joseph T. Foley" Date: Sat, 4 Jul 2026 15:57:49 +0000 Subject: [PATCH 04/25] PWM sweep test working --- lgpio/pwm-test.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lgpio/pwm-test.py b/lgpio/pwm-test.py index e44f005..2f05f1d 100755 --- a/lgpio/pwm-test.py +++ b/lgpio/pwm-test.py @@ -5,21 +5,21 @@ import time # Open GPIO chip (0 = /dev/gpiochip0, on Pi 5 gpiochip4 also possible) h = lgpio.gpiochip_open(0) - +# Hardware PWM on GPIO18 (pin 12) must be PWM-capable +pwm_pin = 18 +pwm_freq = 1000 try: - lgpio.gpio_claim_output(h, 16) # GPIO16 as output (MOTA2) - lgpio.gpio_write(h, 16, 0) # LOW + while True: + for pwm_level in range(0,100): + lgpio.tx_pwm(h, pwm_pin, pwm_freq, pwm_level) + pwm_level += 10 + time.sleep(0.05) + for pwm_level in range(100,0,-1): + lgpio.tx_pwm(h, pwm_pin, pwm_freq, pwm_level) + pwm_level += 10 + time.sleep(0.05) - # Hardware PWM on GPIO18 (pin 12) must be PWM-capable - # Frequency: 1000 Hz, duty cycle: 50% - lgpio.tx_pwm(h, 18, 1000, 50) - time.sleep(2) - - # Reduce duty cycle to 20% - lgpio.tx_pwm(h, 18, 1000, 20) - time.sleep(2) - - lgpio.tx_pwm(h, 18, 0, 0) # Stop PWM - + finally: + lgpio.tx_pwm(h, pwm_pin, 0, 0) # Stop PWM lgpio.gpiochip_close(h) # Always close! From 3358169ad86e4e496721f602f78a57cc2661b1cb Mon Sep 17 00:00:00 2001 From: Joe Foley Date: Sat, 4 Jul 2026 18:41:02 +0000 Subject: [PATCH 05/25] Started documenting setting up PWM --- pwm.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 pwm.md diff --git a/pwm.md b/pwm.md new file mode 100644 index 0000000..243b593 --- /dev/null +++ b/pwm.md @@ -0,0 +1,53 @@ +# Getting the PWM working + +The Raspberry Pi zero (1 and 2) have 2 PWM peripherals, allowing you to pulse pins without the CPU doing any additional work. This is particuarly useful for controlling motor speeds when connected to a motor driver/H-bridge chip. + + +# References +- [Raspberry Pi Official Overlays information (github)](https://github.com/raspberrypi/firmware/blob/master/boot/overlays/README) + - Line 4235 starts the description of the "pwm" overlay configuration +- [Adafruit 2-channel PWM instructions](https://learn.adafruit.com/adding-basic-audio-ouput-to-raspberry-pi-zero/pi-zero-pwm-audio) + +# Discussion + +My raspberry pi zero W 2 wasn't working reliably with the PWM. + +The `gpio` command has been depricated and is now replaced by the `pinctrl` commend. Running it did not list any PWM0 on any of the pins, which was concerning. + +[This forum post](https://forums.raspberrypi.com/viewtopic.php?t=388352) indicated the need for a `pwm-2chan` device tree overlay to get it to show up. After adding `dtoverlay=pwm-2chan` to the `config.txt`: + +``` +$ pinctrl + 0: ip -- | hi // ID_SDA/GPIO0 = input + 1: ip -- | hi // ID_SCL/GPIO1 = input + 2: a0 -- | hi // GPIO2 = SDA1 + 3: a0 -- | hi // GPIO3 = SCL1 + 4: op -- -- | hi // GPIO4 = output + 5: ip -- | hi // GPIO5 = input + 6: ip -- | hi // GPIO6 = input + 7: op -- -- | hi // GPIO7 = output + 8: op -- -- | hi // GPIO8 = output + 9: a0 -- | lo // GPIO9 = SPI0_MISO +10: a0 -- | lo // GPIO10 = SPI0_MOSI +11: a0 -- | lo // GPIO11 = SPI0_SCLK +12: ip -- | lo // GPIO12 = input +13: ip -- | lo // GPIO13 = input +14: ip -- | lo // GPIO14 = input +15: ip -- | hi // GPIO15 = input +16: ip -- | lo // GPIO16 = input +17: ip -- | lo // GPIO17 = input +18: a5 -- | lo // GPIO18 = PWM0 +19: a5 -- | lo // GPIO19 = PWM1 +``` + +Which looks much better. +There is a pwm chip `in /sys/class/pwm` + +After running the `lgpio/pwm-test.py` +the pin is back to being listed as an input, so I'm confused. +The PWM pin (under load) did the right values. Without a load on the Analog Discovery 2, I just saw noise. + +I commented out `dtoverlay=pwm-2chan` and `lgpio/pwm-test.py` worked. +The pwm chip is missing from `/sys/class/pwm` so I'm guess that's not needed. + + From 18d01e181fe7218689a1237f41dd2e005dca7624 Mon Sep 17 00:00:00 2001 From: Joe Foley Date: Sat, 4 Jul 2026 18:43:08 +0000 Subject: [PATCH 06/25] put proper Adafruit title --- pwm.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pwm.md b/pwm.md index 243b593..55bce23 100644 --- a/pwm.md +++ b/pwm.md @@ -6,7 +6,10 @@ The Raspberry Pi zero (1 and 2) have 2 PWM peripherals, allowing you to pulse pi # References - [Raspberry Pi Official Overlays information (github)](https://github.com/raspberrypi/firmware/blob/master/boot/overlays/README) - Line 4235 starts the description of the "pwm" overlay configuration -- [Adafruit 2-channel PWM instructions](https://learn.adafruit.com/adding-basic-audio-ouput-to-raspberry-pi-zero/pi-zero-pwm-audio) +- [Adding Basic Audio Ouput to Raspberry Pi Zero](https://learn.adafruit.com/adding-basic-audio-ouput-to-raspberry-pi-zero/pi-zero-pwm-audio) + +# Important Points +1. The `gpio` program is no longer available. It has been replaced with `pinctrl` which works a little differently. # Discussion From 6009a5eff88c676d74632fa8b9351b9a78a01f0a Mon Sep 17 00:00:00 2001 From: Joe Foley Date: Sat, 4 Jul 2026 18:45:10 +0000 Subject: [PATCH 07/25] procedure reminds people to put the SD card in --- imager.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/imager.md b/imager.md index 7d02e84..a3f4827 100644 --- a/imager.md +++ b/imager.md @@ -44,6 +44,8 @@ We don't have many monitors and keyboards in V207 for students to use, so we wil ![imager-writeimage-verifying.png](Graphics/imager-writeimage-verifying.png) 1. The process is complete! ![imager-complete.png](Graphics/imager-complete.png) +1. Take the SD card and put it into your pi. +1. When you first boot it, it can take 10 minutes to finish configuring itself. Usually when the LED stops blinking for at least 30 seconds, it is done. ## Connecting using Raspberry Pi Connect 1. Login to https://connect.raspberrypi.com if you haven't already From f79b9985db52610662e7ed162f13c98f6a16594b Mon Sep 17 00:00:00 2001 From: "Joseph T. Foley" Date: Sat, 4 Jul 2026 19:34:38 +0000 Subject: [PATCH 08/25] The motor sweeps through the speeds (tested) --- lgpio/motor-test.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 lgpio/motor-test.py diff --git a/lgpio/motor-test.py b/lgpio/motor-test.py new file mode 100755 index 0000000..c7893fb --- /dev/null +++ b/lgpio/motor-test.py @@ -0,0 +1,34 @@ +#!/usr/bin/python +# This test assumes you are trying to control a motor through a DRV8833 motor +# controller chip. This general approach should work for other motors as well. +import lgpio +import time + +# pin assignments +gpio_pwm = 18 #MOTA1 +gpio_reverse = 16 #MOTA2 +gpio_notsleep = 24 #MSLEEP +# parameters +pwm_freq = 1000 + + +h = lgpio.gpiochip_open(0) +try: + lgpio.gpio_claim_output(h, gpio_reverse) + lgpio.gpio_claim_output(h, gpio_notsleep) + lgpio.gpio_write(h, gpio_reverse, 0) # low on reverse pin to turn off h-bridge MOTA2 + lgpio.gpio_write(h, gpio_notsleep, 1) # high on /sleep to enable both h-bridges + + while True: + for pwm_level in range(0,100): + lgpio.tx_pwm(h, gpio_pwm, pwm_freq, pwm_level) + pwm_level += 10 + time.sleep(0.05) + for pwm_level in range(100,0,-1): + lgpio.tx_pwm(h, gpio_pwm, pwm_freq, pwm_level) + pwm_level += 10 + time.sleep(0.05) + +finally: + lgpio.gpio_write(h, gpio_notsleep, 1) # high on /sleep to disable + lgpio.gpiochip_close(h) # Always close! From a743e06b9b86c37c99adb421487c40687e8f102e Mon Sep 17 00:00:00 2001 From: Joe Foley Date: Sat, 4 Jul 2026 19:36:30 +0000 Subject: [PATCH 09/25] reminder to create interface hat --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1d4160a..aa383fe 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Scripts and documentation for setting up a Raspberry Pi (Zero W 1 or 2) for use ## TODO - Finding the Pi using `nmap` or `arp` +- Create a interface HAT that makes it easy to attach to a digital analyzer (v207) and an Analog Discovery # Some Tricks Here is a page of tricks we have discovered that may involve advanced techniques. From 582e2d4a16ba9a29916e2e427896d3ee01e069cd Mon Sep 17 00:00:00 2001 From: Joe Foley Date: Sun, 5 Jul 2026 19:53:58 +0000 Subject: [PATCH 10/25] Blinka uses lgpio, so we get hardware pwm --- README.md | 1 + pwm.md | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/README.md b/README.md index aa383fe..2e38861 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Scripts and documentation for setting up a Raspberry Pi (Zero W 1 or 2) for use 1. [Solder the GPIO header pins](solder-gpio.md) 1. [Gadget Mode to setup a serial connection over USB](gadget.md) 1. [Marcel's script on github to show the IP address on the OLED screen](https://github.com/mkyas/cpsy-display-ip) +1. Install Blinka by following the guide [CircuitPython Libraries on Linux and Raspberry Pi](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi)] ## RU student-only guides at ROB-IOT - [Starting on Raspberry Pi Zero](https://reykjavik.instructure.com/courses/6589/pages/starting-on-raspberry-pi-zero-w) diff --git a/pwm.md b/pwm.md index 55bce23..7b016bb 100644 --- a/pwm.md +++ b/pwm.md @@ -4,15 +4,20 @@ The Raspberry Pi zero (1 and 2) have 2 PWM peripherals, allowing you to pulse pi # References +- [CircuitPython Libraries on Linux and Raspberry Pi](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi) - [Raspberry Pi Official Overlays information (github)](https://github.com/raspberrypi/firmware/blob/master/boot/overlays/README) - Line 4235 starts the description of the "pwm" overlay configuration - [Adding Basic Audio Ouput to Raspberry Pi Zero](https://learn.adafruit.com/adding-basic-audio-ouput-to-raspberry-pi-zero/pi-zero-pwm-audio) +- [CircuitPython pwmio documentation](https://docs.circuitpython.org/en/latest/shared-bindings/pwmio/index.html) + # Important Points 1. The `gpio` program is no longer available. It has been replaced with `pinctrl` which works a little differently. +1. The Blinka/CircuitPython interface uses `lgpio` to do hardware PWM on the pins. This requires the `pwm-2chan` overlay to be setup correctly, which it's documented procedure does. Details on relevant pins on line 4267 of the [overleays README](https://github.com/raspberrypi/firmware/blob/master/boot/overlays/README) # Discussion + My raspberry pi zero W 2 wasn't working reliably with the PWM. The `gpio` command has been depricated and is now replaced by the `pinctrl` commend. Running it did not list any PWM0 on any of the pins, which was concerning. From d86c7a224d36a6d3055edb12657de5e3ef18262e Mon Sep 17 00:00:00 2001 From: Joe Foley Date: Tue, 7 Jul 2026 00:09:35 +0000 Subject: [PATCH 11/25] Got bluetooth talking over a serial port --- bluetooth.md | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 bluetooth.md diff --git a/bluetooth.md b/bluetooth.md new file mode 100644 index 0000000..12392f1 --- /dev/null +++ b/bluetooth.md @@ -0,0 +1,79 @@ +# Bluetooth on the Raspberry Pi Zero W (1 or 2) +The Zero W series has Bluetooth capability because of the Wifi chip on the board. +**Warning: This is a work in progress!!!** + +# Reference +- "Exploring Raspberry Pi" by Derek Molloy pg 537-544 +- [Connecting to a Headless Raspberry Pi using Bluetooth](https://medium.com/@tomw3115/connect-to-a-headless-raspberry-pi-using-bluetooth-0e61c05e1b68) +- https://github.com/sorah/bluetooth-getty +- [Use dbus insteadd of sdptool](https://linuxvox.com/blog/bluez-adding-services-attributes-and-profiles-without-sdptool-command/) + +# Getting Bluetooth working +1. Login to the pi via a USB-serial port or monitor-keyboard-mouse. Configuring bluetooth can sometimes kick you off the wifi. +1. Tell `rfkill` to unblock bluetooth. Perhaps this is a security thing? +``` +sudo rfkill unblock bluetooth +``` +1. Restart the bluetooth service and check it's status +``` +sudo systemctl restart bluetooth +sudo systemctl status bluetooth +``` +1. Figure out the address using `hcitool` +``` +hcitool dev +``` +1. If you have a laptop set to be discoverable, you can check if bluetooth is working +``` +hcitool scan +``` +1. You can then query that device for what services it offers +``` +sdptool browse ADDRESS +``` +1. Now we want to make the Pi discoverable to our phone, tablet, or laptop +``` +sudo hciconfig hci0 piscan +sudo hciconfig hci name RaspberryPi +``` +1. A Serial Port Profile(SPP) is needed for our serial port +``` +sudo sdptool add SP +``` +1. For some reason, we have to put the bluetooth daemon in compatibility mode. (True as of 2016-2026 --foley). Find the line with `ExecStart` and put `--compat` at the end +``` +sudo nano /lib/systemd/system/bluetooth.service +``` +1. Test the SPP is setup + +``` +sudo sdptool browse local +``` +1. Time to pair it with a device +``` +sudo bluetoothctl +discoverable on +agent on +pairable on +scan on +``` +and once you have paired it +``` +discoverable off +exit +``` + +1. If this works, you can install the `bluetooth.services` and `rfcomm.service` files to automate some of this setup. +``` +sudo cp bluetooth.service /etc/systemd/system/bluetooth.target.wants/. +sudo cp rfcomm.service /etc/systemd/system/. +sudo systemctl daemon-reload +sudo systemctl enable rfcomm +sudo reboot + +``` + + + +# Discussion +Apparently Bluez's `sdptool` is now depricated and they are telling people to use this new GATT via `busctl` or `dbus-send`. [linuxvox article on depricated sdptool](https://linuxvox.com/blog/bluez-adding-services-attributes-and-profiles-without-sdptool-command/) From f4283353a21867dfdf15dd6427c27ab4d3f54533 Mon Sep 17 00:00:00 2001 From: Joe Foley Date: Tue, 11 Aug 2026 22:02:54 +0000 Subject: [PATCH 12/25] reminder to use ssh while updating --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2e38861..a37f2c0 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,10 @@ Scripts and documentation for setting up a Raspberry Pi (Zero W 1 or 2) for use 1. [Raspberry Pi Imager procedure to setup ssh and Raspberry Pi Connect](imager.md) 1. Find out your Pi's ethernet addresss in case you need to find it on `iot-research` again. Type `ip addr show dev wlan0` and for something like `link/ether 01:23:45:67:89:ab`. Write this information (the 12 characters and colons) in your notebook. In T-411-MECH we want you to put this information in a spreadsheet links from the main page: this allows us connect Pi's to people in case of any issues. 1. [Solder the GPIO header pins](solder-gpio.md) +1. Login to the pi using ssh or serial. (Pi Connect breaks wihle upgrading the Pi Connect program). You will be doing updates as part of the next procedure. 1. [Gadget Mode to setup a serial connection over USB](gadget.md) 1. [Marcel's script on github to show the IP address on the OLED screen](https://github.com/mkyas/cpsy-display-ip) + 1. Install Blinka by following the guide [CircuitPython Libraries on Linux and Raspberry Pi](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi)] ## RU student-only guides at ROB-IOT From ba377393a5db4b3dc1a708b3be21e5cf3d720aa1 Mon Sep 17 00:00:00 2001 From: Joe Foley Date: Tue, 11 Aug 2026 22:07:57 +0000 Subject: [PATCH 13/25] listed how to do the updates --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a37f2c0..10d008e 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,12 @@ Scripts and documentation for setting up a Raspberry Pi (Zero W 1 or 2) for use 1. [Raspberry Pi Imager procedure to setup ssh and Raspberry Pi Connect](imager.md) 1. Find out your Pi's ethernet addresss in case you need to find it on `iot-research` again. Type `ip addr show dev wlan0` and for something like `link/ether 01:23:45:67:89:ab`. Write this information (the 12 characters and colons) in your notebook. In T-411-MECH we want you to put this information in a spreadsheet links from the main page: this allows us connect Pi's to people in case of any issues. 1. [Solder the GPIO header pins](solder-gpio.md) -1. Login to the pi using ssh or serial. (Pi Connect breaks wihle upgrading the Pi Connect program). You will be doing updates as part of the next procedure. +1. Login to the pi using ssh or serial. (Pi Connect breaks wihle upgrading the Pi Connect program). Now update the packages on your pi: + ``` + sudo apt-get update + sudo apt-get -y upgrade + ``` + 1. [Gadget Mode to setup a serial connection over USB](gadget.md) 1. [Marcel's script on github to show the IP address on the OLED screen](https://github.com/mkyas/cpsy-display-ip) From 447a0fcca37f0be7a1df0c2c9f2750860f0dc02e Mon Sep 17 00:00:00 2001 From: Joe Foley Date: Tue, 11 Aug 2026 22:29:21 +0000 Subject: [PATCH 14/25] reboot after changing the dtoverlay --- gadget.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/gadget.md b/gadget.md index 3c75f9d..edcad0d 100644 --- a/gadget.md +++ b/gadget.md @@ -29,9 +29,16 @@ echo "dwc2" >> /etc/modules ``` modprobe libcomposite ``` +1. Reboot +1. Open a new connection and become root (the superuser) +``` +sudo su +``` + 1. Put a copy of this repository in root's home directory using git ``` apt install -y git +cd /root git clone https://gitea.cs.ru.is/RU-V207/rover-pi.git ``` 1. Go to Scripts and test if the `gadget.sh` runs without errors and hopefully a Serial port appers in Device Manager or equivalent From 464ed79f9522209d2b9295da86a06db2d72e10a6 Mon Sep 17 00:00:00 2001 From: "Joseph T. Foley" Date: Tue, 11 Aug 2026 23:26:41 +0000 Subject: [PATCH 15/25] otg service needs to run before serial-getty --- Scripts/serial-ether-gadget.service | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Scripts/serial-ether-gadget.service b/Scripts/serial-ether-gadget.service index 05a912d..de4ff66 100644 --- a/Scripts/serial-ether-gadget.service +++ b/Scripts/serial-ether-gadget.service @@ -1,6 +1,7 @@ [Unit] Description=Serial and Ethernet over OTG USB -After=systemd-user-sessions.service +#After=systemd-user-sessions.service +Before=serial-getty@ttyGS0.service [Service] ExecStart=/root/rover-pi/Scripts/gadget.sh [Install] From 104aaabde4a63204ca2aff3625095c1a8ef7f35f Mon Sep 17 00:00:00 2001 From: Joe Foley Date: Tue, 11 Aug 2026 23:29:28 +0000 Subject: [PATCH 16/25] reminder about quality of usb cable --- gadget.md | 1 + 1 file changed, 1 insertion(+) diff --git a/gadget.md b/gadget.md index edcad0d..30afa82 100644 --- a/gadget.md +++ b/gadget.md @@ -5,6 +5,7 @@ Based upon Adafruit guide: https://learn.adafruit.com/turning-your-raspberry-pi- The author discovered the guide was out of date, consulted the [2025 Raspberry Pi whitepaper on OTG](https://pip-assets.raspberrypi.com/categories/685-app-notes-guides-whitepapers/documents/RP-009276-WP/Using-OTG-mode-on-Raspberry-Pi-SBCs), found additional errors, and was finally able to correct them thanks to this blog https://www.isticktoit.net/?p=1383 ## Important : +- Some of the serial cables we get from our supplier are flakey: if wiggling the cable when plugged into your computer causes it to sometimes see the serial port and sometimes not, get a new one. - **Using the Gadget Mode will make the usb port unable to connect to a mouse or keyboard!!!** You will need to disable it make them work again. - You will want to disable the serial console on RX/TX in `raspi-config` if you are trying to use that serial port for communicating with other devices. From 075fd124cdc71e3c906b68a995057d32d9ee29f8 Mon Sep 17 00:00:00 2001 From: Joseph Timothy Foley <2+foley@noreply.localhost> Date: Thu, 27 Aug 2026 13:38:17 +0000 Subject: [PATCH 17/25] forgot to go to root's home --- gadget.md | 239 +++++++++++++++++++++++++++--------------------------- 1 file changed, 120 insertions(+), 119 deletions(-) diff --git a/gadget.md b/gadget.md index 3c75f9d..3510cff 100644 --- a/gadget.md +++ b/gadget.md @@ -1,119 +1,120 @@ -# Gadget Mode to setup a serial connection over USB -Prepare Raspberry Pi OS connection over a microUSB serial cable - -Based upon Adafruit guide: https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget/serial-gadget: -The author discovered the guide was out of date, consulted the [2025 Raspberry Pi whitepaper on OTG](https://pip-assets.raspberrypi.com/categories/685-app-notes-guides-whitepapers/documents/RP-009276-WP/Using-OTG-mode-on-Raspberry-Pi-SBCs), found additional errors, and was finally able to correct them thanks to this blog https://www.isticktoit.net/?p=1383 - -## Important : -- **Using the Gadget Mode will make the usb port unable to connect to a mouse or keyboard!!!** You will need to disable it make them work again. -- You will want to disable the serial console on RX/TX in `raspi-config` if you are trying to use that serial port for communicating with other devices. - - -## Procedure - -Make sure you have your USB cable plugged into the left port **USB** and not the right port marked **PWR**. -1. Open a connection to the device via ssh or Raspberry Pi Connect -1. Become root (the superuser) -``` -sudo su -``` -1. Enable the DWC version 2 USB peripheral device tree overlay by appending it to the end after the `[all]` category (which is at the end) -``` -echo "dtoverlay=dwc2" >> /boot/firmware/config.txt -``` -1. We add the required module `dwc2` for loading at startup -``` -echo "dwc2" >> /etc/modules -``` -1. Check that we can load the libcomposite module -``` -modprobe libcomposite -``` -1. Put a copy of this repository in root's home directory using git -``` -apt install -y git -git clone https://gitea.cs.ru.is/RU-V207/rover-pi.git -``` -1. Go to Scripts and test if the `gadget.sh` runs without errors and hopefully a Serial port appers in Device Manager or equivalent - ``` -cd rover-pi/Scripts/ -./gadget.sh -``` -1. Install the systemd -``` -cp serial-ether-gadget.service /lib/systemd/system/. -``` -1. Enable the service -``` -systemctl enable serial-ether-gadget.service -``` -1. Enable the getty (which connects a login terminal to the serial port) -``` -systemctl enable serial-getty@ttyGS0.service -``` -1. Reboot and see if the serial port appears - -# Connecting to the serial terminal -You will need a serial terminal program (which used to be very very common) in order to communicate over your serial port - -## Windows -- Putty: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html -- MobaXterm: https://mobaxterm.mobatek.net/ - -## Linux -If using a Linux VM, you will need to pass the Pi USB connection there. -This is similar to the procedure for accessing an Arduino. -1. Start the Linux VM if you haven't already -1. Menu: Devices > USB -1. Look for "Silicon Labs CP210x USB to UART Bridge" and click on it. -1. check if port /dev/ttyUSB0 is found. The Pi will most often be accessible over this port. - ``` - ls -l /dev/ttyU* - ``` -1. The user must be in the dialout group to access it. Use the Linux command: `uid` -to check if you are in the group. If not in the dialout group, use: - ``` - sudo usermod -a -G dialout - ``` -1. You will use the GNU `screen` application in Linux to access the Pi. You can install it with: - ``` - sudo apt install screen - ``` -1. You should now gain access to the Pi with: - ``` - ` sudo screen /dev/ttyUSB0 115200 - ``` -1. Press enter and you will be prompted to log in to the Pi with default credentials. User: pi - PW: raspberry - -# Disabling the OTG mode to use a keyboard and mouse again - -**WARNING: Untested** -1. Comment out the dwg2 line in `/boot/firmware/config.txt` using a `#` in front of the line - ``` - [all] - #dtoverlay=dwg2 - ``` -1. Comment out the dwg2 line in `/etc/modules` using a `#` in front of the line - ``` - #dwg2 - ``` -1. Disable the gadget service - ``` - systemctl disable serial-ether-gadget.service - ``` -1. Disable the getty - ``` - systemctl disable serial-getty@ttyGS0.service - ``` - -# Connecting over the USB ethernet to ssh into your Pi - -I have not tested this procedure because the serial console and normal network connection is sufficient for me --foley - -https://www.raspberrypi.com/news/usb-gadget-mode-in-raspberry-pi-os-ssh-over-usb/ - - -# Untested - Here are items that bear investigating but haven't been tested yet - - - [Modify Imager manifest to install otg](https://github.com/raspberrypi/rpi-imager/tree/main/doc/local_json) - +# Gadget Mode to setup a serial connection over USB +Prepare Raspberry Pi OS connection over a microUSB serial cable + +Based upon Adafruit guide: https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget/serial-gadget: +The author discovered the guide was out of date, consulted the [2025 Raspberry Pi whitepaper on OTG](https://pip-assets.raspberrypi.com/categories/685-app-notes-guides-whitepapers/documents/RP-009276-WP/Using-OTG-mode-on-Raspberry-Pi-SBCs), found additional errors, and was finally able to correct them thanks to this blog https://www.isticktoit.net/?p=1383 + +## Important : +- **Using the Gadget Mode will make the usb port unable to connect to a mouse or keyboard!!!** You will need to disable it make them work again. +- You will want to disable the serial console on RX/TX in `raspi-config` if you are trying to use that serial port for communicating with other devices. + + +## Procedure + +Make sure you have your USB cable plugged into the left port **USB** and not the right port marked **PWR**. +1. Open a connection to the device via ssh or Raspberry Pi Connect +1. Become root (the superuser) +``` +sudo su +``` +1. Enable the DWC version 2 USB peripheral device tree overlay by appending it to the end after the `[all]` category (which is at the end) +``` +echo "dtoverlay=dwc2" >> /boot/firmware/config.txt +``` +1. We add the required module `dwc2` for loading at startup +``` +echo "dwc2" >> /etc/modules +``` +1. Check that we can load the libcomposite module +``` +modprobe libcomposite +``` +1. Put a copy of this repository in root's home directory using git +``` +cd /root +apt install -y git +git clone https://gitea.cs.ru.is/RU-V207/rover-pi.git +``` +1. Go to Scripts and test if the `gadget.sh` runs without errors and hopefully a Serial port appers in Device Manager or equivalent + ``` +cd rover-pi/Scripts/ +./gadget.sh +``` +1. Install the systemd +``` +cp serial-ether-gadget.service /lib/systemd/system/. +``` +1. Enable the service +``` +systemctl enable serial-ether-gadget.service +``` +1. Enable the getty (which connects a login terminal to the serial port) +``` +systemctl enable serial-getty@ttyGS0.service +``` +1. Reboot and see if the serial port appears + +# Connecting to the serial terminal +You will need a serial terminal program (which used to be very very common) in order to communicate over your serial port + +## Windows +- Putty: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html +- MobaXterm: https://mobaxterm.mobatek.net/ + +## Linux +If using a Linux VM, you will need to pass the Pi USB connection there. +This is similar to the procedure for accessing an Arduino. +1. Start the Linux VM if you haven't already +1. Menu: Devices > USB +1. Look for "Silicon Labs CP210x USB to UART Bridge" and click on it. +1. check if port /dev/ttyUSB0 is found. The Pi will most often be accessible over this port. + ``` + ls -l /dev/ttyU* + ``` +1. The user must be in the dialout group to access it. Use the Linux command: `uid` -to check if you are in the group. If not in the dialout group, use: + ``` + sudo usermod -a -G dialout + ``` +1. You will use the GNU `screen` application in Linux to access the Pi. You can install it with: + ``` + sudo apt install screen + ``` +1. You should now gain access to the Pi with: + ``` + ` sudo screen /dev/ttyUSB0 115200 + ``` +1. Press enter and you will be prompted to log in to the Pi with default credentials. User: pi - PW: raspberry + +# Disabling the OTG mode to use a keyboard and mouse again + +**WARNING: Untested** +1. Comment out the dwg2 line in `/boot/firmware/config.txt` using a `#` in front of the line + ``` + [all] + #dtoverlay=dwg2 + ``` +1. Comment out the dwg2 line in `/etc/modules` using a `#` in front of the line + ``` + #dwg2 + ``` +1. Disable the gadget service + ``` + systemctl disable serial-ether-gadget.service + ``` +1. Disable the getty + ``` + systemctl disable serial-getty@ttyGS0.service + ``` + +# Connecting over the USB ethernet to ssh into your Pi + +I have not tested this procedure because the serial console and normal network connection is sufficient for me --foley + +https://www.raspberrypi.com/news/usb-gadget-mode-in-raspberry-pi-os-ssh-over-usb/ + + +# Untested + Here are items that bear investigating but haven't been tested yet + + - [Modify Imager manifest to install otg](https://github.com/raspberrypi/rpi-imager/tree/main/doc/local_json) + From 2ca5bc2dd20daf2a84f9983e3a327008e62e6aad Mon Sep 17 00:00:00 2001 From: Joseph Timothy Foley Date: Fri, 28 Aug 2026 10:01:43 +0000 Subject: [PATCH 18/25] Improving the oled instructions and configuration file --- oled-display/LICENSE | 21 +++++++++ oled-display/README.md | 38 ++++++++++++++++ oled-display/display-ip.py | 78 +++++++++++++++++++++++++++++++++ oled-display/display-ip.service | 13 ++++++ 4 files changed, 150 insertions(+) create mode 100644 oled-display/LICENSE create mode 100644 oled-display/README.md create mode 100644 oled-display/display-ip.py create mode 100644 oled-display/display-ip.service diff --git a/oled-display/LICENSE b/oled-display/LICENSE new file mode 100644 index 0000000..929ff3b --- /dev/null +++ b/oled-display/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Marcel + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/oled-display/README.md b/oled-display/README.md new file mode 100644 index 0000000..eda4ed2 --- /dev/null +++ b/oled-display/README.md @@ -0,0 +1,38 @@ +# cpsy-display-ip +Demonstrates the SSD1306 OLED by showing the IP address on the system boot. +Originally at https://github.com/mkyas/cpsy-display-ip and customized for Mechatronics 1 students. + +This project contains the file `display-ip.py` that displays an IP +address on the SSD1306 OLED screen and the file `display-ip.service` +that executes the script on boot after the Raspberry Pi is connected +to a network. + +To use the script, you first must connect the screen to the Pi's +I2C bus. + +## Setup + +Do the following steps on your Raspberry Pi. + +1. Enable I2C in `raspi-config` +1. Go to your home directory: `cd ~` +1. Clone this repository to your raspberry pi `git clone https://gitea.cs.ru.is/RU-V207/rover-pi` +1. Install [pillow](https://python-pillow.org/), numpy, and venv: + > sudo apt install python3-venv python3-pil python3-numpy +1. Create a virtual environment, here called cpsy: + > python3 -m venv cpsy +1. Edit the generated `pyvenv.cfg` to set `include-system-site-packages = true` +1. Activate the environment `source cpsy/bin/activate` +1. Install the display driver: `pip install adafruit-circuitpython-ssd1306` +1. Edit `display-ip.service`: + - Replace `foley` with your user name on the Pi. + - the `WorkingDirectory` points to the directory containing `display-ip.py` + - the path to python points to your virtual environment +1. Copy the edited `display-ip.service` to `/etc/systemd/system` + ```sudo cp display-ip.service /etc/systemd/system/.``` +1. Enable the service `sudo systemctl enable display-ip.service` +1. Test by starting the service `sudo systemctl start display-ip.service` which should turn on the screen. +1. If nothing happens, check on the status with `sudo systemctl status display-ip` and look for error messages. +1. If you make additional changes to the service file, you may have to reload the `systemd`: + ```sudo systemctl daemon-reload ``` + diff --git a/oled-display/display-ip.py b/oled-display/display-ip.py new file mode 100644 index 0000000..d4c6857 --- /dev/null +++ b/oled-display/display-ip.py @@ -0,0 +1,78 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +""" +This demo will fill the screen with white, draw a black box on top +and then print the device's IP address in the center of the display + +This example is for use on (Linux) computers that are using CPython with +Adafruit Blinka to support CircuitPython libraries. CircuitPython does +not support PIL/pillow (python imaging library)! +""" + +import board +import digitalio +from PIL import Image, ImageDraw, ImageFont +import adafruit_ssd1306 +import socket + +# Define the Reset Pin +oled_reset = None +try: + oled_reset = digitalio.DigitalInOut(board.D4) +except: + pass + # No reset pin + +# Change these +# to the right size for your display! +WIDTH = 128 +HEIGHT = 64 +BORDER = 5 + +# Use for I2C. +i2c = board.I2C() # uses board.SCL and board.SDA +oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset) + +# Clear display. +oled.fill(0) +oled.show() + +# Create blank image for drawing. +# Make sure to create image with mode '1' for 1-bit color. +image = Image.new("1", (oled.width, oled.height)) + +# Get drawing object to draw on image. +draw = ImageDraw.Draw(image) + +# Draw a white background +draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255) + +# Draw a smaller inner rectangle +draw.rectangle( + (BORDER, BORDER, oled.width - BORDER - 1, oled.height - BORDER - 1), + outline=0, + fill=0, +) + +# Load default font. +font = ImageFont.load_default() + +# Get the local IP address as a string. +with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s.connect(("8.8.8.8", 80)) + text = s.getsockname()[0] + +# Draw it into a box. +bbox = font.getbbox(text) +(font_width, font_height) = bbox[2] - bbox[0], bbox[3] - bbox[1] +draw.text( + (oled.width // 2 - font_width // 2, oled.height // 2 - font_height // 2), + text, + font=font, + fill=255, +) + +# Display image +oled.image(image) +oled.show() diff --git a/oled-display/display-ip.service b/oled-display/display-ip.service new file mode 100644 index 0000000..621f6cf --- /dev/null +++ b/oled-display/display-ip.service @@ -0,0 +1,13 @@ +[Unit] +Description=display-ip +After=network-online.target + +[Service] +Type=simple +User=foley +WorkingDirectory=/home/foley/rover-pi/oled-display +ExecStart=/home/foley/cpsy/bin/python3 display-ip.py +Restart=never + +[Install] +WantedBy=multi-user.target From c72108a0aaafe75d41fe64bbdde9f115e0dc95e8 Mon Sep 17 00:00:00 2001 From: Joseph Timothy Foley Date: Fri, 28 Aug 2026 10:02:44 +0000 Subject: [PATCH 19/25] pointer to documentation --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 42174c0..795fa08 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,8 @@ Scripts and documentation for setting up a Raspberry Pi (Zero W 1 or 2) for use 1. Find out your Pi's ethernet addresss in case you need to find it on `iot-research` again. Type `ip addr show dev wlan0` and for something like `link/ether 01:23:45:67:89:ab`. Write this information (the 12 characters and colons) in your notebook. In T-411-MECH we want you to put this information in a spreadsheet links from the main page: this allows us connect Pi's to people in case of any issues. 1. [Solder the GPIO header pins](solder-gpio.md) 1. [Gadget Mode to setup a serial connection over USB](gadget.md) -1. [Marcel's script on github to show the IP address on the OLED screen](https://github.com/mkyas/cpsy-display-ip) +1. [Setup the OLED screen to print the IP](oled-display/README.md) + Based upon [Marcel's script on github to show the IP address on the OLED screen](https://github.com/mkyas/cpsy-display-ip) ## RU student-only guides at ROB-IOT - [Starting on Raspberry Pi Zero](https://reykjavik.instructure.com/courses/6589/pages/starting-on-raspberry-pi-zero-w) From 9c49411159038efc998d723b9e31ccdb79b4ed23 Mon Sep 17 00:00:00 2001 From: Joe Foley Date: Fri, 4 Sep 2026 09:42:39 +0000 Subject: [PATCH 20/25] Adding aleksa24 detect for pre-compiled DWC2 --- Scripts/gadget.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Scripts/gadget.sh b/Scripts/gadget.sh index 490c1ef..719aa08 100755 --- a/Scripts/gadget.sh +++ b/Scripts/gadget.sh @@ -11,8 +11,10 @@ fi # Is the dwc2 overlay installed? Nothing will work if it isn't. -if lsmod | grep -q "dwc2"; then - systemd-cat -t gadget.sh -p info echo "DWC2 module loaded, setting up OTG gadget serial and ethernet" +if [ -d /sys/class/udc ] && [ -n "$(ls -A /sys/class/udc)" ]; then + systemd-cat -t gadget.sh -p info echo "DWC2 precompiled, setting up OTG gadget serial and ethernet" +elif lsmod | grep -q "dwc2"; then + systemd-cat -t gadget.sh -p info echo "Pre 6.18.39 kernel. DWC2 module loaded, setting up OTG gadget serial and ethernet" else systemd-cat -t gadget.sh -p warning echo "DWC2 module missing. Aborting OTG setup." systemctl disable serial-getty@ttyGS0.service From e6a0556147a62b18a81237024c9d8c5278a7ed31 Mon Sep 17 00:00:00 2001 From: Joe Foley Date: Fri, 4 Sep 2026 10:01:40 +0000 Subject: [PATCH 21/25] Check for GADGET_MODE in boot/firmware to enable --- Scripts/gadget.sh | 7 ++++++ gadget.md | 54 ++++++++++++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/Scripts/gadget.sh b/Scripts/gadget.sh index 719aa08..de1e432 100755 --- a/Scripts/gadget.sh +++ b/Scripts/gadget.sh @@ -9,6 +9,13 @@ if [ $(id -u) -ne 0 ]; then exit 1 fi +if [ -f /boot/firmware/GADGET_MODE ]; then + systemd-cat -t gadget.sh -p info echo "/boot/firmware/GADGET_MODE exists: GADGET mode enabled" +else + systemd-cat -t gadget.sh -p warning echo "/boot/firmware/GADGET_MODE missing: GADGET mode disabled" + systemctl disable serial-getty@ttyGS0.service + exit 1 +fi # Is the dwc2 overlay installed? Nothing will work if it isn't. if [ -d /sys/class/udc ] && [ -n "$(ls -A /sys/class/udc)" ]; then diff --git a/gadget.md b/gadget.md index 2b46559..d36483b 100644 --- a/gadget.md +++ b/gadget.md @@ -18,14 +18,6 @@ Make sure you have your USB cable plugged into the left port **USB** and not the ``` sudo su ``` -1. Enable the DWC version 2 USB peripheral device tree overlay by appending it to the end after the `[all]` category (which is at the end) -``` -echo "dtoverlay=dwc2" >> /boot/firmware/config.txt -``` -1. We add the required module `dwc2` for loading at startup -``` -echo "dwc2" >> /etc/modules -``` 1. Check that we can load the libcomposite module ``` modprobe libcomposite @@ -94,7 +86,38 @@ This is similar to the procedure for accessing an Arduino. ``` 1. Press enter and you will be prompted to log in to the Pi with default credentials. User: pi - PW: raspberry -# Disabling the OTG mode to use a keyboard and mouse again +# Enabling/Disabling the OTG mode to use a keyboard and mouse again + +**WARNING: Untested** +1. Disable: Delete a file called `GADGET_MODE` in the boot partition/drive when not on a pi. If the Pi is booted, you can delete `sudo rm /boot/firmware/GADGET_MODE` and reboot +1. Enable: create a file called `GADGET_MODE` in the boot partition/drive when not on a pi. If the Pi is booted, you can create `sudo touch /boot/firmware/GADGET_MODE` and reboot + +# Connecting over the USB ethernet to ssh into your Pi + +I have not tested this procedure because the serial console and normal network connection is sufficient for me --foley + +https://www.raspberrypi.com/news/usb-gadget-mode-in-raspberry-pi-os-ssh-over-usb/ + + +# Untested + Here are items that bear investigating but haven't been tested yet + + - [Modify Imager manifest to install otg](https://github.com/raspberrypi/rpi-imager/tree/main/doc/local_json) + +# Old DWC Instructions (for older versions) + +## Setting up +1. Enable the DWC version 2 USB peripheral device tree overlay by appending it to the end after the `[all]` category (which is at the end) +``` +echo "dtoverlay=dwc2" >> /boot/firmware/config.txt +``` +1. We add the required module `dwc2` for loading at startup +``` +echo "dwc2" >> /etc/modules +``` + + +## Disabling the OTG mode to use a keyboard and mouse again **WARNING: Untested** 1. Comment out the dwg2 line in `/boot/firmware/config.txt` using a `#` in front of the line @@ -114,16 +137,3 @@ This is similar to the procedure for accessing an Arduino. ``` systemctl disable serial-getty@ttyGS0.service ``` - -# Connecting over the USB ethernet to ssh into your Pi - -I have not tested this procedure because the serial console and normal network connection is sufficient for me --foley - -https://www.raspberrypi.com/news/usb-gadget-mode-in-raspberry-pi-os-ssh-over-usb/ - - -# Untested - Here are items that bear investigating but haven't been tested yet - - - [Modify Imager manifest to install otg](https://github.com/raspberrypi/rpi-imager/tree/main/doc/local_json) - From a73e40b960beb2eb0d37cb9e29168a2baa19721d Mon Sep 17 00:00:00 2001 From: Joseph Timothy Foley Date: Fri, 4 Sep 2026 16:08:27 +0000 Subject: [PATCH 22/25] Tested. IT works. Updating directions to enable and disable --- gadget.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/gadget.md b/gadget.md index d36483b..4712d74 100644 --- a/gadget.md +++ b/gadget.md @@ -88,9 +88,15 @@ This is similar to the procedure for accessing an Arduino. # Enabling/Disabling the OTG mode to use a keyboard and mouse again -**WARNING: Untested** -1. Disable: Delete a file called `GADGET_MODE` in the boot partition/drive when not on a pi. If the Pi is booted, you can delete `sudo rm /boot/firmware/GADGET_MODE` and reboot -1. Enable: create a file called `GADGET_MODE` in the boot partition/drive when not on a pi. If the Pi is booted, you can create `sudo touch /boot/firmware/GADGET_MODE` and reboot +## Disable +1. Remove the SD card and access it on your computer +1. Delete the file called `GADGET_MODE` in the boot partition/drive usually called `bootfs`. +1. If the Pi is booted, you can `sudo rm /boot/firmware/GADGET_MODE` and reboot. + +## Enable +1. Remove the SD card and access it on your computer +1. Create a file called `GADGET_MODE` in the boot partition/drive called `bootfs`. +2. If the Pi is booted, you can `sudo touch /boot/firmware/GADGET_MODE` and reboot # Connecting over the USB ethernet to ssh into your Pi From 9ba6b30e1f3b63daa6215612bcff5f3f009814fe Mon Sep 17 00:00:00 2001 From: Joseph Timothy Foley Date: Fri, 4 Sep 2026 16:15:29 +0000 Subject: [PATCH 23/25] Update gadget.md --- gadget.md | 1 + 1 file changed, 1 insertion(+) diff --git a/gadget.md b/gadget.md index 4712d74..e6f19c0 100644 --- a/gadget.md +++ b/gadget.md @@ -5,6 +5,7 @@ Based upon Adafruit guide: https://learn.adafruit.com/turning-your-raspberry-pi- The author discovered the guide was out of date, consulted the [2025 Raspberry Pi whitepaper on OTG](https://pip-assets.raspberrypi.com/categories/685-app-notes-guides-whitepapers/documents/RP-009276-WP/Using-OTG-mode-on-Raspberry-Pi-SBCs), found additional errors, and was finally able to correct them thanks to this blog https://www.isticktoit.net/?p=1383 ## Important : +- **We are still working out the kinks with the gadget mode switching on the Pi Zero W 2** - **Using the Gadget Mode will make the usb port unable to connect to a mouse or keyboard!!!** You will need to disable it make them work again. - The blue USB cables included in the kit in 2026 are flakey. We recommend replacing them ASAP. - You will want to disable the serial console on RX/TX in `raspi-config` if you are trying to use that serial port for communicating with other devices. From 73b4e98a363583adcc2d2143ff478f139274d559 Mon Sep 17 00:00:00 2001 From: Joseph Timothy Foley Date: Fri, 4 Sep 2026 19:45:55 +0000 Subject: [PATCH 24/25] suggested using supported rpi-usb-gadget instead of my script --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a477c5e..14d5808 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Scripts and documentation for setting up a Raspberry Pi (Zero W 1 or 2) for use sudo apt-get -y upgrade ``` -1. [Gadget Mode to setup a serial connection over USB](gadget.md) +1. [Gadget Mode to setup a network connection over USB](https://github.com/raspberrypi/rpi-usb-gadget) 1. [Setup the OLED screen to print the IP](oled-display/README.md) Based upon [Marcel's script on github to show the IP address on the OLED screen](https://github.com/mkyas/cpsy-display-ip) 1. Install Blinka by following the guide [CircuitPython Libraries on Linux and Raspberry Pi](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi)] From e74eff43a27299f5441c89776589491fe97028df Mon Sep 17 00:00:00 2001 From: Joseph Timothy Foley Date: Sun, 6 Sep 2026 19:41:51 +0000 Subject: [PATCH 25/25] the lgpio examples do not work reliably. Starting new Blinka --- lgpio/blink.py | 19 ------------------- lgpio/motor-test.py | 34 ---------------------------------- lgpio/pwm-test.py | 25 ------------------------- 3 files changed, 78 deletions(-) delete mode 100755 lgpio/blink.py delete mode 100755 lgpio/motor-test.py delete mode 100755 lgpio/pwm-test.py diff --git a/lgpio/blink.py b/lgpio/blink.py deleted file mode 100755 index 7c6b782..0000000 --- a/lgpio/blink.py +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/python -# From https://raspberry.tips/en/raspberrypi-tutorials/raspberry-pi-gpio-python -import lgpio -import time - -# Open GPIO chip (0 = /dev/gpiochip0, on Pi 5 gpiochip4 also possible) -h = lgpio.gpiochip_open(0) - -try: - lgpio.gpio_claim_output(h, 18) # GPIO18 (pin 12) as output (MOTA1) - - for _ in range(5): - lgpio.gpio_write(h, 18, 1) # HIGH - time.sleep(0.5) - lgpio.gpio_write(h, 18, 0) # LOW - time.sleep(0.5) - -finally: - lgpio.gpiochip_close(h) # Always close! diff --git a/lgpio/motor-test.py b/lgpio/motor-test.py deleted file mode 100755 index c7893fb..0000000 --- a/lgpio/motor-test.py +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/python -# This test assumes you are trying to control a motor through a DRV8833 motor -# controller chip. This general approach should work for other motors as well. -import lgpio -import time - -# pin assignments -gpio_pwm = 18 #MOTA1 -gpio_reverse = 16 #MOTA2 -gpio_notsleep = 24 #MSLEEP -# parameters -pwm_freq = 1000 - - -h = lgpio.gpiochip_open(0) -try: - lgpio.gpio_claim_output(h, gpio_reverse) - lgpio.gpio_claim_output(h, gpio_notsleep) - lgpio.gpio_write(h, gpio_reverse, 0) # low on reverse pin to turn off h-bridge MOTA2 - lgpio.gpio_write(h, gpio_notsleep, 1) # high on /sleep to enable both h-bridges - - while True: - for pwm_level in range(0,100): - lgpio.tx_pwm(h, gpio_pwm, pwm_freq, pwm_level) - pwm_level += 10 - time.sleep(0.05) - for pwm_level in range(100,0,-1): - lgpio.tx_pwm(h, gpio_pwm, pwm_freq, pwm_level) - pwm_level += 10 - time.sleep(0.05) - -finally: - lgpio.gpio_write(h, gpio_notsleep, 1) # high on /sleep to disable - lgpio.gpiochip_close(h) # Always close! diff --git a/lgpio/pwm-test.py b/lgpio/pwm-test.py deleted file mode 100755 index 2f05f1d..0000000 --- a/lgpio/pwm-test.py +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/python -# From https://raspberry.tips/en/raspberrypi-tutorials/raspberry-pi-gpio-python -import lgpio -import time - -# Open GPIO chip (0 = /dev/gpiochip0, on Pi 5 gpiochip4 also possible) -h = lgpio.gpiochip_open(0) -# Hardware PWM on GPIO18 (pin 12) must be PWM-capable -pwm_pin = 18 -pwm_freq = 1000 -try: - while True: - for pwm_level in range(0,100): - lgpio.tx_pwm(h, pwm_pin, pwm_freq, pwm_level) - pwm_level += 10 - time.sleep(0.05) - for pwm_level in range(100,0,-1): - lgpio.tx_pwm(h, pwm_pin, pwm_freq, pwm_level) - pwm_level += 10 - time.sleep(0.05) - - -finally: - lgpio.tx_pwm(h, pwm_pin, 0, 0) # Stop PWM - lgpio.gpiochip_close(h) # Always close!