After learning how to upload data to Firebase using ESP32, the next important step is to read data from Firebase to ESP32. This is essential for remote control applications, real-time monitoring, and smart automation systems.
In this tutorial, you’ll learn how to fetch realtime values from Firebase Realtime Database using the ESP32 and the latest Firebase_ESP_Client
library. Whether you’re controlling GPIO pins, updating displays, or responding to commands from the cloud, this guide is the foundation of two-way communication in IoT.
Prerequisites
Make sure you have the following:
- ESP32 board
- Arduino IDE (1.8.19 or newer)
- Install ESP32 Boards in arduino IDE
- Firebase project with Realtime Database enabled
- Working Wi-Fi connection
- Firebase secret and database URL
Firebase_ESP_Client
library installed
Use Case: Firebase Sends a Command to ESP32
In this example, we will:
- Write a value manually in Firebase (like “ON” or “OFF”)
- Read that value from Firebase to ESP32
- Print it to the Serial Monitor
- Optionally, control an LED or any output pin based on the value
Install Firebase ESP Client Library
- Open Arduino IDE
- Go to Sketch > Include Library > Manage Libraries
- Search for
Firebase ESP Client
- Install the library by Mobizt named Firebase_ESP_Client
This library provides full support for ESP32 Firebase operations, including authentication, secure HTTPS requests, and real-time database communication.

Create Firebase Realtime Database
- Open the Firebase Console
- Click “Add Project” and follow the steps to create a project
- Go to Build > Realtime Database
- Click “Create Database”
- Choose your location and start in test mode
This will give you a working Realtime Database that accepts write requests without restrictions.
Get Firebase Credentials
To use the ESP32 Firebase connection method shown in this guide, you need two things:
1. Database URL
- Found at the top of your Realtime Database dashboard
- Format:
your-project-id.firebaseio.com

2. Database Secret
- Go to Project Settings > Service Accounts > Database Secrets
- Click “Show” and copy the secret key

This secret allows the ESP32 to connect using the legacy authentication method.
Arduino Code to Read Data from Firebase to ESP32
Here is the code to read a string value from the Firebase Realtime Database and display it on the Serial Monitor.
//Firebase to ESP32
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
#include <addons/RTDBHelper.h>
// Wi-Fi credentials
#define WIFI_SSID "YOUR_WIFI_SSID"
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"
// Firebase credentials
#define DATABASE_URL "your-project-id.firebaseio.com"
#define DATABASE_SECRET "your_firebase_database_secret"
// Firebase objects
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long lastReadTime = 0;
String lastValue = "";
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
config.database_url = DATABASE_URL;
config.signer.tokens.legacy_token = DATABASE_SECRET;
Firebase.reconnectNetwork(true);
fbdo.setBSSLBufferSize(4096, 1024);
Firebase.begin(&config, &auth);
}
void loop() {
if (Firebase.ready() && millis() - lastReadTime > 3000) {
lastReadTime = millis();
if (Firebase.RTDB.getString(&fbdo, "/esp32/command")) {
String command = fbdo.stringData();
if (command != lastValue) {
Serial.print("New command received: ");
Serial.println(command);
lastValue = command;
// You can control an output here
// Example:
// if (command == "ON") digitalWrite(LED_BUILTIN, HIGH);
// else digitalWrite(LED_BUILTIN, LOW);
}
} else {
Serial.print("Failed to read: ");
Serial.println(fbdo.errorReason());
}
}
}
Add the required data and upload the code in ESP32.
Firebase Console Testing
- In the Firebase Realtime Database, update the value under
/esp32/command
to"ON"
,"OFF"
or any other string. - The ESP32 will detect and print the new value on the Serial Monitor every 3 seconds.
- You can extend the code to control GPIO, relays, or displays based on the value received.
SEDING ON COMMAND:

RECEIVING IN SERIAL MONITOR:

SENDING OFF COMMAND:

RECEIVING IN SERIAL MONITOR:

Try This Small Task: Control an LED or Relay from Firebase
Now that you’re able to read data from Firebase to ESP32, try this simple practical task:
Objective:
Turn an LED (or relay) ON or OFF based on a boolean command in your Firebase database.
Hardware Required:
- 1 x ESP32
- 1 x LED (or Relay Module)
- 1 x 220-ohm resistor (if using LED)
- Jumper wires
Wiring:
- LED: Connect to GPIO 5 via 220Ω resistor
- Relay: Connect IN pin to GPIO 5
Firebase Database Structure:
Update your Firebase Realtime Database to include a command like this:
{
"commands": {
"led": true
}
}
Code Snippet (Add to loop):
#define RELAY_PIN 5 // or LED_PIN
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Default OFF
// ... WiFi and Firebase setup
}
void loop() {
if (Firebase.RTDB.getBool(&fbdo, "/commands/led")) {
bool ledState = fbdo.boolData();
digitalWrite(RELAY_PIN, ledState ? HIGH : LOW);
Serial.println(ledState ? "Relay ON" : "Relay OFF");
}
delay(5000);
}
expected Result
- When you change
/commands/led
in Firebase totrue
, your relay or LED will turn ON. - Set it to
false
to turn it OFF.
Must share your result in comments!
Applications of Firebase to ESP32 Communication
- Remote device control via mobile or web app
- Realtime command execution in smart home systems
- Cloud-based device configuration
- Two-way communication in industrial IoT setups
This simple example of reading from Firebase to ESP32 can be extended into more powerful cloud-controlled applications.
Conclusion
This tutorial demonstrated how to read realtime data from Firebase to ESP32 using the Firebase_ESP_Client
library. Now that your ESP32 can both send and receive data from Firebase, you have built the foundation for a full IoT communication loop.
By mastering two-way communication with Firebase to ESP32, you open the door to endless possibilities — from remote control dashboards to intelligent automation systems. In future guides, we’ll show you how to:
- Timestamp and log incoming Firebase values
- Trigger hardware actions based on cloud changes
- Display data on OLED or LCD screens
- Use Firebase functions or security rules for advanced logic
HOW TO SEND DATA FROM ESP32 TO FIREBASE IN REALTIME DATABASE:
ESP32 Firebase Guide: Send Any Sensor Data To Firebase Using Esp32 – ArduinoYard