My problem is, that the I2C connection is very unreliable. I'm using 4.7k ohm PullUps on both SDA and SCL. I also use the Adafruit TCS34725 library. Because I can't change the sensor I2C adress, I use two i2c buses on the ESP32. But my problem is that it doesn't recognise the sensors when I initialise both of them at the same time. It also only works sometimes when I use only one sensor, and don't initialise the second. I checked all the sensors and connections multiple times, but can't find the issue. The SDA and SCL cables are ~15cm each.
Here is part of my code:
Adafruit_TCS34725 leftColorSensor = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_180MS, TCS34725_GAIN_16X);
TwoWire i2c_left = TwoWire(0);
Adafruit_TCS34725 rightColorSensor = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_180MS, TCS34725_GAIN_16X);
TwoWire i2c_right = TwoWire(1);
int blackThreshold = 300;
void setup() {
Serial.begin(115200);
pinMode(15, INPUT);
pinMode(14, INPUT);
pinMode(34, INPUT);
pinMode(33, INPUT);
pinMode(27, INPUT);
i2c_left.begin(21, 22, 100000);
i2c_right.begin(25, 13, 100000);
delay(500);
if (leftColorSensor.begin(0x29, &i2c_left)) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1);
}
if (rightColorSensor.begin(0x29, &i2c_right)) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1);
}
And yet you failed to mention that. What else have you tried that people might reasonably ask about because you didn't say that you'd tried it?
For example, have you tried looking at the return values from the I2C begin methods? And if you did, what did you see? That would seem to be an obvious thing to look at it, and yet you've made no mention of it.
I tried it after you mentioned it, I didn't know there was a dedicated Wire1 object. I'm quite the newbie when it comes to I2C, I don't know how to debug it properly. I will look into it now. Thanks for the suggestion
Okay, I enabled Error debug mode in the Arduino IDE, and get this error: "[ 521][E][Wire.cpp:449] endTransmission(): NULL TX buffer pointer". I still have to find out what it means, but it's something atleast. i2c_left.begin(21, 22, 100000); returns 1, again, I have to find out what that means(probably "true", right?). I will also try to lower the pullups resistance, as ledsyn suggested.
ah, never mind, my mistake. I haven't passed the i2c_left object in the begin() function. I don't get any error message now, except the "No TCS34725 found ... check your connections" message.
For example, have you tried looking at the return values from the I2C begin methods? And if you did, what did you see? That would seem to be an obvious thing to look at it, and yet you've made no mention of it.
I know this is asking much, but could you maybe explain how I can get the return values?
Haha, yes, I only had yellow and blue cables left, and blue is for GND. Already ordered new ones. Maybe a beginner question, but should I use higher or lower resistance pull ups to counter interference? I will try them tomorrow. Thanks for your help
Small Edit: the weird behaving sensor just shorted. It got really hot and released some of it's magic smoke. Luckily I have 3 more sensors laying around. Hopefully it was "just" a defective PCB. I will test it tomorrow. My other guess is, that it can't be powered via 3.3V directly. It has a 5v to 3.3v regulator, but the datasheet says, it can also be directly powered via 3.3V. But I haven't seen anyone power it with that, they all use 5V, so that could be the problem.
Lower resistance, to combat interference. Another thing to consider is to lower the frequency. You prolly runs it at 400 kHz now, and 100 kHz will maybe be enough too. You have to experiement.
If they take 5 V but regulate to 3.3 V it should be ok. Weird that it release the magic smoke. Double check your wirings.
Well, after testing a new sensor, this time using 5V as input, it works very well. I also had switch from Pins 21 and 22 to 19 and 18, not sure why the others won't work, but at least I have a solution. Thanks for all your help tho.