Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
9f30952
Update TEMPLATE.md
madelinefruin Nov 20, 2024
e4955a9
Merge pull request #1 from madelinefruin/madelinefruin-patch-1
madelinefruin Nov 20, 2024
50c29ce
Update TEMPLATE.md
madelinefruin Nov 20, 2024
07e683e
Merge pull request #2 from madelinefruin/madelinefruin-patch-2
madelinefruin Nov 20, 2024
ef1762e
Create README.md
madelinefruin Nov 20, 2024
ed41222
Update TEMPLATE.md
madelinefruin Nov 20, 2024
55d3275
Update TEMPLATE.md
madelinefruin Nov 20, 2024
136c256
Update TEMPLATE.md
madelinefruin Nov 20, 2024
5cc7db8
Update TEMPLATE.md
madelinefruin Nov 20, 2024
2cb4ad3
Update TEMPLATE.md
madelinefruin Nov 20, 2024
6f6cedd
Update TEMPLATE.md
madelinefruin Nov 20, 2024
9a548d6
Update TEMPLATE.md
madelinefruin Nov 20, 2024
026c477
Update TEMPLATE.md
madelinefruin Nov 20, 2024
c76dcfd
Update TEMPLATE.md
madelinefruin Nov 20, 2024
cb54210
Update TEMPLATE.md
madelinefruin Nov 20, 2024
31fc14e
Update TEMPLATE.md
madelinefruin Nov 20, 2024
7dd1a94
Add files via upload
madelinefruin Nov 20, 2024
69e2f69
Add files via upload
madelinefruin Nov 20, 2024
95c5d3e
Update TEMPLATE.md
madelinefruin Nov 20, 2024
5e0ed82
Update TEMPLATE.md
madelinefruin Nov 20, 2024
2aa43ed
Update TEMPLATE.md
madelinefruin Nov 20, 2024
794e53e
Update TEMPLATE.md
madelinefruin Nov 20, 2024
e43fd84
Update TEMPLATE.md
madelinefruin Nov 20, 2024
228bc0d
Update TEMPLATE.md
madelinefruin Nov 20, 2024
c9bac50
Update TEMPLATE.md
madelinefruin Nov 20, 2024
54ecd2d
Update TEMPLATE.md
madelinefruin Nov 20, 2024
da0856f
Update TEMPLATE.md
madelinefruin Nov 20, 2024
5b0f5a6
Update TEMPLATE.md
madelinefruin Nov 20, 2024
6e292f5
Update TEMPLATE.md
madelinefruin Nov 20, 2024
9b48e62
Update TEMPLATE.md
madelinefruin Nov 20, 2024
8bf5ec6
Update TEMPLATE.md
wluong101 Nov 21, 2024
b0ed4c0
Update TEMPLATE.md
wluong101 Nov 21, 2024
c085f21
Update TEMPLATE.md
wluong101 Nov 21, 2024
97b5fa9
Update TEMPLATE.md
madelinefruin Nov 21, 2024
13cdb8e
Add files via upload
wluong101 Nov 21, 2024
c350c2f
Update TEMPLATE.md
wluong101 Nov 21, 2024
9a243ee
Add files via upload
wluong101 Nov 21, 2024
0a3a792
Update TEMPLATE.md
madelinefruin Nov 21, 2024
52de971
Update TEMPLATE.md
wluong101 Nov 21, 2024
59b1dd5
Merge pull request #3 from madelinefruin/wluong101-patch-1
wluong101 Nov 21, 2024
5ddda56
Add files via upload
madelinefruin Nov 21, 2024
3ce225a
Update TEMPLATE.md
madelinefruin Nov 21, 2024
164f144
Update TEMPLATE.md
madelinefruin Nov 21, 2024
523885e
Update TEMPLATE.md
wluong101 Nov 21, 2024
b3f8b79
Update TEMPLATE.md
wluong101 Nov 21, 2024
9472932
Update TEMPLATE.md
wluong101 Nov 21, 2024
d7e491c
Update TEMPLATE.md
madelinefruin Nov 22, 2024
d140a75
Update TEMPLATE.md
madelinefruin Nov 22, 2024
e0d9da1
Update TEMPLATE.md
madelinefruin Nov 22, 2024
256709f
Update TEMPLATE.md
madelinefruin Nov 22, 2024
209f569
Update TEMPLATE.md
madelinefruin Nov 22, 2024
437c5e9
Update TEMPLATE.md
madelinefruin Nov 22, 2024
08db9cc
Rename TEMPLATE.md to Team13Tutorial.md
madelinefruin Nov 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Arduino_output.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 added BatteryCharged.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Final_circuit_Image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Fritz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is ECE196 Fall 2024 Team 13's tutorial. We are explaining how to make a battery percentage indicator with the ESP32.
107 changes: 0 additions & 107 deletions TEMPLATE.md

