D1 Mini with PCA9685 for LED Dimming

I’ve been experimenting with the above setup and wanted to piece things bit by bit to make sure I get each piece working properly since I’m a total newb with circuitry. The ultimate goal is to be able to control a set of lights and this little project would be a dumbed-down version of this guide.

I’m starting out with the Wemos D1 Mini that connects to the PC9685. From the D1 mini pinouts, the GPIO 4 and 5 are connected to the SDA and SCL pins on the PCA9685 (as well as power and ground).

After reading up on some ESPHOME docs and seeing some of the posts here, my sketch currently consists of:

i2c:
  sda: 4
  scl: 5
  scan: true
  frequency: 10000

pca9685:
    id: 'pca9685_hub'
    frequency: 500
    address : 0x40

# Individual outputs
output:
  - platform: pca9685
    pca9685_id: 'pca9685_hub'
    id: "PWM1"
    channel: 1
  - platform: pca9685
    pca9685_id: 'pca9685_hub'
    id: "PWM2"
    channel: 2

light:
  - platform: monochromatic
    name: "Reef Blue"
    output: PWM1
  - platform: monochromatic
    name: "Reef White"
    output: PWM2

When I fire things up, I get the log below:

[11:52:46][C][i2c.arduino:039]:   SDA Pin: GPIO4
[11:52:46][C][i2c.arduino:040]:   SCL Pin: GPIO5
[11:52:46][C][i2c.arduino:041]:   Frequency: 10000 Hz
[11:52:46][C][i2c.arduino:044]:   Recovery: bus successfully recovered
[11:52:46][I][i2c.arduino:054]: Results from i2c bus scan:
[11:52:46][I][i2c.arduino:060]: Found i2c device at address 0x27
[11:52:46][I][i2c.arduino:060]: Found i2c device at address 0x28
[11:52:46][I][i2c.arduino:060]: Found i2c device at address 0x29
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x2B
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x2C
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x2D
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x2E
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x2F
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x30
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x31
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x32
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x33
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x34
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x35
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x36
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x37
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x38
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x39
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x3A
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x3B
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x3C
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x3D
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x3E
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x3F
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x40
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x41
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x42
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x43
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x44
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x45
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x46
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x47
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x48
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x49
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x4A
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x4B
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x4C
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x4D
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x4E
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x4F
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x50
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x51
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x52
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x53
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x54
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x55
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x56
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x57
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x58
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x59
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x5A
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x5B
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x5C
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x5D
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x5E
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x5F
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x60
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x61
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x62
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x63
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x64
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x65
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x66
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x67
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x68
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x69
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x6A
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x6B
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x6C
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x6D
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x6E
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x6F
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x70
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x71
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x72
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x73
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x74
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x75
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x76
[11:52:46][E][i2c.arduino:062]: Unknown error at address 0x77
[11:52:46][C][pca9685:079]: PCA9685:
[11:52:46][C][pca9685:080]:   Mode: 0x0C
[11:52:46][C][pca9685:081]:   Frequency: 500 Hz
[11:52:46][E][pca9685:083]: Setting up PCA9685 failed!
[11:52:46][C][light:104]: Light 'Reef Blue'
[11:52:46][C][light:106]:   Default Transition Length: 1.0s
[11:52:46][C][light:107]:   Gamma Correct: 2.80
[11:52:46][C][light:104]: Light 'Reef White'
[11:52:46][C][light:106]:   Default Transition Length: 1.0s
[11:52:46][C][light:107]:   Gamma Correct: 2.80

A couple of questions:
(1) I currently have no light entity after setting this up. I assume it is because of the line ‘Setting up PCA9685 failed!’?
(2) Why is the above failing? What’s wrong with the PCA9685 portion of the code above? Seems others have been able to get by with this, what’s wrong on my end?
(3) Under the pca9685 portion of the code, I set the address parameter for 0x40. Is that supposed to detect something in the log? I see that it says Unknown error at address 0x40, does that mean it’s not finding the pca9685 on 0x40, hence the issue with item (1) above?
(4) Lastly, it does find i2c devices, but they are at 0x27, 0x28 and 0x29. It seems to be that the scans results in different things every so often. I’ve come across a scan that shows errors in all addresses except for 16 of them (and they weren’t all consecutive), so at first I thought it was for each of the 16 channels on the PCA9685. Sometimes it starts up and all addresses are in error. Why does this seem so random?

The log only tells you if something was or was not found at address 40. In fact, you can run it once without the address: line and find your address that way. (Same with the Dallas one-wire devices).

I am unfamiliar with the PCA9685 (it would have been nice of you to provide a link). The pullup resistors on SCL and SDA on the board are weak. I would like to see 4K7 there. Try putting another 10K there to make the pullup stronger. This could be causing random I2C data.

The weak, 10K pullups may be because Adafruit anticipates that a user would have two or even three boards on the I2C bus, and if they used a 4K7 resistor on each, then the effective 2K may be too “strong”. The I2C bus works by pulling the data and clock lines low, and if the pullup resistor is too small the GPIO port or device driver transistor may not be able to pull Vcc to a logic low. Similarly if the pullup resistors are too weak then the data and clock lines may not reliably go to a logic high.

Here is the formula for the exact pullup values, but most users have success with 4K7.
i2c

PCA9685 info is here

I can give the extra resistor a try. That is, connecting it in series from the SDA and SCL from the D1 to the PCA9685, right?

Ok, completely user error on my part. The power from the D1 mini wasn’t connecting properly to the PCA9685 but once that was fixed the address 0x40 shows up in the i2c scan and the pca9685 fail was gone.

However, the light entity does not appear to be created. I’m expecting the light entity to be called light.reef_blue and light.reef_white but I don’t see it in HA.

Edit: ok now that there was no errors, finally figured out I wasn’t seeing this little project in the esphome integration with all my other devices. Once I added that in, the light entities showed up.

No. A pullup resistor goes from the SDA wire to the +V of the processor.

With everything hooked up … I thought I’d post this code here in case anyone comes looking in the future.

i2c:
  sda: 4
  scl: 5
  scan: true
  frequency: 10000

pca9685:
    id: 'pca9685_hub'
    frequency: 1500
    address: 0x40

# Individual outputs
output:
  - platform: pca9685
    pca9685_id: 'pca9685_hub'
    id: "PWM0"
    channel: 0
    inverted: true
  - platform: pca9685
    pca9685_id: 'pca9685_hub'
    id: "PWM1"
    channel: 1
    inverted: true

light:
  - platform: monochromatic
    name: "Reef Blue"
    output: PWM0
  - platform: monochromatic
    name: "Reef White"
    output: PWM1

The only thing that was unexpected was that when the lights were controlled off within HA, the output voltage was at its max 10V, but when controlled on (again within HA), the output voltage was at 0 V. The simple solution was to put in an invert command on the outputs and everything worked perfectly.

However, I’m not sure if there are any implications of doing this? Since the software “invert” was necessary, does that mean the lights will fail in the ‘on’ position if the controller craps out? or any other kind of hardware failure?