This page shows how to perform an I2C scan to demonstrate how to use the LTC4316 for with Arduino. You'll connect two AHT20 sensors, which have the same I2C address (0x38), to experiment with translating the address for one of them. Remember that you need to restart the LTC4316 every time you adjust the DIP switch.
Then, you'll use the Adafruit_AHTx0 library to use two AHT20 sensors, with the help of the LTC4316, to read temperature and humidity data.
Remember that you need to restart the LTC4316 every time you adjust the DIP switch.
Wiring
Wire as shown for a 5V board like an Uno. If you are using a 3V board, like an Adafruit Feather, wire the board's 3V pin to the breakout VIN.
Here is an Adafruit Metro wired up to the two AHT20 sensors and the LTC4316 using the STEMMA QT connector:
-
Board 5V to sensor 1 VIN (red wire)
-
Board GND to sensor 1 GND (black wire)
-
Board SCL to sensor 1 SCL (yellow wire)
- Board SDA to sensor 1 SDA (blue wire)
- Sensor 1 VIN to LTC4316 VIN (red wire)
- Sensor 1 GND to LTC4316 GND (black wire)
- Sensor 1 SCL to LTC4316 SCI (yellow wire)
- Sensor 1 SDA to LTC4316 SDI (blue wire)
- LTC4316 VIN to sensor 2 VIN (red wire)
- LTC4316 GND to sensor 2 GND to (black wire)
- LTC4316 SCO to sensor 2 SCL (yellow wire)
- LTC4316 SDO to sensor 2 SDA (blue wire)
Here is an Adafruit Metro wired up using a solderless breadboard:
-
Board 5V to sensor 1 VIN (red wire)
-
Board GND to sensor 1 GND (black wire)
-
Board SCL to sensor 1 SCL (yellow wire)
- Board SDA to sensor 1 SDA (blue wire)
- Sensor 1 VIN to LTC4316 VIN (red wire)
- Sensor 1 GND to LTC4316 GND (black wire)
- Sensor 1 SCL to LTC4316 SCI (yellow wire)
- Sensor 1 SDA to LTC4316 SDI (blue wire)
- LTC4316 VIN to sensor 2 VIN (red wire)
- LTC4316 GND to sensor 2 GND to (black wire)
- LTC4316 SCO to sensor 2 SCL (yellow wire)
- LTC4316 SDO to sensor 2 SDA (blue wire)
There are no additional libraries needed for the I2C scan.
// SPDX-FileCopyrightText: 2023 Carter Nelson for Adafruit Industries // // SPDX-License-Identifier: MIT // -------------------------------------- // i2c_scanner // // Modified from https://playground.arduino.cc/Main/I2cScanner/ // -------------------------------------- #include <Wire.h> // Set I2C bus to use: Wire, Wire1, etc. #define WIRE Wire void setup() { WIRE.begin(); Serial.begin(9600); while (!Serial) delay(10); Serial.println("\nI2C Scanner"); } void loop() { byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; for(address = 1; address < 127; address++ ) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the address. WIRE.beginTransmission(address); error = WIRE.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address<16) Serial.print("0"); Serial.print(address,HEX); Serial.println(" !"); nDevices++; } else if (error==4) { Serial.print("Unknown error at address 0x"); if (address<16) Serial.print("0"); Serial.println(address,HEX); } } if (nDevices == 0) Serial.println("No I2C devices found\n"); else Serial.println("done\n"); delay(5000); // wait 5 seconds for next scan }
Both AHT20 sensors are on address 0x38. The sensor on the input side of the LTC4316 will remain on 0x38, but the sensor on the output side of the LTC4316 will be translated depending on the position of the DIP switches.
To start, setup the LTC4316 with both the A4 and A5 switches off. Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 9600 baud. You'll see this result in the Serial Monitor:
Set switch A5 to on and power cycle the board. Now you'll see these addresses in the Serial Monitor:
Set switch A5 to off and A4 to on. Power cycle the board and you'll see these addresses in the Serial Monitor:
Finally, set both A5 and A4 to on and power cycle the board. In this iteration, sensor 2's translated address is 0x78, which is a reserved I2C address and is usually not used. However, it does show up in with this scan:
Now that you've run the I2C scan test, you can run an example for both of the AHT20 sensors.
Library Installation
You can install the Adafruit AHTX0 library for Arduino using the Library Manager in the Arduino IDE.
Click the Manage Libraries... menu item, search for Adafruit AHTX0, and select the Adafruit AHTX0 library:
If asked about dependencies, click "Install all".
If the "Dependencies" window does not come up, then you already have the dependencies installed.
If the dependencies are already installed, you must make sure you update them through the Arduino Library Manager before loading the example!
AHT20 Example
For this example, you'll have the LTC4315 switch A5 on and switch A4 off. This will give the translated AHT20 sensor an I2C address of 0x68.
// SPDX-FileCopyrightText: 2023 Carter Nelson for Adafruit Industries // // SPDX-License-Identifier: MIT // LTC4315 with two AHT20 sensors example // Set the LTC4315 switch A5 on and switch A4 off // The translated sensor will be on address 0x68 #include <Adafruit_AHTX0.h> Adafruit_AHTX0 default_aht; Adafruit_AHTX0 ltc4316_aht; void setup() { Serial.begin(115200); while ( !Serial ) delay(10); Serial.println("Adafruit AHT20 with LTC4316 demo!"); if (! default_aht.begin(&Wire, 0, 0x38)) { Serial.println("Could not find AHT20 on 0x38? Check wiring."); while (1) delay(10); } if (! ltc4316_aht.begin(&Wire, 0, 0x68)) { Serial.println("Could not find AHT20 attached to LTC4316 on 0x68? Check wiring and switches"); while (1) delay(10); } Serial.println("AHT20 sensors found on 0x38 and 0x68"); } void loop() { sensors_event_t humidity0, temp0; sensors_event_t humidity1, temp1; default_aht.getEvent(&humidity0, &temp0);// populate temp and humidity objects with fresh data ltc4316_aht.getEvent(&humidity1, &temp1);// populate temp and humidity objects with fresh data Serial.println("AHT20 on 0x38:"); Serial.print("Temperature: "); Serial.print(temp0.temperature); Serial.println(" degrees C"); Serial.print("Humidity: "); Serial.print(humidity0.relative_humidity); Serial.println("% rH"); Serial.println(); Serial.println("AHT20 on 0x68:"); Serial.print("Temperature: "); Serial.print(temp1.temperature); Serial.println(" degrees C"); Serial.print("Humidity: "); Serial.print(humidity1.relative_humidity); Serial.println("% rH"); Serial.println(); delay(500); }
Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. You'll see the two AHT20 sensors recognized over I2C. The first is on its default address of 0x38. The second is on the translated address of 0x68. Then, temperature and humidity readings from both sensors will be printed to the Serial Monitor.
Text editor powered by tinymce.