SerialPassthrough

Demonstrates how to virtually connect Serial and Serial1.

This example demonstrates how to virtually connect together Serial and Serial1. This tutorial can be so loaded on boards that have two different UART interfaces on the 0 and 1 pins and the USB port (like Zero, MKR1000, 101). In particular every data coming from the RX pin of the Serial1 is transmitted to Serial and vice versa.

Hardware Required

  • Arduino Board

Circuit

circuit

None, but the board has to be connected to the computer; the Arduino Software (IDE) serial monitor may be used to communicate the single or multiple characters and receive the string back.

Code

1/*
2
3 SerialPassthrough sketch
4
5 Some boards, like the Arduino 101, the MKR1000, Zero, or the Micro, have one
6
7 hardware serial port attached to Digital pins 0-1, and a separate USB serial
8
9 port attached to the IDE Serial Monitor. This means that the "serial
10
11 passthrough" which is possible with the Arduino UNO (commonly used to interact
12
13 with devices/shields that require configuration via serial AT commands) will
14
15 not work by default.
16
17 This sketch allows you to emulate the serial passthrough behaviour. Any text
18
19 you type in the IDE Serial monitor will be written out to the serial port on
20
21 Digital pins 0 and 1, and vice-versa.
22
23 On the 101, MKR1000, Zero, and Micro, "Serial" refers to the USB Serial port
24
25 attached to the Serial Monitor, and "Serial1" refers to the hardware serial
26
27 port attached to pins 0 and 1. This sketch will emulate Serial passthrough
28
29 using those two Serial ports on the boards mentioned above, but you can change
30
31 these names to connect any two serial ports on a board that has multiple ports.
32
33 created 23 May 2016
34
35 by Erik Nyquist
36
37*/
38
39void setup() {
40
41 Serial.begin(9600);
42
43 Serial1.begin(9600);
44}
45
46void loop() {
47
48 if (Serial.available()) { // If anything comes in Serial (USB),
49
50 Serial1.write(Serial.read()); // read it and send it out Serial1 (pins 0 & 1)
51
52 }
53
54 if (Serial1.available()) { // If anything comes in Serial1 (pins 0 & 1)
55
56 Serial.write(Serial1.read()); // read it and send it out Serial (USB)
57
58 }
59}

Learn more

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last revision 2016/05/26 by AG

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.