Formule conversione for NTC temperature

Hi,
I linked a Arduino UNO with in firmata firmware by USB port to read a NTC temperature sensor, like this scheme:

This work !
So I can to read in Home Assistant voltage from NTC sensor !
Now, I want to transduce voltage in temperature by this formula (in Arduino code):

int ThermistorPin = 0;
int Vo;
float R1 = 10000;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

void setup() {
Serial.begin(9600);
}

void loop() {

  Vo = analogRead(ThermistorPin);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  T = T - 273.15;
  T = (T * 9.0)/ 5.0 + 32.0; 

  Serial.print("Temperature: "); 
  Serial.print(T);
  Serial.println(" F"); 

  delay(500);
}

I need a function …
How I Can to do ?

You should ask on an Arduino forum.

I’m sorry but i don’t understand…I would make a function in Home Assistant. This function will have constants e calculus like arduino code.
How i Can to do ?
Is it possible to make a python function to call in configuration.yaml ?

Sorry I thought you meant in the Arduino.

Have you seen this?

I loadded firmata in Arduino.
I can to read voltage with this configuration.yaml


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

firmata:
  - serial_port: /dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_85037313835351415100-if00
    serial_baud_rate: 57600
    sensors:
      - name: ntc
        pin: A0
        pin_mode: ANALOG
        differential: 5

template:
  sensor:
    - name: Temperatura NTC
      state: "{{ (states('sensor.my_sensor')) | float * 2 }}"
      unit_of_measurement: '°C'





# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

Now, I must to tranduce voltage in temperature, with mathematical function, like in Arduino code.
I have tried to use template, but is more complicated because I have any constants in mathematical formula.
I need a function. Can i to use python code to transduce voltage in temperature ?

firmata:
  - serial_port: /dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_85037313835351415100-if00
    serial_baud_rate: 57600
    sensors:
      - name: ntc
        pin: A0
        pin_mode: ANALOG
        differential: 5

template:
  sensor:
    - name: Temperatura NTC
      state: >
        {% set Vo = states('sensor.ntc')|float %}
        {% set c1 = 1.009249522e-03 %}
        {% set c2 = 2.378405444e-04 %}
        {% set c3 = 2.019202697e-07 %}
        {% set R1 = 10000 %}
        {% set R2 = R1 * 1023 / ( Vo - 1 ) %}
        {% set logR2 = log(R2,10) %}
        {% set T = ( 1 / ( c1 + c2 * logR2 + c3 * logR2**3 ) - 273.15 ) * 9 / 5 + 32 %}
        {{ T }}
      unit_of_measurement: '°C'

I’m pretty sure I got that right. Either way it should give you an idea how it can be done in a jinja template sensor. No need for Python.

ok. thank you. there are any mathematicals errors but template sensor work. I write corrects formulas:

template:
  sensor:
    - name: NTC1_temperature
      state: >
        {% set c1 = 1.009249522e-03 %}
        {% set c2 = 2.378405444e-04 %}
        {% set c3 = 2.019202697e-07 %}
        {% set R1 = 10000  %}
        {% set R2 = R1 * (1023.0 / ( states('sensor.my_sensor')|float) -1) %}
        {% set logR2 = log(R2,e) %}
        {% set T =  1 / ( c1 + c2 * logR2 + c3 * logR2**3 )    %}
        {% set T = (T-273) | round(2)    %}
        {{ T}}
      unit_of_measurement: '°C'

So, I have temperature in celsius grade.
Thank’s !!!

1 Like

But the real question is why would you NOT do it in the arduino instead of loading up the HA processor with the template?

Because only the raw ADC values are available using the Firmata integration.

Ah, I don’t have any experience with that so I don’t know the advantage of running that over the standard ArdiunoIDE or even ESPHome.

The Arduino integration has been deprecated. Firmata is the replacement.

ESPHome would be considerably better but does not run on that hardware.