-1

Has anyone successfully used the AREF pin on MKR1000?

I'm trying to use an EXTERNAL voltage reference (I need 1.5V instead of the default 5V) with:

analogReference(EXTERNAL)

But when I compile I receive the following error message:

MKR_001:161: error: invalid conversion from 'int' to 'eAnalogReference {aka _eAnalogReference}' [-fpermissive]

In the documentation (for analogReference) I found the following:

DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)
INTERNAL: an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 (not available on the Arduino Mega)
INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only)
INTERNAL2V56: a built-in 2.56V reference (Arduino Mega only)
EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference.

Thanks, Corrado

3
  • 3
    What doesn't work. What have you tried and in what way did it fail? Commented Nov 26, 2016 at 11:36
  • Please edit your question with this information and any more that you have. Also, try to make your question more specific. Commented Nov 28, 2016 at 16:58
  • Voted to reopen, you've edited the question and I think it's a very good question now. Commented Nov 28, 2016 at 17:38

2 Answers 2

1

The compiler error

MKR_001:161: error: invalid conversion from 'int' to 'eAnalogReference {aka _eAnalogReference}' [-fpermissive] 

Is actually really simple once you understand what it means.

analogReference(EXTERNAL);

The function analogReference takes an integer as a parameter, but you are passing it EXTERNAL which is an enumeration. Its probably an enumeration that uses integers for its values, but to the compiler there is a difference.

If you want to disable the warning then you can use the -fpermissive flag on the compile command line, this is not a good idea because it invalidates the "type-safeness" of the language for the whole program. Instead it is better to address them on a case by case basis and just cast EXTERNAL to an int.

analogReference((int)EXTERNAL);
3
  • Interesting, it's somewhat a bug of the 'analogReference()' function to expect an int (could be much more than just one of the enum values)? Commented Nov 30, 2016 at 13:27
  • Its not a bug, its a feature :) I think it stems from the tightening of implicit type conversion within the gcc compiler. I knew it straight away, because I see it so often at work, dragging some legacy code into this century. Someone could raise it as a fault and have it changed, but its not going to be a priority. Commented Nov 30, 2016 at 15:51
  • Well, yeah, if you keep it too long out there, people will go use integers to choose the reference. And then correcting it may break those peoples code again. But your answer is a very good one. Commented Nov 30, 2016 at 15:57
1

In fact, the documentation suggests that for SAMD boards (MKR1000 is based on Zero) a different enumeration should be used where each value is prefixed with AR_ so for example you should use analogReference(AR_EXTERNAL);.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.