Skip to main content

Basic IO

Overview​

This lab is a simple introduction to programming pin inputs and outputs for the ESP32/Arduino. If you want to challenge yourself, try completing the objectives below in order before referring to the example code!

Objectives​

  • Turn on LED
  • Blink LED every 1 second
  • Make LED only turn on when holding a button pressed (Using on-board button)⭐︎

Wiring Diagram​

PlatformIO Banner

Code Solutions​

#include <Arduino.h>

#define LED_PIN 25 // Defining LED pin number
#define BUTTON_PIN 2 // Defining button pin number

void setup() {
pinMode(LED_PIN, OUTPUT); // Initializing LED pin as an output
pinMode(BUTTON_PIN, INPUT_PULLUP); // Initializing button pin as an input
}

void loop() {
// Checking if button is pressed
if (digitalRead(BUTTON_PIN)) {
digitalWrite(LED_PIN, HIGH); // Turning LED on
delay(1000); // 1000ms delay
digitalWrite(LED_PIN, LOW); // Turning LED off
delay(1000); // 1000ms delay
}
}
note

One of the major differences between the basic and advanced programming solution is the fact that the advanced solution is non-blocking. Since we are not triggering a delay in the main loop, other components of your script/program can continue to run while waiting for the next LED state switch