Potentiometer values not being displayed with analogRead(). Is it my code?

Hello, I’m just getting 0’s as outputs when viewing serial monitor. I’m trying to get potentiometer values from 0 to 1023.

int redLed = 2;
int blueLed = 3;
int potPin = A0;

void setup() {
  Serial.begin(9600);
  pinMode(potPin, OUTPUT);
  pinMode(blueLed, INPUT);
  pinMode(redLed, INPUT);
}

int potValue = analogRead(potPin);

void loop() {
  Serial.println(potValue);
}

This line

int potValue = analogRead(potPin);

is out of place. It should be in the loop() function immediately before the Serial.print(...) statement.

However, having fixed this, you may have a new problem from trying to take readings too fast. The delay(...) function could fix this but millis() may be a better solution in the long run. It really depends upon what you are trying to accomplish.

@vaj4088 Interesting. Thank's for the hint. However, my value only goes to about 890, instead of the 1023 which seems to be the norm. Also, mind explaining why exactly why that part had to go inside the loop? I've seen others, specifically with digitalRead()'s outside the loop, and it seemed to work fine.

You have your INPUTs and OUTPUTs mixed up.
A pot is an input for the processor, and a LED an output.

Pins don't have to be declared INPUT (can do), because they default to INPUT at bootup.
Leo..

You need full 5.0 volts on A0 to get 1024 as a value.

Most potentiometers don’t go all the way down to 0 ohms of resistance.

@Wawa Good catch, thank you. That fixed it for me.

Try this pot sketch I wrote for a 0-100% display.
Leo…

// converts the position of a 10k lin(B) pot to 0-100%
// pot connected to A0, 5volt and ground

int rawValue;
int oldValue;
byte potPercentage;
byte oldPercentage;

void setup() {
  Serial.begin(115200); // set serial monitor to this baud rate, or change the value
}

void loop() {
  // read input twice
  rawValue = analogRead(A0);
  rawValue = analogRead(A0); // double read
  // ignore bad hop-on region of a pot by removing 8 values at both extremes
  rawValue = constrain(rawValue, 8, 1015);
  // add some deadband
  if (rawValue < (oldValue - 4) || rawValue > (oldValue + 4)) {
    oldValue = rawValue;
    // convert to percentage
    potPercentage = map(oldValue, 8, 1008, 0, 100);
    // Only print if %value changes
    if (oldPercentage != potPercentage) {
      Serial.print("Pot percentage is: ");
      Serial.print(potPercentage);
      Serial.println(" %");
      oldPercentage = potPercentage;
    }
  }
}

vaj4088:
This line

int potValue = analogRead(potPin);

is out of place. It should be in the loop() function immediately before the Serial.print(...) statement.

This line: 'int redLed = 2;' is also 'out of place'; but, the compiler takes it as a part of the program (the sketch). What is the technical reason for the line 'int potValue = analogRead(potPin);' not to be executed unless it is included either in the setup() or the loop() function?

The line is not in the loop() function, so it gets executed once but not repeatedly. If it happens to be zero, "potValue" will stay zero.

'int redLed = 2;' only needs to be executed once.

The reversed inputs and outputs was a good catch that I missed and was also a problem.

I did not think that a double read was needed unless the channel was changed. Was I wrong?

vaj4088:
I did not think that a double read was needed unless the channel was changed. Was I wrong?

Do you mean the extra analogRead line in my code?
Indeed not needed here, maybe only if you use more channels/pots.
The ‘double read’ line can be removed.
Leo…

vaj4088:
'int redLed = 2;' only needs to be executed once.

Is the above quoted statement an executable statement? Is this command for the Compiler or for the MCU or for both?

@Wawa Very cool code. I only hope one day I'll be able to make something so clean and functional. Definitely the best pot UI I've seen to date.

@vaj4088 Thanks for explaining. It makes sense that it has to be in the loop to continuously output the change. It's honestly something I should have probably figured out by now. I feed dumb.

Anyone have any suggestions of where to learn more Arduino programming, after having completed the first official starter kit?

Slumpert:
You need full 5.0 volts on A0 to get 1024 as a value.

Most potentiometers don’t go all the way down to 0 ohms of resistance.

You will NEVER get 1024 with a ten bit ADC.

@ AWOL

AWOL:
You will NEVER get 1024 with a ten bit ADC.

Why not?

@Wawa

rawValue = analogRead(A0);
  rawValue = analogRead(A0); // double read

Mind explaining what the point of the double read is?

and

Serial.print("Pot percentage is: ");
      Serial.print(potPercentage);
      Serial.println(" %");

why this works like it does? The first two, I get don't get printed on a new line, and you get "pot percentage is: (potPercentage)". But then you tell the "%" to get printed on a new line, shouldn't that get printed below "pot percentage is: (potPercentage)" instead of right after?

@toxicarrow: you do the arithmetic.

toxicxarrow:
Why not?

Mind explaining what the point of the double read is?

But then you tell the "%" to get printed on a new line

Count your fingers, but don't start at 1, start at 0.

That's a common 'fix' if you use more than one analogue channel, and if the channels are influencing each other because the presented source impedance is more than 10k.
Not needed here, because only one analogue channel is used, and the pot is 10k.
Forgot to remove it. Can leave it in though, because it's only using a tiny bit of processor time (~100us).

Serial.println(); // is using a new line after it has printed '%'.
Leo..

Wawa:
Count your fingers, but don't start at 1, start at 0.

When a human being is asked to count his fingers thinking himself as a human being, he will always always start counting from 1. There is no way to stop it; use of don't simply will not work. If he is asked to count his fingers thinking himself as a machine/computer, he fantastically starts counting from 0 and stops at 9 with a result of 10 fingers.

toxicxarrow:
@ AWOL Why not?

A simple exercise:-

  1. Write down 1024 converted to binary (you may use a calculator).

  2. Count the number of digits required to do this (ignore leading zeros).

  3. How does this compare to the 10 bits available from the A to D converter?

  4. Repeat 1 to 3 for 1023.

Ian

AWOL:
You will NEVER get 1024 with a ten bit ADC.

toxicxarrow:
@ AWOL Why not?

AWOL:
@toxicarrow: you do the arithmetic.

Assume that VREF of the 10-bit ADC is connected with 5V. That means that the Full Scale (FS) of the ADC is 5V. ‘Full Scale’ is a value which when applied to the input of an ADC, all the bits of the ADC will assume Logic High (1s).

So, when we apply 0V (min input) at the input of the ADC, the ADC value is all 0s, = 00 0000 0000 (0 decimal)
So, when we apply 5V (max input) at the input of ADC, the ADC value is all 1s = 11 1111 1111 (1023 decimal)

Anyway, many members of the Forum will not (does not) agree with the above line of thought?