deckle studioOpen in the editor

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.

NetPins
GNDA1 GNDD1 KD2 KD3 KD4 KD5 KD6 KD7 KD8 KD9 KD10 KD11 KD12 KRV1 2
5VA1 5VRV1 1
L1A1 ~D2R1 1
L1_AD1 AR1 2
L2A1 ~D3R2 1
L2_AD2 AR2 2
L3A1 ~D4R3 1
L3_AD3 AR3 2
L4A1 ~D5R4 1
L4_AD4 AR4 2
L5A1 ~D6R5 1
L5_AD5 AR5 2
L6A1 ~D7R6 1
L6_AD6 AR6 2
L7A1 ~D8R7 1
L7_AD7 AR7 2
L8A1 ~D9R8 1
L8_AD8 AR8 2
L9A1 ~D10R9 1
L9_AD9 AR9 2
L10A1 ~D11R10 1
L10_AD10 AR10 2
L11A1 ~D12R11 1
L11_AD11 AR11 2
L12A1 ~D13R12 1
L12_AD12 AR12 2
LINKA1 D18/TX1A1 D17/RX2
POTA1 A0RV1 W
D1
LED
D2
LED
D3
LED
D4
LED
D5
LED
D6
LED
D7
LED
D8
LED
D9
LED
D10
LED
D11
LED
D12
LED

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

More examples: Arduino