Problem by using analogReference() with Mega2560

Hello everybody,
I have an Arduino Mega2560. I connected 6 analog sensor to pins 0-5. the maximum output for each sensor is 1.2 volt. I tried to use the function analogReference(INTERNAL) to change the analog range from 5V to some 1.1V. the problem is that when i add this function to my program i get a compilation error.

const int analogInPin[6] = {0,1, 2, 3, 4, 5};
int sensorValue = {0,0,0,0,0,0}; // value read from the pot
void setup() {
// initialize serial communications at 9600 bps:


void loop() {

// read the analog in value:
for (int i=0; i <= 5; i++){
sensorValue = analogRead(analogInPin*);*
* Serial.print(“s(”);*
* Serial.print(i);*
* Serial.print(")");*
_ Serial.print(sensorValue*);

* }*

* Serial.println();*
when i compile i get an error:
_6DOF_acc_gyr_lesen:8: error: expected constructor, destructor, or type conversion before ‘(’ token
can anybody please help?

Thanks a lot, now it works. :slight_smile:

  1. The analogReference() statement is executable code and must be inside a function, not just sitting by itself outside all functions. (So: Put it in setup().)

  2. For ATmega1280 and '2560 chips there are two internal analog reference possibilities: 1.1 and 2.56

From arduino-0021/hardware/arduino/cores/arduino/wiring.h:

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define INTERNAL1V1 2
#define INTERNAL2V56 3
#define INTERNAL 3

Bottom line change your sketch to somehting like:

void setup() {
    analogReference(INTERNAL1V1); //For 1280 and 2560

void loop() {

Not that the reference value is nominally 1.1 Volts with minimum and maximum specifications of 1.0 and 1.2



Thanks for the helpful info. Now i can read all my sensor values with more precision. But still i am wondering if it is possible to define the measuring range. For example in my case the sensor output voltage range is between (0.8 and 1.2) volts. So i am not using the whole resolution(1024). My output values are fluctuating between (744 and 1024). So am losing the big resolution from ( 0 to 743). Is there any way in which i can map the sensor output voltage(0.8-1.2) to the whole resolution(0-1024)?

I updated the analogReference() page to include the other INTERNAL options:

Is there any way in which i can map the sensor output voltage(0.8-1.2) to the whole resolution(0-1024)?

With a 1.1 Volt A/D reference voltage, each bit of the A/D reading represents about 1.074 millivolts. If the input range is 400 millivolts, you can't get more than 372 different values from the A/D. Once the A/D converter has been read, nothing in software can increase the significant precision.

I mean, you can "spread out" the values to go from 0 to 1023, but you can not improve the precision of the sensor readings since you will still only have 372 different possibilities.

An external level shifting and amplifying op amp (or other circuitry) might be used to expand the sensor values so that the A/D converter is presented with values between zero and 1.1 volts, in which case the A/D readings can be full range (0 through 1023).



sorry for interupting, i have problem

here's my code

void setup() { Serial.begin(9600); analogReference(INTERNAL1V1); }

void loop() { //analogReference(DEFAULT); delay(500); int sensorValue = analogRead(A0); Serial.println(sensorValue, DEC); delay(1000); }

when i use the code above, all i get is 1023..and can't move no matter what the input. but if analog reference is in default, i can capture the data but with lousy resolution...

is there any solution? or my arduino mega is broke or something?

Don't interrupt and don't cross-post

okay sorry for that..