Skip to content

Lab Session 0: Microcontrollers simulation with Wokwi

Estimated time: 1.5h (1 session)

1. Launch an example

  • Access the Wokwi simulation environment via this link.
  • Select the ESP32 template.

    Warning

    Open the ESP32 template from Starter Templates, not from ESP-IDF Templates. The former is based on the Arduino IDE while the latter is based on the ESP-IDF environment that will not be used in the course.

  • Connect an LED to GPIO 21 with a resistor, as shown in the diagram (rotate - R and flip - P components if necessary).

  • Simulate the following sketch:

    #define LED_PIN 21
    #define BUTTON_PIN 35
    
    void setup() {
        pinMode(LED_PIN, OUTPUT);
        pinMode(BUTTON_PIN, INPUT_PULLUP);
    }
    
    void loop() {
        digitalWrite(LED_PIN, HIGH);
        delay(500);
        digitalWrite(LED_PIN, LOW);
        delay(500);
    }
    

    Question

2. Control the LED state with a push button

  • Implement the circuit shown in the following diagram and simulate a program that:

    • Turns the LED ON when the button is pressed.
    • Turns the LED OFF when the button is not pressed.

    Tip

    • Use the function digitalRead() to get the status of the button.
    • You may need to use the if...else condition.

    Warning

    When you place the button, don’t forget to deselect the bounce option to avoid bouncing issues.

    Question

    • What information is included in the diagram.json? Did it change with respect to the previous exercise?
    • Can you do the exercise without using the if...else condition? How?

3. Additional exercises

  • Using the same circuit as in the previous exercise, write a script that Blinks the LED while the button is pushed.

    Tip

    You may need to use the millis() function.

3.2. Short press vs long press

  • Using the same circuit as in the previous exercise, write a script that does the following:

    • Short press (< 500 ms): Toggle LED (if the LED is ON, turns it OFF, and viceversa).
    • Long press (≥ 500 ms): Blink LED.

    Tip

    You may need to use the millis() function, and the if...else condition.