STANNUM
Open the simulator

Tutorials / Output

LED

The first output of every project: a GPIO set HIGH lights it, LOW turns it off.

Parts in the example:
  • LED
  • Button
  • Potentiometer
The LED block as it appears on the canvas.

What it is

A light-emitting diode lights when current flows from its anode to its cathode. On a bench it needs a series resistor (220 to 330 Ω for a 3.3 V GPIO) so the current stays around 10 mA; forget it and the LED, or the pin, goes first.

In the simulator

The block has the two legs and a dome that lights in the color chosen on its card. It follows the level of the GPIO wired to A: HIGH is on, LOW is off, in real time — a delay(500) in the code is half a second on the screen.

Pins

PinWhat it is
Aanode (+), the long leg — wire it to the GPIO that drives the LED
Kcathode (−), the short leg — wire it to a GND symbol

Wiring

The circuit below is the example Blink with button and potentiometer from the lab — open it with Project → Open example… and it comes ready to run.

The wired circuit, as the lab draws it.
LED pinGoes to
AD2 on the board (GPIO 2)
Ka GND symbol

Settings

Double-click the block's title bar to open its card, then the Settings tab. Changes apply to the running simulation right away.

The Settings tab of the LED.
SettingAcceptsDefaultNotes
ColorRed · Green · Blue · Yellow · Orange · WhiteRedOnly the package: the GPIO does not know the color.

Code

sketch.cpp
#include <Arduino.h>

const int LED = 2;
const int BOTAO = 4;
const int POT = 34;

void setup() {
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  pinMode(BOTAO, INPUT_PULLUP);
  Serial.println("ready");
}

void loop() {
  // the potentiometer sets the blink speed
  int leitura = analogRead(POT);
  int intervalo = map(leitura, 0, 4095, 60, 1000);

  // while the button is held down the LED stays on
  if (digitalRead(BOTAO) == LOW) {
    digitalWrite(LED, HIGH);
    delay(100);
    return;
  }

  digitalWrite(LED, HIGH);
  delay(intervalo);
  digitalWrite(LED, LOW);
  delay(intervalo);
  Serial.printf("pot=%d interval=%d\n", leitura, intervalo);
}

Three parts work together in this example: the LED on GPIO 2, a button on GPIO 4 and a potentiometer on GPIO 34. pinMode(LED, OUTPUT) makes the pin an output; digitalWrite(LED, HIGH) and LOW do the rest. The potentiometer reading sets the blink interval, and holding the button keeps the LED on — the Button and Potentiometer guides cover those two.

Run it

Press Build and run. The first build of a project takes a while; after that, only what changed is rebuilt.

The simulation running: the canvas reacts and the serial monitor shows what the code prints.

The LED blinks at the speed set by the potentiometer. Drag the potentiometer's slider and the blink changes; hold the button on the canvas and the LED stays on. The serial monitor prints the reading and the interval on every cycle.

How the simulation models it

Try this

  1. Add a second LED on GPIO 15 and make the two alternate: one on while the other is off.
  2. Replace the two delay() calls by a millis() comparison, so the loop keeps reading the button while the LED is off.
  3. Wire the LED to GPIO 34 and build. Read what the compiler and the serial monitor say — 34 is input only.

See also