The Raspberry Pi Pico series has 2 independent I2C channels. Which is great because I want to run 2x separate AHT21 temperature & humidity sensors to my board without using a multiplexer. (To be clear, I also can't quite grasp how to do this with a multiplexer either, because I will run into all of the same problems below). I'm using the Adafruit_AHTX0 library to connect & query the sensors. Also, I couldn't get my hands on a Pico 2W yet, so I'm using the Pimoroni Pico Plus 2 until the RPi Pico 2W arrives - just to be totally clear about the HW setup.
The biggest issue I'm having is using the second AHT21 on I2C-1.
So, everything works fine with a single sensor (AHT21 on I2C-0 - I'll call this "AHT21-0"). The major issue was that I needed to find the default pins used for I2C, in this case GPIO4 for SDA-0 & GPIO5 for SCL-0. I was able to try all of the pins combos using the I2C_Scan example from the Adafruit Testbed library. When I finally got a combo that registered, we were golden. After that, the adafruit_aht_test example worked flawlessly & I was able to adapt the code into my own sketch. (Hopefully this all helps some newcomer to the RP2350 & I2C scene - there is not a lot of info out there yet to help with these peculiarities).
OUTPUT OF I2C_Scan:
Default port (Wire) I2C scan: 0x38,
Secondary port (Wire1) I2C scan:,
OUTPUT OF adafruit_aht_test:
Temperature: 18.69 degrees C
Humidity: 55.66% rH
Great! So, now I repeat the process all all of the combos of I2C-1 pins. & I discover that for I2C-1, GPIO6 is used for SDA-1 & GPIO7 for SCL-1. Great! It's all recognized as the "Secondary port"
OUTPUT OF I2C_Scan:
Default port (Wire) I2C scan:,
Secondary port (Wire1) I2C scan: 0x38
The problem is that when I now try to use adafruit_aht_test, it doesn't recognize / find the sensor.
OUTPUT OF adafruit_aht_test:
Nothing.
Getting my sensor to work on I2C-1 is the first problem. Once I do get it working, I'll also need to figure out how to query them separately, or switch between them... But I figure it I can get it working on I2C-1, then that might actually answer the second question. The fact that I can connect to both simultaneously using the I2C_Scan sketch s promising.
OUTPUT OF I2C_Scan (with 2x AHT21 sensors connected):
Default port (Wire) I2C scan: 0x38,
Secondary port (Wire1) I2C scan: 0x38,
I'm not a comms expert, and I'd rather not build my own libraries, header files, etc - I'd love to use the existing libraries as is so that I can benefit from updates, don't need to maintain them, others are much smarter than me, etc. So, I'd REALLY like to avoid writing my own AHT21 parser / comms program.
Any help would be GREATLY appreciated! Thank you all SOOOO MUCH.
All of my code (currently comparing many different temp sensors - very hacky exploration-mode code before I tighten it up) is below:
#include "DHT.h"
#define DHT11PIN 15
DHT dht(DHT11PIN, DHT11);
#include <Adafruit_AHTX0.h>
Adafruit_AHTX0 aht0;
//Adafruit_AHTX0 aht1; // Trying to follow https://forum.arduino.cc/t/connecting-two-aht21b-sensors-to-i2c-and-serial-printing-it/1252379/6
int led = 25; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// Temp Voltage Divider
float Vmax = 3.3;
int ADC_steps = 1024;
float R0 = 82;
float RT = 0;
uint16_t A0_raw = 0;
uint16_t A1_raw = 0;
float T0 = 0;
float T1 = 0;
//float V_A0 = 0;
void setup() {
Serial.begin(115200);
pinMode(led, OUTPUT);
pinMode(23, OUTPUT);
digitalWrite(23, 1);
if (! aht.begin()) {
Serial.println("Could not find AHT? Check wiring");
while (1) delay(10);
}
Serial.println("AHT10 or AHT20 found");
//delay(500);
}
void loop() {
//Serial.printf("Core temperature: %2.1fC\n", analogReadTemp());
// LM335Z Temp Sensor
A0_raw = analogRead(A0);
Serial.print(String(A0_raw) + " raw A0, ");
T0 = (float)A0_raw * (3300/1024) / 10 - 273 + 17; // +17ÂșC to match temps
Serial.print(String(T0) + "ÂșC T0, ");
/*
//Serial.print(String(A0_raw) + ",");
//V_A0 = A0_raw * Vmax/ADC_steps;
//Serial.print(String(V_A0) + ",");
//RT = R0*V_A0/(Vmax-V_A0);
//Serial.print(String(RT) + ",");
//T = 30 + (RT - 100)*10/2.168;
*/
//
// DF Robotics LM53 Temp Sensor
A1_raw = analogRead(A1);
Serial.print(String(A1_raw) + " raw A1, ");
T1 = A1_raw * 5 / 10.24 - 15; // -15ÂșC to match temps
Serial.print(String(T1) + "ÂșC T1, ");
//
//DHT11 Temp Sensor
float t = dht.readTemperature();
float h = dht.readHumidity();
Serial.print(t, 0);
Serial.print("ÂșC DHT11, ");
Serial.print(h, 0);
Serial.print("% RH DHT11, ");
//
// AHT21 Temp Sensor
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
Serial.print(String(temp.temperature) + "ÂșC AHT21, ");
Serial.print(String(humidity.relative_humidity) + "% RH AHT21, ");
//
// Built-in RP2350 CPU Temp
Serial.print(String(analogReadTemp()) + "ÂșC CPU");
//
// set the brightness of built-in LED:
brightness = abs((long)(millis()/5) % 512 - 256);
analogWrite(led, brightness);
// Old brightness code
/*
// change the brightness for next time through the loop:
//brightness = brightness + fadeAmount;
reverse the direction of the fading at the ends of the fade:
//if (brightness <= 0 || brightness >= 100) {
// fadeAmount = -fadeAmount;
//}
// wait for 30 milliseconds to see the dimming effect
*/
//
Serial.println();
delay(100);
}