Hi folks, was hoping to jump on this with a related question and drain the knowledge from more experienced people than myself 
In regards to the battery pins on the back of the board, are they solely connected to the usb port for charging and nothing else? If thatās the bizarre situation that was described above Iām assuming thatās the problem I have that connecting a battery to those pins does nothing, board doesnāt boot. Iām aware the led is solely for charging notification and no help to me but when I connect battery to the two pins on the back I get nothing from the board at all (should send a temperature every 30 seconds to a mqtt broker for testing) canāt even see it on the wireless network.
If I connect the battery to the 5v and GND pins it works no problem but my lack of experience has me worried it will work well until it all combusts and burns my house down.
I have an adafruit minilipo charger that I can connect the pass through to the 5v and GND and the battery to the charger so that I can charge the battery when needed and power the esp32c3 from which ever is available but I wanted to check that this was correct as the whole situation seems completely mad to me.
Also trying to get voltage reported back, have been using d0 and will change to d3 as per previous advice, currently reporting 0 battery but will see what happens on d3 instead.
Using a lipo 3.7v battery fully charged and reporting 4.1v from terminals.
Works perfectly when connected to usb and also seems to work (except battery reporting) when powered from battery connected to 5v and GND pins.
Hoping to have this running on battery with easy to charge option. AI calculations estimate 170 days +/- per charge when deep sleep and waking every 15 mins for for temp reading.
Hoping someone can help, thanks for reading if you got this far 
Edit: apologies for emojis, I am old.
Hi there, and welcome here,
So the subject is covered in deep detail, Read the WiKi first ,and then come back here and read the main threads (4) of information to achieve true Grasshopper statusā¦
If your not using a battery then donāt worry about it, if you are then get educated to do it properly and to get the best battery life.
HTH
GL
PJ 
1 Like
Hi PJ_Glasso, thanks for the reply. Embarrassing request but could you link me to the wiki? I found the datasheet and pin outs etc as attachments at the store I bought them. By the sounds of it the wiki is the missing piece in my puzzle.
Thanks folks, Ive been through it and looks like I was going about this completely in the wrong direction. Genuinely turned my day around, thank you so much for taking the time.
1 Like
Thank you for the information.
can I clarify with you something? Iām hesitant as it exposes my ignorance, I used chatgpt to do a lot of the code and it insists on a 100 and 220 resistor for the voltage monitoring. My monitoring is reporting 0 and I havenāt delved into it deeply yet but I notice in your solution you use 2x 220. Iām using a 3.7v with essentially the same setup you provided but with the different resistors. Is that likely to be my problem?
1 Like
Using 100k+220k resistors in the voltage divider, the AD converter input is 2.9V at a maximum battery voltage of 4.2V.
Since the full scale of the AD converter is 2.5V, the input must be less than 2.5V.
If you show me your sketch, I may be able to give you some other advice.
edit
I think chatgpt is assuming that VDD (3.3V) is used as the reference voltage for AD conversion. if VDD is used, voltage below 3.3V cannot be measured.
1 Like
I think this is a part of where I went badly wrong. At one point trying to wade through chatgpts assumptions and reality I moved my temp sensor positive to the 5v pin which worked fine with usb but as I now know wouldnāt work from battery (assuming Iām on the right page now) Iāve had a few reconfigurations as I was learning about it from scratch and I think I need to start again with the knowledge I have now and see what happens. Sketch below just for curiosities sake, will update when I can if youāre interested. Currently trying to get it out of my mind to enjoy Christmas with the kids but it keeps gnawing at me 
#include <WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Wi-Fi credentials
const char* ssid = ānetworkā;
const char* password = āpwordā;
// MQTT broker details
const char* mqtt_server = ā192.168.1.19ā;
const char* mqtt_user = āfridge1ā;
const char* mqtt_password = āfridge1ā;
// Device hostname
const char* hostname = āS3-Fridge-1ā;
// Pin definitions
#define DS18B20_PIN 6 // GPIO6 for DS18B20
#define BATTERY_PIN 2 // GPIO0 (D0) for ADC battery monitoring
// Voltage divider ratio
#define DIVIDER_RATIO 0.3125
// Battery voltage limits
#define BATTERY_MAX 4.2
#define BATTERY_MIN 3.0
// Deep sleep time in microseconds (15 minutes)
#define SLEEP_TIME 900000000
// DS18B20 setup
OneWire oneWire(DS18B20_PIN);
DallasTemperature sensors(&oneWire);
// Wi-Fi and MQTT clients
WiFiClient espClient;
PubSubClient client(espClient);
// Function to read battery voltage
float readBatteryVoltage() {
analogSetPinAttenuation(BATTERY_PIN, ADC_11db); // Configure ADC for 0-3.9V range
int raw = analogRead(BATTERY_PIN); // Read raw ADC value
float vOut = (raw / 4095.0) * 3.3; // Convert ADC value to voltage
float vBattery = vOut / DIVIDER_RATIO; // Scale back to battery voltage
return vBattery;
}
// Function to calculate battery percentage
int calculateBatteryPercentage(float voltage) {
float percentage = ((voltage - BATTERY_MIN) / (BATTERY_MAX - BATTERY_MIN)) * 100.0;
if (percentage > 100) percentage = 100;
if (percentage < 0) percentage = 0;
return (int)percentage;
}
// Function to connect to Wi-Fi
void connectToWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.setHostname(hostname);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
}
// Function to connect to MQTT broker
void connectToMQTT() {
while (!client.connected()) {
if (client.connect(hostname, mqtt_user, mqtt_password)) {
// Connected
} else {
delay(5000);
}
}
}
void setup() {
// Initialize DS18B20
sensors.begin();
// Connect to Wi-Fi
connectToWiFi();
// Setup MQTT
client.setServer(mqtt_server, 1883);
connectToMQTT();
// Read temperature
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
if (temperature == DEVICE_DISCONNECTED_C) {
temperature = -127; // Error value
}
// Read battery voltage
float batteryVoltage = readBatteryVoltage();
// Calculate battery percentage
int batteryPercentage = calculateBatteryPercentage(batteryVoltage);
// Publish data
char payload[100];
snprintf(payload, sizeof(payload), "{\"hostname\":\"%s\",\"temperature\":%.2f,\"battery_percentage\":%d}", hostname, temperature, batteryPercentage);
client.publish("fridge/temperature", payload);
// Prepare for deep sleep
esp_sleep_enable_timer_wakeup(SLEEP_TIME); // Enable timer wake-up
delay(1000); // Allow MQTT messages to be sent
esp_deep_sleep_start(); // Enter deep sleep
}
void loop() {
// Not used
}
Thanks again for the replies, huge help to me
Yesā¦ sometimes the WIKI is hard to find!
for informationā¦ from the main page you can click here
or
from the product page you can click here
It can be a little confusing at first so dont feel bad!
1 Like
Hi there,
Not to worry , we are here to help.
So you can help US help You, Edit your code post and use the code tags above ā</>ā paste your code stuff in there,
and It helps with the readability of the code posted and thus getting better quality help and comments because itās easier to read. Not to mention LQQKās way better 
HTH
GL
PJ 
2 Likes
I wasnt aware it existed to look for it. Handy thing to have 
1 Like
Lol, that is much better to read, thank you.
1 Like
Sorry for the very basic question, Iām working on the battery reporting now, getting 0 and will whittle down why but wanted to double check if having the resistors connected the battery cables half way down the cable is the same as having them connected at the pad points? Iāve extended the battery cable by a few inches just for ease with testing etc and have the resistors connected to the joins for the extensions then both going to my gpio pin. Iām still using a 100 and 220omh and will change that but wanted to clarify my wiring before changing anything. Thanks for reading my novice ramblings.
should not matter that much
1 Like
I thought so, thanks. It not like they are 5 metres longer or anything like that. Thanks for advice, will report back as I progress.
1 Like
So, I was expecting this latest revision to work perfectly but alas no joy 
The sketch iām using is:
#include <WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Wi-Fi credentials
const char* ssid = "network";
const char* password = "pwd";
// MQTT broker details
const char* mqtt_server = "192.168.1.19";
const char* mqtt_user = "fridge1";
const char* mqtt_password = "fridge1";
// Device hostname
const char* hostname = "C3-Fridge-1";
// Pin definitions
#define DS18B20_PIN 6 // GPIO6 for DS18B20
#define BATTERY_PIN 4 // GPIO4 (D4) for ADC battery monitoring
// Voltage divider resistors
#define R1 220000 // 220kĪ©
#define R2 220000 // 220kĪ©
// Battery voltage limits
#define BATTERY_MAX 4.2
#define BATTERY_MIN 3.0
// Deep sleep time in microseconds (15 minutes)
#define SLEEP_TIME 900000000
// DS18B20 setup
OneWire oneWire(DS18B20_PIN);
DallasTemperature sensors(&oneWire);
// Wi-Fi and MQTT clients
WiFiClient espClient;
PubSubClient client(espClient);
// Function to read battery voltage
float readBatteryVoltage() {
analogSetPinAttenuation(BATTERY_PIN, ADC_11db); // Configure ADC for 0-2.5V range
int raw = analogRead(BATTERY_PIN); // Read raw ADC value
float vOut = (raw / 4095.0) * 2.5; // Convert ADC value to voltage
float vBattery = vOut / 0.5; // Scale to battery voltage
return vBattery;
}
// Function to calculate battery percentage
int calculateBatteryPercentage(float voltage) {
float percentage = ((voltage - BATTERY_MIN) / (BATTERY_MAX - BATTERY_MIN)) * 100.0;
if (percentage > 100) percentage = 100;
if (percentage < 0) percentage = 0;
return (int)percentage;
}
// Function to connect to Wi-Fi
void connectToWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.setHostname(hostname);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
}
// Function to connect to MQTT broker
void connectToMQTT() {
while (!client.connected()) {
if (client.connect(hostname, mqtt_user, mqtt_password)) {
// Connected
} else {
delay(5000);
}
}
}
void setup() {
sensors.begin(); // Start DS18B20
connectToWiFi();
client.setServer(mqtt_server, 1883);
connectToMQTT();
// Read temperature
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
if (temperature == DEVICE_DISCONNECTED_C) {
temperature = -127; // Error value
}
// Read battery voltage
float batteryVoltage = readBatteryVoltage();
// Calculate battery percentage
int batteryPercentage = calculateBatteryPercentage(batteryVoltage);
// Publish data
char payload[100];
snprintf(payload, sizeof(payload), "{\"hostname\":\"%s\",\"temperature\":%.2f,\"battery_percentage\":%d}", hostname, temperature, batteryPercentage);
client.publish("fridge/temperature", payload);
// Deep sleep
esp_sleep_enable_timer_wakeup(SLEEP_TIME);
delay(1000); // Ensure MQTT message is sent
esp_deep_sleep_start();
}
void loop() {
// Not used
}
Iāve included a picture which probably doesnāt help very much (I have not included the picture as āYou cannot embed media items in a postā) but what it shows is the battery connected to battery pads with 2 x 220k Ohm resistors 1 going from each battery connection to GPIO 4. The temperature sensor is in GPIO 6 with power from 3v3 and GND. Everything works but Iām getting 0 from battery percentage on the MQTT broker. While posting this I am also noticing the label on my resistor bag says 220Ohm and not actually 220KOhm so iām going to go and ask the wife to tell me what colours are on these as being colour blind makes this much more of an effortā¦ I promise this isnāt a satirical post. Posting just now in case the resistors are correct but will update.
Yup, wrong resistorsā¦Iām battling the urge to resign from this forum and disappear into obscurity I figure by leaning into it and owning my farcical attempts at a very basic project I may at least give you all a bit of entertainment 
Correct resistors arriving tomorrow, will update again then.
1 Like
i really like your code ā¦ nice style and comments!
1 Like
how do you think we know so muchā¦ we watch people like you who show us what not to doā¦lol!
1 Like