How to use Software Serial and Hardware Serial with Arduino Mega

Did you connect the bluetooth module to the correct pins?

You are checking Serial1.available() to see if there is at least one character in the buffer, but then reading nine characters. Even at 115200 baud the arduino is much faster than the serial communications and the buffer is unlikely to have all nine characters in it.

I had not noticed the baud rate in your original code - software serial is basically unusable at 115200 baud, not sure how you ever had it working.

Hi @sterretje , yes I connected bluetooth module to tx0 and rx0 pins, and I disconnected during uploading the program.

Hi david_2018,

regarding characters in code, I have used the code from TF mini product page as it is.
Default baud rate is 115200. This is for TF mini LIDAR sensor.

https://cdn.sparkfun.com/assets/8/a/f/a/c/16977-TFMini-S_-_Micro_LiDAR_Module-Product_Manual.pdf

example code is here

Thanks

You will have to connect the Bluetooth module to tx1 and rx1 when you use that in your code, not tx and rx.

Hi @sterretje ,
I used LIDAR on tx1 and rx1 and Bluetooth on tx and rx.

Why ?

1 Like

@Garyp36, the implied question is why Bluetooth is on Serial and not on Serial2 or Serial3 :smiley:

@lastchancename, sorry for spoiling the fun a little.

1 Like

Hi @lastchancename and @sterretje , thanks for the point, actually there is no specific reason.
But to mention here is, I have disconnected the bluetooth from tx and rx using switch during this trial.
Actually I am not able to understand about the thing is, the circuit is same for software serial and hardware serial, only little modification in code for hardware serial as mentioned above, but still with Hardware serial I am not able to get the results in serial monitor. Maybe I am missing something in code.

Thanks

In simple terms, you should only have one device on any single serial port. (when you know more, we’ll explain)

Hardware serial and Software serial achieve the same result, but ‘software’ generated (bit bashed) serial has a LOT more limitations.

When we have multiple serial ports on any processor, one is often reserved as a ‘craft port’… coincidentally that’s also used by Arduino as the programming or ‘console’ port for debug and other user interaction.
Ideally you won’t use that console port for device communications unless you know what you’re doing.

The separate hardware serial ports are SEPARATE. They don’t interact with each other at all, unless you specifically write code that moves data between them.

Any software serial ports are also ‘separate’, but may interfere with your program execution or vice-versa - if you don’t accommodate the vagaries of software generated serial data.

If you have unused hardware ports available, they’re ALWAYS preferable to creating unnecessary software serial ports.

1 Like

Dear @lastchancename
you are right about interference due to software serial in main program.
Main code which includes servo movement is getting interfered as soon as I start software serial at different baud rate.
Software Serial works perfectly independently.
I want to use Hardware serial now but not able to do.

Actually, I am using Arduino Mega for one light tracking application

  • I want to use LIDAR on any serial port (either software or hardware serial)
  • on TX0 and RX0, I have connected HC05 for receiving the data on Smartphone

My query is

  • how to exactly use Hardware serial, lets say Serial1 or Serial2 or Serial3?
  • why simple Serial1.print command also dont work?
  • can I have two different baud rates? Serial.begin(9600) for Main program, Serial.begin(115200) for LIDAR (as per spec sheet)

I am confused because, I can work with LIDAR in local code using Software Serial. But not able to work using Hardware serial.

int dist;     //actual distance measurements of LiDAR
int strength; //signal strength of LiDAR
float temprature;
int check;   //save check value
int i;
int uart[9]; //save data measured by LiDAR
const int HEADER=0x59;   //frame header of data package

void setup() 
{    
Serial.begin(9600);       //set bit rate of serial port connecting Arduino with computer
Serial1.begin(115200);    //set bit rate of serial port connecting LiDAR with Arduino
} 

void loop() 
{     

if(Serial1.available()) {   //check if serial port has data input        

    if (Serial1.read() == HEADER) {   //assess data package frame header 0x59            
        uart[0]=HEADER;            
    if (Serial1.read() == HEADER) {   //assess data package frame header 0x59                
        uart[1] = HEADER;                
    for (i = 2; i < 9; i++) {         //save data in array                    
        uart[i] = Serial1.read();                
        }                
    check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];                
    if (uart[8] == (check & 0xff)){                   //verify the received data as per protocol                    
        dist = (uart[2] + uart[3] * 256)*10;          //calculate distance value                    
        strength = uart[4] + uart[5] * 256;           //calculate signal strength value                    
        temprature = uart[6] + uart[7] *256;          //calculate chip temprature                    
        temprature = temprature/8 - 256;     
        Serial.print("distance (mm)= ");                    
        Serial.print(dist); //output measure distance value of LiDAR                    
        Serial.print('\t');                    
        Serial.print("strength = ");                    
        Serial.print(strength); //output signal strength value
        Serial.print("\t Chip Temprature = ");                    
        Serial.print(temprature);                    
        Serial.println(" celcius degree"); //output chip temperature of Lidar  
                      
        }            
      }        
    }    
  }
}

Dear @david_2018
Yes, I removed that line and checked again.
Actually that line was from some of the trials I was doing to check how the code can work.

Actually this code works perfectly with Software Serial, but with Hardware Serial, there is no output on Serial Monitor.

Try changing the if statement condition to ( Serial1.available() >= 8) to endure all the data has arrived before you try to read it. Still going to have problems synchronizing the data reads if you ever miss a character, but don't have time to comment on that this morning.

Dear @david_2018
thanks for suggestion.
I just tried with following line but still not able to see any output on Serial Monitor

if(Serial1.available()>=8) {   //check if serial port has data input  

Oops, should have said >= 9 instead off 8, was reading code too fast on a phone.

1 Like

Dear @david_2018
Thank you so much.
You have found the exact problem in the code and given the exact solution.
Below mentioned code is working perfectly as Hardware Serial for LIDAR.
And I am able to see the exact output in Serial Monitor.

Thank you

int dist;     //actual distance measurements of LiDAR
int strength; //signal strength of LiDAR
float temprature;
int check;   //save check value
int i;
int uart[9]; //save data measured by LiDAR
const int HEADER=0x59;   //frame header of data package

void setup() 
{    
Serial.begin(9600);       //set bit rate of serial port connecting Arduino with computer
Serial1.begin(115200);    //set bit rate of serial port connecting LiDAR with Arduino
} 

void loop() 
{     

if(Serial1.available()>=9) {   //check if serial port has data input        
    if (Serial1.read() == HEADER) {   //assess data package frame header 0x59            
        uart[0]=HEADER;            
    if (Serial1.read() == HEADER) {   //assess data package frame header 0x59                
        uart[1] = HEADER;                
    for (i = 2; i < 9; i++) {         //save data in array                    
        uart[i] = Serial1.read();                
        }                
    check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];                
    if (uart[8] == (check & 0xff)){                   //verify the received data as per protocol                    
        dist = (uart[2] + uart[3] * 256)*10;          //calculate distance value                    
        strength = uart[4] + uart[5] * 256;           //calculate signal strength value                    
        temprature = uart[6] + uart[7] *256;          //calculate chip temprature                    
        temprature = temprature/8 - 256;     
        Serial.print("distance (mm)= ");                    
        Serial.print(dist); //output measure distance value of LiDAR                    
        Serial.print('\t');                    
        Serial.print("strength = ");                    
        Serial.print(strength); //output signal strength value
        Serial.print("\t Chip Temprature = ");                    
        Serial.print(temprature);                    
        Serial.println(" celcius degree"); //output chip temperature of Lidar  
                      
        }            
      }        
    }    
  }
}

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