Welcome to Solarduino , A blog about DIY Solar PV and Arduino projects

How to add or increase Serial Ports for NodeMCU using ESP Software Serial Library.

There are typically 3 communication protocols in Arduino or NodeMCU boards which are UART, SPI and I2C. If your Arduino project requires communication between modules or other micro controllers, you will regularly heard of these 3 terms. Before going further, let’s have a look on the 3 Common Communication Peripherals as below:

NodeMCU micro-controller

The NodeMCU micro-controller is similar to Arduino micro-controller board. It is compatible with Arduino IDE software and is built-in with the ESP8266 module which can be connected to the internet. You can grab some at our affiliate link here !!!  

Node MCU Micro controller Base

The based below the Node MCU can provide more flexibility on the connection ports to support more sensors. Besides, it also has 12V input port and able to provide 5V power supply to support the sensors. Support us by purchase it from our affiliate link here !!!

1) UART

UART stands for Universal Asynchronous Reception and Transmission, is a simple communication protocol that allows the Arduino to communicate with serial devices. It communicate through Rx and Tx pins and with other computer via USB port. It is also commonly applied on certain modules or equipment such as Energy Monitoring Device and even online monitoring platform such as Blynk App Web interface. Beginner or entry level board such as Arduino UNO, Nano and Mini have only 1 Serial Port. It requires 2 wires for communication.

2) I2C

I2C stands for inter-integrated-circuit, is a serial communications protocol specially designed for micro controllers communication. It is very popular among modules and sensors which could potentially connect up to 128 devices on the Arduino board. I2C makes it possible to connect multiple masters and slaves to your board using the exact same wires. The trade off for this simplified wiring is slower speeds than SPI. It requires 2 wires for communication. The most common modules that use I2C communication would be the Real Time Cloak (RTC) and the LCD Display module.

3) SPI

SPI stands for Serial Peripheral Interface. It is similar to I2C which  the communication protocol is specially designed for communication between micro controllers. Unlike I2C, SPI allows a single master device with slave devices up to 4 units only. However, SPI is much faster than I2C due to simple protocol. SPI is commonly found in modules where speed is important such as SD card module, or sometimes sensors that requires fast information changes like temperature sensors. It requires 4 wires for communication.

Different micro controller board has different pin location of communication protocols. Above is an overview for the 3 communication protocols on Arduino UNO. 

PZEM-017 DC Energy Module

PZEM-017 is a DC Energy Module that can support up to 300Vdc and the current measurement is subject to external shunt installed. It can be up to 50A, 100A, 200A and 300A. You can get it at a very competitive price at our affiliate link here !!!

How to increase Serial / UART Ports ?

If your project requires several Serial Communication ports, the direct solution will be selecting an Arduino board with more Serial Ports such as Arduino Mega. However, if you are beginner that want to focus on NodeMCU board which has built in wifi that can do a lot of IOT projects and only occasionally requires two Serial Ports, below is the common method that you can increase the UART port for Serial communication by using ESPSoftwareSerial library codes. It is done by program codes but they may exhibits some limitations and minor problems. 

ESPSoftwareSerial Library

ESP Software Serial is a library codes that allow users to convert 2 Digital Pins into a Software Serial Ports. As you know, Serial Communication is a series of binary (I/O) command which originates from Digital Pins that can create High and Low voltage signals. The library code adds the encoder and decoder on the two Digital Pins to allow them to use for transmitter and receival signal similar to Serial Port. This library is different to SoftwareSerial which is the common library for Arduino Boards, this library is mainly for ESP8266 boards including NodeMCU.

ESP Software Serial allows you to convert few Serial Ports using Digital Pins on your board. Since it uses internal timer for calculation, not all Digital Pins are supported and there may be some pins may not be working if applied for this library. There are people out there claiming that using software for extra Serial may not be reliable. However, based on my usage I don’t think it has any big issues but only with some minor coding problems. The issue I faced was the code crashed during initial startup unless unplug some wires initially, but I finally spotted the issue and the problem can be solved by adding additional line of codes.

The library can support baud rate up to 115200 bps which should be compatible with most standard equipment or device that designed for 9600. The advantage of having ESP Software Serial Library compared to regular Software Serial Library is that it can set Serial configuration such as set Serial’s data, parity, and stop bits. This is important as some of the equipment or devices may not be able to communicate with standard configuration: SERIAL_8N1 which represents 1 start bits, 8 data bits, No parity bits, and 1 stop bit. 

Example : 

2 different (XYZ & PZEM) meters are connected with NodeMCU with Serial Monitor to display measurement values. So in this case it will be total of 3 Serial Ports needed:

