cd74hc4067 Analog Multiplexer
=============================

.. seo::
    :description: Instructions for setting up cd74hc4067 Analog Multiplexer and sensors.
    :image: cd74hc4067.jpg


The ``cd74hc4067`` Analog Multiplexer component allows you to attach up to 16 analog input sensors to a single ADC pin.
The component is used to activate the right multiplexer pin for each of the defined cd74hc4067 sensors.

For further information about the cd74hc4067 chip see the `spec sheet  <https://www.ti.com/lit/ds/symlink/cd74hc4067.pdf>`__
and there are also some guides around like the one from `Adam Meyer <http://adam-meyer.com/arduino/CD74HC4067>`__.

Actually, the chip is bidirectional and could also be used as output, but this is not supported by the component yet.

First, you need to configure the component with the digital pins that control the multiplexer.
Then you need to set up a voltage sensor source (e.g. :doc:`ADC sensor <adc>`) and pass it to the cd74hc4067 sensors with the ``sensor`` option.
Each cd74hc4067 sensor is configured for one of the 16 input pins of the multiplexer.

.. code-block:: yaml

    # Example configuration entry
    cd74hc4067:
      - id: cd74hc4067_1
        pin_s0: D0
        pin_s1: D1
        pin_s2: D2
        pin_s3: D3

    sensor:
      - platform: adc
        id: adc_sensor
        pin: A0
      - platform: cd74hc4067
        id: adc_0
        number: 0
        sensor: adc_sensor
      - platform: cd74hc4067
        id: adc_1
        number: 1
        sensor: adc_sensor

Component/Hub
-------------

Configuration Variables:
************************

- **pin_s0** to **pin_s3** (**Required**, :ref:`config-pin`): The I/O pins connected to the S0 to S3 channel selection pins
- **delay** (*Optional*, :ref:`config-time`): A small delay duration needed for the chip to switch inputs, defaults to 2ms.

Sensor
------

Configuration Variables:
************************

- **sensor** (**Required**, :ref:`config-id`): The source sensor to measure voltage values from, e.g. :doc:`ADC sensor <adc>`.
- **cd74hc4067_id** (**Required**, :ref:`config-id`): The id of the cd74hc4067 component to use for this sensor.
- **number** (*Required*, int): The number of the cd74hc4067 input pin (0-15)
- All other options from :ref:`Sensor <config-sensor>`.

Application Example
-------------------

In this example, the component is used to measure the AC power output of two solar inverters to integrate them
as energy sources in `Home Assistant <https://www.home-assistant.io/docs/energy/solar-panels/>`__.

For this purpose, :doc:`CT clamp sensors <ct_clamp>` are attached on each of the sensors.
Filters are needed to calibrate the output and cut off the noise of near-zero values.
The ``adc`` and ``cd74hc4067`` sensors updates are triggered by the ``ct_clamp`` sensors and have a very high internal update frequency.

.. code-block:: yaml

    cd74hc4067:
      - id: cd74hc4067_1
        pin_s0: D0
        pin_s1: D1
        pin_s2: D2
        pin_s3: D3

    sensor:
      - platform: adc
        id: adc_sensor
        pin: A0
        update_interval: 3600s
      - platform: cd74hc4067
        id: solar_1_raw
        number: 0
        sensor: adc_sensor
        update_interval: 3600s
      - platform: cd74hc4067
        id: solar_2_raw
        number: 1
        sensor: adc_sensor
        update_interval: 3600s
      - platform: ct_clamp
        name: "SolarPower1"
        sensor: solar_1_raw
        update_interval: 5s
        unit_of_measurement: "W"
        device_class: "power"
        filters:
          - lambda: "return x > 0.001 ? x * 56221 : 0;"
      - platform: ct_clamp
        name: "SolarPower2"
        sensor: solar_2_raw
        update_interval: 5s
        unit_of_measurement: "W"
        device_class: "power"
        filters:
          - lambda: "return x > 0.001 ? x * 57519 : 0;"

See Also
--------

- :doc:`adc`
- :doc:`ads1115`
- :doc:`ct_clamp`
- :ghedit:`Edit`
