# 14.1 Serial

## Hardware Wiring

Cited directly from <https://www.arduino.cc/en/Tutorial/SoftwareSerialExample>:

> "There is no circuit for this example. Make sure that your Arduino or Genuino board is attached to your computer via USB to enable serial communication through the serial monitor window of the Arduino Software (IDE).".

Due to the comments inside the sketch code,

> "The circuit:
>
> * RX is digital pin 10 (connect to TX of other device)
> * TX is digital pin 11 (connect to RX of other device)"

So, to test the serial communication, it's better we found some device/module, which comes with an **RX** and **TX** pins. Therefore, we carry out the test with a Wifi ESP8266 module, which will be detailedly discussed again in [**14.2 Wifi - ESP8266**](/arduino-full-stack/14_communication/02_wifi.md).

We then connect the pins as:

| ESP8266 | Arduino UNO R3             |
| ------- | -------------------------- |
| GND     | GND                        |
| VCC     | 3.3V                       |
| CH\_PD  | 3.3V                       |
| TX      | Virtual RX, namely, pin 10 |
| RX      | Virtual TX, namely, pin 11 |

![Serial](/files/-LCFEU-6UTIrLtQ0IAuH)

## Sketch

The code can be found at [Examples\_Arduino - arduinocc - \_001\_Serial - \_001\_Serial.ino](https://github.com/LongerVisionRobot/Examples_Arduino/blob/master/arduinocc/_001_Serial/_001_Serial.ino), which is directly copied from <https://www.arduino.cc/en/Tutorial/SoftwareSerialExample>.

```
/*
  Software serial multple serial test

 Receives from the hardware serial, sends to software serial.
 Receives from software serial, sends to hardware serial.

 The circuit:
 * RX is digital pin 10 (connect to TX of other device)
 * TX is digital pin 11 (connect to RX of other device)

 Note:
 Not all pins on the Mega and Mega 2560 support change interrupts,
 so only the following can be used for RX:
 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

 Not all pins on the Leonardo and Micro support change interrupts,
 so only the following can be used for RX:
 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

 created back in the mists of time
 modified 25 May 2012
 by Tom Igoe
 based on Mikal Hart's example

 This example code is in the public domain.

 */
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
  mySerial.println("Hello, world?");
}

void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}
```

## Outcome

![Serial Port Output from Arduino IDE](/files/-LCFEUDBJCDxoWVZCZxq)

## However?

So far, we can clearly see **Goodnight moon!** has been successfully output to the **Monitor Serial** dialog. However, in our sketch, it's quite clear that **Hello, world?** should also be output somewhere. But, where? Clearly, **mySerial** defines the virtual serial port using pin 10 and pin 11. So, there must be a 2nd serial port on the [**Wifi Module ESP8266**](/arduino-full-stack/14_communication/02_wifi.md).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://longer-vision-robot.gitbook.io/arduino-full-stack/14_communication/01_serial.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
