I2c devices cabled to the external GPIO

Has anyone tinkered with talking to i2c devices cabled to the external GPIO port?

Any success? How’d you do it?

Digging in my Arduino experiment part pile, I found a GY-65 module for measuring temperature and pressure. I wired up…

GY-65   PicoCalc
VCC --> 3V3
SDA --> GP4
SCL --> GP5
GND --> GND

and then tried something like this to ask for temperature:

DIM ARRAY(2)
SETPIN GP4, GP5, I2C
I2C OPEN 100, 500
I2C WRITE &hEE, 0 , 2, &hF4, &h2E
PAUSE 200
I2C READ &HEF, 0,2,ARRAY()
PRINT "ARRAY(0): ", ARRAY(0)
PRINT "ARRAY(1): ", ARRAY(1)

…but no luck. I am not even sure I have the correct module address at this point, and think I might put the sensor onto an Arduino to make sure there’s something there!

1 Like

Okay, something about needing pullup resistors. Not sure if the SETPIN sda,scl, I2C command turns on internal pullups. Is there a separate command to turn them on? Or do I need to provide discrete component resistors?

Oh, also…

The command I2C CHECK addr can be used to check if a device is present at the address ‘addr’. This will set
the read only variable MM.I2C to 0 if a device responds or 1 if there is no response.

So it’s possible to walk to walk an I2C bus, looking for devices?

Is this what you are looking for?

2 Likes

Managed to get I2C working, talking to an Adafruit AHT20 temperature and humidity sensor.

Kinda neat: you throw some bytes out the port, read some bytes coming back in, then interpret what you get.

Dug around in some arduino library code and stared at the sensor datasheet to see how it worked.

Arduino and CircuitPython are definitely easier, using the prebuilt libraries…


PicoCalc Basic version:

' AHT20 Temperature/Humidity sensor

' set i2c pins, bus kHz, timeout mS
SetPin GP4,GP5,I2C
I2C OPEN 100,500
Pause 100

' I2C address of Adafruit AHT20 module
ADDR = &h38

' Get status, initialize if needed

' Trigger measurement
Sub trigger_measurement()
  I2C WRITE ADDR, 0, 3, &hAC, &h33, &h00
  Pause 100
End Sub

' Get status
Sub get_status()
  Local status, ready
  I2C WRITE &h38, 0, 1, &h71
  I2C READ  &h38, 0, 1, status
  Print "status: " status
End Sub

' Get measurements
Sub get_results()
  I2C WRITE &h38, 0, 1, &h71
  I2C READ  &h38, 0, 6, a()
End Sub

Sub print_results
  ' See Arduino code on GitHub at
  ' Seeed_Arduino_AHT20/sc/AHT20.cpp
  S_rh = a(1)<<12 Or a(2)<<4 Or a(3)>>4
  S_t = (a(3)And&hF)<<16 Or a(4)<<8 Or a(5)

  ' See www.aosong.com Data_Sheet_AHT20
  RH = (S_rh/2^20)*100
  T  = (S_t/2^20) * 200 - 50

  Print "Relative Humidity: " Format$(RH,"%2.1f") "%"
  Print "Temperature: " Format$(T,"%2.1f") "C"
End Sub

Dim a(5)
Do
  trigger_measurement()
  get_results
  print_results
  Pause 2000
Loop
3 Likes