#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <esp_arduino_version.h>
constexpr int POT = 34, LED = 25;
constexpr uint8_t ADDRESS = 0x3C; // Cambiar a 0x3D si corresponde
constexpr uint32_t SAMPLE_MS = 100;
constexpr int N = 128;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
uint16_t history[N];
int count = 0;
uint32_t lastSample = 0;
void stopWithError(const char *message) {
Serial.println(message);
while (true) { delay(1000); }
}
void setup() {
Serial.begin(115200);
pinMode(POT, INPUT);
analogReadResolution(12);
analogSetPinAttenuation(POT, ADC_11db);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
Wire.begin(21, 22);
Wire.beginTransmission(ADDRESS);
if (Wire.endTransmission() != 0) stopWithError("OLED no responde: revisar I2C");
if (!oled.begin(SSD1306_SWITCHCAPVCC, ADDRESS, true, false))
stopWithError("No hay memoria para OLED");
#if ESP_ARDUINO_VERSION_MAJOR >= 3
if (!ledcAttach(LED, 5000, 8)) stopWithError("Error PWM");
ledcWrite(LED, 0);
#else
ledcSetup(0, 5000, 8);
ledcAttachPin(LED, 0);
ledcWrite(0, 0);
#endif
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.display();
lastSample = millis();
}
void loop() {
uint32_t now = millis();
if (now - lastSample < SAMPLE_MS) return;
lastSample = now;
int adc = analogRead(POT);
int pwm = map(adc, 0, 4095, 0, 255);
#if ESP_ARDUINO_VERSION_MAJOR >= 3
ledcWrite(LED, pwm);
#else
ledcWrite(0, pwm);
#endif
if (count < N) history[count++] = adc;
else {
for (int i = 1; i < N; i++) history[i - 1] = history[i];
history[N - 1] = adc;
}
oled.clearDisplay();
oled.setCursor(0, 0);
oled.print("ADC:"); oled.print(adc);
oled.print(" PWM:"); oled.print(pwm);
oled.drawFastHLine(0, 14, 128, SSD1306_WHITE);
// Area de trazado: y=16 (4095) a y=63 (0).
if (count == 1) oled.drawPixel(0, map(history[0], 0, 4095, 63, 16), SSD1306_WHITE);
for (int i = 1; i < count; i++) {
int y0 = map(history[i - 1], 0, 4095, 63, 16);
int y1 = map(history[i], 0, 4095, 63, 16);
oled.drawLine(i - 1, y0, i, y1, SSD1306_WHITE);
}
oled.display();
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306