I am using a 100k and a 700k resistor to make a resistive divider to be able to measure up to 40 volts on the analog input, but when i measure anything then the voltage i am getting is fluctuating by like +-0.5 volts? i measured the resistors and the are exactly the right value and i dont know what else can be the problem.
here is the full code, its still in progress so some of the parts arent used
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
unsigned long previousMillis;
unsigned long millisPassed;
float capacity;
float current;
float mA;
float voltage;
float low_volt;
float volt;
int button1;
int buttonPin1=5;
int button2;
int buttonPin2=6;
int button3;
int buttonPin3=7;
int button4;
int buttonPin4=8;
int buzzerpin=2;
int freq=2000;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
pinMode(A3, INPUT);
pinMode(A2, INPUT);
pinMode(11, OUTPUT);
pinMode(2, OUTPUT);
pinMode(button1, INPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Serial.begin(9600);
}
void loop() {
button1 = digitalRead(buttonPin1);
button2 = digitalRead(buttonPin2);
button3 = digitalRead(buttonPin3);
button4 = digitalRead(buttonPin4);
if(button1!=0){
low_volt = low_volt+0.1;
}
if(button2!=0){
low_volt = low_volt-0.1;
}
if(button3!=0){
low_volt = low_volt+1;
}
if(button4!=0){
low_volt = low_volt-1;
}
if(low_volt>volt){
digitalWrite(11,LOW);
tone(buzzerpin,freq);
delay(100);
noTone(buzzerpin);
delay(100);
tone(buzzerpin,freq);
delay(100);
noTone(buzzerpin);
delay(100);
tone(buzzerpin,freq);
delay(100);
noTone(buzzerpin);
delay(5000);
}
int v_sense = analogRead(A3);
float voltage = map(v_sense,0,1023,0,4720); //4720 is the actual voltage on the 5v pin
volt = voltage/125; //devide by 125 because i need to devide by 1000 to get value in volts then multiply by 8 to get back to real value because of the devider
int c_sense = analogRead(A2);
float current = map(c_sense,0,1023,0,4720);
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(0,10);
display.print("Voltage: ");
display.print(voltage/125);
display.println(" V");
display.print("Current: ");
display.print(current/1000);
display.println(" A");
display.print("Power: ");
display.print((voltage/125)*(current/1000));
display.println(" W");
display.print("Capacity:");
display.print(capacity);
display.println(" mAh");
display.print("Low volt:");
display.print(low_volt);
display.print(" V");
display.display();
Serial.println(low_volt);
Serial.println(volt);
millisPassed = millis() - previousMillis;
mA = current * 1000.0 ;
capacity = capacity + mA * (millisPassed / 3600000000.0);
previousMillis = millis();
delay(250);
}
