Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Complete Sensor Guide
      • Engineering Deep Dives
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Tech Terms
    • Webinars & Digital Events
  • Resources
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • White Papers
  • Guest Post Guidelines
  • Advertisers
    • Advertise
    • PR Submissions
    • Eblast Submissions
  • Subscribe

How to build an IoT-based flood monitor using ESP8266

By Nikhil Agnihotri April 2, 2024

Floods are generally considered natural disasters. Depending on their severity, they can have a devastating impact on human life, agriculture, and infrastructure. In serious cases, floods can result in significant economic losses due to property damage, disruption of transportation, and the cost of emergency response and recovery efforts. 

This is why, flood monitoring is crucial for public safety, infrastructure protection, economic stability, and quick and effective disaster responses. With the increasing frequency and intensity of extreme weather events linked to climate change, monitoring has become even more critical. 

In this project, we’ll design an Internet-of-Things (IoT) based online flood monitor using the ThingSpeak platform — a popular IoT analytics service platform. We’ll build this device on an ESP8266 WiFi development board, integrating it with the ThingSpeak server for the online flood indicator. 

Required components 

  1. ESP8266 x1
  2. Red LED x1
  3. Green LED x1
  4. HC-SR04 Ultrasonic sensor x1
  5. 330Ω Resistor x2
  6. Connecting wires or jumper wires 

Circuit connections
This flood-monitoring device uses the ESP8266 Wi-Fi development board to measure the level of water in a river, lake, or any water body. If the water level rises to an alarming level, the device turns on the flood indication.

ESP8266 is interfaced with the HC-SR04 ultrasonic sensor. The sensor’s echo and trigger pins are connected to ESP8266’s GPIO2 and GPIO1 pins, respectively. The sensor’s VCC and GND pins are connected to ESP8266’s 3V out and ground pins.

Two LEDs are also interfaced with ESP8266 for on-spot indication. A red LED is connected to ESP8266’s GPIO3 and a green LED is connected to ESP8266’s GPIO4 via series resistors (330 ohms). The LEDs are connected with ESP8266 to source current from its pins.

 

Circuit connections

Creating a channel on ThingSpeak
After the circuit connections of the device are complete, we must create a ThingSpeak channel for it. Visit ThingSpeak.com and sign up for an account. Verify your email and login to your ThingsSpeak account. Click on ‘Create New Channel’ to create a channel.

Name the channel, ‘ThingSpeak Flood Indicator’. Then, rename the field1 to ‘River Level’ and click on ‘Save Channel.’

 

In the ‘Private View’ tab, click on the ‘Add Widgets’ button.

Select ‘Lamp Indicator’ and set its attributes as shown below.

Click on ‘Create’ button to finish creating the widget. Now, you have a channel on the ThingSpeak server that can monitor water levels and warn of a potential flood.

Note down your channel ID and Author key from the main dashboard, as well as the Write API key from under the ‘API Keys’ tab.

These will be required later in the sketch.

The Arduino sketch
Upload the following sketch to ESP8266. Remember to replace the ThingSpeak channel ID, Author Key, Write API Key, Wi-Fi SSID, and Wi-Fi password with your own.

How it works
The ultrasonic sensor is interfaced with ESP8266, which measures the water level. If the distance measured between the sensor and the (lake or river) water level is less than four meters, it signals a flood.

If ESP8266 measures a water level above four meters, a green LED remains ON. If ESP8266 measures a water level below four meters, a red LED is switched ON and the green LED is turned OFF.

The value of the distance between the sensor and the water level is sent to the ThingSpeak server at regular intervals. The value of the river level for the ThingSpeak server is under field1. If this value goes below four, the ThingSpeak’s lamp indicator also turns red.

An overview of how the flood-monitoring system works.

The code
The sketch begins by importing the ThingSpeak.h and ESP8266WiFi.h libraries. The ThingSpeak.h library is required to connect and communicate data with the ThingSpeak server. The ESP8266WiFi.h library is used to connect ESP8266 with a Wi-Fi network.

