enabling variable internal ref voltage on mega

according to the atmega1280 specification, the internal reference voltage can be selected at either 1.1v or 2.56v. (not just 2.56v, as the wiki suggests)

The analogReference(uint8_t mode) function simply sets a variable that is (later) used by analogRead(uint8_t pin) to set bits 6 and 7 (REFS1:0) of the ADMUX register. (These details can be observed in .../arduino/hardware/cores/arduino/wiring_analog.c) The REFS1:0 bits determine the source of the reference voltage as follows:

REFS1 REFS0 Voltage reference selection
0 0 AREF, internal Vref off
0 1 AVCC
1 0 Internal 1.1v Voltage reference
1 1 Internal 2.56v Voltage reference

.../arduino/hardware/cores/arduino/wiring.h only makes the following defines:
#define INTERNAL 3
#define DEFAULT 1
#define EXTERNAL 0
as you can see - there is no definition for the internal 1.1vref.

Adding the following to wiring.h will allow you to select an internal 1.1v voltage reference:
#if defined(AVR_ATmega1280)
#define INTERNAL_1V1 2
#define INTERNAL_2V56 3 //added for completeness
#endif

selecting the analog reference is as simple as:
analogReference(INTERNAL_1V1);
or
analogReference(INTERNAL_2V56);

of course, INTERNAL_1V1 is just a macro... to use the internal 1.1v reference without modifying any of the arduino code, simply use the following:
analogReference(2)

I have tested this and can easily switch between an internal reference voltage of 1.1v or 2.56v. I hope this comes in handy!

I do NOT know how this will behave on other hardware - hence the '#if defined(AVR_ATmega1280)' - consult your hardware manual!

Happy Hacking!

Nice find. This should be brought to the attention of the Arduino development team so that it can be added to a future release of the IDE.

Lefty

Issue created: Google Code Archive - Long-term storage for Google Code Project Hosting.

Thanks,
i have also use this in the 168 and 328, (the 1.1Volt i mean)
could you add this also in those?
Thanks
Pat Gadget
Montreal

Thanks,
i have also use this in the 168 and 328, (the 1.1Volt i mean)
could you add this also in those?
Thanks
Pat Gadget
Montreal

1.1v is the only internal voltage reference on the 168/328. Nothing to add!
(I finally checked the 168 and 328 documentation. Setting bits 6 and 7 of ADMUX register [which is all the analogReference(...) does] to a value of 2 is "Reserved." In short, don't do it.)

The 1280 has either 2v56 or 1v1. The above modification simply makes a define that isn't present for the 1280.

Is there any way to allow INTERNAL to still be valid on mega bords? If someone has a sketch for another board that uses analogReference(INTERNAL) it will nolonger compile.

Thanks

George :slight_smile:

I believe this...

  analogReference( INTERNAL1V1 )

...is the replacement. You can also do this...

  analogReference( INTERNAL2V56 )

neat to see this make it into the last release.

myrdyn: take codingbadly's advice and change the parameter for analogReference(..) -- when compiling for the mega

http://code.google.com/p/arduino/issues/detail?id=194
-- "I got rid of the INTERNAL define on the Mega because it's ambiguous. "

...

or you could just hack "#define INTERNAL 3" into the top of your code