Reading battery voltage

I want to use Blynk to monitor a few things,
I have it working to
Read
4 Ds18b20’s
1 Dth11
1 voltage sensor, voltage divider to read battery voltage

I can not get the right figure for the battery sensor I get a reading of 520 from the ADC0,with a 9volt battery(going to be using a 12 volt car battery) just can’t figure out the right formula and place to put it.

My code


//working on chip 3,all Ds18b20's and the dth11 and 1 voltage sensor
//need to fine turn voltage sensor
//
// pin D2 is connented to 4- Ds18b20's
// pin D7 is DTH11
// Pin D6 is Led Out Button
// A0  is voltage sensor
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <Wire.h>
//#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <OneWire.h>

//DHT sensor
#define DHTPIN 13
#define b1pin 15
#define DHTTYPE DHT11


#define VT_PIN A0 

// define other voltage pin here
DHT dht(DHTPIN, DHTTYPE);


  
#include <OneWire.h>
#include <DallasTemperature.h> 
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
DeviceAddress Probe01 = { 0x28, 0xff, 0x40, 0x93, 0x3c, 0x04, 0x00, 0x96 }; //S1
DeviceAddress Probe02 = { 0x28, 0xff, 0x82, 0x93, 0x3c, 0x04, 0x00, 0x37 }; //S2
DeviceAddress Probe03 = { 0x28, 0xff, 0xe6, 0x70, 0x3b, 0x04, 0x00, 0x82 }; //S3
DeviceAddress Probe04 = { 0x28, 0xff, 0x8d, 0x51, 0x3a, 0x04, 0x00, 0xb7 }; //S4
//DeviceAddress Probe05 = { 0x28, 0xAB, 0x58, 0x2D, 0x17, 0x13, 0x01, 0x2B }; //S5



SimpleTimer timer;


//Blynk token
char auth[] = "------------------------------------f";

// WiFi credentials.
//char ssid[] = "lvl1";
//char pass[] = "----------";

char ssid[] = "lockadoc";
char pass[] = "---------";

//DHT Sensor
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(true);


  Blynk.virtualWrite(V6, h);
  Blynk.virtualWrite(V7, t);

}

 
void getTempData()
{
  sensors.requestTemperatures();
  Blynk.virtualWrite(V1, sensors.getTempFByIndex(0));
  Blynk.virtualWrite(V2, sensors.getTempFByIndex(1));
  Blynk.virtualWrite(V3, sensors.getTempFByIndex(2));
  Blynk.virtualWrite(V4, sensors.getTempFByIndex(3));
  Blynk.virtualWrite(V8, sensors.getTempFByIndex(4));
}

// Voltage Sensor
void getVoltage() 
{



int vt_read = analogRead(VT_PIN);
  float voltage4 = vt_read * (3.2 / 1024.0) * 5.0;
  Blynk.virtualWrite(V15, voltage);  
}

void setup()
{ 
  Blynk.begin(auth, ssid, pass);

//DHT Sensor
  dht.begin();
  
//watertemp sensor
  sensors.begin();
  sensors.setResolution(10);


  
  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1500L, getTempData);
  timer.setInterval(2000L, getVoltage); // no need to add another of these!
}

void loop()
{
  Blynk.run();
  timer.run();
}




Fixed your formatting… backticks, not commas.

Blynk - FTFC

An ADC reads a range based on the MCU limitations… Arduino usually 0-1024 based on 0-5v, ESP8266 0-1024 based on 0-3.3v and it’s own internal voltage divider.

If you are using an ESP8266 like a NodeMCU or Wemos D1Mini, then you need to account for that internal voltage divider and adjust additional series resistance (not another divider) for the max voltage you need to monitor. Then, yes, you need to account for the algebraic equation to make 0-15v (car battery under charge - aka vehicle running) give a corresponding range from the ADC of 0-1023

I did something similar here for my 12v powered rover (battery NOT ever monitored under charge… but I calculated up to 14.8v I think).

Of course since me and Math do not get along, I think I found a formula online and fiddled with the numbers until the Blynk reading matched the multimeter reading :stuck_out_tongue:

Thanks, for the code edit ``` never used that keyboard character before.