AntiNoiseSerial is a lightweight Arduino library for cleaning text received
through a Stream such as Serial, Serial1, Serial2, or a compatible
software serial implementation.
The library removes non-printable bytes, keeps a bounded FIFO-style text
buffer, and provides simple pattern matching with MySQL LIKE-style
wildcards.
- Accepts printable ASCII characters (
32–126), carriage return, and newline. - Discards other bytes commonly seen as corrupted or unwanted serial data.
- Optional spacing prevents valid words on either side of a noise burst from being joined together.
- Bounded buffer with configurable maximum size.
- Works with any Arduino class derived from
Stream. - Pattern matching with
%and_wildcards. - No external dependencies beyond the Arduino core.
The library is intended for boards that provide the Arduino Stream and
String APIs, including Arduino boards and Arduino-core projects for ESP32 and
STM32. It can be used above UART-to-RS-232 or UART-to-RS-485 hardware, but it
does not configure the electrical interface itself.
- Download this repository as a ZIP file.
- Open Arduino IDE.
- Select Sketch → Include Library → Add .ZIP Library.
- Select the downloaded ZIP file.
- Add
#include <AntiNoiseSerial.h>to the sketch.
Copy the inner AntiNoiseSerial directory into the Arduino libraries folder,
then restart Arduino IDE.
Add the Git repository to platformio.ini:
lib_deps =
https://github.com/DprasK/AntiNoiseSerial.git#include <AntiNoiseSerial.h>
// Stream, maximum buffer size, insert a space after a noise burst.
AntiNoiseSerial input(Serial, 128, true);
void setup() {
Serial.begin(9600);
}
void loop() {
input.update();
String message = input.getBuffer();
if (message.indexOf('\n') == -1) {
return;
}
if (input.like("%SET_TEMP:%")) {
Serial.println("Temperature command received");
} else if (input.like("%ID__%")) {
Serial.println("Two-character ID received");
}
input.clear();
}A complete example is available in
AntiNoiseSerial/examples/sample/sample.ino.
AntiNoiseSerial(Stream& serialPort,
size_t maxBufSize = 128,
bool useSpacing = false);| Parameter | Description |
|---|---|
serialPort |
Any Arduino-compatible Stream instance. |
maxBufSize |
Maximum number of characters retained in the buffer. |
useSpacing |
Inserts one space between valid text segments separated by discarded bytes. |
| Method | Description |
|---|---|
void update() |
Reads all currently available bytes and updates the clean buffer. Call it repeatedly from loop(). |
String getBuffer() |
Returns a copy of the current clean buffer. |
void clear() |
Clears the internal buffer before receiving the next message. |
bool like(String pattern) |
Compares the entire buffer using % and _ wildcards. |
like() follows two common SQL wildcard rules:
| Wildcard | Meaning | Example |
|---|---|---|
% |
Zero or more characters | %ON% matches text containing ON. |
_ |
Exactly one character | ID__ matches ID01 or IDAB. |
Matching is case-sensitive. Add % at the start and end of a pattern when the
value may occur anywhere inside a longer buffer.
input.like("READY"); // Exact buffer match
input.like("%READY%"); // Contains READY
input.like("SENSOR_?"); // '?' is literal; it is not a wildcard
input.like("SENSOR__"); // One arbitrary character after SENSOR_With spacing disabled, discarded bytes are simply removed:
Input bytes: HELLO [noise] WORLD
Buffer: HELLOWORLD
With spacing enabled, a single separator is inserted:
Input bytes: HELLO [noise] WORLD
Buffer: HELLO WORLD
Consecutive noise bytes do not create repeated spaces.
When the buffer reaches maxBufSize, each new valid character removes the
oldest character before being appended. Choose a size large enough for the
longest expected message, including line endings.
- The library filters text by byte value; it cannot determine whether a printable character was corrupted into another printable character.
- It is not a replacement for CRC/checksum validation, acknowledgements, retries, shielding, grounding, termination, or proper serial-level hardware.
- Do not use it for binary protocols because valid non-printable bytes will be discarded.
- Validate commands separately before controlling machinery or other safety-critical equipment.
AntiNoiseSerial/
├── library.properties
├── examples/sample/sample.ino
└── src/
├── AntiNoiseSerial.cpp
└── AntiNoiseSerial.h
Issues and pull requests are welcome. When reporting a serial problem, include the board, Arduino core version, baud rate, serial interface, and a sample of the received bytes.