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
#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.
#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);
}