LED stays on seemingly without power

Hello, I am trying to code a basic water-level sensor by scratch and have run across problem. I have power going out of one pin via jumper wire and into a glass of water, I then have another jumper wire from another pin into the water, this one is listening. I said that if the 2nd pin gets power, turn the led on, and if it doesn't, turn it off. The problem is the following: When I place the listening wire into the water, the LED turns on like it should, but when I take it out the LED stays on until I let go of it.

Here is my code:

// setting the pin variables
const int readPin = 12;
const int ledPin1 = 5;
const int powerSupply = 3;

void setup() {
  Serial.begin(9600);
  pinMode(readPin, INPUT);
  pinMode(powerSupply, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  digitalWrite(powerSupply, HIGH); // starting the powersupply
}

void loop() {
  int waterState = digitalRead(readPin);
  if (waterState == HIGH) {
    digitalWrite(ledPin1, HIGH);
    Serial.println(waterState);
  }
  else {
    digitalWrite(ledPin1, LOW);
    Serial.println(waterState);
  }
delay(1);
}

Is there a problem in the code? Are my jumper wires the problem?

Extra Info:
I am using an Arduino Uno, but it is an Elegoo board not the official.
Sometimes the LED randomly comes on when wires are not in water.
The water container is a mug (not sure if that matters at all).
The jumper wires and board came from a kit:Amazon.com: Customer reviews: ELEGOO UNO R3 Project Most Complete Starter Kit w/Tutorial Compatible with Arduino IDE (63 Items)
I had not error messages.

Your read pin is floating. You would need a resistor to tie the pin to ground so that it will read 0V when it is not touching anything. Google "floating input" for a better explanation.

Delta_G:
Your read pin is floating. You would need a resistor to tie the pin to ground so that it will read 0V when it is not touching anything. Google “floating input” for a better explanation.

I’ve had green and blue SMT LEDS that were so sensitive that merely soldering them to the (unpowered) board caused a faint glow (ESPECIALLY the blue ones).

To tell which is the case, try printing out to the serial monitor what you read from the read pin. I strongly suspect that you will see that the pin is still high when you remove it from the water so that it is connected to nothing.

The key point there is that connected to nothing doesn’t mean LOW. LOW means connected to ground. HIGH means connected to 5V (actually 3V is enough). But connected to nothing at all doesn’t put you in either of those conditions. When the wire is connected to nothing it is essentially an antennae picking up whatever stray noise and reading that.

Delta_G:
To tell which is the case, try printing out to the serial monitor what you read from the read pin. I strongly suspect that you will see that the pin is still high when you remove it from the water so that it is connected to nothing.

The key point there is that connected to nothing doesn't mean LOW. LOW means connected to ground. HIGH means connected to 5V (actually 3V is enough). But connected to nothing at all doesn't put you in either of those conditions. When the wire is connected to nothing it is essentially an antennae picking up whatever stray noise and reading that.

This makes so much sense! Thanks you! How would I go about attaching a resistor to it though? I have tried putting the readPin to the breadboard and from there a resistor to ground and another wire into the water, but now it never comes on, its like the resistor is stripping away the energy when it does get those 5Vs. Is this an invalid way to fix my problem?

techman1221:
This makes so much sense! Thanks you! How would I go about attaching a resistor to it though? I have tried putting the readPin to the breadboard and from there a resistor to ground and another wire into the water, but now it never comes on, its like the resistor is stripping away the energy when it does get those 5Vs. Is this an invalid way to fix my problem?

Did you ever wonder why schematics have those lines all over them, instead of pages of text?

techman1221:
This makes so much sense! Thanks you! How would I go about attaching a resistor to it though? I have tried putting the readPin to the breadboard and from there a resistor to ground and another wire into the water, but now it never comes on, its like the resistor is stripping away the energy when it does get those 5Vs. Is this an invalid way to fix my problem?

Well, in the case of a button or switch it is easy. But in your case the water also has some resistance, so you have to use a large enough resistor to ground that when the wire is in the water it will read from that side. In essence, you are creating a voltage divider.

Try adding a little salt to the water and see if that will lower the resistance enough to help. Or use a larger value pull-down.

What value of resistor did you try?

groundFungus:
What value of resistor did you try?

I used a 10k resistor.

Delta_G:
Well, in the case of a button or switch it is easy. But in your case the water also has some resistance, so you have to use a large enough resistor to ground that when the wire is in the water it will read from that side. In essence, you are creating a voltage divider.

Try adding a little salt to the water and see if that will lower the resistance enough to help. Or use a larger value pull-down.

I would use a diagram to describe the following, but I find it hard considering a component of my circuit is a glass of water and not very many programs offer that. In words, I have changed by circuit to: 5V into the water, wire out of water and into bread board, from that wire into a 10k Ohm resister back to ground, and wire after that back to pin 2. I then have a led set up to turn on when pin 2 reads high voltage.

Are you suggesting I use a greater value resister than the 10k Ohm, or add salt with that 10k? Or is it possible that my resister should be in a different place?

Please tell me if I am being unclear. Thanks for the help so far :D!

Can you draw a picture? That would be worth a thousand words.

Delta_G:
Can you draw a picture? That would be worth a thousand words.

Crudely drawn with a mouse for the interweb’s enjoyment.

Again, any unclarity tell me and I will attempt to explain.

OK, yeah you've got it wired right. Now think about the resistance in the water and the 10K resistor. Do you see how they form a voltage divider with your pin reading the center point?

So you can measure the resistance between the two leads in the water and you can calculate what voltage the pin will see. It will read a HIGH when it sees about 3V or higher. Less than 1.5V will read as low. In between is, well, in between and could go either way. So if you know the resistance across the glass of water you could calculate what value of resistor it would take for the pin to see more than 3V. My guess is that it will take considerably more than 10K ohms.

The other way you could attack that would be to reduce the amount of resistance in the glass of water. That's what I was talking about when I mentioned adding some salt to give it a lower resistance.

And it may even take a combination of those two.

Delta_G:
OK, yeah you've got it wired right. Now think about the resistance in the water and the 10K resistor. Do you see how they form a voltage divider with your pin reading the center point?

So you can measure the resistance between the two leads in the water and you can calculate what voltage the pin will see. It will read a HIGH when it sees about 3V or higher. Less than 1.5V will read as low. In between is, well, in between and could go either way. So if you know the resistance across the glass of water you could calculate what value of resistor it would take for the pin to see more than 3V. My guess is that it will take considerably more than 10K ohms.

The other way you could attack that would be to reduce the amount of resistance in the glass of water. That's what I was talking about when I mentioned adding some salt to give it a lower resistance.

And it may even take a combination of those two.

Oooooooohhh! Now it makes sense! I will test a combination of the two tomorrow as it 11 where I am and I need to get to bed. Thanks in advance as I'm sure this will work!

Delta_G:
OK, yeah you've got it wired right. Now think about the resistance in the water and the 10K resistor. Do you see how they form a voltage divider with your pin reading the center point?

So you can measure the resistance between the two leads in the water and you can calculate what voltage the pin will see. It will read a HIGH when it sees about 3V or higher. Less than 1.5V will read as low. In between is, well, in between and could go either way. So if you know the resistance across the glass of water you could calculate what value of resistor it would take for the pin to see more than 3V. My guess is that it will take considerably more than 10K ohms.

The other way you could attack that would be to reduce the amount of resistance in the glass of water. That's what I was talking about when I mentioned adding some salt to give it a lower resistance.

And it may even take a combination of those two.

Huzzah success at last! I used a 1 million Ohm resistor and no salt was needed! Thanks so much for your help and time :slight_smile: