EV Battery — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by crganesh537.

Components

Sketch code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>

// =====================================================
// PIN DEFINITIONS
// =====================================================

#define VOLTAGE_PIN   34
#define CURRENT_PIN   35

#define TEMP_PIN      4
#define DHT_TYPE      DHT22

#define RELAY_PIN     23
#define BUZZER_PIN    16

#define GREEN_LED     25
#define RED_LED       26

#define START_BUTTON  33
#define STOP_BUTTON   32

// =====================================================
// COMPONENTS
// =====================================================

LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(TEMP_PIN, DHT_TYPE);

// =====================================================
// VARIABLES
// =====================================================

float voltage = 10.0;
float current = 0.0;
float temperature = 25.0;
float soc = 0.0;

String fault = "NORMAL";

bool charging = false;
bool buzzerActive = false;
bool dhtWorking = false;

// =====================================================
// BUTTON VARIABLES
// =====================================================

int lastStartState = HIGH;
int lastStopState = HIGH;

unsigned long lastStartPress = 0;
unsigned long lastStopPress = 0;

const unsigned long DEBOUNCE_TIME = 200;

// =====================================================
// DHT22 VARIABLES
// =====================================================

unsigned long lastDHTRead = 0;
const unsigned long DHT_INTERVAL = 2000;

// =====================================================
// DISPLAY
// =====================================================

unsigned long lastDisplayUpdate = 0;
const unsigned long DISPLAY_INTERVAL = 500;

// =====================================================
// SERIAL
// =====================================================

unsigned long lastSerialUpdate = 0;
const unsigned long SERIAL_INTERVAL = 500;

// =====================================================
// BUZZER VARIABLES
// =====================================================

// 2000 Hz = 500 us full period
// Half period = 250 us
unsigned long lastBuzzerToggle = 0;
bool buzzerPinState = false;

const unsigned long BUZZER_HALF_PERIOD = 250;

// =====================================================
// READ BATTERY VOLTAGE
// =====================================================

float readVoltage()
{
    int adc = analogRead(VOLTAGE_PIN);

    // Simulation mapping:
    // ADC 0    = 10 V
    // ADC 4095 = 16 V

    float v = 10.0 + ((float)adc / 4095.0) * 6.0;

    return v;
}

// =====================================================
// READ BATTERY CURRENT
// =====================================================

float readCurrent()
{
    int adc = analogRead(CURRENT_PIN);

    // Simulation mapping:
    // ADC 0    = 0 A
    // ADC 4095 = 20 A

    float i = ((float)adc / 4095.0) * 20.0;

    return i;
}

// =====================================================
// READ DHT22
// =====================================================