Arduino-Port-Expander Problem with Analog and Digital Inputs

Hi,

I am using the Arduino Port Expander to get 8 Digital Outputs and 6 Analog Inputs.

esphome:
  name: test_arduino
  includes:
    - arduino_port_expander.h

esp8266:
  board: d1_mini

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

api:

ota:

# define i2c device
# for an ESP8266 SDA is D2 and goes to Arduino's A4
#                SCL is D1 and goes to Arduino's A5
i2c:
  id: i2c_component

logger:
  level: DEBUG

# define the port expander hub, here we define one with id 'expander1',
# but you can define many
custom_component:
  - id: expander1
    lambda: |-
      auto expander = new ArduinoPortExpander(i2c_component, 0x08, true);
      return {expander};

# define binary outputs, here we have 4, as the relays are inverse logic
# (a path to ground turns the relay ON), we defined the inverted: true
# option of ESPHome outputs.
output:
- platform: custom
  type: binary
  lambda: |-
    return {ape_binary_output(expander1, 2),
            ape_binary_output(expander1, 3),
            ape_binary_output(expander1, 4),
            ape_binary_output(expander1, 5),
            ape_binary_output(expander1, 6),
            ape_binary_output(expander1, 7),
            ape_binary_output(expander1, 8),
            ape_binary_output(expander1, 9)};

  outputs:
    - id: relay_1
      inverted: true
    - id: relay_2
      inverted: true
    - id: relay_3
      inverted: true
    - id: relay_4
      inverted: true
    - id: relay_5
      inverted: true
    - id: relay_6
      inverted: true
    - id: relay_7
      inverted: true
    - id: relay_8
      inverted: true

switch:
  - platform: output
    name: Relais 1
    id: relais01
    output: relay_8
  - platform: output
    name: Relais 2
    id: relais02
    output: relay_7
  - platform: output
    name: Relais 3
    id: relais03
    output: relay_6
  - platform: output
    name: Relais 4
    id: relais04
    output: relay_5
  - platform: output
    name: Relais 5
    id: relais05
    output: relay_4
  - platform: output
    name: Relais 6
    id: relais06
    output: relay_3
  - platform: output
    name: Relais 7
    id: relais07
    output: relay_2
  - platform: output
    name: Relais 8
    id: relais08
    output: relay_1

# A4 and A5 reserved for i2c
# define analog sensors
sensor:
  - platform: custom
    lambda: |-
      return {ape_analog_input(expander1, 0),
              ape_analog_input(expander1, 1),
              ape_analog_input(expander1, 2),
              ape_analog_input(expander1, 3),
              ape_analog_input(expander1, 6),
              ape_analog_input(expander1, 7)};
    sensors:
      - name: "A0 Rasen vor Pool"
        unit_of_measurement: "%"
        device_class: humidity
        state_class: measurement
        accuracy_decimals: 0
        id: analog_a0
        filters:
        - throttle: 10s
        - median:
            window_size: 60
            send_every: 10
            send_first_at: 1
        - lambda: return x * (5.0 / 1023.0);
        - calibrate_linear:
            - 0.95 -> 100
            - 2.5 -> 0
        - lambda: |
            if (x < 0) return 0; 
            else if (x > 100) return 100;
            else return (x);
      - name: "A1 Rasen vor Pool"
        unit_of_measurement: "%"
        device_class: humidity
        state_class: measurement
        accuracy_decimals: 0
        id: analog_a1
        filters:
        - throttle: 10s
        - median:
            window_size: 60
            send_every: 10
            send_first_at: 1
        - lambda: return x * (5.0 / 1023.0);
        - calibrate_linear:
            - 0.95 -> 100
            - 2.5 -> 0
        - lambda: |
            if (x < 0) return 0; 
            else if (x > 100) return 100;
            else return (x);
      - name: "A2 Rasen vor Pool"
        unit_of_measurement: "%"
        device_class: humidity
        state_class: measurement
        accuracy_decimals: 0
        id: analog_a2
        filters:
        - throttle: 10s
        - median:
            window_size: 60
            send_every: 10
            send_first_at: 1
        - lambda: return x * (5.0 / 1023.0);
        - calibrate_linear:
            - 0.95 -> 100
            - 2.5 -> 0
        - lambda: |
            if (x < 0) return 0; 
            else if (x > 100) return 100;
            else return (x);
      - name: "A3 Rasen vor Pool"
        unit_of_measurement: "%"
        device_class: humidity
        state_class: measurement
        accuracy_decimals: 0
        id: analog_a3
        filters:
        - throttle: 10s
        - median:
            window_size: 60
            send_every: 10
            send_first_at: 1
        - lambda: return x * (5.0 / 1023.0);
        - calibrate_linear:
            - 0.95 -> 100
            - 2.5 -> 0
        - lambda: |
            if (x < 0) return 0; 
            else if (x > 100) return 100;
            else return (x);
      - name: "A4 Rasen vor Pool"
        unit_of_measurement: "%"
        device_class: humidity
        state_class: measurement
        accuracy_decimals: 0
        id: analog_a4
        filters:
        - throttle: 10s
        - median:
            window_size: 60
            send_every: 10
            send_first_at: 1
        - lambda: return x * (5.0 / 1023.0);
        - calibrate_linear:
            - 0.95 -> 100
            - 2.5 -> 0
        - lambda: |
            if (x < 0) return 0; 
            else if (x > 100) return 100;
            else return (x);
      - name: "A5 Rasen vor Pool"
        unit_of_measurement: "%"
        device_class: humidity
        state_class: measurement
        accuracy_decimals: 0
        id: analog_a5
        filters:
        - throttle: 10s
        - median:
            window_size: 60
            send_every: 10
            send_first_at: 1
        - lambda: return x * (5.0 / 1023.0);
        - calibrate_linear:
            - 0.95 -> 100
            - 2.5 -> 0
        - lambda: |
            if (x < 0) return 0; 
            else if (x > 100) return 100;
            else return (x);

Problem is, that after I activate the Analog Input Section, the Relais 3,4,7,8 are not working. If I comment out the Analog Sensor part, all 8 Relais are working fine.

I am using
D2 → D9 for the Relays
A0, A1, A2, A3, A6, A7 for the Analog Input

I have found new Informations.

If Relay is off, the Digital Out of the Arduino does have 5V → correct.
If Relay is on, the Digital Out of the Arduino does have 0V → correct.
If Relay is no the same Number, as the Analog Sensor (A3 And D3) and is on, the Digital Out of the Arduino does have ~ 3V → not correct.

1 Like

Have found a workaround for my Problem: arduino_port_expander.h · GitHub

1 Like