Docs

Level Display

The Level Display shows a value as a bar that fills. Your board sends the number, the widget draws the fill.

Use it for: a water tank, a battery charge, a silo, a progress indicator.

SettingWhat it does
Pinthe virtual pin your code writes to
Min, Maxthe values for empty and full
Colourthe fill colour
Show valueprint the number over the bar

Open these from the canvas: tap Edit, then long press the widget and choose Configure.

Vertical Level is the same widget drawn as a column instead of a row. The code is identical, and a tank reads better vertically.

Example

A tank level on V24, sent every thirty seconds.

level-display.ino
#include <PlynxSimpleEsp32.h>

#define TRIG_PIN 5
#define ECHO_PIN 18
#define TANK_DEPTH_CM 100

PlynxTimer timer;

void sendLevel()
{
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long us = pulseIn(ECHO_PIN, HIGH, 30000);
  float distanceCm = us * 0.0343 / 2.0;
  float percent = (TANK_DEPTH_CM - distanceCm) * 100.0 / TANK_DEPTH_CM;

  Plynx.virtualWrite(V24, constrain(percent, 0, 100));
}

void setup()
{
  Serial.begin(115200);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  // Auto setup: WiFi and the Plynx token are configured from the app.
  //
  // To provide them directly in the sketch instead, use:
  //   Plynx.begin(auth, ssid, pass);
  Plynx.begin();

  timer.setInterval(30000L, sendLevel);
}

void loop()
{
  Plynx.run();
  timer.run();
}

constrain() keeps the value inside the range you set in the widget. A reflection off the tank wall can produce a negative depth otherwise.

pulseIn() blocks for up to the timeout you give it. Thirty milliseconds is short enough to stay out of the way of Plynx.run().

Troubleshooting

ProblemCheck
Bar stays emptynothing calls virtualWrite() on that pin
Bar is always fullthe widget Min and Max do not match the values you send
Level jumps aroundultrasonic noise; average three readings before sending
Board disconnectsa long blocking read in loop(); keep timeouts short

Next