#include <Wire.h>
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Pin Mapping
#define SIGNAL_PIN 0 // ADC input (GPIO0 → your oscilloscope input)
#define SDA_PIN 3 // I2C SDA
#define SCL_PIN 4 // I2C SCL
#define PWM_PIN 5 // PWM output (GPIO3)
// ADC Parameters
#define NUM_SAMPLES 128
int samples[NUM_SAMPLES];
void setup() {
pinMode(PWM_PIN, OUTPUT);
// Initialize I2C for OLED
Wire.begin(SDA_PIN, SCL_PIN);
// Initialize OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;); // Halt if OLED not found
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0,0);
display.println("ESP32-C3 Oscilloscope");
display.display();
delay(1000);
// Setup ADC
analogReadResolution(10); // 10-bit ADC (0–1023)
}
void loop() {
// Generate a PWM wave by varying duty cycle
for (int duty = 0; duty <= 255; duty++) {
analogWrite(PWM_PIN, duty); // duty cycle: 0-255
delay(10);
}
for (int duty = 255; duty >= 0; duty--) {
analogWrite(PWM_PIN, duty);
delay(10);
}
// Capture samples from ADC
for (int i = 0; i < NUM_SAMPLES; i++) {
samples[i] = analogRead(SIGNAL_PIN);
delayMicroseconds(200); // Sampling rate ≈ 5 kHz
}
// Draw waveform on OLED
display.clearDisplay();
for (int i = 1; i < NUM_SAMPLES; i++) {
int x1 = i - 1;
int y1 = map(samples[i - 1], 0, 1023, SCREEN_HEIGHT - 1, 0);
int x2 = i;
int y2 = map(samples[i], 0, 1023, SCREEN_HEIGHT - 1, 0);
display.drawLine(x1, y1, x2, y2, SSD1306_WHITE);
}
display.display();
}