-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix (firmware, schematic, layout): change vref for adc and resistors …
…to measure battery voltage
- Loading branch information
Showing
17 changed files
with
465 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,36 @@ | ||
--- | ||
layout: code | ||
title: Measure battery voltage | ||
description: View the battery voltage through an access point | ||
description: Measure battery level using the ESP32-C3 ADC | ||
references: | ||
- name: Schematic of ESP32-C3-DevKitM-1 | ||
url: https://dl.espressif.com/dl/schematics/SCH_ESP32-C3-DEVKITM-1_V1_20200915A.pdf | ||
- name: Pinouts | ||
url: https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/hw-reference/esp32c3/user-guide-devkitm-1.html#pin-layout | ||
- name: Arduino Pins | ||
url: https://github.com/espressif/arduino-esp32/blob/master/variants/esp32c3/pins_arduino.h | ||
- name: Create an Access Point | ||
url: https://raw.githubusercontent.com/espressif/arduino-esp32/990e3d5b431b63b4adc364b045a79afdad645a3f/libraries/WiFi/examples/WiFiAccessPoint/WiFiAccessPoint.ino | ||
- name: How to check the battery voltage | ||
url: https://wiki.seeedstudio.com/check_battery_voltage/ | ||
- name: Power Management | ||
url: https://learn.adafruit.com/adafruit-feather-m4-express-atsamd51/power-management#measuring-battery-3010518 | ||
- name: ESP32-C3 ADC issue - reading 4095 at 2.8V | ||
url: https://forum.arduino.cc/t/esp32-c3-adc-issue-reading-4095-at-2-8v/1127687 | ||
- name: Analog to Digital Converter (ADC) | ||
url: https://docs.espressif.com/projects/esp-idf/zh_CN/v4.4.2/esp32c3/api-reference/peripherals/adc.html | ||
- name: Voltage Divider Calculator | ||
url: https://ohmslawcalculator.com/voltage-divider-calculator | ||
- name: ESP32 ADC – Read Analog Input in Arduino IDE | ||
url: https://deepbluembedded.com/esp32-adc-tutorial-read-analog-voltage-arduino/ | ||
difficulty: medium | ||
features: | ||
- esp32c3 | ||
- access | ||
- point | ||
- adc | ||
- measure | ||
- battery | ||
- voltage | ||
images: | ||
# prototype: demo-prototype.jpg | ||
# console: demo-console.png | ||
# schematic: schematic.png | ||
prototype: demo-prototype.jpg | ||
console: measure-battery-level-console.png | ||
--- | ||
|
||
1. Upload the firmware with `make` | ||
1. Remove the USB-C cable used for firmware upload | ||
1. Turn on the power switch | ||
1. Connect to access point `batt` with password `12345678` | ||
1. Browser to `http://192.168.4.1` on the browser | ||
1. View `hello world` | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,61 @@ | ||
// The code turns ON the MOSFET for 2 seconds | ||
// and then turns it OFF for 2 seconds. | ||
// The LED is also turned ON and OFF to indicate the state of the MOSFET. | ||
// The serial monitor is used to print the state of the MOSFET. | ||
|
||
#define LED 3 | ||
#define BATTERY_ENABLE_PIN 6 | ||
#define BATTERY_MEASURE_PIN 0 | ||
|
||
const float R1 = 10000.0; // 10kΩ | ||
const float R2 = 100000.0; // 100kΩ | ||
const float Vref = 3.3; // Reference voltage for ADC (3.3V for ESP32-C3) | ||
// TODO: Amend to 100kΩ resistors in the PCB for lower current consumption | ||
const float R1 = 33000.0; // 33kΩ | ||
const float R2 = 33000.0; // 33kΩ | ||
const int adcMax = 4095; // 12-bit ADC resolution | ||
|
||
// https://forum.arduino.cc/t/esp32-c3-adc-issue-reading-4095-at-2-8v/1127687/7 | ||
const float Vref = 2.84; // Reference voltage for ADC (2.8=(V for ESP32-C3) | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.println(); | ||
|
||
pinMode(LED, OUTPUT); | ||
pinMode(BATTERY_ENABLE_PIN, OUTPUT); | ||
digitalWrite(BATTERY_ENABLE_PIN, HIGH); // Turn OFF the MOSFET | ||
digitalWrite(BATTERY_ENABLE_PIN, HIGH); | ||
} | ||
|
||
void loop() { | ||
digitalWrite(LED, HIGH); | ||
Serial.println("MOSFET is ON"); | ||
digitalWrite(BATTERY_ENABLE_PIN, LOW); // Turn ON the MOSFET | ||
|
||
digitalWrite(BATTERY_ENABLE_PIN, LOW); | ||
delayMicroseconds(10); | ||
int sum = 0; | ||
|
||
for (int i = 0; i < 100; i++) { | ||
sum = sum + analogRead(BATTERY_MEASURE_PIN); | ||
} | ||
|
||
float adcValue = sum / 100.0; | ||
Serial.print("Raw ADC Value: "); | ||
Serial.println(adcValue); | ||
|
||
float voltageAtPin = (adcValue / adcMax) * Vref; | ||
Serial.print("Voltage at Pin: "); | ||
Serial.println(voltageAtPin); | ||
Serial.print(voltageAtPin); | ||
Serial.println("V"); | ||
|
||
float batteryVoltage = voltageAtPin * ((R1 + R2) / R2); | ||
Serial.print("Battery Voltage: "); | ||
Serial.println(batteryVoltage); | ||
|
||
Serial.print(batteryVoltage); | ||
Serial.println("V"); | ||
|
||
float batteryLevel = (batteryVoltage - 3.0) / (4.2 - 3.0) * 100; | ||
// 3.0: Minimum voltage of the battery | ||
// 4.2: Maximum voltage of the battery | ||
// 100: Maximum battery level in percentage | ||
float batteryLevel = (batteryVoltage - 3.0) / (4.2 - 3.0) * 100; | ||
Serial.print("Battery Level: "); | ||
Serial.print(batteryLevel); | ||
Serial.println("%"); | ||
|
||
delay(2000); | ||
|
||
digitalWrite(BATTERY_ENABLE_PIN, HIGH); // Turn OFF the MOSFET | ||
digitalWrite(BATTERY_ENABLE_PIN, HIGH); | ||
digitalWrite(LED, LOW); | ||
Serial.println("MOSFET is OFF"); | ||
Serial.println("-------------------- "); | ||
delay(4000); | ||
|
||
Serial.println(); | ||
delay(2000); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.