Arduino Mega 2560: four serial ports and a 12-LED wave
Serial1 talks to Serial2 over a wire; 12 PWM LEDs.
- Arduino Mega 2560
Open in the simulatorNo account needed: press Simulate and it runs.
How it works
The Mega 2560 has four hardware serial ports and 15 PWM pins. Twelve LEDs on D2–D13 dim up and down one after the other; potentiometer RV1 on A0 sets the speed. Serial1 sends "ping n" on TX1 (D18); a wire takes it to RX2 (D17), Serial2 answers and the USB serial port reports what arrived. Serial3 sends the potentiometer value. The serial monitor shows all four ports (lines of Serial1–3 start with their name) and can send to each of them. Put the logic analyzer on LINK to see the 9600 baud frames.
Wiring
Each line is one connection (net) and the pins it joins.
| Net | Pins |
|---|---|
| GND | A1 GNDD1 KD2 KD3 KD4 KD5 KD6 KD7 KD8 KD9 KD10 KD11 KD12 KRV1 2 |
| 5V | A1 5VRV1 1 |
| L1 | A1 ~D2R1 1 |
| L1_A | D1 AR1 2 |
| L2 | A1 ~D3R2 1 |
| L2_A | D2 AR2 2 |
| L3 | A1 ~D4R3 1 |
| L3_A | D3 AR3 2 |
| L4 | A1 ~D5R4 1 |
| L4_A | D4 AR4 2 |
| L5 | A1 ~D6R5 1 |
| L5_A | D5 AR5 2 |
| L6 | A1 ~D7R6 1 |
| L6_A | D6 AR6 2 |
| L7 | A1 ~D8R7 1 |
| L7_A | D7 AR7 2 |
| L8 | A1 ~D9R8 1 |
| L8_A | D8 AR8 2 |
| L9 | A1 ~D10R9 1 |
| L9_A | D9 AR9 2 |
| L10 | A1 ~D11R10 1 |
| L10_A | D10 AR10 2 |
| L11 | A1 ~D12R11 1 |
| L11_A | D11 AR11 2 |
| L12 | A1 ~D13R12 1 |
| L12_A | D12 AR12 2 |
| LINK | A1 D18/TX1A1 D17/RX2 |
| POT | A1 A0RV1 W |
Parts
Firmware
sketch.ino · 48 lines
// Arduino Mega 2560: four serial ports and twelve PWM channels
// - D2..D13: twelve LEDs, each one dims up and down a little later than the one before (a wave)
// - the potentiometer on A0 sets the speed of the wave
// - Serial1 (TX1 = D18) sends "ping n" every half second; a wire takes it to Serial2 (RX2 = D17)
// - Serial2 answers on its own TX (D16), Serial3 (TX3 = D14) reports the potentiometer
// - Serial (USB) shows what Serial2 received
// The serial monitor shows every port; lines of Serial1..3 start with their name.
const byte LEDS[12] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
unsigned long lastPing = 0, lastPot = 0;
int n = 0;
String line;
void setup() {
Serial.begin(115200);
Serial1.begin(9600);
Serial2.begin(9600);
Serial3.begin(115200);
Serial.println("Mega 2560: 4 serial ports, 12 PWM channels");
}
void loop() {
unsigned long now = millis();
int speed = map(analogRead(A0), 0, 1023, 1, 8);
for (int i = 0; i < 12; i++) {
int p = (now / 4 * speed + i * 43) % 512; // triangle 0..255..0, shifted per LED
analogWrite(LEDS[i], p < 256 ? p : 511 - p);
}
if (now - lastPing >= 500) {
lastPing = now;
Serial1.print("ping ");
Serial1.println(n++);
}
while (Serial2.available()) {
char c = Serial2.read();
if (c == '\n') {
Serial2.print("got: ");
Serial2.println(line);
Serial.print("Serial2 received: ");
Serial.println(line);
line = "";
} else if (c != '\r') line += c;
}
if (now - lastPot >= 1000) {
lastPot = now;
Serial3.print("A0 = ");
Serial3.println(analogRead(A0));
}
}Open in the simulator