1

enter image description here

I want to read my lithium battery voltage.

  1. I WANT TO CREATE A FUNCTION TO READ MY 3.7~4.2V,1200mAH BATTERY VOLTAGE THROUGH A MICROCONTROLLER (ARDUINO OR ATTINY44).

  2. THEN I WANT TO CALL THE CREATED BATTERY FUNCTION IN THE FORMULA TO READ PRECISE SENSOR VALUE

     int voltage = ((CALLING BATTERY FUNCTION) * (READ SENSOR VALUE)) / 1023;
    
  3. I AM USING INTERNAL REFERENCE VOLTAGE 1.1V. NOTE: I want the whole system to run on lithium battery ( I am just creating a prototype on Arduino, if it works, ill be editing the code for my attiny44 using 8mhz external crystal).

     void setup(){
       Serial.begin (115200);
     }
    
     void loop() {  
       //REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
       ADMUX |= B11000000;   
       //We read A1 (MUX0)
       ADMUX |= B00000001;       
       // Start AD conversion
       ADCSRA |= B11000000;
       // Detect end-of-conversion
       while (bit_is_set(ADCSRA,ADSC));
       float val = ADCL | (ADCH << 8);
       val = val * 5.7; //Multiply by the inverse of the divider
       Serial.println(val);
     }
    

This code is working but it is reading from analog Pin. Is there any possibility to do it without using AnalogPin because I dont have any spare pin on my attiny.

9
  • @jsotola Done, Thanks Commented Feb 13, 2023 at 8:08
  • The answer is probably no. You cannot do anything without using pins. You need a pin to interact through. You cannot interact through thin air. So you need to get a bigger Attiny
    – chrisl
    Commented Feb 13, 2023 at 8:09
  • okay so you mean that it is impossible to get constant battery output using AREF pin we need analog pin to do that. is that what you mean Commented Feb 13, 2023 at 8:44
  • 1. You wrote, that you don't have any spare pins. That would mean no AREF pin either. 2. Yes, you need a pin, that can be connected to the ADC, so any pin that is marked that way in the pinout of the Attiny44. If you have AREF free, than you maybe can change the pin connections. Though you didn't specify how the attiny is connected. In your question you show only an Arduino Uno, which seems to be irrelevant for your question.
    – chrisl
    Commented Feb 13, 2023 at 8:57
  • If your attiny is powered by that same battery, you can just measure internal 1.1V reference against Vcc as ref and compute actual Vcc
    – KIIV
    Commented Feb 13, 2023 at 9:45

1 Answer 1

1

I want the whole system to run on lithium battery

If you power the Arduino from the battery, then you can indirectly read the battery voltage by reading the internal voltage reference. This requires no pins - just connect the battery across the Arduino ground and the 5V pin. Here is more explanation on why this works...

https://wp.josh.com/2014/11/06/battery-fuel-guage-with-zero-parts-and-zero-pins-on-avr/

Note: Do not connect more than 5 volts or less than 0 volts to the 5V Arduino pin. This pin is connected directly to the AVR without any overvoltage or reverse polarity protection. Your 3.7~4.2V battery should be fine.

3
  • Hi, thank you for your response. I need to read constant voltage not the current battery voltage and i believe that in the link the author wants to measure battery voltage. Is there a way to measure stable/constant/ unchanged battery voltage irrespective of the current battery level. Commented Feb 14, 2023 at 4:25
  • You want to (1) power your circuit using a lithium battery, and (2) you want to be able to measure the voltage of that lithium battery that is powering the circuit, correct? If so, then the above approach will do that. If not, then can you clarify what you are trying to do?
    – bigjosh
    Commented Feb 18, 2023 at 1:33
  • hi, please check arduino.stackexchange.com/questions/92329/… Commented Feb 20, 2023 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.