slave-master node — ESP32-S3 simulation

An interactive ESP32-S3 circuit simulation you can run free in your browser on Velxio, by 2023beec071.

Components

Sketch code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

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

#define MQ2_PIN     4

#define SERVO_PIN   18

#define RED_PIN     8
#define GREEN_PIN   9
#define BLUE_PIN    10

#define BUZZER_PIN  11
#define FAN_PIN     12

#define SDA_PIN     13
#define SCL_PIN     14


// =====================================================
// OLED
// =====================================================

#define SCREEN_WIDTH  128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(
  SCREEN_WIDTH,
  SCREEN_HEIGHT,
  &Wire,
  -1
);


// =====================================================
// SERVO PWM
// =====================================================

#define SERVO_FREQ       50
#define SERVO_RESOLUTION 14


// =====================================================
// GAS THRESHOLDS
// =====================================================

#define SAFE_LEVEL     1200
#define WARNING_LEVEL  2200


// =====================================================
// RGB LED CONTROL
// WOKWI RGB LED = COMMON ANODE
//
// LOW  = ON
// HIGH = OFF
// =====================================================

void setRGB(bool red, bool green, bool blue)
{
  digitalWrite(
    RED_PIN,
    red ? LOW : HIGH
  );

  digitalWrite(
    GREEN_PIN,
    green ? LOW : HIGH
  );

  digitalWrite(
    BLUE_PIN,
    blue ? LOW : HIGH
  );
}


// =====================================================
// SERVO CONTROL
// =====================================================

void setServoAngle(int angle)
{
  angle = constrain(angle, 0, 180);

  // SG90:
  // 0°   = 500 us
  // 180° = 2500 us

  int pulseWidth = map(
    angle,
    0,
    180,
    500,
    2500
  );

  // 50 Hz = 20 ms = 20000 us

  uint32_t duty =
    ((uint32_t)pulseWidth * 16383UL) / 20000UL;

  ledcWrite(
    SERVO_PIN,
    duty
  );
}


// =====================================================
// OLED STATUS
// =====================================================

void showStatus(
  int gasValue,
  const char *status,
  const char *valve,
  const char *fan
)
{
  display.clearDisplay();

  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);

  display.setCursor(0, 0);
  display.println("SMART RESPIRATOR");

  display.setCursor(0, 15);
  display.print("Gas: ");
  display.println(gasValue);

  display.setCursor(0, 28);
  display.print("Status: ");
  display.println(status);

  display.setCursor(0, 41);
  display.print("Valve: ");
  display.println(valve);

  display.setCursor(0, 54);
  display.print("Fan: ");
  display.println(fan);

  display.display();
}


// =====================================================
// SETUP
// =====================================================

void setup()
{
  Serial.begin(115200);


  // ---------------------------------------------------
  // GPIO
  // ---------------------------------------------------

  pinMode(MQ2_PIN, INPUT);

More projects by 2023beec071