#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// =================================
// PIN CONFIGURATION
// =================================
#define VR1_PIN 35
#define VR2_PIN 34
#define LED_PIN 5
#define OLED_SDA 21
#define OLED_SCL 22
// =================================
// ADC CONFIGURATION
// =================================
#define ADC_MAX 4095
// =================================
// PWM CONFIGURATION
// =================================
#define PWM_FREQ 5000
#define PWM_RES 8
#define PWM_MAX 255
// =================================
// OLED CONFIGURATION
// =================================
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(
SCREEN_WIDTH,
SCREEN_HEIGHT,
&Wire,
-1
);
bool oledOK = false;
// =================================
// VARIABLES
// =================================
int vr1Value = 0;
int vr2Value = 0;
int ledBrightness = 0;
int pwmValue = 0;
unsigned long lastUpdate = 0;
const unsigned long updateInterval = 200;
// =================================
// ADC READING
// =================================
int readADC(int pin) {
long sum = 0;
// Lay trung binh 10 lan doc
for (int i = 0; i < 10; i++) {
sum += analogRead(pin);
}
return sum / 10;
}
// =================================
// OLED DISPLAY
// =================================
void updateOLED() {
if (!oledOK) return;
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("ESP32 ANALOG CONTROL");
display.drawLine(0, 12, 127, 12, SSD1306_WHITE);
// VR1
display.setCursor(0, 20);
display.print("VR1 ADC: ");
display.println(vr1Value);
// VR2
display.setCursor(0, 32);
display.print("VR2: ");
display.print(ledBrightness);
display.println("%");
// PWM
display.setCursor(0, 44);
display.print("PWM: ");
display.print(pwmValue);
display.print("/255");
display.display();
}
// =================================
// UART OUTPUT
// =================================
void sendUART() {
Serial.print("VR1 ADC = ");
Serial.print(vr1Value);
Serial.print(" | VR2 = ");
Serial.print(ledBrightness);
Serial.print("%");
Serial.print(" | PWM = ");
Serial.println(pwmValue);
}
// =================================
// SETUP
// =================================
void setup() {
Serial.begin(115200);
// ADC
analogReadResolution(12);
// PWM
pinMode(LED_PIN, OUTPUT);
analogWriteFrequency(LED_PIN, PWM_FREQ);
analogWriteResolution(LED_PIN, PWM_RES);
analogWrite(LED_PIN, 0);
// I2C OLED
Wire.begin(OLED_SDA, OLED_SCL);
if (display.begin(
SSD1306_SWITCHCAPVCC, 0x3C)) {
oledOK = true;
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(2);
display.setCursor(10, 20);
display.println("ESP32 OK");
display.display();
} else {
Serial.println("OLED ERROR!");
}
Serial.println("==============================");
Serial.println("ESP32 VR1 VR2 PWM CONTROLLER");
Serial.println("UART: 115200 BAUD");
Serial.println("ADC: 12 BIT");
Serial.println("PWM: 8 BIT - 5 KHz");
Serial.println("==============================");
delay(1000);
}
// =================================
// MAIN LOOP
// =================================
void loop() {
if (millis() - lastUpdate >= updateInterval) {
lastUpdate = millis();
// Doc bien tro VR1
vr1Value = readADC(VR1_PIN);
// Doc bien tro VR2
vr2Value = readADC(VR2_PIN);
// Chuyen ADC thanh phan tram
ledBrightness = map(
vr2Value,
0,
ADC_MAX,
0,
100
);
// Tinh gia tri PWM 8 bit
pwmValue = map(
vr2Value,
0,
ADC_MAX,
0,
PWM_MAX
);
// LED active HIGH
analogWrite(LED_PIN, pwmValue);
// Hien thi OLED
updateOLED();
// Gui du lieu UART
sendUART();
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306