This file was deleted.

237 changes: 237 additions & 0 deletions Team13Tutorial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
---
title: Battery Indicator with ESP32
date: 2024-11-21
authors:
- name: Andy Tu
- name: Madeline Fruin
- name: William Luong
---

<img src="BatteryCharged.jpg" alt="[Circuit Fritz](https://www.vectorstock.com/royalty-free-vector/fully-charged-battery-color-icon-vector-28877489)"> </img>

## Introduction

This tutorial will teach readers how to use ESP32's ADC pins to measure a battery's charge. Since the ADC pins can take in a max of 3.3V, we will be covering how to implement a voltage divider to lower the maximum voltage of a battery to an amount allowed by the ESP32. For the coding section, we will use the Arduino IDE to write code that will read, display, and convert the battery readings to a charge percentage using a function.

### Learning Objectives

- Voltage Dividers
- ESP32 ADC/GPIO Pins
- Arduino C
- Battery Charge State

### Background Information

A battery indicator circuit measures the state of charge (SoC) of a battery and visually outputs whether the battery’s capacity is low or full. These circuits are commonly used in battery management systems, smartphones, and other consumer electronics.

Pros:
1. Provides real-time feedback on battery life.
2. Prevents damage to the battery by avoiding overcharging or over-discharging.
3. Offers insights to maximize battery life and performance.

Cons:

1. The more complex the circuit, the higher the cost (e.g., precise resistors may be required).
2. Limited accuracy, as voltage alone isn’t the only factor that determines the charge state.

Key Concepts:

1. Voltage Divider
2. Resistor Ratios with Multiple Batteries
3. Arduino Basics

## Getting Started

You will receive an ESP32 board and USB-C to USB-C connector cable. The ESP32 board utilizes the ESP32 (obviously). The ESP32 is a system on a chip with Wifi and bluetooth capacbilities. This means that it can be used to make IoT projects(Internet of things). However, we will be using it very simply, you do not need to understand everything it can do for this tutorial.

You will also receive a breadboard, jumper cables, and resistors. The rows of the breadboard are connected while the columns are not. The resistors and jumper cables should fit snuggly into the holes of the breadboard.

We will be using the Arduino IDE (Integrated Development Environment). This is a free, open source program that allows users to write code and upload it to boards.

### Required Downloads and Installations

If you don't have the Arduino IDE already, download it <a href="https://www.arduino.cc/en/software"><here>here.</a>

### Required Components

List your required hardware components and the quantities here.

| Component Name | Quanitity |
| -------------- | --------- |
| ESP32 | 1 |
| ESP32 Connector Cable | 1 |
|Resistors (47kΩ)| 2 |
| 18650 Battery | 1 |
| Battery Holder | 1 |
|Mini Breadboard | 1 |
| Jumper Cables | 2 |


### Required Tools and Equipment

Computer, Arduino IDE

## Part 01: Setting up the Circuit

### Introduction

In this section, we will be discussing the voltage divider and circuit setup. We will not be writing any code, so a computer is not yet needed.

### Objective

- Understand how to pick values for a voltage divider.
- Understand how to properly connect the battery, resistors, and ESP32.

### Background Information

Give a brief explanation of the technical skills learned/needed
in this challenge. There is no need to go into detail as a
separation document should be prepared to explain more in depth
about the technical skills

If you've taken a look at the battery we gave you, you might notice that it says 3.7V on it. The maximum voltage input for the ESP32 is 3.3V, so we need to find a way to decrease the voltage from the battery, we can do this with a <b>voltage divider</b>.

A voltage divider is a passive linear circuit made up of two resistors, although sometimes more are used to get a specific resistance value.
<img src = "voltagedivider.png" alt="Voltage Divider Schematic and Equation">

We have given you two 47kΩ resistors, so the output voltage will be 3.7V * 47kΩ/(47kΩ + 47kΩ) = 1.85V.

Notice that our voltage divider halves the input voltage. To combat this division, we will need to remember to multiply our measured voltage by 2 (the reciprocal).

### Components

- Breadboard
- Battery and Battery Holder
- Resistors
- ESP32
- Jumper Cables

### Instructional

Assemble the circuit as shown in the below image.
<img src="Fritz.png" alt="Circuit Fritz"> </img>
Make sure to attach the cathode of the battery to the voltage divider *AND* to the ground pin of the ESP32!

## Part 02: Writing the Code

### Introduction

In this section, we will be discussing the code used to measure the battery's percentage.

### Objective

- Understand the Arduino interface.
- Understand how to assign pin numbers in Arduino.
- Understand how to implement functions in Arduino.

### Background Information

This section focuses on coding in the Arduino IDE. The language is very similar to C or C++. If you have any background knowledge in those languages, you will recognize the code and its formatting. If you don't have experience in C/C++, don't worry, we will be breaking down the code into very manageable chunks.
*Hint: If you don't want to rewrite all the code, use the copy button in the upper right corner of the code blocks!*

