Skip to content

Instantly share code, notes, and snippets.

@pfeerick
Last active March 6, 2022 18:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pfeerick/fe8ebd68623d6d775e7b654aa32ec6fa to your computer and use it in GitHub Desktop.
Save pfeerick/fe8ebd68623d6d775e7b654aa32ec6fa to your computer and use it in GitHub Desktop.
Shell script to toggle LEDs on a MCP23017 I2C I/O expander connected to a pine64 board
#!/bin/bash
#elevate to root to ensure no permission errors
#[ `whoami` = root ] || exec su -c $0 root
if [[ $EUID -ne 0 ]]; then
echo "i2cset usually needs sudo powers to run, please login if prompted to avoid permission errors"
exec sudo $0
fi
echo "Set all Port A pins (GPA0-7) as outputs..."
i2cset -y 1 0x20 0x00 0x00
echo "Ensuring all outputs are off..."
i2cset -y 1 0x20 0x14 0x00
echo "Toggling LED 1 (GPA0)"
i2cset -y 1 0x20 0x14 0x01
sleep 1
i2cset -y 1 0x20 0x14 0x00
sleep 1
echo "Toggling LED 2 (GPA1)"
i2cset -y 1 0x20 0x14 0x02
sleep 1
i2cset -y 1 0x20 0x14 0x00
sleep 1
echo "Toggling LED 3 (GPA2)"
i2cset -y 1 0x20 0x14 0x04
sleep 1
i2cset -y 1 0x20 0x14 0x00
sleep 1
echo "Toggling LED 4 (GPA3)"
i2cset -y 1 0x20 0x14 0x08
sleep 1
i2cset -y 1 0x20 0x14 0x00
sleep 1
echo "Toggling LED 5 (GPA4)"
i2cset -y 1 0x20 0x14 0x10
sleep 1
i2cset -y 1 0x20 0x14 0x00
sleep 1
# Below lines commented as only used five leds in demo due to breadboard space
#echo "Toggling LED 6 (GPA5)"
#i2cset -y 1 0x20 0x14 0x20
#sleep 1
#i2cset -y 1 0x20 0x14 0x00
#sleep 1
#echo "Toggling LED 7 (GPA6)"
#i2cset -y 1 0x20 0x14 0x40
#sleep 1
#i2cset -y 1 0x20 0x14 0x00
#sleep 1
#echo "Toggling LED 8 (GPA7)"
#i2cset -y 1 0x20 0x14 0x80
#sleep 1
#i2cset -y 1 0x20 0x14 0x00
#sleep 1
echo "All done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment