Skip to content

Commit 61efc72

Browse files
committed
fix (firmware, schematic, layout): change vref for adc and resistors to measure battery voltage
1 parent 401c57f commit 61efc72

17 files changed

+465
-218
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"iterator": "cpp",
2222
"serstream": "cpp",
2323
"__locale": "cpp",
24-
"unordered_set": "cpp"
24+
"unordered_set": "cpp",
25+
"complex": "cpp"
2526
}
2627
}

_code/measure-battery-level.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
---
22
layout: code
33
title: Measure battery voltage
4-
description: View the battery voltage through an access point
4+
description: Measure battery level using the ESP32-C3 ADC
55
references:
66
- name: Schematic of ESP32-C3-DevKitM-1
77
url: https://dl.espressif.com/dl/schematics/SCH_ESP32-C3-DEVKITM-1_V1_20200915A.pdf
88
- name: Pinouts
99
url: https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/hw-reference/esp32c3/user-guide-devkitm-1.html#pin-layout
1010
- name: Arduino Pins
1111
url: https://github.com/espressif/arduino-esp32/blob/master/variants/esp32c3/pins_arduino.h
12-
- name: Create an Access Point
13-
url: https://raw.githubusercontent.com/espressif/arduino-esp32/990e3d5b431b63b4adc364b045a79afdad645a3f/libraries/WiFi/examples/WiFiAccessPoint/WiFiAccessPoint.ino
1412
- name: How to check the battery voltage
1513
url: https://wiki.seeedstudio.com/check_battery_voltage/
1614
- name: Power Management
1715
url: https://learn.adafruit.com/adafruit-feather-m4-express-atsamd51/power-management#measuring-battery-3010518
16+
- name: ESP32-C3 ADC issue - reading 4095 at 2.8V
17+
url: https://forum.arduino.cc/t/esp32-c3-adc-issue-reading-4095-at-2-8v/1127687
18+
- name: Analog to Digital Converter (ADC)
19+
url: https://docs.espressif.com/projects/esp-idf/zh_CN/v4.4.2/esp32c3/api-reference/peripherals/adc.html
20+
- name: Voltage Divider Calculator
21+
url: https://ohmslawcalculator.com/voltage-divider-calculator
22+
- name: ESP32 ADC – Read Analog Input in Arduino IDE
23+
url: https://deepbluembedded.com/esp32-adc-tutorial-read-analog-voltage-arduino/
1824
difficulty: medium
1925
features:
2026
- esp32c3
21-
- access
22-
- point
27+
- adc
2328
- measure
2429
- battery
2530
- voltage
2631
images:
27-
# prototype: demo-prototype.jpg
28-
# console: demo-console.png
29-
# schematic: schematic.png
32+
prototype: demo-prototype.jpg
33+
console: measure-battery-level-console.png
3034
---
3135

32-
1. Upload the firmware with `make`
33-
1. Remove the USB-C cable used for firmware upload
34-
1. Turn on the power switch
35-
1. Connect to access point `batt` with password `12345678`
36-
1. Browser to `http://192.168.4.1` on the browser
37-
1. View `hello world`
36+
This code measure the battery voltage using the ESP32-C3 ADC. The battery voltage is measured using a voltage divider circuit. The voltage divider circuit is used to scale down the battery voltage to a level that can be measured by the ESP32-C3 ADC. The ESP32-C3 ADC has a 12-bit resolution.

_code/measure-battery-level/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ BUILD = build
99
default: lint compile upload clean
1010

1111
lint:
12-
cpplint --extensions=ino --filter=-legal/copyright,-runtime/int,-readability/todo *.ino
12+
cpplint --extensions=ino --filter=-legal/copyright,-runtime/int,-readability/todo,-whitespace/line_length *.ino
1313

1414
compile: clean lint
1515
arduino-cli compile --fqbn $(BOARD) --output-dir $(BUILD) ./
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,61 @@
1-
// The code turns ON the MOSFET for 2 seconds
2-
// and then turns it OFF for 2 seconds.
3-
// The LED is also turned ON and OFF to indicate the state of the MOSFET.
4-
// The serial monitor is used to print the state of the MOSFET.
5-
61
#define LED 3
72
#define BATTERY_ENABLE_PIN 6
83
#define BATTERY_MEASURE_PIN 0
94

10-
const float R1 = 10000.0; // 10kΩ
11-
const float R2 = 100000.0; // 100kΩ
12-
const float Vref = 3.3; // Reference voltage for ADC (3.3V for ESP32-C3)
5+
// TODO: Amend to 100kΩ resistors in the PCB for lower current consumption
6+
const float R1 = 33000.0; // 33kΩ
7+
const float R2 = 33000.0; // 33kΩ
138
const int adcMax = 4095; // 12-bit ADC resolution
149

10+
// https://forum.arduino.cc/t/esp32-c3-adc-issue-reading-4095-at-2-8v/1127687/7
11+
const float Vref = 2.84; // Reference voltage for ADC (2.8=(V for ESP32-C3)
12+
1513
void setup() {
1614
Serial.begin(115200);
1715
Serial.println();
1816

1917
pinMode(LED, OUTPUT);
2018
pinMode(BATTERY_ENABLE_PIN, OUTPUT);
21-
digitalWrite(BATTERY_ENABLE_PIN, HIGH); // Turn OFF the MOSFET
19+
digitalWrite(BATTERY_ENABLE_PIN, HIGH);
2220
}
2321

2422
void loop() {
2523
digitalWrite(LED, HIGH);
26-
Serial.println("MOSFET is ON");
27-
digitalWrite(BATTERY_ENABLE_PIN, LOW); // Turn ON the MOSFET
2824

25+
digitalWrite(BATTERY_ENABLE_PIN, LOW);
2926
delayMicroseconds(10);
3027
int sum = 0;
28+
3129
for (int i = 0; i < 100; i++) {
3230
sum = sum + analogRead(BATTERY_MEASURE_PIN);
3331
}
34-
3532
float adcValue = sum / 100.0;
3633
Serial.print("Raw ADC Value: ");
3734
Serial.println(adcValue);
3835

3936
float voltageAtPin = (adcValue / adcMax) * Vref;
4037
Serial.print("Voltage at Pin: ");
41-
Serial.println(voltageAtPin);
38+
Serial.print(voltageAtPin);
39+
Serial.println("V");
4240

4341
float batteryVoltage = voltageAtPin * ((R1 + R2) / R2);
4442
Serial.print("Battery Voltage: ");
45-
Serial.println(batteryVoltage);
46-
43+
Serial.print(batteryVoltage);
44+
Serial.println("V");
4745

48-
float batteryLevel = (batteryVoltage - 3.0) / (4.2 - 3.0) * 100;
4946
// 3.0: Minimum voltage of the battery
5047
// 4.2: Maximum voltage of the battery
5148
// 100: Maximum battery level in percentage
49+
float batteryLevel = (batteryVoltage - 3.0) / (4.2 - 3.0) * 100;
5250
Serial.print("Battery Level: ");
5351
Serial.print(batteryLevel);
52+
Serial.println("%");
53+
54+
delay(2000);
5455

55-
digitalWrite(BATTERY_ENABLE_PIN, HIGH); // Turn OFF the MOSFET
56+
digitalWrite(BATTERY_ENABLE_PIN, HIGH);
5657
digitalWrite(LED, LOW);
57-
Serial.println("MOSFET is OFF");
58-
Serial.println("-------------------- ");
59-
delay(4000);
58+
59+
Serial.println();
60+
delay(2000);
6061
}
529 KB
Loading

images/pcb/3dview-back.png

6.07 KB
Loading

images/pcb/3dview-bottom.png

-12.3 KB
Loading

images/pcb/3dview-front.png

-40.2 KB
Loading

images/pcb/3dview-top.png

-8.22 KB
Loading

images/pcb/layout-back.png

19.2 KB
Loading

0 commit comments

Comments
 (0)