Lesson Progress
0% Complete
GPIO Mastery (Digital & Analog)
Objective: Learn how to send signals (Output) and receive information (Input) using both Digital and Analog methods.
Presentation:
Esp32_4_Digital Input & Output by Infinite Engineers1. Digital I/O (Input/Output)
Digital signals are binary—they are either HIGH (3.3V) or LOW (0V).
- Digital Output: Used to turn things on or off, like an LED or a Buzzer.
- Function:
digitalWrite(pin, HIGH);
- Function:
- Digital Input: Used to check the state of a device that has only two conditions, like a Push Button or an IR Sensor.
- Function:
digitalRead(pin);
- Function:
2. Analog I/O (Input/Output)
Presentation:
Esp32_6_Analog Input by Infinite Engineers Esp32_6_Analog Input by Infinite EngineersThe real world isn’t just ON or OFF; it has ranges (like the brightness of a light or the speed of a motor).
- Analog Input (ADC): The ESP32 converts incoming voltage (0V to 3.3V) into a number between 0 and 4095.
- Components: LDR (Light Sensor) or Potentiometer.
- Function:
analogRead(pin);
- Analog Output (PWM): The ESP32 uses Pulse Width Modulation (PWM) to “fake” an analog voltage by pulsing a digital signal very fast. This is how we dim an LED or control motor speed.
- Function:
ledcWrite(channel, value);(Note: ESP32 usesledcfunctions for PWM instead of the standardanalogWrite).
- Function:
3. Hands-on Experiment:
Project 1: The Toggle Switch (Digital I/O)
In this project, the student will use the pushbutton to toggle the state of an LED.
- Logic: If the button is pressed (Input), change the LED state (Output) from OFF to ON, or vice versa.
- Hardware: Use the ESP32 , a 4-pin switch , and an LED.
- Wiring:
- Connect the Switch to Pin 4 (Input).
- Connect the LED to Pin 2 (Output).
The Code Snippet:
const int buttonPin = 4;
const int ledPin = 2;
bool ledState = LOW;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Internal resistor keeps it HIGH until pressed
pinMode(ledPin, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin) == LOW) { // Button pressed
ledState = !ledState; // Toggle logic
digitalWrite(ledPin, ledState);
delay(200); // Debounce delay
}
}
Project 2: Breathing LED (PWM Output)
This project teaches students how to create a “breathing” effect (fading in and out) using Pulse Width Modulation (PWM). This is the foundation for controlling motor speeds later in the day.
- Concept: Instead of just HIGH or LOW, we vary the Duty Cycle to change the average voltage the LED receives.
- Hardware: ESP32 , LED , and a 1K Resistor.
The Code Snippet:
const int ledPin = 2;
int brightness = 0;
int fadeAmount = 5;
void setup() {
// ESP32 uses ledc for PWM
ledcAttach(ledPin, 5000, 8); // Pin, Frequency (5kHz), Resolution (8-bit: 0-255)
}
void loop() {
ledcWrite(ledPin, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount; // Reverse the fading direction
}
delay(30);
}
Required Things for these Projects
- ESP32 / Esp8266 30 pin board
- USB Cable
- Breadboard
- LED (Assorted)
- 4-pin Switch
- 1K Ohm Resistor
- Jumper Wires (M to M)