Pca9685 Phase_begin Is Always Set To 0000

by ADMIN 42 views

The Problem

The calculation of phase_begin in line 105 of file pca9685_output.cpp always results to 0. This issue is causing problems with the pca9685 component in ESPHome, leading to incorrect phase_begin values.

Reasoning

The issue lies in the calculation of phase_begin. The formula used is:

uint16_t phase_begin = uint16_t(channel - this->min_channel_) / num_channels * 4096;

Here, uint16_t(channel - this->min_channel_) is a value which is always smaller than num_channels. Hence the integer division results to a value of 0, and the subsequent multiplication of 4096 still results to 0.

Solution

To fix this issue, we need to modify the formula to ensure that the division does not result in 0. We can do this by rewriting the formula as:

uint16_t phase_begin = uint16_t(channel - this->min_channel_) * (4096 / num_channels);

Alternatively, we can introduce a new constant before the for-loop:

const uint16_t newfactor = 4096 / num_channels;

And then use this constant within the for-loop:

uint16_t phase_begin = uint16_t(channel - this->min_channel_) * newfactor;

Which Version of ESPHome Has the Issue?

The issue is present in ESPHome version 2025.2.2.

What Type of Installation Are You Using?

The installation is a Home Assistant Add-on.

Which Version of Home Assistant Has the Issue?

The issue is present in Home Assistant version 2025.3.1.

What Platform Are You Using?

The platform being used is ESP32.

Board

The board being used is ESP-WROOM-32.

Component Causing the Issue

The component causing the issue is pca9685.

YAML Config

The YAML config for the pca9685 component is as follows:

esphome:
  name: esp32-dimmer
  friendly_name: ESP32-Dimmer

esp32:
  board: esp32dev
  framework:
    type: arduino
    
# Enable logging
logger:
  level: VERY_VERBOSE

# Enable Home Assistant API
api:
  encryption:
    key: "xxx"

ota:
  - platform: esphome
    password: "xxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

i2c:
   - id: bus
     sda: 
       number: GPIO21
       #ignore_strapping_warning: true
     scl: GPIO22
     scan: true

pca9685:
  - id: pca9685_40
    frequency: 500
    address: 0x40
    i2c_id: bus

output:
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 0
    id: pwm00
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 1
    id: pwm01
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 2
    id: pwm02
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 3
    id: pwm03
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 4
    id: pwm04
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 5
    id: pwm05
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 6
    id: pwm06
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 7
    id: pwm07
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 8
    id: pwm08
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 9
    id: pwm09
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 10
    id: pwm10
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 11
    id: pwm11
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 12
    id: pwm12
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 13
    id: pwm13
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 14
    id: pwm14
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 15
    id: pwm15

light:
  - platform: monochromatic    
    output: pwm00
    name: l00
  - platform: monochromatic    
    output: pwm01
    name: l01
  - platform: monochromatic    
    output: pwm02
    name: l02
  - platform: monochromatic    
    output: pwm03
    name: l03
  - platform: monochromatic    
    output: pwm04
    name: l04
  - platform: monochromatic    
    output: pwm05
    name: l05
  - platform: monochromatic    
    output: pwm06
    name: l06
  - platform: monochromatic    
    output: pwm07
    name: l07
  - platform: monochromatic    
    output: pwm08
    name: l08
  - platform: monochromatic    
    output: pwm09
    name: l09
  - platform: monochromatic    
    output: pwm10
    name: l10
  - platform: monochromatic    
    output: pwm11
    name: l11
  - platform: monochromatic    
    output: pwm12
    name: l12
  - platform: monochromatic    
    output: pwm13
    name: l13
  - platform: monochromatic    
    output: pwm14
    name: l14
  - platform: monochromatic    
    output: pwm15
    name: l15

Anything in the Logs That Might Be Useful for Us?

The logs show the following output:

Current VV log:
[22:34:51][VV][pca9685:120]: Channel 00: amount=0000 phase_begin=0000 phase_end=4096
[22:34:51][VV][pca9685:120]: Channel 01: amount=0215 phase_begin=0000 phase_end=0215
[22:34:51][VV][pca9685:120]: Channel 02: amount=0000 phase_begin=0000 phase_end=4096
[22:34:51][VV][pca9685:120]: Channel 03: amount=0000 phase_begin=0000 phase_end=4096
[22:34:51][VV][pca9685:120]: Channel 04: amount=0000 phase_begin=0000 phase_end=4096
[22:34:51][VV][pca9685:120]: Channel 05: amount=0138 phase_begin=0000 phase_end=0138
[22:34:51][VV][pca9685:120]: Channel 06: amount=0000 phase_begin=0000 phase_end=4096
[22:34:51][VV][pca9685:120]: Channel 07: amount=0000 phase_begin=0000 phase_end=4096
[22:34:51][VV][pca9685:120]: Channel 08: amount=0154 phase_begin=0000 phase_end=0154
[22:34:52][VV][pca9685:120]: Channel 09: amount=0000 phase_begin=0000 phase_end=4096

