Wrong readings from analogRead

I am really a started here and I have this strange situation. I running the script below, it is supposed to get the readings from a water sensor and print the value in the console.
The problem is it prints values when nothing is connected. No pins at all. Only the power is connected via a USB cable. Nothing else. The console should be printing the value of zero but for some reason it prints values around 410 or 420.
I have tried restarting, unplugging the USB cable and use another port but nothing changes. The arduino still gives wrong readings despite nothing being connected to it.

Is my board broken please?

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-water-sensor
 */

#define POWER_PIN  7
#define SIGNAL_PIN A0

int value = 0; // variable to store the sensor value

void setup() {
  Serial.begin(9600);
  pinMode(POWER_PIN, OUTPUT);   // configure D7 pin as an OUTPUT
  digitalWrite(POWER_PIN, LOW); // turn the sensor OFF
}

void loop() {
  digitalWrite(POWER_PIN, HIGH);  // turn the sensor ON
  delay(10);                      // wait 10 milliseconds
  value = analogRead(SIGNAL_PIN); // read the analog value from sensor
  digitalWrite(POWER_PIN, LOW);   // turn the sensor OFF

  Serial.print("Sensor value ");
  Serial.println(value);

  delay(1000);
}

Probably not.

When nothing is connected to an Arduino pin it is said to be "floating" and can pick up electrical signals from the environment.

This will result in a voltage that will be read by analogRead()

Thanks but something doesnt really make sense. When I connect the pins to a sensor I still get the same readings. When I dip the sensor into the water I get the same readings again. They are still within the range 410 and 420. When I clean the sensor from the water, again no change in the range of the values.
The sensor looks to work ok. In the sense that it gets power, it has a led and the led blinks. I even tried another sensor, but still got the same behaviour.

What type of sensor are you using ?

Please post a diagram of your circuit. A picture of a hand drawn circuit is good enough

What type of Arduino board are you using ?

thanks for helping, I took a photo, hope that helps. I am just replicating this mini guide

1 Like

As a quick analog input test on an UNO, simply wire the input in question to the 3.3V pin. Then see what you read.

Your code says that you are reading from pin A0 but your picture shows that you have nothing connected to A0

image

1 Like

Thanks! I managed to confuse myself. I was experimenting, changing the pins to see if it works on another one and forgot to put it back at the correct place at the end. That was so stupid,
I didnt really expect it to have such a high reading when nothing is connected to it but you are right.
Thanks again!

Glad I could help

A second pair of eyes is often usual. Failing that I explain to myself (or the cat) how the project is wired and what the code does which often results in spotting what is wrong

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.