pastebin - collaborative debugging tool
python.kpaste.net RSS


I2C Scanner
Posted by Anonymous on Thu 26th Jan 2023 20:25
raw | new post

  1. #I2C Scanner
  2. # SDA Physical pin #1 - GP0
  3. # SCL Physical pin #2 - GP1
  4. # GND Physical pin #3 - GND
  5. # 3V3 Physical pin #36 - 3V3
  6. import time
  7. import board
  8. import busio
  9.  
  10. i2c = busio.I2C(scl=board.GP1, sda=board.GP0)
  11.  
  12. while not i2c.try_lock():
  13.     pass
  14.  
  15. chipCount = 0
  16.  
  17. try:
  18.     while True:
  19.         print("Scanning....")
  20.         theAddresses = [hex(device_address) for device_address in i2c.scan()]
  21.        
  22.         for i in theAddresses:
  23.             chipCount += 1
  24.             print("Chip",chipCount," = ",i)
  25.  
  26.         print("========================")
  27.         time.sleep(4)
  28.         chipCount = 0
  29.        
  30. finally:  # unlock the i2c bus when ctrl-c'ing out of the loop
  31.     i2c.unlock()

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at