Add TC74 temperature sensor

This commit is contained in:
Seth Girvan 2024-09-16 18:06:17 -07:00
parent 857a3dcf72
commit 858083e6b3
11 changed files with 191 additions and 0 deletions

View file

@ -396,6 +396,7 @@ esphome/components/sun/* @OttoWinter
esphome/components/sun_gtil2/* @Mat931
esphome/components/switch/* @esphome/core
esphome/components/t6615/* @tylermenezes
esphome/components/tc74/* @sethgirvan
esphome/components/tca9548a/* @andreashergert1984
esphome/components/tcl112/* @glmnet
esphome/components/tee501/* @Stock-M

View file

@ -0,0 +1 @@
CODEOWNERS = ["@sethgirvan"]

View file

@ -0,0 +1,41 @@
"""
The TC74 is an i2c temperature sensor available in a breadboard-friendly 5-pin
TO-220 package and a SOT-23 SMD package. It has a temperature tolerance of ±2°C
from 25°C-85°C.
https://www.adafruit.com/product/4375
"""
import esphome.codegen as cg
from esphome.components import i2c, sensor
import esphome.config_validation as cv
from esphome.const import (
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
)
CODEOWNERS = ["@sethgirvan"]
DEPENDENCIES = ["i2c"]
tc74_ns = cg.esphome_ns.namespace("tc74")
TC74Component = tc74_ns.class_("TC74Component", cg.PollingComponent, i2c.I2CDevice)
CONFIG_SCHEMA = (
sensor.sensor_schema(
TC74Component,
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x48))
)
async def to_code(config):
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)

View file

@ -0,0 +1,72 @@
// Based on the TC74 datasheet https://ww1.microchip.com/downloads/en/DeviceDoc/21462D.pdf
#include "tc74.h"
#include "esphome/core/log.h"
namespace esphome {
namespace tc74 {
static const char *const TAG = "tc74";
static const uint8_t TC74_REGISTER_TEMPERATURE = 0x00;
static const uint8_t TC74_REGISTER_CONFIGURATION = 0x01;
static const uint8_t TC74_DATA_READY_MASK = 0x40;
// It is possible the "Data Ready" bit will not be set if the TC74 has not been powered on for at least 250ms, so it not
// being set does not constitute a failure.
void TC74Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up TC74...");
uint8_t config_reg;
if (this->read_register(TC74_REGISTER_CONFIGURATION, &config_reg, 1) != i2c::ERROR_OK) {
this->mark_failed();
return;
}
this->data_ready_ = config_reg & TC74_DATA_READY_MASK;
}
void TC74Component::update() {
this->set_timeout("temperature", 2, [this]() { this->read_temperature_(); });
}
void TC74Component::dump_config() {
ESP_LOGCONFIG(TAG, "TC74:");
LOG_I2C_DEVICE(this);
if (this->is_failed()) {
ESP_LOGE(TAG, "Connection with TC74 failed!");
}
LOG_UPDATE_INTERVAL(this);
LOG_SENSOR(" ", "Temperature", this);
}
void TC74Component::read_temperature_() {
if (!this->data_ready_) {
uint8_t config_reg;
if (this->read_register(TC74_REGISTER_CONFIGURATION, &config_reg, 1) != i2c::ERROR_OK) {
this->status_set_warning();
return;
}
if (config_reg & TC74_DATA_READY_MASK) {
this->data_ready_ = true;
} else {
ESP_LOGD(TAG, "TC74 not ready");
return;
}
}
uint8_t temperature_reg;
if (this->read_register(TC74_REGISTER_TEMPERATURE, &temperature_reg, 1) != i2c::ERROR_OK) {
this->status_set_warning();
return;
}
ESP_LOGD(TAG, "Got Temperature=%d °C", temperature_reg);
this->publish_state(temperature_reg);
this->status_clear_warning();
}
float TC74Component::get_setup_priority() const { return setup_priority::DATA; }
} // namespace tc74
} // namespace esphome

View file

@ -0,0 +1,28 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace tc74 {
class TC74Component : public PollingComponent, public i2c::I2CDevice, public sensor::Sensor {
public:
/// Setup the sensor and check connection.
void setup() override;
void dump_config() override;
/// Update the sensor value (temperature).
void update() override;
float get_setup_priority() const override;
protected:
/// Internal method to read the temperature from the component after it has been scheduled.
void read_temperature_();
bool data_ready_ = false;
};
} // namespace tc74
} // namespace esphome

View file

@ -0,0 +1,8 @@
i2c:
- id: i2c_tc74
scl: 16
sda: 17
sensor:
- platform: tc74
name: TC74 Temperature

View file

@ -0,0 +1,8 @@
i2c:
- id: i2c_tc74
scl: 5
sda: 4
sensor:
- platform: tc74
name: TC74 Temperature

View file

@ -0,0 +1,8 @@
i2c:
- id: i2c_tc74
scl: 5
sda: 4
sensor:
- platform: tc74
name: TC74 Temperature

View file

@ -0,0 +1,8 @@
i2c:
- id: i2c_tc74
scl: 16
sda: 17
sensor:
- platform: tc74
name: TC74 Temperature

View file

@ -0,0 +1,8 @@
i2c:
- id: i2c_tc74
scl: 5
sda: 4
sensor:
- platform: tc74
name: TC74 Temperature

View file

@ -0,0 +1,8 @@
i2c:
- id: i2c_tc74
scl: 5
sda: 4
sensor:
- platform: tc74
name: TC74 Temperature