Taking Actions Based on Conditions Arduino And MicroPython
In this section, we explore how to execute specific blocks of code based on conditions in your ESP32 projects. This capability is essential for implementing responsive behaviors such as activating an LED when a switch is pressed or responding to sensor inputs.
What We Will Learn in This Section:
- How to use conditional statements (if, if…else) in ESP32 programming.
- Implementing actions based on sensor readings or input states.
- Enhancing project interactivity and responsiveness.
Why Is This Lesson Important to You?
Understanding conditional execution is fundamental in programming. It allows you to create dynamic applications that respond intelligently to changing conditions, making your ESP32 projects more interactive and adaptable.
Components List:
- ESP32 Board
- Pushbutton Switch
- LED
- Resistors (if necessary)
- Jumper Wires
- Breadboard (optional, for easier connections)
- Power Source (e.g., USB cable)
Circuit Diagram (With Connection):
LED:
- Anode (long leg): Connect to GPIO 13 (or a designated GPIO pin) through a current-limiting resistor.
- Cathode (short leg): Connect to GND.
Push-button switch:
- One terminal: Connect one terminal to GPIO 17.
- Other terminal: Connect the other terminal to GND.
Code Arduino:
const int LED_BUILTIN = 13; // Built-in LED pin on ESP32 (adjust if needed)
const int inputPin = 17; // Define the input pin connected to a button or sensor
void setup()
{
pinMode(LED_BUILTIN, OUTPUT); // declare LED pin as output
pinMode(inputPin, INPUT_PULLUP); // declare pushbutton pin as input with internal pull-up resistor
}
void loop()
{
int val = digitalRead(inputPin); // read input value
if (val == LOW) // Input is LOW when the button is pressed
{
digitalWrite(LED_BUILTIN, HIGH); // turn LED on if switch is pressed
}
else
{
digitalWrite(LED_BUILTIN, LOW); // turn LED off if switch is not pressed
}
}
Here’s the explanation of the Arduino code:
1. Variable Declarations
const int LED_BUILTIN = 13; // Built-in LED pin on ESP32 (adjust if needed)
const int inputPin = 17; // Define the input pin connected to a button or sensor
- const int LED_BUILTIN = 13;: Defines the pin number for the built-in LED. This is typically pin 13 on many ESP32 boards.
- const int inputPin = 17;: Defines the pin number for the input device, such as a button or sensor.
2. Setup Function
void setup()
{
pinMode(LED_BUILTIN, OUTPUT); // declare LED pin as output
pinMode(inputPin, INPUT_PULLUP); // declare input pin with internal pull-up resistor
}
- pinMode(LED_BUILTIN, OUTPUT);: Configures the LED pin as an output. This allows the pin to send voltage to control the LED.
- pinMode(inputPin, INPUT_PULLUP);: Configures the input pin as an input with an internal pull-up resistor. This means the pin will read HIGH when not pressed and LOW when the button is pressed.
Code MicroPython:
from machine import Pin
import time
input_pin = Pin(2, Pin.IN) # choose the input pin (for a pushbutton)
led_builtin = Pin(3, Pin.OUT) # built-in LED pin
while True:
val = input_pin.value() # read input value
if val == 0: # Input is 0 when the button is pressed
led_builtin.on() # turn LED on if switch is pressed
else:
led_builtin.off() # turn LED off if switch is not pressed
time.sleep(0.1) # slight delay for stability
Here’s the explanation of the MicroPython code:
1. Pin Initialization
input_pin = Pin(2, Pin.IN) # choose the input pin (for a pushbutton)
led_builtin = Pin(3, Pin.OUT) # built-in LED pin
- input_pin = Pin(2, Pin.IN): Initializes pin 2 as an input pin. This pin is connected to a pushbutton or similar input device.
- led_builtin = Pin(3, Pin.OUT): Initializes pin 3 as an output pin. This pin is connected to the built-in LED.
2. Main Loop
while True:
val = input_pin.value() # read input value
if val == 0: # Input is 0 when the button is pressed
led_builtin.on() # turn LED on if switch is pressed
else:
led_builtin.off() # turn LED off if switch is not pressed
time.sleep(0.1) # slight delay for stability
- val = input_pin.value(): Reads the current value of the input pin. The value is 0 if the button is pressed and 1 if it is not pressed.
- if val == 0:: Checks if the input value is 0 (button is pressed).
- led_builtin.on(): Turns the LED on if the button is pressed.
- else:: If the input value is not 0 (button is not pressed).
- led_builtin.off(): Turns the LED off if the button is not pressed.
- time.sleep(0.1): Introduces a small delay to provide stability and avoid rapid toggling due to noise.
Summary:
Mastering conditional statements is crucial for implementing responsive behaviors in your ESP32 projects. Whether you’re reacting to user inputs, sensor readings, or external events, understanding how to structure your code based on conditions allows you to create more interactive and intelligent applications. This capability forms the foundation for more complex logic and decision-making processes in your ESP32 programming journey.
This section equips you with essential skills to enhance the functionality and interactivity of your ESP32 projects. Understanding these concepts will pave the way for more sophisticated applications in IoT and embedded systems.