Openlab — ESP32 simulation

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

Components

Sketch code

"""
ACOUSTIC FLAME SUPPRESSION SYSTEM
ESP32 DevKit V1 - Velxio MicroPython

Connections:
    OLED SDA       GPIO21
    OLED SCL       GPIO22
    MLX90640       Same I2C bus, address 0x33
    Push Button    GPIO27 -> GND
    Red LED        GPIO18
    Green LED      GPIO19
    DAC Speaker    GPIO25

Grove OLED:
    128 x 64
    I2C address 0x3C
    SSD1308Z / SSD1306-compatible

Operation:
    Press GPIO27 to capture 768 raw MLX90640 pixel words.
    Decode big-endian signed16; count values strictly greater than 100.
    Map count 0..409 to integer initial flame 0..100, clamped.
    Run simulated suppression -> pause -> verification, then repeat.

Caveats:
    Raw words are NOT calibrated Celsius temperatures.
    Keep simulator background and hot-spot temperatures fixed: threshold
    and the 409-pixel calibration depend on those settings.
    The simulator hot-spot graphic itself does not shrink. Only this
    program's flame model decays; verification checks that model, not
    physical extinguishment or a freshly measured thermal frame.
    DAC is software-timed at nominal 50 Hz, not precision audio timing.

Simulation only. Not a real fire safety system.
"""

from machine import Pin, I2C, PWM, DAC
from time import ticks_ms, ticks_us, ticks_diff, sleep_ms
from math import sin, pi


# ==================================================
# 1. CONFIGURATION
# ==================================================

FREQ_HZ = 50
BURST_MS = 3000
PAUSE_MS = 2000
MAX_RETRIES = 4

TICK_MS = 100
FRAME_MS = 300
Q_FRAMES = 3

HOT_ON = 5
HOT_STRONG = 20
BASE_HOT = 3
PM_ON = 12.0

DECAY = 0.82
COOL_DECAY = 0.75
FUEL_FACTOR = 0.10

BUTTON_DEBOUNCE_MS = 20

MLX_ADDR = 0x33
PIXEL_START = 0x0400
PIXEL_COUNT = 32 * 24
RAW_HOT_THRESHOLD = 100
HOT_COUNT_FULL_SCALE = 409

OLED_ADDR = 0x3C
OLED_REFRESH_MS = 400


# ==================================================
# 2. PIN CONFIGURATION
# ==================================================

SDA_PIN = 21
SCL_PIN = 22

BUTTON_PIN = 27

RED_PIN = 18
GREEN_PIN = 19
SPEAKER_PIN = 25


# ==================================================
# 3. BUILT-IN OLED FONT
# ==================================================

FONT = {
    " ": (0, 0, 0, 0, 0),
    "A": (126, 17, 17, 17, 126),
    "B": (127, 73, 73, 73, 54),
    "C": (62, 65, 65, 65, 34),
    "D": (127, 65, 65, 34, 28),
    "E": (127, 73, 73, 73, 65),
    "F": (127, 9, 9, 9, 1),
    "G": (62, 65, 73, 73, 122),
    "H": (127, 8, 8, 8, 127),
    "I": (65, 65, 127, 65, 65),
    "J": (32, 64, 65, 63, 1),
    "K": (127, 8, 20, 34, 65),
    "L": (127, 64, 64, 64, 64),
    "M": (127, 2, 12, 2, 127),
    "N": (127, 4, 8, 16, 127),
    "O": (62, 65, 65, 65, 62),
    "P": (127, 9, 9, 9, 6),
    "Q": (62, 65, 81, 33, 94),
    "R": (127, 9, 25, 41, 70),
    "S": (70, 73, 73, 73, 49),
    "T": (1, 1, 127, 1, 1),
    "U": (63, 64, 64, 64, 63),
    "V": (31, 32, 64, 32, 31),
    "W": (127, 32, 24, 32, 127),
    "X": (99, 20, 8, 20, 99),
    "Y": (3, 4, 120, 4, 3),
    "Z": (97, 81, 73, 69,