Arduino analogreference() - Page 1
Forum section: electronics / general

author: metrologist
author_rank: Super Contributor
timestamp: October 04, 2019, 11:28:11 pm
content: Is this reference worth anything? I find it varies ~20% depending on supply voltage to the chip. I'm using a pro micro ko on various USB ports/battery banks, and it's unusable.Is it my junk ko or should it perform as an actual reference? I don't see much diff if just using the 5v range.I have two functions, one that uses an ntc divider with analogReference and the other with an lm35 without, and the lm35 is half a mag better when just swapping supply.basic code example:Code:void ntc() {analogReference(INTERNAL);analogRead(ThermistorPin0);for (int i = 0; i <= avg; i++) {Vntc = Vntc + analogRead(ThermistorPin0);delay(1);}Vntc = Vntc / avg;analogReference(DEFAULT);analogRead(ThermistorPin0);}

author: rstofer
author_rank: Super Contributor
timestamp: October 05, 2019, 12:34:08 am
content: I'm skeptical of your calculation of the average. You take 'avg'+1 samples and divide the total by just 'avg'. I would think the code would be:for (i = 0; i < avg; i++)

author: sokoloff
author_rank: Super Contributor
timestamp: October 05, 2019, 12:55:22 am
content: In addition to the off by one that rstofer notes, you need to be concerned with integer overflow, depending on the value of avg and the type of Vntc (neither of which is shown in the snippet above).

author: ledtester
author_rank: Super Contributor
timestamp: October 05, 2019, 01:13:07 am
content: Continuing the code review, I would add:Vntc = 0;just before the for loop. Perhaps you've set it to 0 elsewhere, but just before the for loop is the most natural and appropriate place to do this initialization.

author: metrologist
author_rank: Super Contributor
timestamp: October 05, 2019, 12:33:03 pm
content: I found this about the analog reference:https://forum.arduino.cc/index.php?topic=193177.0All valid points about the coding. Even more ignorant was that I was sourcing my divider from the supply.

author: iMo
author_rank: Super Contributor
timestamp: October 05, 2019, 01:18:35 pm
content: Around 1.1V and maybe 100ppm/C.

author: OwO
author_rank: Super Contributor
timestamp: October 05, 2019, 01:26:32 pm
content: Actually sourcing the thermistor supply from Vcc *is* the correct thing to do, but in that case you must not use a precision voltage reference for the ADC; you must use Vcc as the voltage reference.

author: rstofer
author_rank: Super Contributor
timestamp: October 05, 2019, 02:50:58 pm
content: I don't often see multiple variables initialized in the first parameter of the 'for' statement but it is a perfectly logical place to initialize 'vntc'.Code:void loop() {uint16_t vntc;const uint16_t avg = 8;for (uint16_t i = 0, vntc = 0; i < avg; i++){vntc += 1; // just a place holder}vntc /= avg;}I picked a 16 bit unsigned integer, choose wisely...

author: SiliconWizard
author_rank: Super Contributor
timestamp: October 05, 2019, 11:37:54 pm
content: Quote from: rstofer on October 05, 2019, 02:50:58 pmI do that all the time. When the additional variables are all to be initialized at the beginning of the for loop, it's a pretty natural place to do that indeed.

author: metrologist
author_rank: Super Contributor
timestamp: October 08, 2019, 02:05:58 am
content: Thanks all for the programming help. I think I'm also having breadboard connection problems.

author: jackthomson41
author_rank: Regular Contributor
timestamp: October 10, 2019, 03:38:12 pm
content: Breadboard always makes the things worst, always use PCB or at least wero board.https://www.theengineeringprojects.com/2018/09/introduction-to-arduino-due.html