Learn

Simulate two Arduinos talking over serial

Intermediate

Or see every lesson.

  • 261parts on the bench
  • 31boards running now
  • 1.00xreal time, on every board
Two Unos playing ping-ponglive0.000 s 0.00x
Click to open it in the editor
Uno A sends "ping 1", "ping 2" and so on; Uno B answers each with "pong". Press Run and read both boards in the serial monitor.

Two boards that talk to each other are where a lot of real projects start: a sensor node and a display, a controller and a motor driver, a main board and a helper that does one job well. On a bench that means two USB cables, two serial monitors and a lot of swapping between them. Here it is one project in a browser tab, with both boards on the canvas and both sketches open in the editor.

The circuit above is two Arduino Unos joined by three wires. Pin 3 of each board goes to pin 2 of the other, so each one transmits on 3 and listens on 2, and the third wire joins their grounds. That third wire is the one people forget. Two boards with no common ground have no common idea of what a volt is, and the receiving pin reads noise.

Each board runs its own sketch, compiled in your browser. Uno A sends a line, "ping 1", then waits for an answer. Uno B reads characters until the end of the line, prints what it heard, and sends back "pong 1". Uno A prints the reply, waits a moment, and sends "ping 2". The serial monitor prefixes every line with the board it came from, so you can watch the conversation from both ends at once.

The link is SoftwareSerial, the library most two-board tutorials use, on ordinary pins at 9600 baud. Mokxi runs the library’s bit timing at the chip’s real cycle counts, so a baud rate that does not match on both sides fails here the way it fails on a real pair of boards: garbage characters, or nothing at all. In Mokxi the hardware serial port of each board goes to the serial monitor rather than to pins 0 and 1, which is why the link uses two other pins.

SoftwareSerial is half duplex: while a board is sending, it cannot listen. That is why the sketches take turns, with A asking and B answering, and why a protocol between two boards usually has one side in charge.

Things to try: unplug the ground wire and run it again, change the baud rate on one board only, or make B answer with a number it computes. Then add a third board. A project can hold as many boards as you like, each with its own sketch.

Questions

Can I simulate more than one Arduino at a time?

Yes. A Mokxi project can hold several boards, each with its own sketch and its own tab in the editor, and they share the circuit, so a wire between two of their pins is a real connection the simulation carries.

Can two simulated Arduinos talk over I2C?

Not yet. Wire works as a master talking to sensors and displays, but a board acting as an I2C slave is not simulated, and a sketch that calls Wire.begin with an address is told so. Use SoftwareSerial, as this project does, or a hand-clocked link like the SPI pair.

Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.