UNO analog to digital data conversion

discussion about how to light up a LED using a water level sensor with an IDE sketch

Easy enough to do but you need to post what sensor(s) you plan to use? There is plenty of ways to detect a water level and make things happen, however, the type sensor needs to be known,

Welcome to the community.

Eon

Also, the type of Arduion (UNO/NANO/MEGA) you are using.

i'm using arduino UNO and the water level sensor that comes with the ARDUINO UNO set and also thank you for welcoming me here

Does it look like this?

Also, which Uno, the R3, R4 or Q?

Ok. That's a sensor that works with measuring the resistance using an analog input correct?

Here is a simple sketch that uses

a) a slide potentiometer which can be replaced by an analog water level sensor (Edit: But read my comment below ... )

b) in parallel a digital switch that replaces a level switch (e.g. a float switch)

As the sketch measures once every second there is no need to debounce the switch.

/*
  Forum: https://forum.arduino.cc/t/uno-analog-to-digital-data-conversion/1446441
  Wokwi: https://wokwi.com/projects/465468745125615617

  2026/05/30
  ec2021


*/

constexpr byte ledPin {10};
constexpr byte analogPin {A0};
constexpr byte digitalPin {2};
constexpr uint16_t threshold {512};
constexpr unsigned long interval {1000}; // Check the water level every second (1000 ms)
unsigned long lastCheck;
byte ledState;

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  pinMode(digitalPin, INPUT_PULLUP);
  Serial.println("Start");
  ledOff();
}

void loop() {
  if (millis() - lastCheck >= interval) {
    lastCheck = millis();
    checkWaterLevel();
  }
}

void ledOn() {
  digitalWrite(ledPin, HIGH);
  ledState = HIGH;
}

void ledOff() {
  digitalWrite(ledPin, LOW);
  ledState = LOW;
}

void checkWaterLevel() {
  boolean digitalAlert = !digitalRead(digitalPin);
  boolean analogAlert  = (analogRead(analogPin) > threshold);  // or < threshold depending on realization
  if (digitalAlert || analogAlert) {
    if (ledState == LOW) {
      Serial.println("Water Level LOW");
    }
    ledOn();
  } else {
    if (ledState == HIGH) {
      Serial.println("Water Level HIGH");
    }
    ledOff();
  }
}

You can test it on Wokwi: https://wokwi.com/projects/465468745125615617

Wiring

Good luck!
ec2021

BTW:

  • Check the correct threshold for your sensor
  • Check the values of your sensor
    • when it's dry
    • when it's wet

You may have to change something in the code to get the appropriate alert ... ;-)

yes that one and i use UNO R3

thx a lot

Then see this tutorial.
It explains everything you need to know and more.

thank you very much for your help i appreciate it a lot

If you have any problems just ask.

The most important thing is:

Try to understand how the code works

The example I posted works but not 1:1 with the sensor you have ...

It provides a lot of information but you will have to change a little bit for your application.

If you are interested in learning to write applications for Arduino you will surely find where to make changes.

Good luck and have fun with Arduino!
ec2021

I moved your topic to a more appropriate forum category @kaito_kid1412.

The UNO Family > UNO Breakout Carrier category you chose is only used for discussions directly related to the Arduino UNO Breakout Carrier product. You aren't using an UNO Breakout Carrier, so it was not appropriate to choose this category for your forum topic.

In the future, when creating a topic please take the time to pick the forum category that best suits the subject of your question. There is an "About the _____ category" topic at the top of each category that explains its purpose.

Thanks in advance for your cooperation.

1 Like

You did this experiment? Congratulatons!!

As you are beginning with programming you may try to get a copy of Michael Margolis Arduino Cookbook.

Just search the Internet for further information!

There are PDFs available for free (but probably not legal) ...

wdym by "probably not legal" i don't get it

There are sources in the internet where Michael Margolis book can be downloaded in PDF format.

It's a good possibility to have a look but I doubt that the copies are legally available.

If you want to be sure you better buy the book. It's worth the price, especially if you go for a used one.

Just search the Internet using the keywords

"Arduino Cookbook Michael Margolis"..

(I'm not a friend of illegal copies where people have spent a lot of time and effort to create a helpful document. The effort should honored.)