2021-09-20 12:00:52 +01:00
##########################
Blink Interactive Tutorial
##########################
Introduction
------------
2021-09-20 12:39:05 +01:00
This is the interactive blink tutorial using `Wokwi`_ . For this tutorial, you don't need the ESP32 board or the Arduino toolchain.
2021-09-20 12:00:52 +01:00
2021-09-21 14:34:21 +01:00
.. note :: If you don't want to use this tutorial with the simulation, you can copy and paste the :ref: `blink_example_code` from `Wokwi`_ editor and use it on the `Arduino IDE`_ or `PlatformIO`_ .
2021-09-20 12:00:52 +01:00
About this Tutorial
-------------------
2021-09-21 14:34:21 +01:00
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.
2021-09-20 12:00:52 +01:00
Step by step
------------
In order to make this simple blink tutorial, you'll need to do the following steps.
2021-09-21 14:34:21 +01:00
1. **Define the GPIO for the LED.**
2021-09-20 12:00:52 +01:00
.. code-block ::
#define LED 2
This `` #define LED 2 `` will be used to set the GPIO2 as the `` LED `` output pin.
2021-09-21 14:34:21 +01:00
2. **Setup.**
2021-09-20 12:00:52 +01:00
Inside the `` setup() `` function, we need to add all things we want to run once during the startup.
Here we'll add the `` pinMode `` function to set the pin as output.
.. code-block ::
void setup() {
pinMode(LED, OUTPUT);
}
2021-09-21 14:34:21 +01:00
The first argument is the GPIO number, already defined and the second is the mode, here defined as an output.
2021-09-20 12:00:52 +01:00
2021-09-21 14:34:21 +01:00
3. **Main Loop.**
2021-09-20 12:00:52 +01:00
After the `` setup `` , the code runs the `` loop `` function infinitely. Here we will handle the GPIO in order to get the LED blinking.
.. code-block ::
void loop() {
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);
}
The first function is the `` digitalWrite() `` with two arguments:
* GPIO: Set the GPIO pin. Here defined by our `` LED `` connected to the GPIO2.
2021-09-21 14:34:21 +01:00
* State: Set the GPIO state as HIGH (ON) or LOW (OFF).
2021-09-20 12:00:52 +01:00
2021-09-21 14:34:21 +01:00
This first `` digitalWrite `` we will set the LED ON.
2021-09-20 12:00:52 +01:00
After the `` digitalWrite `` , we will set a `` delay `` function in order to wait for some time, defined in milliseconds.
Now we can set the GPIO to `` LOW `` to turn the LED off and `` delay `` for more few milliseconds to get the LED blinking.
2021-09-21 14:34:21 +01:00
4. **Run the code.**
2021-09-20 12:00:52 +01:00
2021-09-21 14:34:21 +01:00
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.
2021-09-20 12:00:52 +01:00
Simulation
----------
2021-09-21 14:34:21 +01:00
This simulator is provided by `Wokwi`_ and you can test the blink code and play with some modifications to learn more about this example.
2021-09-20 12:00:52 +01:00
.. raw :: html
<iframe src="https://wokwi.com/arduino/projects/305566932847821378?embed=1" width="100%" height="400" border="0"></iframe>
2021-09-21 14:34:21 +01:00
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.
2021-09-20 12:00:52 +01:00
2021-09-20 12:39:05 +01:00
.. _blink_example_code:
Example Code
------------
2021-09-20 12:00:52 +01:00
Here is the full blink code.
.. code-block ::
#define LED 2
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);
}
Resources
---------
* `ESP32 Datasheet`_ (Datasheet)
* `Wokwi`_ (Wokwi Website)
.. _ESP32 Datasheet: https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf
2021-09-21 14:34:21 +01:00
.. _Wokwi: https://wokwi.com/
.. _PlatformIO: https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html#platformio
.. _Arduino IDE: https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html#installing-using-boards-manager