Have problems with Multi serial in Arduino Mega and LCD 16x2

Hey guys, im here again, i have a problem with multiserial and LCD16x2, maybe is that im not used to Arduino mega and it multiserial ports or functions. When i use the normal serial (serial.begin), my lcd show me the value of the MG811 sensor, but when i use the Serial2.begin and the condition of that serial, my LCD don't show me anything. Don't know what is the problem, here is my code:

#include "CO2Sensor.h"
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

CO2Sensor co2Sensor(A0, 0.99, 100);

String val_CO2;

void setup() {
  
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.setCursor(0, 1);
  lcd.print("CO2:");
  //lcd.setCursor(7, 1);
  //lcd.print("CO:"); 

  Serial2.begin(9600);
  co2Sensor.calibrate();
}

void loop() {
  if (Serial2.available()){
  int val = co2Sensor.read();
  lcd.setCursor(4, 1);
  lcd.print(val);
  delay(1000);
  }
  //CO2 sensor

}

If you change the Serial2.begin to serial.begin, and erase the condition of Serial2.available, it works fine. Hope somebody can help me

What do you have connected to the Rx pin of Serial2?

nothing, i thought it would not be a problem using analog pins like i show because im using libraries of the MQ7 and MG811 sensors

How do you expect a UART port to be of any use without a device connected to it?

Then why have you made the reading of the sensor on A0 dependent upon something available to read at Serial2

The Mega has 4 UARTs (serial ports). The UART (Serial) is connected to

  1. Pins 0 and 1
  2. A serial-to-usb converter for communication with the PC

The other UARTs are connected to other pins and are not connected to a / the serial-to-usb converter. So there is no way that Serial1, Serial2 and Serial3 can communicate with a PC unless you add external serial-to-usb adapters and connect those to a / the PC.

Ty, i wasn't sure about that fact, then i need to change some parts of my code to use the serial 2, I hope i can send the data of Rx 2 to the LCD. At first i use serial 0 to communicate my FPGA Artix 7, now i will change to UART 2

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.