Hi, I seem to be having a very basic problem with analog read on an industruino proto, I'm basically reading the analog pin from a maxbotix ultrasonic sensor but all my readings are value + 20, i have used the exact same sketch just to read an analog pin on an uno board and the values are correct, am I missing something here! Help very much appreciated?
Hi, i'm not sure what your error value of 20 refers to, it is 20 on the analogRead scale of 0-4095 or 20cm after mapping to your sensor specs?
Anyway, if it is 20 on the analogRead(), i think this goes back to a bug in the original Arduino SAMD core https://github.com/arduino/ArduinoCore-samd/issues/446 - an offset of about 35mV in analog readings. Arduino has fixed this in a core release late last year but we have not had a chance yet to implement it in the Industruino core. The easiest way to fix this is to add these lines in your Industruino core file wiring_analog.c, which on my Linux machine is at ~/.arduino15/packages/industruino/hardware/samd/1.0.1/cores/industruino/wiring_analog.c
At line 169, add this block:
// Waiting for the 1st conversion to complete
while (ADC->INTFLAG.bit.RESRDY == 0);
Just the same as in this github bug fix https://github.com/arduino/ArduinoCore-samd/commit/aef4af4c1e20ffeee2a8e62c6998d1fcb2c7fa4b#diff-63b7293bbdf8f8f0f8b98336a5e85d83
I have tested that with my PROTO unit, with D4/A4 connected to GND, it brings back the raw analogRead() value (should be 0) from around 20 (before the above change) down to 0-4. Please try and let us know if that solves your issue.
UPDATE: setting a stable analogReference() is very important to get accurate results. See here for options. The default is 3.3V reference which may not be stable during operation. My testings shows the best results using any of these 2 settings:
Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!