Built-in project · Arduino Uno R3

RS-232 loopback on an Arduino Uno

The Uno’s TX goes out through a MAX232 at RS-232 levels, round a loopback jumper and back in on pin 8, the way a COM port is tested. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate Runs in your browser. Free, and no account needed.

The parts retail for about $31. On Mokxi they're free.

RS-232 loopback · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A MAX232 and a loopback plug: the Uno's serial at RS-232 levels and back.
//
// A PC's COM port, a lab instrument or a CNC controller speaks RS-232: about
// -7 V for a 1 and +7 V for a 0. A board's 0 V and 5 V are the wrong levels,
// the wrong way round. The MAX232 makes its own plus and minus supplies from
// 5 V with a charge pump and its capacitors, and turns TTL into RS-232 and
// back.
//
//   T1IN  <- TX (pin 1)     T1OUT -> R1IN, the loopback jumper
//   R1OUT -> pin 8          C1 and C2, the pump capacitors, 1 uF each
//   VCC -> 5V, GND -> GND
//
// The jumper from T1OUT to R1IN is what a technician plugs into a port to
// test a cable: whatever goes out comes straight back. Every second the
// sketch sends "ping" and a number on Serial, and SoftwareSerial on pin 8
// hears it come back through both halves of the chip.
//
// THINGS TO TRY. Pull the jumper: nothing comes back, and R1OUT sits high,
// because an open RS-232 input reads as a mark. Or take C2 out: there is no
// negative rail, and the chip says no C1/C2.

#include <SoftwareSerial.h>

SoftwareSerial back(8, 9);      // RX on pin 8 from R1OUT; 9 unused

unsigned long count = 0;
unsigned long last = 0;
char line[32];
uint8_t len = 0;

void setup() {
  Serial.begin(9600);
  back.begin(9600);
  Serial.println("RS-232 loopback: TX out through the MAX232, back on pin 8");
}

void loop() {
  if (millis() - last >= 1000) {
    last = millis();
    count++;
    Serial.print("ping ");
    Serial.println(count);
  }
  while (back.available()) {
    char c = back.read();
    if (c == '\r') continue;
    if (c == '\n') {
      line[len] = 0;
      if (strncmp(line, "ping ", 5) == 0) {
        Serial.print("came back: ");
        Serial.println(line);
      }
      len = 0;
    } else if (len < sizeof(line) - 1) {
      line[len++] = c;
    }
  }
}

Parts list

5 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.

How it is wired

9 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; RS-232 transceiver, MAX232 pin GND
  • Arduino Uno R3 pin 8; RS-232 transceiver, MAX232 pin R1OUT
  • Arduino Uno R3 pin 1; RS-232 transceiver, MAX232 pin T1IN
  • Arduino Uno R3 pin 5V; RS-232 transceiver, MAX232 pin VCC
  • RS-232 transceiver, MAX232 pin C1+; Capacitor, 1 µF (2) pin 1
  • RS-232 transceiver, MAX232 pin C1-; Capacitor, 1 µF (2) pin 2
  • RS-232 transceiver, MAX232 pin C2+; Capacitor, 1 µF (1) pin 1
  • RS-232 transceiver, MAX232 pin C2-; Capacitor, 1 µF (1) pin 2
  • RS-232 transceiver, MAX232 pin R1IN; RS-232 transceiver, MAX232 pin T1OUT

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.