From 9aeb1820b54b1fe20f803609999f71e070203ccb Mon Sep 17 00:00:00 2001 From: Piclaw Date: Tue, 16 Jun 2026 15:03:47 -0700 Subject: [PATCH] Convert i2c.sh to Python Modernized port of the I2C-enable installer. The original 363-line Pimoroni-template script carried a large amount of dead scaffolding (Wheezy/Jessie/Squeeze checks, armv6 detection, the no-device-tree branch, the legacy i2c-bcm2708 module and raspi-blacklist.conf handling) that no longer applies to current Raspberry Pi OS. The modern equivalent is small: enable I2C via raspi-config's do_i2c, and ensure the i2c-dev character interface loads at boot so /dev/i2c-* is available. Uses append_if_missing (adafruit-python-shell >= 1.14.0) for the /etc/modules entry. --- i2c.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 i2c.py diff --git a/i2c.py b/i2c.py new file mode 100644 index 0000000..71b7ee9 --- /dev/null +++ b/i2c.py @@ -0,0 +1,52 @@ +# SPDX-FileCopyrightText: 2016 ladyada for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +# INSTALLER SCRIPT TO ENABLE I2C ON A RASPBERRY PI + +try: + from adafruit_shell import Shell +except ImportError: + raise RuntimeError( + "The library 'adafruit_shell' was not found. To install, try typing: " + "sudo pip3 install adafruit-python-shell" + ) + +shell = Shell() +shell.group = "I2C" + + +def main(): + shell.clear() + print("This script will enable I2C on your Raspberry Pi") + print("") + shell.warn("--- Warning ---") + print("Always be careful when running scripts and commands copied") + print("from the internet. Ensure they are from a trusted source.") + print("") + + if not shell.prompt("Do you wish to continue?", default="n"): + shell.bail("Aborting...") + + print("") + print("Enabling I2C...") + # raspi-config performs the device-tree (dtparam=i2c_arm=on) edit and + # loads the i2c kernel module on the current Raspberry Pi OS. + shell.run_raspi_config("do_i2c 0") + + # Ensure the i2c-dev character interface is loaded at boot so /dev/i2c-* + # is available to user-space tools (i2cdetect, Blinka, etc.). + shell.append_if_missing("/etc/modules", "i2c-dev") + shell.run_command("modprobe i2c-dev") + + print("") + shell.info("Enabled") + print("") + print("Settings take effect on next boot.") + print("") + shell.prompt_reboot() + + +if __name__ == "__main__": + shell.require_root() + main()