readAnalog() returns constant 33x value

Hey all,

I’m not sure this is a hardware or “programmer” issue, but let me post it an see what the experts think.

First, I have no problem reading digital values on Pin 4 (Or any of the analog pins).

But, when I use readAnalog on a pot or voltage divided input, or even direct gnd, or direct +5, I only get the a constant value of 33x .

Here is my code:

Created by David Cuartielles
Modified 4 Sep 2010
By Tom Igoe

This example code is in the public domain.


int sensorPin = 4;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
  // pinMode(sensorPin,INPUT);
  // digitalWrite(sensorPin, LOW);

void loop() {

  // read the value from the sensor:
  sensorValue = analogRead(sensorPin); 
  // sensorValue = 1020; 
  // sensorValue = digitalRead(sensorPin);

  Serial.println(sensorValue, DEC); 
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for <sensorValue> milliseconds:
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for <sensorValue> milliseconds:

Output is: 336, 337,336,337,336 …
Notice the digital read setup/loop commands are commented out, but work fine when I switch to digital.
Using the same pin…

What am i doing wrong? or is my board a problem?

  • Ken

Are you definitely connecting your electronics to the Analog pin 4 NOT the digital pin 4?

There is nothing wrong with your code, as far as I can see.


int sensorPin = 4; // select the input pin for the potentiometer

for: int sensorPin = A4; // select the input pin for the potentiometer

Wow, a little sleep and some great advice does wonders.

FYI: Changing 4 to A4 has no effect, I think that nomenclature is for readability (Not a bad thing).

However, using the clearly marked "ANALOG IN" bank of pins did the trick. :D After yrs of programming, I'm still humbled by simple assumptions (not to mention cabling is 90% of issues).

I suspect I let myself get distracted with the documentation that indicates you can re-provision the analog input pins for digital use....

Thanks guys!

I think that nomenclature is for readability

And for interoperability - you can write digitalRead (A4); or analogRead (A4); (or "pinMode" or "digitalWrite"), without having to remember all that tedious "pin 14 to...19 if on a Uno else if on a Mega" stuff.