Forum

Using two serial po...
 
Notifications
Clear all

Using two serial ports

3 Posts
3 Users
0 Likes
604 Views
Posts: 2
Moderator
Topic starter
(@shaunhiemstra)
Member
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!)

Thanks,
Chas Glewen, student (Laconia)

2 Replies
Posts: 23
Admin
(@compase)
Member
Joined: 16 years ago

Chas,

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: https://www.arduino.cc/en/Reference/softwareSerial

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: https://forum.arduino.cc/t/arduino-every-second-hardware-serial-port/600373/3

Hope that helps!

Eric

Reply
Posts: 3
(@nerdlord)
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
Serial2.listen();

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

// Then, listen back on your bluetooth port
Serial1.listen();

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

 

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

Henry, Golda Meir School 

Reply