Additional Information

Q: What is the issue with pca9685 phase_begin being always set to 0000?

A: The issue lies in the calculation of phase_begin in line 105 of file pca9685_output.cpp. The formula used is:

uint16_t phase_begin = uint16_t(channel - this->min_channel_) / num_channels * 4096;

This formula results in a value of 0 due to integer division, causing phase_begin to be always set to 0000.

Q: What is the solution to this issue?

A: To fix this issue, we need to modify the formula to ensure that the division does not result in 0. We can do this by rewriting the formula as:

uint16_t phase_begin = uint16_t(channel - this->min_channel_) * (4096 / num_channels);

Alternatively, we can introduce a new constant before the for-loop:

const uint16_t newfactor = 4096 / num_channels;

And then use this constant within the for-loop:

uint16_t phase_begin = uint16_t(channel - this->min_channel_) * newfactor;

Q: Which version of ESPHome has the issue?

A: The issue is present in ESPHome version 2025.2.2.

Q: What type of installation are you using?

A: The installation is a Home Assistant Add-on.

Q: Which version of Home Assistant has the issue?

A: The issue is present in Home Assistant version 2025.3.1.

Q: What platform are you using?

A: The platform being used is ESP32.

Q: Board

A: The board being used is ESP-WROOM-32.

Q: Component causing the issue

A: The component causing the issue is pca9685.

Q: What is the YAML config for the pca9685 component?

A: The YAML config for the pca9685 component is as follows:

esphome:
  name: esp32-dimmer
  friendly_name: ESP32-Dimmer

esp32:
  board: esp32dev
  framework:
    type: arduino
    
# Enable logging
logger:
  level: VERY_VERBOSE

# Enable Home Assistant API
api:
  encryption:
    key: "xxx"

ota:
  - platform: esphome
    password: "xxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

i2c:
   - id: bus
     sda: 
       number: GPIO21
       #ignore_strapping_warning: true
     scl: GPIO22
     scan: true

pca9685:
  - id: pca9685_40
    frequency: 500
    address: 0x40
    i2c_id: bus

output:
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 0
    id: pwm00
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 1
    id: pwm01
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 2
    id: pwm02
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 3
    id: pwm03
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 4
    id: pwm04
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 5
    id: pwm05
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 6
    id: pwm06
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 7
    id: pwm07
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 8
    id: pwm08
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 9
    id: pwm09
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 10
    id: pwm10
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 11
    id: pwm11
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 12
    id: pwm12
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 13
    id: pwm13
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 14
    id: pwm14
  - platform: pca9685
    pca9685_id: pca9685_40
    channel: 15
    id: pwm15

light:
  - platform: monochromatic    
    output: pwm00
    name: l00
  - platform: monochromatic    
    output: pwm01
    name: l01
  - platform: monochromatic    
    output: pwm02
    name: l02
  - platform: monochromatic    
    output: pwm03
    name: l03
  - platform: monochromatic    
    output: pwm04
    name: l04
  - platform: monochromatic    
    output: pwm05
    name: l05
  - platform: monochromatic    
    output: pwm06
    name: l06
  - platform: monochromatic    
    output: pwm07
    name: l07
  - platform: monochromatic    
    output: pwm08
    name: l08
  - platform: monochromatic    
    output: pwm09
    name: l09
  - platform: monochromatic    
    output: pwm10
    name: l10
  - platform: monochromatic    
    output: pwm11
    name: l11
  - platform: monochromatic    
    output: pwm12
    name: l12
  - platform: monochromatic    
    output: pwm13
    name: l13
  - platform: monochromatic    
    output: pwm14
    name: l14
  - platform: monochromatic    
    output: pwm15
    name: l15

Q: What is the output of the logs?

A: The logs show the following output:

Current VV log:
[22:34:51][VV][pca9685:120]: Channel 00: amount=0000 phase_begin=0000 phase_end=4096
[22:34:51][VV][pca9685:120]: Channel 01: amount=0215 phase_begin=0000 phase_end=0215
[22:34:51][VV][pca9685:120]: Channel 02: amount=0000 phase_begin=0000 phase_end=4096
[22:34:51][VV][pca9685:120]: Channel 03: amount=0000 phase_begin=0000 phase_end=4096
[22:34:51][VV][pca9685:120]: Channel 04: amount=0000 phase_begin=0000 phase_end=4096
[22:34:51][VV][pca9685:120]: Channel 05: amount=0138 phase_begin=0000 phase_end=0138
[22:34:51][VV][pca9685:120]: Channel 06: amount=0000 phase_begin=0000 phase_end=4096
[22:34:51][VV][pca9685:120]: Channel 07: amount=0000 phase_begin=0000 phase_end=4096
[22:34:51][VV][pca9685:120]: Channel 08: amount=0154 phase_begin=0000 phase_end=0154
[22:34:52][VV][pca9685:120]: Channel 09: amount=0000 phase_begin=0000 phase_end=4096

Q: Is there any additional information that might be useful for us?

A: No additional information is provided.