gehacktes /// noniq.at

0031Using a TLC549 A/D converter in ESPHome

The TLC549 is an old but versatile analog-digital converter chip. With a few lines of code it can be integrated into ESPHome.

ESPHome Electronics Sensors

The TLC5491 use SPI to communicate with a microcontroller. ESPHome supports SPI out of the box. We just need to configure the PINs that should be used:

spi:
  clk_pin: GPIO13
  miso_pin: GPIO12

spi_device:

  # Set a custom ID for the SPI device. This ID will be used
  # to reference the device later on.
  id: adc

  cs_pin: GPIO14
  mode: 0

Depending on the used device different pins can be used. In my project I’m working with an ESP8266 dev board and chose the pins such that the TLC549 can be easily connected when placed directly next to the ESP board:

The TLC549 (front) next to the ESP8266 devboard, using GPIO 12, 13, and 14 for MISO, CLK, and CS respectively.

Next, we need to define a generic template sensor:

sensor:
  - platform: template
    name: "Analog sensor"

    # Note the usage of `adc`: This references the SPI device
    # we defined earlier.
    lambda: |-
      id(adc).enable();
      uint8_t sensor_value = id(adc).read_byte();
      id(adc).disable();
      return sensor_value;

    # Of course you can also use all the other options for
    # sensors in ESPHome.
    device_class: moisture
    unit_of_measurement: "%"
    state_class: measurement
    filters:
      - calibrate_linear:
          datapoints:
            - 0 -> 0.0
            - 255 -> 100.0

To read the sensor value we use a lambda block. id(adc) gives a reference to the SPI device we defined earlier. This class of devices supports various methods (see API) – in our case we need to enable the device, read a single byte from it2, and then disable it again.

Update 2024-10-05

Added state_class: measurement to the sensor options. This is necessary for Home Assistant to generate long term statistics.


  1. As well as the very similar TC548. 

  2. The TLC549 has 8 bit resolution, so each sensor value is exactly one byte. 


Leave a comment

Please be polite and constructive. Comments will be reviewed and approved manually.

Comment could not be submitted. Please make sure to fill in all fields.
There seems to be a technical problem. Please try again later.
Thank you! We’ll notify you as soon as your comment gets published.

0030Wenn der Wasserkocher zu laut piepst