-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathI2C_scanner.lua
More file actions
29 lines (25 loc) · 853 Bytes
/
Copy pathI2C_scanner.lua
File metadata and controls
29 lines (25 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-- Based on work by zeroday & sancho among many other open source authors
-- This code is public domain, attribution to gareth@l0l.org.uk appreciated.
id=0 -- need this to identify (software) IC2 bus?
sda=5 -- connect to pin GPIO0
scl=6 -- connect to pin GPIO2
-- initialize i2c with our id and pins in slow mode :-)
i2c.setup(id,sda,scl,i2c.SLOW)
-- user defined function: read from reg_addr content of dev_addr
function read_reg(dev_addr, reg_addr)
i2c.start(id)
i2c.address(id, dev_addr ,i2c.TRANSMITTER)
i2c.write(id,reg_addr)
i2c.stop(id)
i2c.start(id)
i2c.address(id, dev_addr,i2c.RECEIVER)
c=i2c.read(id,1)
i2c.stop(id)
return c
end
print("Scanning I2C Bus")
for i=0,127 do
if (string.byte(read_reg(i, 0))==0) then
print("Device found at address "..string.format("%02X",i))
end
end