RS485 to UART Serial Port

RS485 Modbus RTU is the common communication for industrial devices and equipment. You will be dealing much with RS485 to Serial converter as it is the common way to communicate RS485 devices with Arduino and ESP8266 micro controller board. Generally there are 2 types of RS485 converter module 1) only 2 communication output pins named Tx and Rx and 2) 4 communication output pins named DI, DE, RE and RO). For all of my video and example codes, I will be commonly using 4 pins module (Type 2).

  

MAX485 UART TTL RS485 converter

This is a module that converts between RS485 communication and Serial (UART TTL) communication. This module enables Arduino to communicate, read or provide command to devices that using RS485 communication. Quickly grab this module at our affiliate link here !!!

Hardware Wiring

For a demo, we will be using PZEM 017 DC Energy Meter as an example. In this example, we will be utilizing 2 Serial Ports; 1 Serial Port for PZEM energy module reading (using RS485 modbus RTU) and another Serial Port for Serial Monitor (to display the measurement values in Serial Monitor). We will be using ESP Software Serial Library to create another port for PZEM energy module for communication with the board. The hardware wiring as below:

 

If you get a PZEM 017 DC energy meter, it may comes with a shunt of different sizes, you can program the shunt value into the energy module so that it could display the correct current value. Any direct connection from power source need to be protected with over-current device such as fuse. The fuse rating must be lower than the cable current carrying capacity to avoid cable burn and fire risk. It is highly recommend to use ferrule lugs at cable terminals.

Wiring of Serial to RS485 Converter Summary :

  1. A to A terminal of Energy Module
  2. B to B terminal of Energy Module
  3. Vcc to 5V positive
  4. Gnd to 5V ground 
  5. RO to Pin D2 (GPIO 4) of Node MCU
  6. DI to Pin D3 (GPIO 0) of Node MCU
  7. RE to pin D1 (GPIO 5) of Node MCU
  8. DE to pin D0 (GPIO 16) of Node MCU

Dupont line Wires

In order to connect wiring between Arduino board and module fast and easy, you need the dupont line cables. These cables are suitable during testing and troubleshoot where a lot of wire modifications. You can get it at our affiliate link here !!! 

Software Installation

There are few things you need to download and install including the Arduino IDE software, board manager and some libraries. Below are the checklist whether you have it all installed.

  1. Install Arduino IDE Programming Software (If you do not have the software, click here to download)
  2. Install Node MCU Board Manager for using Node MCU in Arduino IDE Programming Software. (see below section)
  3. Install Modified Modbus Library for using Modbus communication codes in Node MCU (see below section)
  4. Install ESP Software Serial Library to add Serial Ports. (See below section)
  5. Upload the code to Node MCU Board. 

Install Node MCU Board Manager 

If this is the first time you are dealing with NodeMCU, you need to add NodeMCU board manager file into the IDE software so that the software can support the Node MCU board. To add the Support File, open Arduino IDE Software, go to files>preferences>Additional Boards Manager URLs, paste this link http://arduino.esp8266.com/stable/package_esp8266com_index.json to the black space and click OK. Go to Tool > Board > make sure the board is showing NodeMCU 1.0 (ESP-12E module).

Install Modified Modbus Master Library

If this is the first time you are dealing with RS485 modbus communication, this Modified Modbus Master Library file is needed to be installed to the Arduino IDE Software so that it can understand the RS485 programming function. If you have installed the original  ModbusMaster.h library from library management, kindly look into the folder and delete it or it will clash with this library

The Modified Modbus Master Library I get it from EvertDekker.com and I have uploaded the zip file here so that you can download and install it. Go to Sketch > Include Library > Add.ZIP Library…. and locate the zip file. Make sure you download it to your PC first (preferably download at desktop)

Install ESP Software Serial Library

If this is the first time you use the Software Serial to add Serial Port, you may need to install and download the ESPSoftwareSerial Library in the manage library section. This Library is different with the standard Software Serial, so be sure to install the correct library. Go to Tools > Manage Libraries … and type “ESPSoftwareSerial”. Download from Peter Lerup.

Before we end, we would like to give gratitude to you for taking the time to read the post. We would need readers like you to support us in order to keep growing. You can support us in the following ways :

Donate & Fund Raising

If you like my work, please send me a donation to encourage me to do more. Thanks

Aliexpress Affiliate

We are the member of Aliexpress affiliate marketing. Do support us by clicking the affiliate product links if you do wish to purchase them.

Like and Share

If you like our post, we need your support to like and share our posts or videos so that it can reach more and more people like you !!

ESP Software Serial with PZEM meter.ino