analogread() only returning 8 bits

I’m trying to use a pentometer with my Arduino Mega, but analogread only seems to return the 8 lower bits, ignoring the first two. As I turn the pentometer it goes up to around 256, the jumps back to 0. It does this four times. I’ve tried multiple pentometers all different ports, but it’s the same problem.

The Knob servo example shows this problem, as does simply sending the values to the computer over serial. Is there something physical I’m missing (like adding a resistor)?

My code (I commented out the servo code):

// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> 

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
Servo myservoB;
 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
int valB;
 
void setup() 
{ 
  Serial.begin(9600);
  Serial.println("Pent Readings");
 /* myservo.attach(6);
  myservoB.attach(9); */ // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  Serial.println(String(val));
/*  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val); */
  delay(10);
}

Thank you.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom… :slight_smile:

Let's see your code: what variable type are you reading the value into? Maybe an int?.....

JimboZA: Let's see your code: what variable type are you reading the value into? Maybe an int?.....

Arduino ints are 16 bit signed. Maybe he used code from a sketch with the cross platform types. uint_8 is popular and that is a true unsigned 8 bitter and would cause a problem.

Yeah I meant he should use an int, sorry I worded it badly but in my defence it was 6am on a Saturday. It might be that at the moment he's trying to fit it into a byte.

Well, as we always say: |500x161

(Yes, of course he is clipping it to a byte.)

Sounds like your reading the analogue value into a byte instead of an int.

My bet is it is a char not a byte. :)

Grumpy_Mike: My bet is it is a char not a byte. :)

Whatever!

It was always a normal int.
Sorry, I added the code to the top post.

A Pentometer?

// Per.

Turn the knob until analogRead() returns 250-255, or something near it.

Then use a multimeter and measure the voltage going into the analog pin.

What voltage do you measure?

  Serial.println(String(val));

Serial.println(val); //works just fine

Are you sure you have the pot connected right? (I know, stupid question)

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Please don't update previous posts, although you added the code it makes for a discontinuous run of posts that follow it.

Tom..... :)