Arduino Forum

Using Arduino => Project Guidance => Topic started by: mhart9 on Mar 14, 2017, 06:03 pm

Title: Help with Arduino Mkr Zero
Post by: mhart9 on Mar 14, 2017, 06:03 pm
Hello Everyone,
I was hoping to solicit some help with understanding the Mkr Zero board a little better.  I've tried extensive searches and have come up with fairly little information on the Mkr Zero.  Mostly what I'm interested in is wiring help.  So here are the questions I have regarding the Mkr Zero and I sincerely apologize if they are illustrated in a tutorial or information page, I have just been searching for awhile and have not come up with concrete answers.  Also I'm pretty new to microconcrollers and so have a hard time deciphering the schematics of the mkr Zero posted on the site. 

So here are my questions: 

What options do I have for powering the mkr zero?  I see that a 3.7V lipo battery is recommended but as I will be powering the rest of the project with a solar recharged 12V system, I was thinking of using a buck converter to drop it to acceptable voltage and power the mkr Zero that way.  Is 5V wired to the VIN pin what is called for?  Are any current regulators needed with a large Ah battery bank?

How do I wire up that AREF pin and is it the default?  I've tried using the analogReference(EXTERNAL) command and it throws errors

Code: [Select]
Arduino: 1.8.1 (Mac OS X), Board: "Arduino MKRZero"



/Users/matthart/Documents/Arduino/experiment_with_aref/experiment_with_aref.ino: In function 'void setup()':
experiment_with_aref:3: error: invalid conversion from 'int' to 'eAnalogReference {aka _eAnalogReference}' [-fpermissive]
 analogReference(EXTERNAL)
                         ^
In file included from /Users/matthart/Library/Arduino15/packages/arduino/hardware/samd/1.6.12/cores/arduino/Arduino.h:91:0,
                 from sketch/experiment_with_aref.ino.cpp:1:
/Users/matthart/Library/Arduino15/packages/arduino/hardware/samd/1.6.12/cores/arduino/wiring_analog.h:47:13: error:   initializing argument 1 of 'void analogReference(eAnalogReference)' [-fpermissive]
 extern void analogReference( eAnalogReference ulMode ) ;
             ^
experiment_with_aref:4: error: expected ';' before '}' token
 }
 ^
exit status 1
invalid conversion from 'int' to 'eAnalogReference {aka _eAnalogReference}' [-fpermissive]

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
.


 Upon searching for this I found a forum thread that didn't help me understand the problem very well.

Also in regard to AREF I found this resource which recommends a technique for getting a more accurate reference voltage: http://www.skillbank.co.uk/arduino/measure.htm

Yet after reading that page I am unsure of how to wire a MCP1525 to the AREF pin or for that matter if its even necessary, as he is using an UNO and I'm using a mkr Zero.  For example, did they make it so the Arduino uses a more accurate internal voltage reference?

http://ww1.microchip.com/downloads/en/DeviceDoc/21653C.pdf


Also here is the code that generated that error, it was simply an experiment and so is very basic
Code: [Select]
void setup() {
  // put your setup code here, to run once:
analogReference(EXTERNAL)
}

void loop() {
  // put your main code here, to run repeatedly:

}



As for my project in particular, I really don't have any wiring diagrams or anything of that sort because I simply don't know how I should wire it yet, hence your help.  Just let me know if I can provide any other information that could help you help me. 

Thanks again for any help and sorry if this stuff is basic or is the same as other boards, I'm just unsure as it seems like all the boards are different in regards to wiring and I have found little information that is specific to the mkr Zero

Edit: Actually I think he's using the ATMEGA chip not the UNO (http://www.skillbank.co.uk/arduino/measure.htm)
Title: Re: Help with Arduino Mkr Zero
Post by: anwarhahj on Mar 15, 2017, 12:20 am
Try:

Code: [Select]
analogReference(AR_EXTERNAL);

Please observe the need for semicolon after the line. I get the same error as you do with plain analogReference(EXTERNAL);

...but looking at cores/arduino/wiring_analog.h in https://github.com/arduino/ArduinoCore-samd (https://github.com/arduino/ArduinoCore-samd/blob/master/cores/arduino/wiring_analog.h), this seems to be the solution, and it compiles, although I can't test it on the actual board for a few days. It would appear that for SAMD boards, the prefix "AR_" has been added to the AnalogReference modes.
Title: Re: Help with Arduino Mkr Zero
Post by: sterretje on Mar 15, 2017, 04:12 am
I don't have an MKR but your code is missing a semi-colon after the analogReference(...) statement that causes the compiler error; see above post where it needs to be added.

