Skip to content

Instantly share code, notes, and snippets.

@dave-malone
Created April 8, 2021 21:05
Show Gist options
  • Save dave-malone/10ccb306b9863451965105b4122ab119 to your computer and use it in GitHub Desktop.
Save dave-malone/10ccb306b9863451965105b4122ab119 to your computer and use it in GitHub Desktop.
Raspberry Pi GPIO LED Binary Clock driven by the MCP23017 port expander
import smbus
import time
from datetime import datetime
bus = smbus.SMBus(1)
DEVICE = 0x20 # Device address (A0-A2)
IODIRA = 0x00
OLATA = 0x14
GPIOA = 0x12
# Set all GPA pins as outputs
bus.write_byte_data(DEVICE, IODIRA, 0x00)
# Set output all 7 output bits to 0
bus.write_byte_data(DEVICE,OLATA,0)
try:
while True:
for LedOut in range(0,60):
# Count from 1 to 255
now = datetime.now()
current_seconds = int(now.strftime("%S"))
bus.write_byte_data(DEVICE, OLATA, current_seconds)
# print(current_seconds)
# bus.write_byte_data(DEVICE, OLATA, LedOut)
# print(LedOut)
time.sleep(0.1)
except KeyboardInterrupt:
# Set all bits to zero
bus.write_byte_data(DEVICE, OLATA, 0)
@dave-malone
Copy link
Author

dave-malone commented Apr 8, 2021

sudo apt-get install -y i2c-tools

pip3 install smbus

Also requires the i2c interface to be enabled on the Raspberry Pi. Enable that using sudo raspi-config, and then reboot.

Open a terminal and check the I2C address by typing in sudo i2cdetect -y 1

Helpful resource: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment