In a project with a "low consumption" aim I have created a 3.7V LiPo battery test that works fine - starting with a test sketch. I use AnalogReference(INTERNAL) and a divider (4.7KOhm and 1K for the 5V experiment).
I work on Arduino UNO R3 - original - with no other components.
As the divider will consume energy itself I'd like to check at a specific program command.
The basic project takes the voltage from 3.3 or 5V pin with appropriate dividers. I tried to get the voltage in from pin2 writing it HIGH but it does not work. The voltage goes down a lot. ???
I then tried with an OptoCoupler 4N35 (which was in my Arduino rookie kit) - as it should work as a completely detached "switch". It works, but the voltage is not precise and varies erratically of about 20%.
4N35 is connected (as for examples) with pin 1 to a 220Ohm and then to pin2 of the UNO. Pin2 GND. Pin4 gets the voltage from the divider and Pin5 is connected to the A1 pin on the UNO.
Thanks
Flavio
P.S. I am looking and using a MOSFET... next step...
int analoginput = 1; // our analog pin
int analogamount = 0; // stores incoming value
float percentage = 0; // used to store our percentage value
float voltage =0; // used to store voltage value
float referenceVoltage = 0;
int resist1 = 4604; // 4.7 kOhm nominali
int resist2 = 975; // 1 kOhm nominali
// int resist1 = 675000; // 680 kOhm nominali
// int resist2 = 216000; // 220 kOhm nominali
void setup()
{
Serial.begin(9600);
Serial.println("Test Voltmetro");
analogReference(INTERNAL); // use INTERNAL (1.1V) for reference voltage
referenceVoltage = 1100;
// analogReference(DEFAULT); // use DEFAULT (5V) for reference voltage
// referenceVoltage = 5000;
}
void loop()
{
analogamount = 0;
digitalWrite(2, HIGH);
float t_zero = millis();
while ((millis() - t_zero) < 1000) {
if (analogamount < analogRead(analoginput)) {
analogamount = analogRead(analoginput);
}
}
digitalWrite(2, LOW);
percentage=(analogamount/1024.00)*100;
voltage=(analogamount/1024.00)*referenceVoltage; // in millivolts
Serial.print("Lettura analog (0-1023): ");
Serial.print(analogamount);
Serial.print(" -------- % of AREF: ");
Serial.print(percentage,2);
Serial.print(" -------- A0 (mV): ");
Serial.print(voltage,2);
Serial.print(" -------- Real - no divider (V): ");
Serial.println((voltage/1000*(resist1+resist2)/resist2),2); // V_s = V_out * (R1+R2) / R2
delay(2000);
}
