Skip to content

Commit

Permalink
fix (firmware, schematic, layout): change vref for adc and resistors …
Browse files Browse the repository at this point in the history
…to measure battery voltage
  • Loading branch information
sayanee committed Jul 9, 2024
1 parent 401c57f commit 61efc72
Show file tree
Hide file tree
Showing 17 changed files with 465 additions and 218 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"iterator": "cpp",
"serstream": "cpp",
"__locale": "cpp",
"unordered_set": "cpp"
"unordered_set": "cpp",
"complex": "cpp"
}
}
27 changes: 13 additions & 14 deletions _code/measure-battery-level.md
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.
2 changes: 1 addition & 1 deletion _code/measure-battery-level/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ BUILD = build
default: lint compile upload clean

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

compile: clean lint
arduino-cli compile --fqbn $(BOARD) --output-dir $(BUILD) ./
Expand Down
41 changes: 21 additions & 20 deletions _code/measure-battery-level/measure-battery-level.ino
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);
}
Binary file added images/code/measure-battery-level-console.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/pcb/3dview-back.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/pcb/3dview-bottom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/pcb/3dview-front.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/pcb/3dview-top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/pcb/layout-back.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/pcb/layout-front.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/pcb/schematic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 61efc72

Please sign in to comment.