Page 1 of 1

ESP32 I2C randomly stops working

Posted: Sat Jan 18, 2025 7:28 pm
by Fazeli24
I've been trying to get 2 TCS34725 Color sensors to work, these pcb to be exact: https://www.az-delivery.de/en/products/ ... d_source=1

I use a ESP32 Node MCU I bought here: https://www.berrybase.de/esp32-nodemcu- ... d_source=1
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:
  1. Adafruit_TCS34725 leftColorSensor = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_180MS, TCS34725_GAIN_16X);
  2. TwoWire i2c_left = TwoWire(0);
  3. Adafruit_TCS34725 rightColorSensor = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_180MS, TCS34725_GAIN_16X);
  4. TwoWire i2c_right = TwoWire(1);
  5.  
  6. int blackThreshold = 300;
  7.  
  8. void setup() {
  9.   Serial.begin(115200);
  10.   pinMode(15, INPUT);
  11.   pinMode(14, INPUT);
  12.   pinMode(34, INPUT);
  13.   pinMode(33, INPUT);
  14.   pinMode(27, INPUT);
  15.  
  16.  
  17.   i2c_left.begin(21, 22, 100000);
  18.   i2c_right.begin(25, 13, 100000);
  19.   delay(500);
  20.   if (leftColorSensor.begin(0x29, &i2c_left)) {
  21.     Serial.println("Found sensor");
  22.   } else {
  23.     Serial.println("No TCS34725 found ... check your connections");
  24.     while (1);
  25.   }
  26.  
  27.   if (rightColorSensor.begin(0x29, &i2c_right)) {
  28.     Serial.println("Found sensor");
  29.   } else {
  30.     Serial.println("No TCS34725 found ... check your connections");
  31.     while (1);
  32.   }

Re: ESP32 I2C randomly stops working

Posted: Sun Jan 19, 2025 6:44 am
by horace99
try running https://wokwi.com/projects/350122233155289684 which scans Wire and Wire1 for I2C devices

Re: ESP32 I2C randomly stops working

Posted: Sun Jan 19, 2025 11:04 am
by horace99
I have one TCS34725 so could not carry out full test but running the following on an ESP32
  1. #include <Wire.h>
  2. #include "Adafruit_TCS34725.h"
  3.  
  4. Adafruit_TCS34725 leftColorSensor = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_180MS, TCS34725_GAIN_16X);
  5. TwoWire i2c_left = TwoWire(0);
  6. Adafruit_TCS34725 rightColorSensor = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_180MS, TCS34725_GAIN_16X);
  7. TwoWire i2c_right = TwoWire(1);
  8.  
  9. int blackThreshold = 300;
  10.  
  11. boolean leftOK = true, rightOK = true;
  12. void setup() {
  13.   Serial.begin(115200);
  14.   pinMode(15, INPUT);
  15.   pinMode(14, INPUT);
  16.   pinMode(34, INPUT);
  17.   pinMode(33, INPUT);
  18.   pinMode(27, INPUT);
  19.  
  20.  
  21.   i2c_left.begin(21, 22, 100000);
  22.   i2c_right.begin(25, 13, 100000);
  23.   delay(500);
  24.   if (leftColorSensor.begin(0x29, &i2c_left)) {
  25.     Serial.println("Found left sensor");
  26.   } else {
  27.     Serial.println("No left TCS34725 found ... check your connections");
  28.     leftOK = false;  //while (1);
  29.   }
  30.  
  31.   if (rightColorSensor.begin(0x29, &i2c_right)) {
  32.     Serial.println("Found right sensor");
  33.   } else {
  34.     Serial.println("No right TCS34725 found ... check your connections");
  35.     rightOK = false;  //while (1);
  36.   }
  37. }
  38.  
  39. void loop(void) {
  40.   uint16_t r, g, b, c, colorTemp, lux;
  41.   if (leftOK) {
  42.     leftColorSensor.getRawData(&r, &g, &b, &c);
  43.     // colorTemp = tcs.calculateColorTemperature(r, g, b);
  44.     colorTemp = leftColorSensor.calculateColorTemperature_dn40(r, g, b, c);
  45.     lux = leftColorSensor.calculateLux(r, g, b);
  46.  
  47.     Serial.print("left Color Temp: ");
  48.     Serial.print(colorTemp, DEC);
  49.     Serial.print(" K - ");
  50.     Serial.print("Lux: ");
  51.     Serial.print(lux, DEC);
  52.     Serial.print(" - ");
  53.     Serial.print("R: ");
  54.     Serial.print(r, DEC);
  55.     Serial.print(" ");
  56.     Serial.print("G: ");
  57.     Serial.print(g, DEC);
  58.     Serial.print(" ");
  59.     Serial.print("B: ");
  60.     Serial.print(b, DEC);
  61.     Serial.print(" ");
  62.     Serial.print("C: ");
  63.     Serial.print(c, DEC);
  64.     Serial.print(" ");
  65.     Serial.println(" ");
  66.   }
  67.   if (rightOK) {
  68.     rightColorSensor.getRawData(&r, &g, &b, &c);
  69.     // colorTemp = tcs.calculateColorTemperature(r, g, b);
  70.     colorTemp = rightColorSensor.calculateColorTemperature_dn40(r, g, b, c);
  71.     lux = rightColorSensor.calculateLux(r, g, b);
  72.  
  73.     Serial.print("right Color Temp: ");
  74.     Serial.print(colorTemp, DEC);
  75.     Serial.print(" K - ");
  76.     Serial.print("Lux: ");
  77.     Serial.print(lux, DEC);
  78.     Serial.print(" - ");
  79.     Serial.print("R: ");
  80.     Serial.print(r, DEC);
  81.     Serial.print(" ");
  82.     Serial.print("G: ");
  83.     Serial.print(g, DEC);
  84.     Serial.print(" ");
  85.     Serial.print("B: ");
  86.     Serial.print(b, DEC);
  87.     Serial.print(" ");
  88.     Serial.print("C: ");
  89.     Serial.print(c, DEC);
  90.     Serial.print(" ");
  91.     Serial.println(" ");
  92.   }
  93. }
