Objective
The objective of this laboratory exercise is to learn how to read digital input signals using the ESP32 development board. In this experiment, students will connect a push button and use it to control an LED.
This laboratory exercise helps students understand:
- Digital input and output in microcontrollers
- How push buttons work in electronic circuits
- Using conditional statements in embedded programming
- Interacting with hardware using the ESP32
Introduction
Microcontrollers interact with the external world through input and output pins. Inputs allow the system to receive signals from sensors or switches, while outputs allow it to control devices such as LEDs, motors, and relays.
A push button is one of the simplest input devices used in electronics. When the button is pressed, it changes the voltage level of a GPIO pin. The ESP32 reads this signal and performs an action based on the program logic.
This experiment demonstrates how the ESP32 reads a button input and uses it to control an LED.
Laboratory Equipment and Components
The following components are required for this experiment.
| Component | Quantity |
|---|---|
| ESP32 DevKit V1 development board | 1 |
| Breadboard | 1 |
| LED | 1 |
| Push button | 1 |
| 220Ω resistor (for LED) | 1 |
| 10kΩ resistor (pull-down resistor) | 1 |
| Jumper wires | Several |
| USB cable | 1 |
| Computer with Arduino IDE installed | 1 |
Theory
A push button acts as a momentary switch that connects or disconnects a circuit when pressed.
To properly read a button with a microcontroller, a pull-down resistor is used. This resistor ensures that the GPIO pin reads a stable LOW signal when the button is not pressed.
Button states:
| Button State | GPIO Signal |
|---|---|
| Not pressed | LOW (0V) |
| Pressed | HIGH (3.3V) |
When the ESP32 detects a HIGH signal from the button, the program will turn the LED ON.
Circuit Diagram
Follow these connections to build the circuit.
LED Circuit
GPIO2 → Resistor → LED → GND
Push Button Circuit
GPIO4 → Push Button → 3.3V
GPIO4 → 10kΩ Resistor → GND
This configuration ensures stable input readings.
Experimental Procedure
Step 1: Assemble the Circuit
Connect the LED and push button to the ESP32 using a breadboard according to the circuit diagram.
Step 2: Connect the ESP32
Connect the ESP32 board to your computer using the USB cable.
Step 3: Open Arduino IDE
Launch the Arduino IDE software.
Step 4: Select the Board
Navigate to:
Tools → Board → ESP32 Arduino → ESP32 Dev Module
Step 5: Write the Program
Enter the following code in the Arduino IDE.
int ledPin = 2;
int buttonPin = 4;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if(buttonState == HIGH){
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
}
Step 6: Upload the Program
Click the Upload button in the Arduino IDE.
If necessary, press the BOOT button during the upload process.
Expected Output
After uploading the program:
- When the push button is pressed, the LED turns ON
- When the push button is released, the LED turns OFF
This confirms that the ESP32 is correctly reading the digital input signal.
Discussion
This experiment demonstrates how microcontrollers interact with external input devices. The ESP32 continuously checks the state of the button using the digitalRead() function.
The program uses an if-else conditional statement to control the LED depending on the button state.
Such input systems are widely used in real-world applications such as:
- User interface buttons
- Control panels
- Industrial automation switches
- Safety control systems
Understanding digital inputs is an important step toward building more complex embedded systems.
Conclusion
In this laboratory exercise, students learned how to read a digital signal from a push button and use it to control an LED using the ESP32 development board.
This experiment demonstrated:
- Basic digital input operation
- Button interfacing techniques
- Conditional programming in embedded systems
These concepts are essential for building interactive embedded devices.
Guide Questions
- What is the purpose of the pull-down resistor in the push button circuit?
- What happens if the pull-down resistor is removed?
- What function is used to read the state of the button?
- What type of signal does the push button generate?
- How could the program be modified so that the button toggles the LED instead of holding it ON?
1. What is the purpose of the pull-down resistor in the push button circuit?
A pull-down resistor guarantees that the ESP32 input pin maintains a LOW voltage level (0) when the push button is open. This provides a digital signal that is stable and reliable.
2. What happens if the pull-down resistor is removed?
Without the pull-down resistor, the input pin is floating, and it can be interpreted as HIGH or LOW due to electrical noise. This can cause the LED to turn ON and OFF with button presses when in fact the button is not pressed.
3. What function is used to read the state of the button?
The function used is: digitalRead(buttonPin).
– this function reads the voltage level of the GPIO pin and determines whether the button is pressed or not.
4. What type of signal does the push button generate?
The push button generates a digital signal.
It produces only two states:
• HIGH (3.3V) – button pressed
• LOW (0) – button not pressed
5. How could the program be modified so that the button toggles the LED instead of holding it ON?
The program that will be used in the system can be modified to store the LED’s current state and change it every time the button is pressed. Instead of just keeping the LED ON while the button is pressed, the program can be designed such that when the button is pressed (LOW) it toggles the state of the LED. This will cause a single press to turn the LED ON and another press to turn it OFF. This will provide a toggle effect.
6. Application in Agricultural and Biosystems Engineering (ABE)
Programming a push button is important in Agricultural and Biosystems Engineering because many agricultural systems require manual control and user interaction for proper operation. Push buttons can be used to manually start or stop water pumps in irrigation systems, allowing farmers to manage water distribution effectively. They are also useful for dispensing feed for livestock or poultry, ensuring controlled and timely feeding. In agricultural processing, push buttons serve as start and stop controls for machines such as rice dryers, grain conveyors, and threshers, helping maintain efficient workflow. Additionally, push buttons can function as manual override controls in automated sensor-based systems, giving operators the ability to intervene when necessary. They also play a critical role as emergency stop buttons for agricultural machines, improving safety by enabling quick shutdown during emergency situations.
makaBUGong_agricultura_lab2
makaBUGong_agricultura_lab2-page2
makaBUGong_agricultura_lab2-page3
1. The pull-down resistor keeps the circuit stable at zero volts when the button is not being pressed. This prevents the input pin from picking up random electrical noise that could cause errors. It ensures the microcontroller always sees a clear “OFF” signal until you actually push the button.
2. Removing the resistor causes the pin to “float,” meaning it has no solid connection to ground or power. This makes the circuit very sensitive, leading to the LED turning on or off randomly without any touch. The system becomes unpredictable because it cannot distinguish between a real press and background static.
3. To check the button’s status on an ESP32, you still use the digitalRead() function within the development environment. This command tells the controller to look at a specific GPIO pin to see if it is receiving a high or low voltage. By assigning the result to a variable, the program can then decide whether to turn the LED on or off.
4. A push button generates a digital signal, which means it only has two distinct states. It is either “1” (on) when the circuit is closed or “0” (off) when the circuit is open. There are no values in between, making it a simple binary input for the controller.
5. To make the button toggle the LED, the code must remember the last state of the light using a variable. Instead of just checking if the button is held, the program should only switch the light when it detects a brand-new press. Adding a tiny delay also helps the computer ignore “bouncing” signals that happen in a split second.
6. Greenhouse Ventilation and Lighting – Buttons on a control panel allow for the manual toggling of grow lights or exhaust fans during plant inspections. This provides a safe way to pause automated climate systems while performing physical labor inside the greenhouse. The pull-down resistor is especially important here to prevent moisture or electrical noise from causing false triggers.
1. What is the purpose of the pull-down resistor in the push button circuit?
– According to the website provided under theory section, the pull-down resistor is utilized for the button to be read by the microcontroller, as well as to ensure that the pins in the ESP32 will read a stable LOW signal when the button is not pressed. Basically, the pull-down resistor is what connects or functions the push button to the circuit and the ESP32.
2. What happens if the pull-down resistor is removed?
– Since it is the component which connects the push button to the circuit and the ESP32, the circuit will not work and there will be no connection between the LED light on whether it will light up or not when the button is pressed.
3. What function is used to read the state of the button?
– In the code, the function digitalRead( ) is used to read the state if the button, following an if-else conditional statement to manage and control the LED in accordance to the button state. This function specifically shows how microcontrollers connect and interact with external input devices, namely the ESP32 and the push button.
4. What type of signal does the push button generate?
– Basically, the HIGH signal indicated that the LED will turn on while the LOW signal will make the LED turned off as it is not pressed or pressed after pushing the button. When the HIGH signal is indicated, it uses the 3.3V connection from the microcontroller.
5. How could the program be modified so that the button toggles the LED instead of holding it ON?
– The program could be modified so the button toggles the LED by adding a variable that changes the LED state every time the button is pressed. Instead of keeping the LED ON while holding the button, the LED will switch ON and OFF each time the button is clicked.
6. Where can you apply programming a push button in your current course Agricultural and Biosystems Engineering?
– Programming a push button can be applied in different agricultural machines and automated systems. Push button can be used to control or operate equipment easily. For example, it can be programmed to start or stop irrigation pumps, turn on greenhouse ventilation fans, or activate feeding systems in livestock farms. It can also be used in grain dryers and small agricultural machines where the operator only needs to press a button to run or stop the system. Because of this, programming a push button helps make farm operations simpler, safer, and more efficient.
GROUP CORNHUB.COM (UBINA, DOLOR, IDMILAO, CARANGUIAN, VIERNES
1. What is the purpose of the pull-down resistor in the push button circuit?
Pull-down resistor keeps and makes sure that the circuit is at low when the push button is not pressed. It helps the circuit read the button correctly and consistently.
2. What happens if the pull-down resistor is removed?
If the pull-down resistor is removed, the input pin will float, meaning it has no clear high and low value. This causes the circuit to randomly think that the push button is being pressed even though is not. This makes the program behaves unpredictally and confused.
3. What function is used to read the state of the button?
In most microcontroller platforms such as Arduino, the function used to read the button state is:
digitalRead(pin);
This function reads the voltage level of the specified input pin and returns either HIGH or LOW.
4. What type of signal does the push button generate?
A push button generates a digital signal. It has only two states:
HIGH (1) – when the button is pressed and voltage is applied to the input pin.
LOW (0)– when the button is not pressed and the pull-down resistor connects the pin to ground.
5. How could the program be modified so that the button toggles the LED instead of holding it ON?
The program can be modified by detecting a button press event and changing the LED state each time the button is pressed. This requires storing the previous state of the LED and switching it whenever the button transitions from LOW to HIGH.
Example concept:
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && lastButtonState == LOW) {
ledState = !ledState; // toggle LED
digitalWrite(ledPin, ledState);
}
lastButtonState = buttonState;
In this modification, every press of the button toggles the LED state (ON OR OFF) instead of keeping it ON only while the button is pressed.
6. Applications of Button LEDs in Agricultural and Biosystems Engineering
Button LEDs (small, compact LED indicators) have several practical applications in this field:
1. *Sensor & Monitoring Systems*
– Status indicators on soil moisture sensors, pH meters, and nutrient analyzers
– Visual alerts on weather stations and microclimate monitors
– Indicator lights on data loggers showing active recording or battery status
2. *Irrigation & Water Management*
– Control panel indicators on automated irrigation systems showing zone activation
– Status lights on drip irrigation controllers (on/off, fault, standby modes)
– Pump operation indicators in water distribution systems
3. *Greenhouse & Controlled Environment Agriculture*
– Status indicators on climate controllers (temperature, humidity, CO₂ regulators)
– Fault/alarm indicators on ventilation and shading systems
– Power and operation status on grow light controllers
4. *Machinery & Equipment*
– Dashboard indicator lights on tractors and farm equipment
– Safety warning indicators on harvesters and planters
– Fault indicators on post-harvest processing equipment (threshers, dryers)
5. *Food Processing & Biosystems*
– Status lights on fermentation monitoring systems
– Indicators on sterilization and pasteurization equipment
– Quality control panel lights on sorting and grading machines
6. *Animal Production Systems*
– Feeding system status indicators in poultry and livestock houses
– Alarm lights on environmental control systems in animal enclosures
– Indicators on milking machine control panels
7. *Precision Agriculture Devices*
– Power/connection status on GPS and drone ground stations
– Indicator lights on variable-rate application controllers
– Status lights on field data collection devices
8. *Bioprocess Engineering*
– Status indicators on bioreactors and fermenters
– Fault lights on centrifuges and separation equipment
– Operation indicators on waste treatment systems
1. What is the purpose of the pull-down resistor in the push button circuit?
– A pull-down resistor ensures that the microcontroller pin stays at LOW (0V) when the button is not being pressed. When the ESP32 detects a HIGH(3.3V) signal from the button, the program will turn the LED ON.
2. What happens if the pull-down resistor is removed?
– The GPIO pins become floating. The microcontroller might read high or low randomly. And the ESP 32 might think that the button is pressed even when it is not.
3. What function is used to read the state of the button?
– The function used to read the state of the button is digitalRead().
4. What type of signal does the push button generate?
– The push button generates a digital signal.
When the button is pressed, the LED turns on. When the button is released, the LED turns off.
5. How could the program be modified so that the button toggles the LED instead of holding it ON?
– To make the button toggle the LED, the program should be changed so that the LED switches its state every time the button is pressed. Instead of the LED staying ON only while the button is held down, the program records the LED’s current state and reverses it when the button is pressed. This means the first press turns the LED ON, and the next press turns it OFF.
6. Where can you apply programming a button in your course Agricultural and Biosystems Engineering
– Programming a button in Agricultural and Biosystems Engineering has several applications, including irrigation systems where a button is used to control irrigation systems based on moisture levels in the soil, greenhouse environments where a button is used to control temperature, humidity, or lighting levels, agricultural robots where a button is used for override control or for specific functions in agricultural robots, and data logging where a button is used to start or stop data recording of environmental parameters such as temperature or humidity levels. All these applications are aimed at increasing efficiency in agricultural activities.