|
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 | | - |
6 | 1 | #define LED 3 |
7 | 2 | #define BATTERY_ENABLE_PIN 6 |
8 | 3 | #define BATTERY_MEASURE_PIN 0 |
9 | 4 |
|
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Ω |
13 | 8 | const int adcMax = 4095; // 12-bit ADC resolution |
14 | 9 |
|
| 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 | + |
15 | 13 | void setup() { |
16 | 14 | Serial.begin(115200); |
17 | 15 | Serial.println(); |
18 | 16 |
|
19 | 17 | pinMode(LED, OUTPUT); |
20 | 18 | pinMode(BATTERY_ENABLE_PIN, OUTPUT); |
21 | | - digitalWrite(BATTERY_ENABLE_PIN, HIGH); // Turn OFF the MOSFET |
| 19 | + digitalWrite(BATTERY_ENABLE_PIN, HIGH); |
22 | 20 | } |
23 | 21 |
|
24 | 22 | void loop() { |
25 | 23 | digitalWrite(LED, HIGH); |
26 | | - Serial.println("MOSFET is ON"); |
27 | | - digitalWrite(BATTERY_ENABLE_PIN, LOW); // Turn ON the MOSFET |
28 | 24 |
|
| 25 | + digitalWrite(BATTERY_ENABLE_PIN, LOW); |
29 | 26 | delayMicroseconds(10); |
30 | 27 | int sum = 0; |
| 28 | + |
31 | 29 | for (int i = 0; i < 100; i++) { |
32 | 30 | sum = sum + analogRead(BATTERY_MEASURE_PIN); |
33 | 31 | } |
34 | | - |
35 | 32 | float adcValue = sum / 100.0; |
36 | 33 | Serial.print("Raw ADC Value: "); |
37 | 34 | Serial.println(adcValue); |
38 | 35 |
|
39 | 36 | float voltageAtPin = (adcValue / adcMax) * Vref; |
40 | 37 | Serial.print("Voltage at Pin: "); |
41 | | - Serial.println(voltageAtPin); |
| 38 | + Serial.print(voltageAtPin); |
| 39 | + Serial.println("V"); |
42 | 40 |
|
43 | 41 | float batteryVoltage = voltageAtPin * ((R1 + R2) / R2); |
44 | 42 | Serial.print("Battery Voltage: "); |
45 | | - Serial.println(batteryVoltage); |
46 | | - |
| 43 | + Serial.print(batteryVoltage); |
| 44 | + Serial.println("V"); |
47 | 45 |
|
48 | | - float batteryLevel = (batteryVoltage - 3.0) / (4.2 - 3.0) * 100; |
49 | 46 | // 3.0: Minimum voltage of the battery |
50 | 47 | // 4.2: Maximum voltage of the battery |
51 | 48 | // 100: Maximum battery level in percentage |
| 49 | + float batteryLevel = (batteryVoltage - 3.0) / (4.2 - 3.0) * 100; |
52 | 50 | Serial.print("Battery Level: "); |
53 | 51 | Serial.print(batteryLevel); |
| 52 | + Serial.println("%"); |
| 53 | + |
| 54 | + delay(2000); |
54 | 55 |
|
55 | | - digitalWrite(BATTERY_ENABLE_PIN, HIGH); // Turn OFF the MOSFET |
| 56 | + digitalWrite(BATTERY_ENABLE_PIN, HIGH); |
56 | 57 | digitalWrite(LED, LOW); |
57 | | - Serial.println("MOSFET is OFF"); |
58 | | - Serial.println("-------------------- "); |
59 | | - delay(4000); |
| 58 | + |
| 59 | + Serial.println(); |
| 60 | + delay(2000); |
60 | 61 | } |
0 commit comments