Docs

LED

The LED widget shows a state your board sends. Your code sets it, you look at it.

Use it for: pump running, door open, alarm tripped, sensor above threshold.

SettingWhat it does
Pinthe virtual pin your code writes to
Colourthe colour when lit
Labelthe text under the light

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

Brightness comes from the value: 0 turns it off, 255 lights it fully, anything in between dims it.

Example

A door sensor on GPIO 4 lights an LED widget on V4.

led.ino
#include <PlynxSimpleEsp32.h>

#define DOOR_PIN 4

WidgetLED doorLed(V4);
PlynxTimer timer;

void checkDoor()
{
  if (digitalRead(DOOR_PIN) == HIGH) {
    doorLed.on();
  } else {
    doorLed.off();
  }
}

void setup()
{
  Serial.begin(115200);
  pinMode(DOOR_PIN, INPUT_PULLUP);

  // 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(1000L, checkDoor);
}

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

WidgetLED doorLed(V4) binds the object to the pin. After that, on() and off() are the whole API.

Use doorLed.setValue(128) for half brightness, and doorLed.getValue() to read back what you last sent.

Send it only when it changes

on() writes to the pin every time you call it. Calling it every second sends the same value every second. Track the state and write on the edge:

Write on change only
bool wasOpen = false;

void checkDoor()
{
  bool isOpen = digitalRead(DOOR_PIN) == HIGH;

  if (isOpen != wasOpen) {
    isOpen ? doorLed.on() : doorLed.off();
    wasOpen = isOpen;
  }
}

Troubleshooting

ProblemCheck
LED never lightsthe widget pin and the WidgetLED pin do not match
LED lights but stays litnothing calls off(); check the branch that should run
Board disconnectsdelay() in loop(); use PlynxTimer

Next