embedded project — ESP32 simulation

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

Components

Sketch code

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

// =====================================================
// ESP32 ADC INPUT PINS
// =====================================================

#define PH_PIN          34
#define TDS_PIN         35
#define TURBIDITY_PIN   32

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

#define OLED_RESET -1
#define OLED_ADDRESS 0x3C

Adafruit_SSD1306 display(
  SCREEN_WIDTH,
  SCREEN_HEIGHT,
  &Wire,
  OLED_RESET
);

// ESP32 default I2C pins
#define OLED_SDA 21
#define OLED_SCL 22

// =====================================================
// ADC SETTINGS
// =====================================================

#define ADC_MIN 0
#define ADC_MAX 4095

// =====================================================
// YOUR CALIBRATION POINTS
// =====================================================
//
// TDS:
// 315 ppm -> ADC 819
// 510 ppm -> ADC 1455
//
// Turbidity:
// 530 NTU  -> ADC 3857
// 2730 NTU -> ADC 1185
//
// pH:
// 5.5 -> ADC 3437
// 9.1 -> ADC 2633
//
// These are used for linear interpolation.
// =====================================================


// -----------------------------------------------------
// Generic linear interpolation
// -----------------------------------------------------

float interpolate(
  float adc,
  float adc1,
  float value1,
  float adc2,
  float value2
) {
  return value1 +
         (adc - adc1) *
         (value2 - value1) /
         (adc2 - adc1);
}


// =====================================================
// pH CONVERSION
// =====================================================

float readPH(int raw) {

  float ph;

  // Calibration:
  // ADC 3437 = pH 5.5
  // ADC 2633 = pH 9.1

  ph = interpolate(
    raw,
    3437,
    5.5,
    2633,
    9.1
  );

  // Limit to realistic range
  if (ph < 0.0)
    ph = 0.0;

  if (ph > 14.0)
    ph = 14.0;

  return ph;
}


// =====================================================
// TDS CONVERSION
// =====================================================

float readTDS(int raw) {

  float tds;

  // Calibration:
  // ADC 819  = 315 ppm
  // ADC 1455 = 510 ppm

  tds = interpolate(
    raw,
    819,
    315.0,
    1455,
    510.0
  );

  if (tds < 0)
    tds = 0;

  return tds;
}


// =====================================================
// TURBIDITY CONVERSION
// =====================================================

float readTurbidity(int raw) {

  float turbidity;

  // Calibration:
  // ADC 3857 = 530 NTU
  // ADC 1185 = 2730 NTU
  //
  // Notice that turbidity decreases as ADC increases,
  // therefore the two calibration points are intentionally
  // in this order.

  turbidity = interpolate(
    raw,
    3857,
    530.0,
    1185,
    2730.0
  );

  if (turbidity < 0)
    turbidity = 0;

  return turbidity;
}


// =====================================================
// OLED DISPLAY

More projects by manyanaik1713