TCS34725 connected to GPIO21 SDA and GPIO22 SCL (VCC to 3.3V) gave

Code: Select all

Found left sensor
No right TCS34725 found ... check your connections
left Color Temp: 3918 K - Lux: 390 - R: 642 G: 613 B: 503 C: 1300  
left Color Temp: 4162 K - Lux: 432 - R: 712 G: 693 B: 588 C: 1478  
left Color Temp: 4186 K - Lux: 448 - R: 743 G: 723 B: 617 C: 1543  
left Color Temp: 4141 K - Lux: 464 - R: 782 G: 753 B: 643 C: 1613  
left Color Temp: 4051 K - Lux: 431 - R: 714 G: 685 B: 571 C: 1490  
left Color Temp: 4033 K - Lux: 462 - R: 737 G: 707 B: 566 C: 1652  
left Color Temp: 4309 K - Lux: 1068 - R: 1632 G: 1610 B: 1288 C: 4206  
left Color Temp: 5038 K - Lux: 3917 - R: 4604 G: 5473 B: 4408 C: 14530  
left Color Temp: 5608 K - Lux: 7580 - R: 7718 G: 10352 B: 8544 C: 27312  
left Color Temp: 5590 K - Lux: 8622 - R: 8078 G: 11253 B: 8903 C: 28630  
left Color Temp: 4747 K - Lux: 10936 - R: 13057 G: 14949 B: 11503 C: 40660  
left Color Temp: 4523 K - Lux: 8350 - R: 11376 G: 11967 B: 9352 C: 34216  
left Color Temp: 3918 K - Lux: 4549 - R: 9195 G: 7603 B: 6101 C: 24387  
left Color Temp: 3366 K - Lux: 2732 - R: 8317 G: 5442 B: 4313 C: 18840  
left Color Temp: 3937 K - Lux: 5244 - R: 9815 G: 8383 B: 6559 C: 25313  
left Color Temp: 4369 K - Lux: 7323 - R: 10446 G: 10575 B: 8165 C: 29442  
left Color Temp: 4790 K - Lux: 9255 - R: 11302 G: 12865 B: 10085 C: 34861  
left Color Temp: 2972 K - Lux: 2637 - R: 9017 G: 5315 B: 3858 C: 17793  
TCS34725 connected to GPIO25 SDA and GPIO13 SCL gave

Code: Select all

No left TCS34725 found ... check your connections
Found right sensor
right Color Temp: 3840 K - Lux: 426 - R: 795 G: 721 B: 620 C: 1526  
right Color Temp: 3836 K - Lux: 432 - R: 803 G: 729 B: 625 C: 1545  
right Color Temp: 3833 K - Lux: 429 - R: 801 G: 726 B: 623 C: 1539  
right Color Temp: 3838 K - Lux: 428 - R: 798 G: 724 B: 622 C: 1531  
right Color Temp: 3845 K - Lux: 426 - R: 795 G: 722 B: 621 C: 1526  
right Color Temp: 3840 K - Lux: 426 - R: 796 G: 722 B: 621 C: 1527  
right Color Temp: 7523 K - Lux: 8046 - R: 5180 G: 10030 B: 8338 C: 24398  
right Color Temp: 6638 K - Lux: 11877 - R: 8103 G: 14367 B: 11160 C: 34891  
right Color Temp: 6964 K - Lux: 18521 - R: 12171 G: 22494 B: 17804 C: 54352  
right Color Temp: 6337 K - Lux: 14565 - R: 11338 G: 18386 B: 14719 C: 45763  
right Color Temp: 5725 K - Lux: 12307 - R: 10771 G: 15695 B: 12253 C: 39822  
right Color Temp: 5324 K - Lux: 10189 - R: 9795 G: 13160 B: 10113 C: 33899  
right Color Temp: 5376 K - Lux: 10697 - R: 10389 G: 13954 B: 10868 C: 36143  
right Color Temp: 5096 K - Lux: 14725 - R: 14843 G: 19077 B: 14436 C: 49198  
right Color Temp: 4199 K - Lux: 8809 - R: 11393 G: 11819 B: 8398 C: 31603  
right Color Temp: 5237 K - Lux: 12093 - R: 11913 G: 15690 B: 12028 C: 40372  
right Color Temp: 5160 K - Lux: 11412 - R: 11795 G: 15068 B: 11669 C: 39250  
right Color Temp: 5160 K - Lux: 11412 - R: 11795 G: 15068 B: 11669 C: 39250  
right Color Temp: 4935 K - Lux: 11054 - R: 12811 G: 15166 B: 11919 C: 40665  
right Color Temp: 4708 K - Lux: 10145 - R: 12826 G: 14245 B: 11168 C: 38672  
right Color Temp: 4361 K - Lux: 9094 - R: 13136 G: 13214 B: 10243 C: 36838  
right Color Temp: 5027 K - Lux: 11236 - R: 13247 G: 15707 B: 12644 C: 42348  
right Color Temp: 5121 K - Lux: 11741 - R: 13403 G: 16281 B: 13123 C: 43616  
right Color Temp: 5131 K - Lux: 10357 - R: 11655 G: 14265 B: 11442 C: 37878  
did not attach pullups - there appear to be pullups on the TCS34725 PCB
what else do you have attached to the ESP32? could be having power supply problems