Arduino MEGA 2560 Oscilloscope With 2.8" TFT Lcd Screen

by jamesmainmon in Circuits > Arduino

68 Views, 1 Favorites, 0 Comments

Arduino MEGA 2560 Oscilloscope With 2.8" TFT Lcd Screen

Screenshot 2025-11-17 at 2.13.01 PM.png

This simple portable arduino oscilloscope uses a 2.8" TFT LCD to project an analog wave from analog pin 15.

Supplies

Photo on 11-14-25 at 8.52 AM.jpg
Photo on 11-14-25 at 8.50 AM.jpg

Attach the Shield

You need to connect the pins of the touchscreen shield to The mega carefully, Making sure that you don't bend the pins. Connect the same pins as you would on an UNO R3. then move on to step 2.

Upload the Code

Screenshot 2025-11-17 at 2.28.24 PM.png

you will need the following libraries:

Elegoo_GFX

Elegoo_TFTLCD


1) Open Arduino IDE and connect the Arduino to your computer via USB.

2) Download the libraries via "download ZIP library" in sketch toolbar.

3) copy and the code below after removing the starting code.

4) Select your Arduino in the select board" portion.

5) Upload the code to the Arduino.

6) connect the alligator clip wires to A14 and A15.

#include <Elegoo_GFX.h>
#include <Elegoo_TFTLCD.h>

// --- TFT Shield pin mapping ---
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

// --- Display constants ---
const int screenWidth = 320;
const int screenHeight = 240;

const uint16_t COLOR_BG = 0x0000; // Black
const uint16_t COLOR_GRID = 0x7BEF; // Light gray
const uint16_t COLOR_CENTER = 0xF800; // Red
const uint16_t COLOR_CH1 = 0x001F; // Blue
const uint16_t COLOR_CH2 = 0x07E0; // Green

// --- Input pins ---
const int analogPin1 = A5; // Channel 1 (Blue)

// --- Variables ---
int prevY1 = screenHeight / 2;
int prevY2 = screenHeight / 2;
int currentX = 0;

bool logging = false; // logging control flag

// --- Moving average filter ---
const int FILTER_SIZE = 8;
int buffer1[FILTER_SIZE];
int buffer2[FILTER_SIZE];
int filterIndex = 0;
long sum1 = 0;
long sum2 = 0;

// --- Draw grid and center line ---
void drawGrid() {
tft.fillScreen(COLOR_BG);
for (int y = 0; y <= screenHeight; y += 40)
tft.drawFastHLine(0, y, screenWidth, COLOR_GRID);
for (int x = 0; x <= screenWidth; x += 40)
tft.drawFastVLine(x, 0, screenHeight, COLOR_GRID);
tft.drawFastHLine(0, screenHeight / 2, screenWidth, COLOR_CENTER);

// Label 2.5V line
tft.setTextColor(COLOR_CENTER);
tft.setTextSize(1);
tft.setCursor(5, screenHeight / 2 - 10);
tft.print("2.5V");
}

// --- Setup ---
void setup() {
Serial.begin(9600);
tft.reset();
uint16_t identifier = 0x9341; // ILI9341 controller
tft.begin(identifier);
tft.setRotation(1);
drawGrid();

analogReference(DEFAULT);

// Initialize filters
for (int i = 0; i < FILTER_SIZE; i++) {
int v1 = analogRead(analogPin1);
buffer1[i] = v1;
sum1 += v1;
}

Serial.println("Type 'start' to begin logging and 'end' to stop.");
}

// --- Smooth analog read ---
int smoothRead(int pin, int *buffer, long &sum) {
int val = analogRead(pin);
sum -= buffer[filterIndex];
buffer[filterIndex] = val;
sum += val;
return sum / FILTER_SIZE;
}

// --- Process Serial Commands ---
void checkSerialCommands() {
if (Serial.available()) {
String cmd = Serial.readStringUntil('\n');
cmd.trim();
cmd.toLowerCase();

if (cmd == "start") {
logging = true;
Serial.println("▶️ Logging started.");
Serial.println("Time(s),Channel1(V),Channel2(V)");
}
else if (cmd == "end") {
logging = false;
Serial.println("⏹️ Logging stopped.");
}
}
}

// --- Main loop ---
void loop() {
checkSerialCommands();

int val1 = smoothRead(analogPin1, buffer1, sum1);
filterIndex = (filterIndex + 1) % FILTER_SIZE;

// Map to screen coordinates
int y1 = map(val1, 0, 1023, screenHeight - 1, 0);

// Erase current column
tft.drawFastVLine(currentX, 0, screenHeight, COLOR_BG);

// Redraw grid and labels
if (currentX % 40 == 0)
tft.drawFastVLine(currentX, 0, screenHeight, COLOR_GRID);
for (int gy = 0; gy <= screenHeight; gy += 40)
tft.drawPixel(currentX, gy, COLOR_GRID);
tft.drawPixel(currentX, screenHeight / 2, COLOR_CENTER);
tft.setTextColor(COLOR_CENTER, COLOR_BG);
tft.setTextSize(1);
tft.setCursor(5, screenHeight / 2 - 10);
tft.print("2.5V");

// Draw waveforms
tft.drawLine(currentX, prevY1, currentX, y1, COLOR_CH1);
tft.drawLine(currentX, prevY1 + 1, currentX, y1 + 1, COLOR_CH1);
prevY1 = y1;

// --- Serial output (only when logging) ---
if (logging) {
float volts1 = val1 * (5.0 / 1023.0);
float timeSeconds = millis() / 1000.0;

Serial.print(timeSeconds, 3);
Serial.print(",");
Serial.print(volts1, 3);
Serial.print(",");
}

// Advance and wrap
currentX++;
if (currentX >= screenWidth) {
currentX = 0;
drawGrid();
prevY1 = prevY2 = screenHeight / 2;
}

delayMicroseconds(700);
}