Skip to main content
Associate
March 5, 2026
Question

Developing a capacitive liquid level detector with STM8L

  • March 5, 2026
  • 5 replies
  • 199 views

Hi, I am trying to develop a capacitive liquid level detector with the STM8L152 adapting an example of the STMTouch Library.

It is fine to detect if there is water or not inside a container. But when I try it in my final solution, that is to detect the presence of battery liquid inside a battery container, it does not able to differenciate the presence of the liquid. The reason is that there are metalic plates inside the battery container and I believe that the plates are blinding the capacity of the sensor to detect the liquid.

Do you know if there is any fine-tuning that I can use to help the application differentiate the the liquid from the metalic plates?

Thank you,

Rodrigo

    5 replies

    Peter BENSCH
    Technical Moderator
    March 5, 2026

    Welcome @bernardo, to the community!

    this is mainly a sensing‑geometry problem rather than a simple fine‑tuning issue.

    Metal plates inside the battery act as strong electrodes and shield the electric field of your capacitive sensor, so the STM8L152 only sees the metal, not the change in dielectric from the liquid.

    A few practical options:

    • Change placement and/or shape: move the sensor electrodes outside the battery case, higher or lower than the internal plates, or use a narrow vertical strip electrode so that the field lines pass through the liquid region that is not fully shielded by metal
    • Use differential / reference electrode: one electrode at the liquid‑sensing height and one in a region always covered by liquid or always dry, then measure the difference to cancel the dominant metal influence
    • Increase electrode area / spacing to metal: more distance or dielectric between sensor and metal plates reduces shielding and increases sensitivity to the liquid

    Library parameters (gain, threshold, acquisition frequency) can help with noise and resolution, but they cannot fully compensate if the internal metal geometry shields the field. A mechanical/electrode redesign is usually required in this kind of battery application.

    Hope that helps?

    Regards
    /Peter

    bernardoAuthor
    Associate
    March 5, 2026

    Hi Peter, I really appreciate your insigths.

    Before put it in practical, I would like to share some images.

    The first one shows the sensor shape. I've tried to connect the peripheral area of the sensor to the ground and to an active shield. The final result was the same.

    20260305_162141.jpg

    The second image shows the sample (in yellow) and the channel (in blue) waveforms. I am using a 15nF capacitor connected to the sample.

    20260305_162549.jpg

    The last image shows the sample (in yellow) and the active shield (in blue) waveforms.

    20260305_162728.jpg

    I am observing such behavior. When I bring my finger close to the sensor, the charging and discharging time of the channel input decreases, and I expected the opposite, since I am increasing the capacitance by bringing my finger closer.

    For information, I am based on the STM8L152C6_Ex02 from the STM8L STMTouch Examples V1.1.0 library.

    Please, I appreciate it if you could comment about the described bahavior and if the waveforms makes sense for you.

    Thank you so much,

    bernardoAuthor
    Associate
    March 9, 2026

    Hi, please, any suggestion regarding the charge and discharging of the channel (and sample) behavoiur?

    Thank you,

    Peter BENSCH
    Technical Moderator
    March 13, 2026

    I was at Embedded World in Nuremberg, so I wasn't able to help more deeply in the community for several days.

    Thanks for the additional scope shots. The waveforms all look perfectly normal for the STMTouch charge‑transfer front‑end: clean, regular ramps and discharges, no obvious instability. That means the electronics and timing are essentially fine.

    The fact that the curves hardly change between “liquid” and “no liquid” confirms my suspicion: the internal metal plates are capturing most of the field, so the sensor mainly sees metal, not the electrolyte. Library tuning won’t fix that.

    As I mentioned before: to get a usable signal you will need to change the electrode geometry (long vertical strip on the outside, minimal guard towards the liquid, ideally a second reference electrode) and increase the distance or dielectric between your electrode and the internal plates. After that, fine‑tuning thresholds and gain in STMTouch should be sufficient.

    Regards
    /Peter

    bernardoAuthor
    Associate
    March 15, 2026

    Hi Peter, I hope you had a great experiance at EW2026 and thanks for the waveforms analyses.

    Having a second reference seems good, but do you suggest that one of the electrodes be outside the metallized area? If yes, unfortunately the metal plates occupy the entire internal area of ​​the battery.

    About the electrode geometry, regardless of the method used for measurement, with one or two electrodes, which would be more efficient for my scenario?

    a) with a GND shield;

    b) with an active shield;

    c) with no shield.

    Captura de tela 2026-03-15 141748.png

    Best regards,

    Rodrigo

    Peter BENSCH
    Technical Moderator
    March 18, 2026

    In your specific battery application the priority is to maximize the field lines that reach the electrolyte behind the internal metal plates. Any shield ring around the electrode - whether grounded (a) or active (b) - will intercept part of these field lines and therefore reduce your sensitivity to the liquid.

    For that reason, option (c) no shield is the most efficient starting point: a single vertical strip electrode on the outside of the battery case, with no surrounding shield, placed where the liquid level changes.

    Ground or active shields are useful to reduce environmental noise and hand effects, but only after you have confirmed that the bare electrode provides a detectable signal in the presence of the internal metal plates.

    • Keep each strip narrow rather than wide (e.g. a few mm), but long. Long strips give better resolution along the height
    • Make sure there is no ground copper nearby on the same side of the PCB, because it would short‑circuit field lines before they reach the battery interior
    • If you need noise immunity, put a ground or active shield only on the opposite side of the PCB, below the sensor traces, not around them on the battery side

    Regards
    /Peter

    bernardoAuthor
    Associate
    March 19, 2026

    Hi Peter, I have tested it with a couple of electrode geometry, including your welcome latest suggestion, but with no improvements in the electrolyte detection.

    I have the same Delta value with or without electrolyte inside the battery, like the image below.

    Captura de tela 2026-03-19 110301.png

    And just for your information, this is the battery type that I am talking about.

    20260319_112815.jpg

    Also, I had the opportunity to test/explore a commercial fill level capacitive sensor. It uses the STM8L151G unit. I thought it was using the same STM8L touch-sensing library, but based on the waveform below I believe it uses a proprietary solution. The waveform was collected at the electrode point. It worked perfectly in all tested battery.

    20260319_105150.jpg

    Regards,

    Rodrigo