How do I compare an adc sensor reading with a reference reading?

I have a rudder indicator sensor in my boat that goes from 5-90 ohm, depending on position, with one 180 ohm resistor on the plus and one 22 ohm on ground to make it readable by the adc pin. The power comes from 3.3V out from the ESP that’s connected to it and moving the indicator gives me from 1,33V to 0,46V in a rather even curve.

But I would like to compare that to another reading, which is just 3,3V over similar resistors, to make sure the voltage output from the ESP is totally even and doesn’t affect the reading. Can somebody please tell me if this is possible within the ESP32, or do I have to compare in Hass? I would like to have it out from the ESP if possible, because I take the value into SignalK (boating software) and would like to avoid any extra jumps that can fail.

Also, if possible, it would be great to split 1,33 to 0,46 to a level from 0-100 and get the actual position of the rudder position sensor in numbers from the ESP.

Found it! I needed to use this code to get it working all the way. The calibration is the first lambda, which divides the rudder indicator voltage by the reference voltage and multiplies it with a 100. The rest is to get it to a nice 0-100 % with jumps on 5 %.

  - platform: template
    name: "Motorheisposisjon"
    lambda: |-
      return ((id(motorheisspenning).state / (id(referansespenning).state)) * 100);
    update_interval: 100ms
    filters:
      - sliding_window_moving_average:
          window_size: 10
          send_every: 10
      - calibrate_linear:
            - 17.3 -> 0.0
            - 48.5 -> 100.0
      - lambda: return 5*round(x/5);
      - clamp:
          min_value: 0
          max_value: 100

1 Like