Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By trackerj
#53891 Updated code to reflect the latest great I2C feature, the i2c.setup() function !

Will change the default pins for the i2c interface.
i2c.setup({SDA}, {SCL})

Now you can define your I2C bus pins as you wish!

Code: Select alli2c.setup(4,5)

for address = 1 to 127
 i2c.begin(address)
 stat = i2c.end()

 if stat < 1 then
  'print stat
  wprint "Found I2C device at address: 0x" & hex(address)
  wprint " - > " & address
  wprint " <br>"
 endif

next

wait


Also updated the ESPBasic i2c_scanner() example article to reflect the new great feature.
User avatar
By russm
#56877 Don't want to take too far off topic but I tried this to find my Wii Nunchuck and no joy. Do I need pullups on the default GPIO00 and GPIO02? Any help would be great. TIA
User avatar
By russm
#56884 I figured it out. Scanner worked fine, identified Wii Nunchuck as address 82. Here is code to read the 6 bytes. Now to put them together correctly to do something with them.

Code: Select allmemclear

let address = 82  '82 which is what was scanned
i2c.setup(D3,D4)
i2c.begin(82)
i2c.write(64)
i2c.write(0)

i2c.end()
'delay 10

i2c.begin(82)
i2c.write(0)
i2c.end()
i2c.requestfrom(82,6)
b1 = (val(i2c.read()) xor 23) + 23
b2 = (val(i2c.read()) xor 23) + 23
b3 = i2c.read()
b4 = i2c.read()
b5 = i2c.read()
b6 = i2c.read()

print b1
print b2
print b3
print b4
print b5
print b6