The error message is quite clear from that perspective ;)
Title: Re: Help with Arduino Mkr Zero
Post by: mhart9 on Mar 15, 2017, 02:36 pm
Thanks Everyone for the comments,
I did indeed forget that semicolon, must have dropped it putting code into empty sketch in order to show the problem more simply.  The real error that I am concerned with and which is not affected by ; is the following
Code: [Select]
exit status 1
invalid conversion from 'int' to 'eAnalogReference {aka _eAnalogReference}' [-fpermissive]


The analogReference(
Code: [Select]
analogReference(AR_EXTERNAL) does seem to do the trick though as far as error messages, I will experiment with it today on the actual unit.
Title: Re: Help with Arduino Mkr Zero
Post by: mhart9 on Mar 15, 2017, 03:41 pm
OK, so I've done some tests with the internal references on the MKR ZERO and think I must be doing something wrong:

I am testing a Sensor return voltage and comparing the MKR Arduino voltage to that of a multimeter.

Here is whats happening:

The multimeter consistently reads about 310 mV

The Arduino reads [390mV, 510mV, 720mV] , with the following code respectively
Code: [Select]
analogReference(AR_DEFAULT), analogReference(AR_INTERNAL2V23), analogReference(AR_INTERNAL1V65), 

attached is a simple wiring diagram.

Perhaps I'm going about this all wrong, just let me know

Also here is the sketch I'm using to test this, not that I have played around with the parameters to produce the above issue
Code: [Select]
double voltage;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0, INPUT_PULLUP);
analogReference(AR_INTERNAL2V23);
pinMode(LED_BUILTIN, OUTPUT);
//analogReadResolution(12);


}

void loop() {
 
  delay(2000);
  voltage=analogRead(A0)* 1000.0 * (3.3 / 1023.0);
  printDouble_sensorMillivolt( voltage, 6);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);

}

void printDouble_sensorMillivolt( double val, byte precision) {
  Serial.print (int(val));  //prints the int part
  if ( precision > 0) {
    Serial.print("."); // print the decimal point
    unsigned long frac;
    unsigned long mult = 1;
    byte padding = precision - 1;
    while (precision--)
      mult *= 10;

    if (val >= 0)
      frac = (val - int(val)) * mult;
    else
      frac = (int(val) - val ) * mult;
    unsigned long frac1 = frac;
    while ( frac1 /= 10 )
      padding--;
    while (  padding--)
      Serial.print("0");
    Serial.print(frac, DEC) ;
    Serial.print(", ");   //comma seperator
  }
}
Title: Re: Help with Arduino Mkr Zero
Post by: anwarhahj on Mar 15, 2017, 07:23 pm
It's unclear whether you did in the tests you performed, however the line:
Code: [Select]
voltage=analogRead(A0)* 1000.0 * (3.3 / 1023.0);

Will need to change for each different analog reference voltage you use. The 3.3 should be OK for the first measurement with AR_DEFAULT, but it makes sense that the values would be inconsistent if you're not changing the scale in your calculation. For instance, when you use AR_INTERNAL1V65, you should swap 1.65 for 3.3 in the code.

As for the discrepancy between your meter and the measurement, I'm not sure what to tell you, but I would maybe suggest seeing if you get consistent values by changing the calculation as above in conjunction with the analogReference mode and then troubleshooting your circuit and how you're using the meter and whatnot.
Title: Re: Help with Arduino Mkr Zero
Post by: mhart9 on Mar 15, 2017, 08:01 pm
Yes that was my problem, changed the 3.3 to the correct internal voltage and works fine, thanks for pointing that out.  Also the discrepancy between multimeter and Arduino decreased significantly with lower reference voltage. 

Does anyone know if its bad to use the
Code: [Select]
pinMode(x, INPUT_PULLUP) command with an internal reference voltage?
Title: Re: Help with Arduino Mkr Zero
Post by: anwarhahj on Mar 16, 2017, 02:05 am

Afaik, it should be OK to use INPUT_PULLUP. I always thought the default mode was an internal reference voltage anyway. You maybe want to search this forum about it more.

If you read the Arduino page on digital pins (https://www.arduino.cc/en/Tutorial/DigitalPins), you might gain more insights. Some applications call instead for using a stronger external pullup (https://learn.sparkfun.com/tutorials/pull-up-resistors) too.

Forgot to mention before, but perhaps see how things look with a backup multimeter if you have one.