# esp32-Scale
A high-precision digital scale powered by an ESP32, HX711 load cell amplifier, and an SSD1306 OLED display. Optimized for milligram-level accuracy and hardware stability.

<img src="1_IMGS/final_scale.jpg" alt="Final ESP32 Scale" width="500">
<img src="1_IMGS/logo.png" alt="Rodin Logo" width="600">

## ✨ Features
- High Precision: 24-bit resolution with dual decimal place output (0.01g).
- Smart UI: Optimized 0.96" OLED interface for real-time weight monitoring.
- Reset Button: Fast and easy zero-setting (Tare) with one click.

## 🛠️ Hardware
- Controller: ESP32 DevKit V1
- Sensor: HX711 Load Cell Amplifier
- Display: SSD1306 128x64 I2C OLED

| Component | Image | Recommended Link |
| :--- | :---: | :--- |
| **ESP32 DevKit V1** | <img src="1_IMGS/esp32.png" alt="ESP32" width="150"> | [Buy here](https://www.amazon.de/dp/B0DHRV7784) |
| **HX711 Amplifier** | <img src="1_IMGS/hx711.png" alt="HX711" width="150"> | [Buy here](https://www.amazon.de/dp/B0DH1XNGHT) |
| **Load Cell (100g)** | <img src="1_IMGS/waegzelle.png" alt="Load Cell 100g" width="150"> | [Buy here](https://www.amazon.de/dp/B0BZ62BRWK) |
| **SSD1306 OLED (I2C)** | <img src="1_IMGS/display.png" alt="OLED Display" width="150"> | [Buy here](https://www.amazon.de/dp/B074N9VLZX) |
| **Push Button** | <img src="1_IMGS/buttons.png" alt="Button" width="150"> | [Buy here](https://www.amazon.de/dp/B07ZTQZP9G) |


## 🔌 Pin Connections

The ESP32 acts as the brain of the scale. Below is the wiring diagram for all components.

### 1. HX711 (Load Cell Amplifier) to ESP32
| HX711 Pin | ESP32 Pin | Description |
| :--- | :--- | :--- |
| **VCC** | 3.3V / VIN | Power Supply |
| **GND** | GND | Ground |
| **DT** | GPIO 18 | Data Pin |
| **SCK** | GPIO 19 | Clock Pin |

### 2. SSD1306 OLED Display (I2C) to ESP32
| OLED Pin | ESP32 Pin | Description |
| :--- | :--- | :--- |
| **VCC** | 3.3V | Power Supply |
| **GND** | GND | Ground |
| **SCL** | GPIO 22 | I2C Clock |
| **SDA** | GPIO 21 | I2C Data |

### 3. Tare Button
| Button Pin | ESP32 Pin | Description |
| :--- | :--- | :--- |
| **(S)** | GPIO 4 | Signal Pin (Input Pullup) |
| **(-)** | GND | Ground |

### 4. Load Cell to HX711
* **Red:** E+
* **Black:** E-
* **White:** A-
* **Green:** A+
*(Note: Colors might vary depending on your specific load cell model)*

## 🛠 Soldering 🛠

To make the scale durable and prevent loose connections (especially for the precise HX711), I soldered all components together.

<img src="1_IMGS/soldered.png" alt="Soldered ESP32 Scale" width="300">

### 💡 Soldering Tips

* **Use Thin Wires:** I used thin, strippable wires to connect everything without making a huge mess.
* **Keep it Small:** Solder the components directly or onto a small prototype board (Perfboard) to save space.
* **Double-Check:** Before you plug it into power, check all connections again to make sure you have no short circuits between 3.3V and GND!

## 🔍 Code Insights & Logic

Here is a breakdown of the core logic used in this project to ensure stability and precision.

### 🔌 1. Boot Stability (Setup)
The ESP32 can be picky with power. This setup ensures everything initializes correctly.

<img src="1_IMGS/setup.png" alt="Setup Logic" width="600">

* **Safety Buffer:** `delay(2000)` prevents crashes during boot if the power supply has a slow voltage ramp-up.
* **I2C Pin Definition:** `Wire.begin(21, 22)` manually sets the pins for the OLED to avoid conflicts with default ESP32 configurations.
* **Calibration:** The `set_scale` factor that converts raw sensor data into grams.

---

### ⚡ 2. Speed-Tare (Interrupt Logic)
The reset button is handled via a Hardware Interrupt for instant response.

<img src="1_IMGS/interrupt.png" alt="Interrupt Logic" width="600">

* **IRAM_ATTR:** This attribute forces the function into the ESP32's internal RAM. This means the CPU doesn't have to fetch it from flash memory, making the button response lag-free.
* **Software Debouncing:** We check `millis()` to ensure the scale only tares once every 500ms, ignoring "ghost clicks" from the physical button.

---

### ⚖️ 3. Precision Measurement (Loop)

<img src="1_IMGS/measure.png" alt="Measurement Logic" width="600">

* **Average Sampling:** `get_units(5)` takes five quick readings and calculates the mean to filter out electronic noise and vibrations.
* **Display Formatting:** `dtostrf` is used to force exactly 2 decimal places. Standard `print` functions often struggle with precision formatting on microcontrollers.

  
## 💻 Software & IDE Setup ⚙️

To upload the code to your ESP32, follow these steps in the **Arduino IDE**:

### 1. ESP32 Board Setup
1. Open **File > Preferences**.
2. Paste this URL into "Additional Boards Manager URLs":
   `https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json`
3. Go to **Tools > Board > Boards Manager** and search for `esp32`.
4. **Recommended:** Install the official version by **Espressif Systems**. It’s the most stable one for this project.
1. Open **File > Preferences**.
2. Paste this URL into "Additional Boards Manager URLs":
   `https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json`
3. Go to **Tools > Board > Boards Manager** and search for `esp32`.
4. **Recommended:** Install the official version by **Espressif Systems**. It’s the most stable one for this project.

* ### 2. Choosing the Board
Select this board to make sure the pins work:
* **Board:** `DOIT ESP32 DEVKIT V1`
* **Upload Speed:** `115200`
* * **Port:** Select the COM port where your ESP32 is plugged in.

### 3. Required Libraries
You need to install these libraries via the **Library Manager** (Tools > Manage Libraries):
* **HX711 Arduino Library** (by Bogdan Necula)
* **U8g2** (by oliver) - for the OLED display

### 4. Get the Code & Flash it
You have two options here:

* **Use my Code (Easy):** Just download the `.ino` file from this repository. It’s already optimized and ready to go.
* **DIY:** You can also use the code as a base and add your own features if you like.

**Steps to Flash:**
1. Connect the ESP32 via USB.
2. Open the file in Arduino IDE.
3. Hit **Upload**. 
4. **Pro-Tip:** If the console says "Connecting...", just hold the **BOOT** button on your ESP32 for a second until the upload starts.


