Zum Inhalt springen

How to connect multiple sensors to one Arduino Uno serial port?

Arduino Uno only has one hardware UART (pins 0/1 shared with the USB), but you can still hook up several UART-type sensors if you pick the right tactic. Here are the practical options, when to use each, plus wiring and code you can drop in.

TL;DR decision guide

Option 1 — Add “software UARTs” (quickest)

Uno has Serial (hardware) on D0/D1. You can add one or two extra low-speed UARTs in software:

  • AltSoftSerial (recommended): reliable timing; fixed pins on Uno (RX=8, TX=9).
  • NeoSWSerial: more efficient than SoftwareSerial; supports 9600/19200/38400.
  • SoftwareSerial: flexible pins; okay up to ~9600 baud.

Limitation: only one software UART can listen at a time. Good for polled sensors, not for always-streaming devices (like GPS).

Example: two UART sensors polled alternately (NeoSWSerial + SoftwareSerial)

#include <NeoSWSerial.h>
#include <SoftwareSerial.h>

// AltSoftSerial (if used) would be on 8/9; here we demonstrate Neo + Soft

NeoSWSerial     sen1(10, 11); // RX, TX  (must use change-capable pin for RX)
SoftwareSerial  sen2(4, 5);   // RX, TX

void setup() {
  Serial.begin(115200);   // USB to PC (debug)
  sen1.begin(9600);
  sen2.begin(9600);
}

void pollAndDump(Stream& s, const char* name, const char* query = nullptr) {
  if (query) s.print(query);        // optional command to request a reading
  delay(20);                        // sensor response time
  unsigned long t0 = millis();
  Serial.print(name); Serial.print(": ");
  while (millis() - t0 < 50) {      // read for 50 ms
    if (s.available()) Serial.write(s.read());
  }
  Serial.println();
}

void loop() {
  // Only one “software RX” should be active at a time:
  ((NeoSWSerial&)sen1).listen();
  pollAndDump(sen1, "SEN1", "READr");

  ((SoftwareSerial&)sen2).listen();
  pollAndDump(sen2, "SEN2", "READr");
}

Tips

  • Keep baud low (9600) and polling deterministic.
  • Avoid heavy interrupt-using libraries at the same time (e.g., Servo).
  • If one sensor streams continuously → this method will miss data. Use Option 2/3/5.

Option 2 — Use a UART multiplexer/switch (hardware gating)

You can hard-switch the Uno’s single RX/TX pair to one sensor at a time:

  • 2:1 analog switch for RX (e.g., 74HC4053, TS3A24159, SGM3157).
  • Two tri-state buffers for TX fan-out (e.g., 74HC125/126) so only the selected sensor sees TX.
  • Select lines from two Arduino GPIOs choose which sensor is “connected”.
                 +-----+           +--------+
UNO_RX <---+-----| MUX |--- TX1 ---| Sensor1 |
           |     | (2:1)           +--------+
           |                      +--------+
           +---------- TX2 -------| Sensor2 |
                   |
UNO_TX --> [Tri-state buffers] --> RX1 / RX2 (enable only the selected one)
SEL pins -> choose path

Pros: Works at higher baud; sensors can stream (you switch to the one you want).
Cons: Extra chips/wiring; still only talks to one at a time.

Option 3 — RS-485 multidrop bus (robust, many sensors)

Convert UART to half-duplex RS-485 (A/B differential). All nodes share the same two wires; only one talker at a time. Give each sensor an address in your protocol.

Hardware (per node):

  • MAX485/THVD4850 transceiver (RO, DI, A, B, RE̅/DE).
  • 120 Ω termination at both ends of the bus, bias resistors (or integrated).

Uno wiring (half-duplex):

UNO_TX -> DI
UNO_RX <- RO
DE/RE̅  -> a digital pin to toggle TX/RX
A/B twisted pair across all nodes

Sketch snippet (half-duplex control):

#define DE_RE 2  // driver enable (HIGH=TX, LOW=RX)

void rs485Write(const uint8_t* p, size_t n) {
  digitalWrite(DE_RE, HIGH);
  Serial.write(p, n);
  Serial.flush();           // wait for shift register to empty
  digitalWrite(DE_RE, LOW); // back to receive
}

void setup() {
  pinMode(DE_RE, OUTPUT);
  digitalWrite(DE_RE, LOW);
  Serial.begin(19200);      // UART to RS-485 transceiver
}

void loop() {
  // Example: poll node 0x12
  uint8_t cmd[] = {0x12, 0x01, 0x00, 0x13}; // addr, READ, len, CRC(simple)
  rs485Write(cmd, sizeof(cmd));

  // read reply (with timeout)
  unsigned long t0 = millis();
  while (millis() - t0 < 50) {
    if (Serial.available()) {
      int b = Serial.read();
      // process...
    }
  }
}

Pros: Long cables, many sensors, noise-robust.
Cons: You must define an addressed protocol and ensure nodes only speak when asked.

Option 4 — I²C/SPI-to-UART bridge (clean expansion)

Add real UARTs over a shared bus. Examples:

  • SC16IS752 (NXP): dual UART via I²C or SPI.
  • MAX14830: quad UART via SPI.

You talk I²C/SPI from the Uno; the bridge presents multiple independent UARTs to connect your sensors.

Pros: True simultaneous UARTs; scalable; fewer timing headaches.
Cons: Extra IC; library setup.

Option 5 — Use a board with more UARTs

An Arduino Mega 2560 gives you Serial, Serial1, Serial2, Serial3. If you truly need multiple streaming sensors at higher baud, this is the simplest hardware fix.

If your “multiple sensors” aren’t strictly UART…

  • I²C (A4/A5): many sensors share the same two wires; each has an address. If two clash, add a TCA9548A I²C multiplexer.
  • SPI: share SCK/MOSI/MISO; give each sensor its own CS pin.

Practical gotchas

  • Don’t tie multiple TTL TX pins together. Idle HIGH means bus fights; use MUX/tri-state or RS-485.
  • Pins 0/1 are shared with USB. If you use them for sensors you can’t use the Serial Monitor at the same time (unless you disconnect during programming).
  • Voltage levels: Uno is 5 V. Many sensors are 3.3 V. Use level shifters.
  • Baud rate & CPU load: Software serial is best ≤9600; for higher rates use AltSoftSerial (pins 8/9) or hardware solutions.
  • Grounds: Common GND between Uno and all sensors.
  • Protocol: Poll sensors or give them addresses; avoid two devices talking at once.

Quick recommendation

  • If your sensors can be polled at 9600: use AltSoftSerial for one, NeoSWSerial/SoftwareSerial for the other(s).
  • If they stream or need >9600: use a UART switch or RS-485 bus; or move to Mega / I²C-to-UART bridge.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert