Using two serial po...
Clear all

Using two serial ports

3 Posts
3 Users
Posts: 2
Topic starter
Joined: 2 years ago

We have our moisture sensor and Lidar code working independently but we aren't able to get them to both send data to the app.  We have a serial port open for the bluetooth, as well as another serial port open using #include <SoftwareSerial.h>. So, I have the original serial port for communication on pins 0 and 1 for communicating with the computer and then a Software.Serial1 for communicating with the bluetooth, as well as a Software.Serial2 for communication with the Lidar.  With this setup, none of the serial ports work, but when I comment out the Serial2, the Serial1 port and RX and TX work correctly.

Is there a trick to getting both serial ports working? (

(Collaboration points if any schools can help us!)

Chas Glewen, student (Laconia)

2 Replies
Posts: 23
Joined: 16 years ago


Are you using the Arduino Uno or the Nano Every? Serial ports work a bit different on each, so that will change your code slightly. On the Uno, you'll need to use the SoftwareSerial for both non-USB port serials (bluetooth and lidar). See here for more:

On the Nano Every, Serial and Serial1 are both "hardware" ports, and you don't want to use SoftwareSerial to create them. You would, though, need to do so for Serial2. See:

Hope that helps!


Posts: 3
New Member
Joined: 2 years ago

Hi Chas,

To utilize two SoftwareSerial ports, you have to call the listen() function on the port you currently want to use. You can only listen to one SoftwareSerial and one HardwareSerial port at once, so you have to swap between your ports on the fly. The code below, which has been thoroughly commented, assumes that you have included SoftwareSerial.h and you have tweaked your code to match the comments below.

// This code should run triggered by bluetooth.

// Assuming your Bluetooth port is Serial1, and your sensor port is Serial2, this code should work to get your values from your sensor on Serial2, assuming you replace the placeholder code in the middle with your own function or procedure for getting the values.

//////// Begin Main Code ////////

// Listen to sensor

// Get your data (nonsense code, replace with your working code)

// Then, listen back on your bluetooth port

//////// End Main Code /////////


Hope this helps (If it doesn't, PLEASE tell me),

Henry, Golda Meir School