Yes. No resistor required. (assuming your first point is correct).
The analogRead only has a resolution that goes from 0 to 1023 (inclusive). This equates to 0 to the reference voltage. Using the default 5v reference voltage ( of 5v) each step represents about 4.8 mv. If this resolution isn't sufficient for you then you can use an alternative reference voltage.
Thank you both for the guidance. I like the idea of using the 1.1v reference.
It will change the reference for all analog pins though, won't it? I have some other things I need analog pins for in the same project, including I2C. Not sure how it affects that. Also using a couple of sensors that use 3.3 and 5v.
How can I accommodate both? Even if I had to use a second arduino to sense the voltage, it wouldn't be a big problem. I'd just need a way to feed the readings over to the other one for use in the script.
Maybe an ATtiny85 for the voltage sensing and send it over to the nano?
Yes, changing the analog reference changes it for all pins.
I2C is not analog function - so you can use A4/A5 for I2C, and A0-A3 for analog in. (and A6, A7 on SMD boards).
Your sketch can change the analog reference - read the datasheet, there is some small time allowance needed to let it settle between reads using different references.
So if I'm understanding correctly-- I only need to refresh my value every couple of minutes. So I could refresh the values I need at the 5v reference, then change the ref to 1.1v, update that value, then change it back to 5v?
bradix14:
So if I'm understanding correctly-- I only need to refresh my value every couple of minutes. So I could refresh the values I need at the 5v reference, then change the ref to 1.1v, update that value, then change it back to 5v?
That's correct. Normal practice is to simply make one analog read after the change of reference voltage and discard the reading (this one being unusable) THEN make another to get the actual value
for example
analogReference( INTERNAL );
analogRead( myPin ); //make one reading discarding value
int value=analogRead( myPin ); //make actual reading storing value
Configures the reference voltage used for analog input (i.e. the value used as the top of the input range). The options are:
•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
Note
After changing the analog reference, the first few readings from analogRead() may not be accurate.
So your sketch would toggle between those two. You'll have to experiment some and see how long it takes to get an accurate reading for analog sources.
I've been tinkering with it for a while this morning. This is the code I'm using:
void getVoltage() {
analogReference(INTERNAL);
delay(1000);
analogRead(voltPin);
voltage = analogRead(voltPin) * 1.0742;
if (analogRead(voltPin) == 1023){
voltage = 0;
}
delay(50);
analogReference(DEFAULT);
delay(1000);
}
It seems to work pretty well when it is all by itself in a sketch. The math is a tad off. Not sure why, but it varies between 3-9mv difference from my DMM.
When I add this to my full sketch, it gives me all kinds of weird things. There is only one other analogRead as of right now, which seems to still work, but the voltage value bounces all over the place with no load attached. When I attach the load, it appears to be reading based on DEFAULT voltage reference.
The time to change between references starts when an analogRead is called, not when analogReference is defined. So I just added a little delay between the garbage reading and the stored reading and it seems to be working fine.
bradix14:
The time to change between references starts when an analogRead is called, not when analogReference is defined. So I just added a little delay between the garbage reading and the stored reading and it seems to be working fine.