2.8 KiB
Button-Triggered Sensor Display
This document explains how to use the button-triggered sensor display example, which demonstrates an energy-efficient approach to reading and displaying sensor data on ESP32/ESP8266 devices.
Overview
The button-triggered display example shows how to:
- Set up a button input on an ESP device
- Use low-power sleep mode to conserve energy
- Wake up and read sensor data when the button is pressed
- Display the data on an OLED screen
This approach is ideal for battery-powered applications where energy conservation is important.
Hardware Requirements
- ESP32 or ESP8266 development board
- DHT22 temperature and humidity sensor
- SSD1306 OLED display (128x64 pixels recommended)
- Pushbutton
- 10K pull-up resistor (if your button doesn't have an internal pull-up)
- Breadboard and jumper wires
Wiring
-
DHT22 Sensor:
- Connect VCC to 3.3V
- Connect GND to ground
- Connect DATA to GPIO4 (or change the pin in the code)
-
OLED Display:
- Connect VCC to 3.3V
- Connect GND to ground
- Connect SCL to GPIO22 (or change the pin in the code)
- Connect SDA to GPIO21 (or change the pin in the code)
-
Button:
- Connect one side to GPIO0 (or change the pin in the code)
- Connect the other side to ground
- Connect a 10K pull-up resistor between GPIO0 and 3.3V (if not using internal pull-up)
Running the Example
- Flash MicroPython to your ESP device if you haven't already
- Upload the
button_triggered_display.pyscript to your device - Run the script
import button_triggered_display
button_triggered_display.main()
How It Works
Energy Conservation
The example uses ESP32's light sleep mode to conserve energy when not actively reading or displaying data. In light sleep mode:
- The CPU is paused
- RAM is preserved
- Peripherals can be configured to wake the device
- Power consumption is significantly reduced
Button Wake-Up
The device is configured to wake up from sleep when the button is pressed. This is done using the wake_on_ext0 function, which allows an external pin to trigger a wake-up event.
Simulation Mode
The example includes a simulation mode that runs when not on actual ESP hardware. This allows you to test the functionality on a development computer before deploying to the ESP device.
Customization
You can customize the example by:
- Changing the GPIO pins for the sensor, display, or button
- Adjusting the display time before going back to sleep
- Adding additional sensors
- Modifying the information displayed on the OLED screen
Power Consumption
Typical power consumption in different states:
- Active mode (reading sensors and updating display): ~80-120mA
- Light sleep mode: ~0.8-1.5mA
This represents a power saving of approximately 98% during idle periods, significantly extending battery life.