Communication between devices

Send commands and readings over USB serial, then choose between UART, I²C, SPI, and networking.

NRF24 wireless module

Ingredients

Follow a part’s name for an introduction and compatibility notes.

A sensor project becomes more useful when it can explain what it is doing. USB serial is an inexpensive first interface: the computer can display readings and send commands without a screen, Wi-Fi setup, or another board.

A two-way serial exercise

Connect the Uno by USB, upload this sketch, and open Serial Monitor at 9600 baud. Send 1 to light the built-in LED and 0 to turn it off. Newline characters are ignored. Each accepted command produces an acknowledgement; a status line arrives once per second.

Download the complete Arduino sketch

unsigned long sentAt = 0;
unsigned long count = 0;
bool ledOn = false;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Send 1 for on, 0 for off");
}

void loop() {
  while (Serial.available() > 0) {
    const char command = Serial.read();
    if (command == '1' || command == '0') {
      ledOn = command == '1';
      digitalWrite(LED_BUILTIN, ledOn ? HIGH : LOW);
      Serial.println(ledOn ? "ACK on" : "ACK off");
    }
  }
  const unsigned long now = millis();
  if (now - sentAt >= 1000) {
    sentAt = now;
    Serial.print("count="); Serial.print(count++);
    Serial.print(",led="); Serial.println(ledOn ? 1 : 0);
  }
}

print(65) sends the text “65”; write(65) sends one byte whose ASCII representation is “A”. Choose a consistent message format before mixing text and binary data. The sketch reads incoming data frequently, instead of sleeping for a full second between reports.

Choose a connection by the job

Common interfaces
ConnectionDestination or purpose
UART / serialA transmit line crosses to the other device’s receive line; grounds join. Match logic levels, baud rate, and framing. The Uno’s D0/D1 are shared with its USB serial interface.
I²CSDA and SCL form a shared addressed bus. Uno R3 uses A4/SDA and A5/SCL. Check pull-up voltage and duplicate addresses.
SPISeparate clock and data lines plus chip-select signals. Uno R3 uses D11/MOSI, D12/MISO, and D13/SCK. Every slave needs the correct selection behavior.
Wi-Fi + MQTTA networked board publishes messages through a broker. Use a native ESP32 development board when it avoids buying a second controller and an extra interface.

I²C and SPI are normally short board-level connections, not cables to stretch around a house. Long runs require an interface designed for that environment. USB serial and logic-level UART are also not the same electrical interface as RS-232.

A reliable message has context

For a real sensor, send its name, value, units, and whether the reading is valid. A receiver should know when the last reading arrived. A missing sensor must not silently become “0 °C” or “door closed”. For commands, define accepted values and report whether the action succeeded.

Troubleshooting

  • Unintelligible text: match the baud rate.
  • Uploads fail: remove devices sharing D0/D1 and close another application that has the port open.
  • Device-to-device serial stays silent: check TX-to-RX wiring, ground, and voltage compatibility. Never connect two actively driven TX outputs together.
  • I²C device missing: check the actual address, pull-up supply, and whether another board already adds pull-ups.

Next, read a sensor locally before adding networking. Then learn MQTT authentication and permissions. The Arduino Serial reference documents the interface used here.