Author Topic: Arduino analogreference()  (Read 1820 times)

0 Members and 1 Guest are viewing this topic.

Offline metrologistTopic starter

  • Super Contributor
  • ***
  • Posts: 2212
  • Country: 00
Arduino analogreference()
« on: October 04, 2019, 11:28:11 pm »
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: [Select]
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);
}
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Arduino analogreference()
« Reply #1 on: October 05, 2019, 12:34:08 am »
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++)
 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: Arduino analogreference()
« Reply #2 on: October 05, 2019, 12:55:22 am »
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).
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3036
  • Country: us
Re: Arduino analogreference()
« Reply #3 on: October 05, 2019, 01:13:07 am »
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.
 

Offline metrologistTopic starter

  • Super Contributor
  • ***
  • Posts: 2212
  • Country: 00
Re: Arduino analogreference()
« Reply #4 on: October 05, 2019, 12:33:03 pm »
I found this about the analog reference: https://forum.arduino.cc/index.php?topic=193177.0

All valid points about the coding. Even more ignorant was that I was sourcing my divider from the supply.  :-BROKE
 

Online iMo

  • Super Contributor
  • ***
  • Posts: 4782
  • Country: pm
  • It's important to try new things..
Re: Arduino analogreference()
« Reply #5 on: October 05, 2019, 01:18:35 pm »
Around 1.1V and maybe 100ppm/C.
 

Offline OwO

  • Super Contributor
  • ***
  • Posts: 1250
  • Country: cn
  • RF Engineer.
Re: Arduino analogreference()
« Reply #6 on: October 05, 2019, 01:26:32 pm »
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.
Email: OwOwOwOwO123@outlook.com
 
The following users thanked this post: Someone

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Arduino analogreference()
« Reply #7 on: October 05, 2019, 02:50:58 pm »
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: [Select]
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;
}
[/font]

I picked a 16 bit unsigned integer, choose wisely...
« Last Edit: October 05, 2019, 06:27:20 pm by rstofer »
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14471
  • Country: fr
Re: Arduino analogreference()
« Reply #8 on: October 05, 2019, 11:37:54 pm »
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'.

I 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.
 

Offline metrologistTopic starter

  • Super Contributor
  • ***
  • Posts: 2212
  • Country: 00
Re: Arduino analogreference()
« Reply #9 on: October 08, 2019, 02:05:58 am »
Thanks all for the programming help. I think I'm also having breadboard connection problems.
 

Offline jackthomson41

  • Regular Contributor
  • *
  • Posts: 53
  • Country: us
Re: Arduino analogreference()
« Reply #10 on: October 10, 2019, 03:38:12 pm »
Breadboard always makes the things worst, always use PCB or at least wero board.  |O https://www.theengineeringprojects.com/2018/09/introduction-to-arduino-due.html
« Last Edit: March 04, 2021, 05:30:27 am by jackthomson41 »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf