# Attiny85 battery tester shows wrong reading

**URL:** <https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809>\
**Category:** Programming\
**Created:** [January 18, 2024, 6:21pm UTC](https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809 "2024-01-18T18:21:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![spiderman288888](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@spiderman288888](https://forum.arduino.cc/u/spiderman288888)\
**Post date:** [January 18, 2024, 6:21pm UTC](https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809/1 "2024-01-18T18:21:28Z")

</div>

I created this attiny85 battery tester but it is showing the wrong voltage on new batteries. It should show 1.5V but it is showing over 2.5V. The battery tester has an OLED and and RGB LED to display the voltages. The main issue is the OLED display showing the wrong voltage. Here is the code

```arduino
#include <Tiny4kOLED.h>

int redPin = 1;
int greenPin = 3;

int analogValue = 0;
float voltage = 0;
int ledDelay = 1000;
// ============================================================================

void setup() {
   oled.begin();
  oled.clear();
    oled.on();
   oled.switchRenderFrame();
 pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop() {
  oled.clear();
  oled.setFont(FONT8X16);
  oled.setCursor(32, 0);
  oled.print(F("Voltage"));
  oled.setFont(FONT6X8);
  oled.setCursor(13, 2);
  oled.print(F(""));
  oled.setCursor(16, 3);
  oled.print(F(""));
  oled.switchFrame();
  delay(10);
analogValue = analogRead(A2);
  voltage = 0.0048*analogValue;
 oled.println(voltage);
  

  if( voltage >= 1.6 )
    setColor(0, 255, 0); //green LED color
  else if (voltage > 1.2 && voltage < 1.6)
     setColor(255, 255, 0); // yellow LED color
  else if( voltage <= 1.2)
    setColor(255, 0, 0); // red LED color

    oled.clear();
}

void setColor(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
    
}

```

I am pretty sure this part is to blame in the code

voltage = 0.0048\*analogValue;

This code below works perfectly on an arduino and it displays the correct voltage of any battery. I am guessing I need to change something in the code going from an arduino nano to attiny 85. Any help would greatly be appreciated. Here is the code for the battery tester on an arduino nano.

```arduino
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(OLED_RESET);

int redPin = 3;
int greenPin = 5;

int analogValue = 0;
float voltage = 0;
int ledDelay = 1000;

void setup()
{
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();

  
   pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  
}

void loop()
{
  analogValue = analogRead(A1);
  voltage = 0.0048*analogValue;
 display.println(voltage);

  if( voltage >= 1.6 )
    setColor(0, 255, 0);
  else if (voltage > 1.2 && voltage < 1.6)
    setColor(255, 255, 0); // yellow
  else if( voltage <= 1.2)
    setColor(255, 0, 0); // red
    
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Voltage");
  display.display();
   display.clearDisplay();

}

void setColor(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  //analogWrite(bluePin, blue);  
}

```

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [January 18, 2024, 6:43pm UTC](https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809/2 "2024-01-18T18:43:19Z")

</div>

1. Which core are you compiling with?
2. Which physical pin # are you connecting the battery to on the ATtiny85?

---

<div class="post-metadata">

**Author:** ![hmeijdam](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hmeijdam/32/218564_2.png) [@hmeijdam](https://forum.arduino.cc/u/hmeijdam)\
**Post date:** [January 18, 2024, 6:46pm UTC](https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809/3 "2024-01-18T18:46:12Z")

</div>

Are the Nano and the Attiny getting the same VCC?

e.g. if you power the Attiny85 from a lower voltage the analogread will show a higher value.

---

<div class="post-metadata">

**Author:** ![spiderman288888](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@spiderman288888](https://forum.arduino.cc/u/spiderman288888)\
**Post date:** [January 18, 2024, 8:15pm UTC](https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809/4 "2024-01-18T20:15:35Z")

</div>

The attiny85 is powered by a 3V battery, pin4 is ground and pin 8 is VCC. The arduino is connected to the computer. Is this the part of the code that needs changing?

```arduino
voltage = 0.0048*analogValue;

```

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [January 18, 2024, 8:41pm UTC](https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809/5 "2024-01-18T20:41:19Z")

</div>

```arduino
voltage = 0.0048*analogValue;

```

can also be written as

```arduino
voltage = 5.0 * analogValue / 1023.0; // (or 1024.0 depending on your POV)

```

The 5.0 is for 5V operation. For 3.3V operation the equation should be

```arduino
voltage = 3.3 * analogValue / 1023.0; // (or 1024.0 depending on your POV)

```

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [January 18, 2024, 8:59pm UTC](https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809/6 "2024-01-18T20:59:54Z")

</div>

> [@hmeijdam](#):
>
> from a lower voltage the analogread will show a higher value.

Is the use of Vref a valid way to avoid this?

---

<div class="post-metadata">

**Author:** ![hmeijdam](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hmeijdam/32/218564_2.png) [@hmeijdam](https://forum.arduino.cc/u/hmeijdam)\
**Post date:** [January 18, 2024, 9:09pm UTC](https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809/7 "2024-01-18T21:09:04Z")

</div>

Yes, as long as you can ensure that the voltage to be measured is below the 2.56 Volt of the internal voltage reference. The internal voltage reference requires a VCC of minimum 3 volts. So your 3Volt battery is the absolute minimum.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/7/2/a/72ad1cae54868ca974d015743b859fd4087dced4.png)

O, and you still need to adjust the calculation like @van_der_decken suggested, but then with a 2.56 factor instead of 3.3 or 5.0

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [January 19, 2024, 9:06am UTC](https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809/8 "2024-01-19T09:06:52Z")

</div>

1.1volt Aref is a safer option. Then 3volt VCC is not a problem any more.

When using 1.1volt Aref, battery voltage that you measure must then be reduced to ~1volt with a voltage divider. Say 10k from pin to ground and 4k7 between pin and (for a 1.5volt) battery.

That is not a bad thing, because you should never connect a power source directly to an I/O pin.  
Leo..

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [January 19, 2024, 10:08am UTC](https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809/9 "2024-01-19T10:08:58Z")

</div>

I measured 3.3V of NANO by PC-connected NANO and have found 3.3V on the Serial Monitor.

I have measured the 3.3V of NANO using PC-connected Digispark ATtiny85 Dev Board, and I have found 3.1V -3.2V on the OLED Monitor.

If yo are are operating ATtiny85 by VCC = 3V, then you have to make a two-point calibration as we use the following code to display the acquired voltage; where, VREF voltage of the ADC is: 5V (default).

```arduino
oled.print((5.0/1023)*analogRead(A2), 1);
oled.on();

```

---

<div class="post-metadata">

**Author:** ![spiderman288888](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@spiderman288888](https://forum.arduino.cc/u/spiderman288888)\
**Post date:** [January 22, 2024, 8:52pm UTC](https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809/10 "2024-01-22T20:52:13Z")

</div>

Changing the voltage from 3.3V to 2.38V worked!

voltage = 2.38 \* analogValue / 1024.0;

This comes to within 0.01-0.04V accuracy in 1.5V batteries. Is there a way to change the code for it to read 3.0V and 9V batteries? This code reads a new 3.0V battery as 2.37V. It also reads a 9V battery as 2.37V.

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [January 22, 2024, 10:41pm UTC](https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809/11 "2024-01-22T22:41:18Z")

</div>

> [@spiderman288888](#):
>
> Is there a way to change the code for it to read 3.0V and 9V batteries?

Voltage dividers on the battery are needed when battery voltage exceeds Aref. If you don't, then the A/D will just peg at it's highest value of 1023, which will display 2.37volt with your current voltage formula.

I hope you didn't connect the 9volt battery directly to the pin, because that would likely kill it.

If you don't use a stable voltage reference, like the internal 1.1volt Aref, then your voltage reading will also depend on supply voltage of the Attiny85.  
Leo..

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 20, 2024, 10:42pm UTC](https://forum.arduino.cc/t/attiny85-battery-tester-shows-wrong-reading/1212809/12 "2024-07-20T22:42:10Z")

</div>

Closed on July 20, 2024, 10:42pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
