ASD Voltage to Percent - is there a mapping?

Hi,

I have a capacitive sensor measuring moisture in a plant.

sensor:
  - platform: adc
    pin: 36
    attenuation: 11db
    name: "Kontor Plante Fugtighed"
    update_interval: 60s

It provides fine readings, but of course a Voltage reading. I guess 3.3v for 0% moisture, and 0.0v for 100% moisture.

Question is if there is a kind of mapping parameter I can use to get this displayed as the % moisture?

In Arduino code I would use

map(value, fromLow, fromHigh, toLow, toHigh)

Does this exist? What should I add to my code?

Thanks!

1 Like

Did it come with a datasheet?

If it is a linear mapping (which is hard to know without a datasheet) then you need to do it just via the maths which is easy enough.

Just multiply the voltage by -30.3 and add 100. Round to 1 decimal place. Voila.

You can use a filter with lambda in esphome to use the map function. The snipped is what I use for HX711

filters:
    - lambda: |-
        auto first_mass = 0.0; 
        auto first_value = 1176425;
        auto second_mass = 122;
        auto second_value = 1279801;
        
        auto r = map(x, first_value, second_value, first_mass, second_mass);
        if (r > 0) return r;
        return 0;
1 Like

Thanks guys,

@nickrout, worked with this:

  - platform: adc
    pin: 36
    attenuation: 11db
    name: "Kontor Plante Fugtighed"
    update_interval: 60s
    filters:
    - lambda: return x * (-30.3) + 100;
    unit_of_measurement: "%"

@Puneit_Thukral, I tried your code like this

  - platform: adc
    pin: 36
    attenuation: 11db
    name: "Kontor Plante Fugtighed"
    update_interval: 60s
    filters:
    - lambda: |-
        auto first_mass = 100.0; 
        auto first_value = 0.0;
        auto second_mass = 0.0;
        auto second_value = 3.3;
        
        auto r = map(x, first_value, second_value, first_mass, second_mass);
        if (r > 0) return r;
        return 0;
    unit_of_measurement: "%"

But I had forgotten that it is not enough to update the code…it also needs to be uploaded…that occured to me later (new to this)…when I had the top code in…But for the sake of an example…would the mapping look as I put it?

Thanks!

1 Like

yes, it looks good to me. On the ESPhome page, once you edit a code and if syntax is correct the upload button activates. You click that and the node will update OTA.
If button does not activate, then look for a red cross on the page. Hovering over it, it will give an indication what is the mistake in the code.

Use the calibrate_linear filter: https://esphome.io/components/sensor/index.html#calibrate-linear

Should work something like this… (untested code)

sensor:
  - platform: adc
    pin: 36
    attenuation: 11db
    name: "Kontor Plante Fugtighed"
    update_interval: 60s
    filters:
      - calibrate_linear:
          datapoints:
            - 3.3 -> 0
            - 0.0 -> 100
1 Like