Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions i2c.py
Original file line number Diff line number Diff line change
@@ -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()