The variables are declared for the pin assignments for the ESP8266, the HC-SR04 ultrasonic sensor, and the LEDs. This is followed by declaring the variables to store the ThingSpeak channel ID, Write API key, Author Key, Wi-Fi SSID, and Wi-Fi password. The variables are declared and stored in milliseconds, current milliseconds, period (equal to 10000 milliseconds), duration, and distance. A Wi-Fi client is instantiated.

In the setup() function, the pins connected to the sensor’s trigger pin and the LEDs are configured for a digital output. The ESP pin connected to the sensor’s echo pin is configured as a digital input. The LEDs are turned OFF by passing a LOW signal. The baud rate for the serial console is set to 9600 bps.

The Wi-Fi client is initialized and connected to your Wi-Fi network. The Wi-Fi client initializes an object with the ThingSpeak server. The current time lapsed is stored as a ‘startmillis’ variable.

In the loop() function, ESP8266 transmits a LOW-to-HIGH pulse to the sensor’s trigger pin, reading the distance between the sensor and the water level by calling the pulseIn() method. This method returns the duration of the ultrasonic signal. The distance is calculated in the ‘distance’ variable using the following equation.

Distance= (Time x Speed of Sound in Air (340 m/s))/2

If the distance measured is less than four meters, a red LED is turned ON and a green LED is turned OFF. Otherwise, the red LED remains OFF and the green LED remains ON. The current time is stored as a ‘currentmillis’ variable. It’s deducted from the value in the ‘startmillis’ variable to determine the time that elapses between sensor readings.

If the time elapsed is greater than 10000 milliseconds, the distance value is updated to the ThingSpeak server by calling the ThingSpeak.setField() and the ThingSpeak.writeFields() methods. The current time is reset to the start time.

Results

 

You may also like:


  • Forest fire detection system using IoT sensor network

  • What temperature sensors are used for electronics and IoT devices?

  • What are the top weather APIs for the IoT?

  • How to design an IoT-based smart alarm

  • How to extend solar and battery power in a weather…

Filed Under: Electronic Projects, ESP8266
Tagged With: esp8266, flood, floodmonitor, hc-sr04, led, monitor, sensor, thingspeak, ultrasonicsensor
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

Log in to leave a comment:

Lost your password?

Don't have an account? Register here

Submit a Guest Post

submit a guest post

EE TECH TOOLBOX

“ee
Tech Toolbox: Wide Bandgap Semiconductors
Moving from silicon to GaN or SiC feels like a whole new ballgame, doesn’t it? It isn’t just about faster switching; it’s about managing the layout parasitics and thermal realities that come with that speed. This month’s Tech Toolbox features a curated eBook that tackles these design hurdles head-on.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS feed: EDABOARD.com Discussions. EDABOARD.com Discussions.

  • 7555 with no discharge resistor for monostable operation?
  • I have an Acer Aspire 5 A515-54G-53H6 issue with mother board
  • PCB not properly made
  • Flyback converter working incorrectly
  • Improve Equipment Efficiency

RSS feed: Electro-Tech-Online.com Discussions Electro-Tech-Online.com Discussions

  • Electronics and PIC Programming Assembler Update
  • Getting rid of old magazines
  • PIC code for 2.42" I2C OLED Display?
  • Getting Back Into Electronics
  • Best way to check charge and discharge many mixed batteries?

Featured Tutorials

LoRa and LoRaWAN: A Complete Guide to Wireless Communication

  • Best practices for securing LoRa networks
    Best practices for securing LoRa networks
  • Understanding LoRa architecture: nodes, gateways, and servers
    Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
    Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
    How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
    How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices
    How communication works between two LoRa end-node devices
More Tutorials >

Recent Articles

  • Texas Instruments adds agentic AI to CCStudio
  • onsemi adds trench SiC for power systems
  • Silanna launches matched chipset for multi-channel LiDAR
  • Renesas launches browser-based embedded development hub
  • Diodes targets EV charging current sensing

EE ENGINEERING TRAINING DAYS

engineering
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2026 Arrowfly LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of Arrowfly
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Complete Sensor Guide
      • Engineering Deep Dives
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Tech Terms
    • Webinars & Digital Events
  • Resources
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • White Papers
  • Guest Post Guidelines
  • Advertisers
    • Advertise
    • PR Submissions
    • Eblast Submissions
  • Subscribe