I need to take readings from two of these sensors and unfortunately there is no facility for an alternative I2C address on them so . . .
Initially I tried connecting the devices independently and had a separate I2CSETUP command for accessing each one.
This approach did not work on the board I'm using (a TTGO Display board with the display removed) for reasons I've yet to fathom. I could only get GPIO 21 and 22 to work with I2C.
So plan B was to use a common sda and scl and power on each AM2320 individually using a GPIO pin.
The GPIO pins on an ESP32 can source 40mA, and a high output is very close to the supply voltage although this would drop at as current increases. The AM2320 takes under 1mA peak, so no problem there. I've assigned one GPIO pin each to connect to the Vcc of each AM2320.
It's OK to use just one pair of I2C bus pullup resistors (4.3K to 10K) on the ESP32 side and take the common of these to the 3V3 supply.
This approach also has the advantage in an application where you're trying to minimise supply current; you're only powering on a device when you need it.
As well as the temperature and humidity readings from the AM2320 I'll be using a BMP280 for atmospheric pressure readings. I'll connect this in the same way with its own supply, although I could just twin it up with one of the other sensors as they are at different addresses.
Here's example code for what I'm doing. The AM2320 read routine is the the code I posted here some time ago.
Code: [Local Link Removed for Guests]
pause 1000
pin.mode 25, output ' GPIO25 connects to Vcc of 1st AM2320
pin.mode 26, output ' GPIO26 connects to Vcc of 2nd AM2320
pin(25) = 0
pin(26) = 0
'wlog BME280.SETUP(&h76)
iobuff.dim (1, 8) ' Initialise io buffer to accept received data from AM2320
i2c.setup 21,22
wlog "Start"
do
pin(25) = 1 ' Power on 1st device
pin(26) = 0 ' Power off 2nd device
wlog "Int"
gosub read2320
pin(25) = 0 ' Power off 1st device
pin(26) = 1 ' Power on 2nd device
wlog "Ext"
gosub read2320
'gosub readBME280
loop
Read2320:
pause 1500
I2C.BEGIN &h5C ' First write wakes up AM2320
z = i2c.end
I2C.BEGIN &h5C ' Write to device to start measurement
I2C.WRITE &h03
I2C.WRITE &h00
I2C.WRITE &h04
if i2c.end <> 0 then wlog "Device Error"' Check for acknowledgement
pause 2200 ' Delay to allow measurement to complete
I2C.READ_IOBUFF(1), &H5C, 0, 8 ' Check for 8 bytes returned by device
if iobuff.len(1) = 8 then
Hum = ((iobuff.read(1,2) * 256) + iobuff.read(1,3)) /10
if iobuff.read(1,4) > &H7F then ' Test for top bit of temperature byte set which indicates negative value
Tmp = -(((iobuff.read(1,4) and &H7F) * 256) + iobuff.read(1,5)) /10 ' Mask top bit and make negative if set.
else
Tmp = ((iobuff.read(1,4) * 256) + iobuff.read(1,5)) /10
end if
wlog "AM2320",Tmp, Hum
else
wlog "Invalid device read operation"
end if
return
ReadBME280:
wlog "BME280", bme280.temp, bme280.qfe + 21
return