Blink Interactive Tutorial

Introduction

This is the interactive blink tutorial usingWokwi. For this tutorial, you don’t need the ESP32 board or the Arduino toolchain.

Note

If you don’t want to use this tutorial with the simulation, you can copy and paste theExample Code fromWokwi editor and use it on theArduino IDE.

About this Tutorial

This tutorial is the most basic for any get started. In this tutorial, we will show how to set a GPIO pin as an output to drive a LED to blink each 1 second.

Step by step

In order to make this simple blink tutorial, you’ll need to do the following steps.

  1. Define the GPIO for the LED.

#define LED 2

This#defineLED2 will be used to set the GPIO2 as theLED output pin.

  1. Setup.

Inside thesetup() function, we need to add all things we want to run once during the startup.Here we’ll add thepinMode function to set the pin as output.

voidsetup(){pinMode(LED,OUTPUT);}

The first argument is the GPIO number, already defined and the second is the mode, here defined as an output.

  1. Main Loop.

After thesetup, the code runs theloop function infinitely. Here we will handle the GPIO in order to get the LED blinking.

voidloop(){digitalWrite(LED,HIGH);delay(100);digitalWrite(LED,LOW);delay(100);}

The first function is thedigitalWrite() with two arguments:

  • GPIO: Set the GPIO pin. Here defined by ourLED connected to the GPIO2.

  • State: Set the GPIO state as HIGH (ON) or LOW (OFF).

This firstdigitalWrite we will set the LED ON.

After thedigitalWrite, we will set adelay function in order to wait for some time, defined in milliseconds.

Now we can set the GPIO toLOW to turn the LED off anddelay for more few milliseconds to get the LED blinking.

  1. Run the code.

To run this code, you’ll need a development board and the Arduino toolchain installed on your computer. If you don’t have both, you can use the simulator to test and edit the code.

Simulation

This simulator is provided byWokwi and you can test the blink code and play with some modifications to learn more about this example.

Change the parameters, like the delay period, to test the code right on your browser. You can add more LEDs, change the GPIO, and more.

Example Code

Here is the full blink code.

#define LED 2voidsetup(){pinMode(LED,OUTPUT);}voidloop(){digitalWrite(LED,HIGH);delay(100);digitalWrite(LED,LOW);delay(100);}

Resources