Arduino Project

Water Level 2.0

Course Introduction

In this lesson, you’ll learn how to build a simple water level indicator using an analog water level sensor and 8 LEDs with the Arduino UNO R4.

As the water level rises, more LEDs light up to visually display the current level in real time.

Note

If this is your first time working with an Arduino project, we recommend downloading and reviewing the basic materials first.

1.1 Install Arduino IDE(Important)
1.2 Introduction of Arduino IDE

Required Components

In this project, we need the following components:

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Arduino UNO R4 Minima/Arduino UNO R4 WIFI

1

2

USB Cable

1

×

3

Breadboard

1

BUY

4

Wires

Several

5

Water Level Detection Module

1

×

6

Resistor

1KΩ

7

LED

3

Wiring

9.webp__PID:d7e2cf90-e8db-40bc-a8e3-400e92466546

Common Connections:

Water Level Detection Module
A:
Connect to A0 on the Arduino.
G: Connect to breadboard’s negative power bus.
V: Connect to breadboard’s red power bus.

LEDS
Connect the LED cathode to a 1KΩ resistor then to the negative power bus on the breadboard, and the anode to the 2~9 on the Arduino.

Writing the Code

Note
You can copy this code into Arduino IDE.
Don’t forget to select the board(Arduino UNO R4 Minima/WIFI) and the correct port before clicking the Upload button.


// LED pin array
const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
const int numLEDs = 8;

// Water level sensor analog input pin
const int sensorPin = A0;

void setup() {
  Serial.begin(9600);
  // Set all LED pins as output
  for (int i = 0; i < numLEDs; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  // Read the water level sensor value (0~1023)
  int sensorValue = analogRead(sensorPin);
  Serial.print("Water level analog value: ");
  Serial.println(sensorValue);

  // Map the analog value to number of LEDs (0~8)
  int level = map(sensorValue, 0, 450, 0, numLEDs);

  // Turn on LEDs according to the water level
  for (int i = 0; i < numLEDs; i++) {
    if (i < level) {
      digitalWrite(ledPins[i], HIGH);
    } else {
      digitalWrite(ledPins[i], LOW);
    }
  }

  delay(500);  // Delay before next update
}