Skip to content

Commit d664a80

Browse files
committed
i2c scan example added
1 parent ea6033e commit d664a80

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

examples/03-i2c-scan/i2c_scan.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
I2C scanner
3+
4+
Scan the I2C (Inter-Integrated Circuit) bus for connected
5+
devices and print their addresses. This script is useful
6+
for identifying I2C devices connected to your microcontroller.
7+
8+
Authors:
9+
- MicroPython
10+
- Tomas Fryza
11+
12+
Creation date: 2023-06-17
13+
Last modified: 2024-11-02
14+
"""
15+
16+
from machine import I2C
17+
from machine import Pin
18+
19+
# Init I2C using pins GP22 & GP21 (default I2C0 pins)
20+
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=100_000)
21+
22+
print("Scanning I2C... ", end="")
23+
addrs = i2c.scan()
24+
print(f"{len(addrs)} device(s) detected")
25+
26+
print("dec.\t hex.")
27+
for addr in addrs:
28+
print(f"{addr}\t {hex(addr)}")

0 commit comments

Comments
 (0)