Arduino library 路 Communication

AltSoftSerial: a steadier second serial port on fixed pins

AltSoftSerial, by Paul Stoffregen, is the alternative people reach for when SoftwareSerial drops characters or upsets other timing in a sketch. Upstream it times bits with a 16-bit timer鈥檚 input capture and output compare hardware instead of a counted delay loop, so it can send and receive at the same time and does not hold interrupts off for a whole byte.

Built in on Mokxi: #include <AltSoftSerial.h> and it builds, with nothing to install. Free, and no account needed.

What AltSoftSerial does

The price is that the pins are not yours to choose. On an Uno, Nano or Pro Mini, RX is pin 8 and TX is pin 9; on a Mega they are 48 and 46; on a Leonardo or Pro Micro, 13 and 5. The constructor takes no pins, and any you pass are ignored. On the real board it also takes Timer1, so pin 10 loses PWM and the Servo library cannot share the timer.

You get one port per board. For a GPS or Bluetooth module at 9600 or 38400 baud beside other interrupt-driven code, it is usually the more dependable choice on an Uno.

Library Manager nameAltSoftSerial
AuthorPaul Stoffregen
Include#include <AltSoftSerial.h>
BoardsArduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesGPS module, NEO-6M, Bluetooth serial module, HC-05

Install it

It builds on Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V.

In Mokxi it is built in: #include <AltSoftSerial.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "AltSoftSerial" by Paul Stoffregen from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add AltSoftSerial to lib_deps in PlatformIO. Mokxi follows the calls of release 1.4, so a sketch written here builds there too.

What to know in the simulator: The bits are timed as SoftwareSerial鈥檚 are, not by the 16-bit timer鈥檚 input capture, so the pins and calls are upstream鈥檚 and Timer1 is not taken.

Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 Arduino Pro Mini 3.3 V

The calls you will use

In the order a sketch meets them.

AltSoftSerial altSerial;

The one port, on the board鈥檚 fixed pins: RX 8 and TX 9 on an Uno or Nano.

altSerial.begin(9600)

Starts it at a baud rate matching the module.

altSerial.available()

How many received bytes are waiting.

if (altSerial.available()) {
  char c = altSerial.read();
  Serial.print(c);
}

altSerial.read()

The next received byte, or -1 if none is waiting.

altSerial.println("hello")

Sends text and a line ending, as on any serial port.

altSerial.peek()

Looks at the next byte without taking it out of the buffer.

altSerial.end()

Stops the port and frees its pins.

Example: A GPS module read on the second serial port

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino 路 Arduino Uno R3
#include <AltSoftSerial.h>

// The Uno's AltSoftSerial pins are fixed: RX on 8, TX on 9.
AltSoftSerial altSerial;

void setup() {
  Serial.begin(9600);
  Serial.println("AltSoftSerial Test Begin");
  altSerial.begin(9600);
}

void loop() {
  if (altSerial.available()) {
    Serial.write(altSerial.read());
  }
}

Wiring

  • GPS module, NEO-6M TX to Arduino Uno R3 8
  • GPS module, NEO-6M RX to Arduino Uno R3 9
  • Arduino Uno R3 5V to GPS module, NEO-6M VCC
  • Arduino Uno R3 GND to GPS module, NEO-6M GND

Open it in the editor, wired and ready to run

Common errors and how to fix them

Nothing is received with the module on pins 2 and 3

AltSoftSerial ignores the pins you name. Move the module鈥檚 TX to pin 8 and its RX to pin 9 on an Uno or Nano, or to 48 and 46 on a Mega.

analogWrite() on pin 10 or a servo stopped working on a real Uno

Upstream AltSoftSerial uses Timer1, which is also the timer for PWM on pins 9 and 10 and for the Servo library. Move PWM to pins 3, 5, 6 or 11, or use SoftwareSerial if you need Timer1 for something else.

'AltSoftSerial' does not name a type on an ESP32

The library is written for AVR and Teensy timers. ESP32 boards have spare hardware UARTs: use HardwareSerial on Serial1 or Serial2 with the pins you choose instead.

Try AltSoftSerial without installing anything

Open the example, change the sketch, and keep your version in a free account.