### Components

- ESP32 (with the circuit you just built attached)
- USB-C to USB-C Connector cable for ESP32
- Your Computer

### Instructional

Another goal of the project is to integrate both hardware and software to create a simple battery indicator system using the ESP32 microcontroller.
Let's take a dive into the indvidual blocks of code.

<pre><code class = "language-arduino"> //Constants
const int batteryPin = 1;
const float referenceVoltage = 3.3;
const int resolution = 3950;
const float voltageDividerRatio = 2.0;
const float maxBatt = 4.2;
const float minBatt = 3;
</code></pre>
- `batteryPin` is the analog pin connected to the voltage divider's output
- `referenceVoltage` is the maximum voltage the ADC can read. (normally it's 3.3V for the ESP32)
- `resolution` defines the ADC's bit resolution, which determines how anlog voltage is assigned to digital values
- `voltageDividerRatio` the scaling factor of the voltage divider
- `maxBatt` and `minBatt` defines the battery's voltage range.

<pre><code class = "language-arduino"> //Function to calculate battery voltage
float readBatteryVoltage() {
int rawADC = analogRead(?)
float voltageAtPin = ?
float batteryVoltage = ?
return batteryVoltage;
}
</code></pre>

Fill out the code function that calculates the battery voltage.
- `analogRead` captures the raw ADC value corresponding to the voltage at the ESP32 pin.
- `voltageAtPin` using the provided code and understanding of the ADC conversion, write the line of code that calculates it.
- The ADC reading is a fraction of the max resolution.
- mulitply this fraction by the reference voltage.
- `batteryVoltage` After finding the voltageAtPin how can we find the actual battery voltage (Hint: remember the scaling factor).

<pre><code class = "language-arduino"> //setup
void setup() {
Serial.begin(115200);
pinMode(?);
}
</code></pre>

- `Serial.begin(115200)` sets up communication with the serial monitor for real-time data output.
- `pinMode` This should configures the ADC pin as an input to read the voltage of the circuit.

<pre><code class = "language-C"> //loop
void loop() {
float batteryVoltage = readBatteryVoltage();
Serial.print("Battery Voltage: ");
Serial.print(batteryVoltage);
Serial.println(" V");

int battPercent = ((batteryVoltage-minBatt)/(maxBatt-minBatt))*100;
Serial.print("Battery Percentage: ");
Serial.print(battPercent);
Serial.println(" %");

delay(10000);
}
</code></pre>
- The loop continously monitors and outputs the battery's state:
- `readBatteryVoltage()` gets the current battery voltage
- `Serial.print` displays the voltage in the serial monitor
- `battPercent` calculates the battery's percentage
- `delay(10000)` pauses the loop for 10 seconds

Overall this function should provide us with real-time battery status.
## Final Circuit Image


<img src="Final_circuit_Image.jpg" alt="Photo of real-life completed circuit"> </img>

Circuit setup should look similar to the image above.

### Arduino Ouput

<img src="Arduino_output.png" alt="Sample Output"> </img>

Your Arduino output should print this several times (your voltage percentage may differ depending on battery).
### Analysis

The example uses an ESP32 and a voltage divider to measure a battery's state of charge. This circuit demostrates the core concepts like ADC functionality and Arduino coding. The voltage divder lowers the battery voltage(3.7V to 1.85V) to a safe level for the ESP32 to read. Once the circuit is connected on the breadboard and linked to the ESP32, the Arduino code will adjust for the voltage scaling to calculate and provide an ouput of both the actual voltage and charge percentage. You can see how this employs the concepts of circuit design and programming with the readBatteryVoltage function by converting ADC readings into functional voltage data. The serial monitor should output real-time voltage and it helps validate the system's accuracy.

## Challenge Questions

How would this change if we were using a 4.2V 18650 battery? Would you need to change either of the values of the voltage divider or just the code?

What about a 12V battery?

### Challenge Answers

We would not need to change the voltage divider for a 4.2V battery because 4.2V/2 is 2.1V, which is under the maximum that the board can take in. We would only need to change the minBatt and maxBatt values in the code.

We would need to change the voltage divider for a 12V battery because 12V/2 is 6V, which is nearly double what the board can take in! We could choose R1 = 300kΩ and R2 = 100kΩ to get 1/4 of 12V, or 3V. We would have to change our maxBatt, minBatt, *and VoltageDividerRatio* in the code.

## Additional Resources

### Useful links

<a href="https://www.monolithicpower.com/learning/resources/an-introduction-to-batteries-components-parameters-types-and-chargers"><here>Website that tells the nominal voltage and voltage range of many batteries.</a>
Binary file added voltagedivider.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.