Issues running two custom I2C components (Adafruit DS3502)

I’m trying to run two I2C digipots, the Adafruit DS3502 breakout boards.

I’m using the Adafruit DS3502 Arduino library to create a custom component and declare the digipots.

The problem: only one digipot is setup, always the second one (0x29 or 41). Reading the logs, it seems there is some kind of race condition but I can’t get my hands on it.

Any idea? Thanks!

First logs (emphasis mine)

INFO Successfully uploaded program.
INFO Starting log output from /dev/ttyUSB0 with baud rate 115200
[23:03:02][I][logger:243]: Log initialized
[23:03:02][C][ota:465]: There have been 0 suspected unsuccessful boot attempts.
--> [23:03:02][I][custom:016]: Initializing <40>
--> [23:03:02][I][custom\xfe[I][custom:035]: Found DS3502 chip at address <41>
[23:03:02][D][fan:091]: 'PC Fan 1' - Sending state:
[23:03:02][D][fan:092]:   State: OFF
[23:03:02][D][fan:094]:   Speed: 0
[23:03:02][I][custom:040]: Setting wiper of <40> to <0>
[23:03:02][I][custom:043]: --> Wiper value of <40> is now <0>
[23:03:02][D][fan:091]: 'PC Fan 2' - Sending state:
[23:03:02][D][fan:092]:   State: OFF
[23:03:02][D][fan:094]:   Speed: 0
[23:03:02][I][custom:040]: Setting wiper of <41> to <0>
[23:03:02][I][custom:043]: --> Wiper value of <41> is now <0>

config.yaml

esphome:
 name: spacechimney
 libraries:
   - "Arduino"
   - "Wire"
   - "SPI"
   - "Adafruit BusIO"
   - "Adafruit DS3502"
 includes:
   - DS3502.h

#  [...]

fan:
 - platform: speed
   id: pc_fan1
   output: pc_fan_output1
   name: "PC Fan 1"
 - platform: speed
   id: pc_fan2
   output: pc_fan_output2
   name: "PC Fan 2"
 
output:
 - platform: custom
   type: float
   lambda: |-
     auto digipot1 = new DS3502_Component(0x28);
     auto digipot2 = new DS3502_Component(0x29);
     App.register_component(digipot1);
     App.register_component(digipot2);
     return {digipot1, digipot2};
   outputs:
     - id: pc_fan_output1
     - id: pc_fan_output2

DS3502.h

#include "esphome.h"
#include <Adafruit_DS3502.h>

#define WIPER_VALUE_PIN A0

class DS3502_Component : public Component, public FloatOutput
{
public:

    int I2C_Address;
    Adafruit_DS3502 ds3502;

    DS3502_Component(int address = DS3502_I2CADDR_DEFAULT) {
        I2C_Address = address;
        ds3502 = Adafruit_DS3502();
        ESP_LOGI("custom", "Initializing <%i>", I2C_Address);
    }

    void setup() override
    {
        ESP_LOGI("custom", "Setup for <%i>", I2C_Address);
        Serial.begin(115200);
        // Wait until serial port is opened
        while (!Serial)
        {
            delay(1);
            ESP_LOGI("custom", "<%i> waiting for serial...", I2C_Address);
        }

        if (!ds3502.begin(I2C_Address))
        {
            ESP_LOGI("custom", "Couldn't find DS3502 chip");
            while (1);
        }
        ESP_LOGI("custom", "Found DS3502 chip at address <%i>", I2C_Address);
    }

    void write_state(float state) override {
        int mappedState = map(int(state*100), 0, 100, 0, 127);
        ESP_LOGI("custom", "Setting wiper of <%i> to <%i>", I2C_Address, mappedState);
        ds3502.setWiper(mappedState);
        int wiperValue = ds3502.getWiper();
        ESP_LOGI("custom", "--> Wiper value of <%i> is now <%i>", I2C_Address, wiperValue);
    }
};

1 Like

Finally found the solution.

I needed to activate I2C in the config (to pull-up the pins) and remove the Serial.begin(115200) line in my custom code.

config.yaml

i2c:
  sda: 4
  scl: 5
  scan: false

DS3502.h

// ...
void setup() override
    {
        if (!ds3502.begin(I2C_Address))
        {
            ESP_LOGI("custom", "<0x%X> Couldn't find DS3502 chip", I2C_Address);
            while (1);
        }
        ESP_LOGI("custom", "<0x%X> Found DS3502 chip", I2C_Address);
    }
// ...
2 Likes

Thanks for doing this. Do you just add the ds3502.h file into /config/EspHome?

Anyone of you that got issues with this component on esphome 2024.1 and above? It has been working perfect for me on esphome 2023.12 and below but now it doesn’t even start. I can flash it without this component and then it starts again. Unfortunatly this device is not in my home so i haven’t been able to flash it through usb to see the errors.

Since the prior thread has been marked as solved, you should start a new thread. Then more people will see it.

1 Like

Have you see this:

https://esphome.io/custom/spi

Looks like custom ESP Home is deprecated