I am very new to micro controller and circuits. I am not at all with this field. But trying to create a home project where I have a device image attached below.
This is 18650 Lipo battery powered device. Here I want to calculate the battery level. After doing some research on google I found that I need to have Voltage Divider which I think I already included in this with 220k ohms and 100k ohms
People are using different ways to calculate it. Which I found in few examples. Which I am not able to understand at all, that what formula or values they are referring to calculate it.
if someone can help to understand that will be helpful. Here is how I am coding it which I saw somewhere on internet itself.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//#include "DHTesp.h"
#define DHT_PIN 16
//SSID and Password of your WiFi router
const char* ssid = "Asus";
const char* password = "Xmv02488!!**";
ESP8266WebServer server(80); //Server on port 80
/***************************************************************
* SETUP
**************************************************************/
void setup(void){
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
server.on("/", handleRoot); //Which routine to handle at root location
server.begin(); //Start server
Serial.println("HTTP server started");
pinMode(LED_BUILTIN, OUTPUT);
pinMode(A0, INPUT);
}
/***************************************************************
* LOOP
**************************************************************/
void loop(void){
server.handleClient(); //Handle client requests
}
/***************************************************************
* This function converts IPAddress struct to a String
**************************************************************/
String IpAddress2String(const IPAddress& ipAddress)
{
return String(ipAddress[0]) + String(".") +\
String(ipAddress[1]) + String(".") +\
String(ipAddress[2]) + String(".") +\
String(ipAddress[3]) ;
}
/***************************************************************
* This rutine is exicuted when you open its IP in browser
**************************************************************/
void handleRoot() {
IPAddress ip_address = WiFi.localIP();
String ip_str = IpAddress2String(ip_address);
int nVoltageRaw = analogRead(A0);
float fVoltage = (float)nVoltageRaw * 0.00486;
float fVoltageMatrix[22][2] = {
{4.2, 100},
{4.15, 95},
{4.11, 90},
{4.08, 85},
{4.02, 80},
{3.98, 75},
{3.95, 70},
{3.91, 65},
{3.87, 60},
{3.85, 55},
{3.84, 50},
{3.82, 45},
{3.80, 40},
{3.79, 35},
{3.77, 30},
{3.75, 25},
{3.73, 20},
{3.71, 15},
{3.69, 10},
{3.61, 5},
{3.27, 0},
{0, 0}
};
int i, perc;
perc = 100;
for(i=20; i>=0; i--) {
if(fVoltageMatrix[i][0] >= fVoltage) {
perc = fVoltageMatrix[i + 1][1];
break;
}
}
server.send(200, "text/plain", "Hello from esp8266!\n\rIP: " + ip_str +
".\n\rTemp: " + "NO" +
", Hum: " + "NO" +
"\n\r" + "NO" +
"\n\r" + "Voltage: " + fVoltage +
"\n\r" + "Charge: " + perc + '%');
}
This is a copied code. Where I am not sure about where they got the this formula and how they got the value of 0.00486
I copied this code from different source references. But I hope this will help you to understand the issue.
Any suggestions will be helpful!
Thank you! (In advance)



