|
| 1 | +########################## |
| 2 | +Blink Interactive Tutorial |
| 3 | +########################## |
| 4 | + |
| 5 | +Introduction |
| 6 | +------------ |
| 7 | + |
| 8 | +This is the interactive blink tutorial using `Wokwi`_. For this tutorial you don't need the ESP32 board or the Arduino toolchain. |
| 9 | + |
| 10 | +.. note:: If you don't want to use this tutorial with the simulation, you can copy and paste the code from `Wokwi`_ editor and use on the Arduino IDE or PlatformIO. |
| 11 | + |
| 12 | +About this Tutorial |
| 13 | +------------------- |
| 14 | + |
| 15 | +This tutorial is the most basic for any getting started. In this tutorial, we will show how to set a GPIO pin as output to drive a LED to blink each 1 second. |
| 16 | + |
| 17 | +Step by step |
| 18 | +------------ |
| 19 | + |
| 20 | +In order to make this simple blink tutorial, you'll need to do the following steps. |
| 21 | + |
| 22 | +1. Define the GPIO for the LED. |
| 23 | + |
| 24 | +.. code-block:: |
| 25 | +
|
| 26 | + #define LED 2 |
| 27 | +
|
| 28 | +This ``#define LED 2`` will be used to set the GPIO2 as the ``LED`` output pin. |
| 29 | + |
| 30 | +2. Setup. |
| 31 | + |
| 32 | +Inside the ``setup()`` function, we need to add all things we want to run once during the startup. |
| 33 | +Here we'll add the ``pinMode`` function to set the pin as output. |
| 34 | + |
| 35 | +.. code-block:: |
| 36 | +
|
| 37 | + void setup() { |
| 38 | + pinMode(LED, OUTPUT); |
| 39 | + } |
| 40 | +
|
| 41 | +The first argument is the GPIO number, already defined and the second is the mode, here defined as output. |
| 42 | + |
| 43 | +3. Main Loop. |
| 44 | + |
| 45 | +After the ``setup``, the code runs the ``loop`` function infinitely. Here we will handle the GPIO in order to get the LED blinking. |
| 46 | + |
| 47 | +.. code-block:: |
| 48 | +
|
| 49 | + void loop() { |
| 50 | + digitalWrite(LED, HIGH); |
| 51 | + delay(100); |
| 52 | + digitalWrite(LED, LOW); |
| 53 | + delay(100); |
| 54 | + } |
| 55 | +
|
| 56 | +The first function is the ``digitalWrite()`` with two arguments: |
| 57 | + |
| 58 | +* GPIO: Set the GPIO pin. Here defined by our ``LED`` connected to the GPIO2. |
| 59 | +* State: Set the GPIO state as HIGH (on) or LOW (off). |
| 60 | + |
| 61 | +This first ``digitalWrite`` we will set the LED on. |
| 62 | + |
| 63 | +After the ``digitalWrite``, we will set a ``delay`` function in order to wait for some time, defined in milliseconds. |
| 64 | + |
| 65 | +Now we can set the GPIO to ``LOW`` to turn the LED off and ``delay`` for more few milliseconds to get the LED blinking. |
| 66 | + |
| 67 | +4. Run the code. |
| 68 | + |
| 69 | +To run this code, you'll need a development board and the Arduino toolchain installed in your computer. If you don't have both, you can use the simulator to test and edit the code. |
| 70 | + |
| 71 | +Simulation |
| 72 | +---------- |
| 73 | + |
| 74 | +This simulator is provided by `Wokwi`_ and you can test the blink code and play with some modification to learn mode about this example. |
| 75 | + |
| 76 | +.. raw:: html |
| 77 | + |
| 78 | + <iframe src="https://wokwi.com/arduino/projects/305566932847821378?embed=1" width="100%" height="400" border="0"></iframe> |
| 79 | + |
| 80 | +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. |
| 81 | + |
| 82 | +Code |
| 83 | +---- |
| 84 | + |
| 85 | +Here is the full blink code. |
| 86 | + |
| 87 | +.. code-block:: |
| 88 | +
|
| 89 | + #define LED 2 |
| 90 | +
|
| 91 | + void setup() { |
| 92 | + pinMode(LED, OUTPUT); |
| 93 | + } |
| 94 | +
|
| 95 | + void loop() { |
| 96 | + digitalWrite(LED, HIGH); |
| 97 | + delay(100); |
| 98 | + digitalWrite(LED, LOW); |
| 99 | + delay(100); |
| 100 | + } |
| 101 | +
|
| 102 | +Resources |
| 103 | +--------- |
| 104 | + |
| 105 | +* `ESP32 Datasheet`_ (Datasheet) |
| 106 | +* `Wokwi`_ (Wokwi Website) |
| 107 | + |
| 108 | +.. _ESP32 Datasheet: https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf |
| 109 | +.. _Wokwi: https://wokwi.com/ |
0 commit comments