mySerial.begin() and Serial.begin()

in this code

#include<SoftwareSerial.h>
SoftwareSerial mySerial(10,9); // are we creating an object to SoftwareSerial class?

void setup(){
mySerial.begin(115200);
Serial.begin(9600);
//more statements
}

Does this code mean we are creating two objects? How are these objects being initialized and used?How come we used Serial.begin() straight away?
Could you please help me understand the concept?

SoftwareSerial mySerial(10,9); does indeed create an object.

TheSerial object is already created for you in the HardwareSerial library.

so there is both SoftwareSerial and HardwareSerial class in the SoftwareSerial library?

li16:
so there is both SoftwareSerial and HardwareSerial class in the SoftwareSerial library?

No. They are separate. You do not need to explicitly #include a library to use hardware Serial as it is part of the Arduino environment.

But, HardwareSerial and SoftwareSerial both inherit from the Stream class. So, their interfaces are very similar.

1 Like

gfvalvo:
But, HardwareSerial and SoftwareSerial both inherit from the Stream class. So, their interfaces are very similar.

Similar interfaces but different libraries

so where does Serial.print(variable) appear and where does the mySerial.print(variable) appear

you might get it by reading this

then in HardwareSerial.h noticing this class HardwareSerial : public Stream

and in SoftwareSerial.h noticing thisclass SoftwareSerial : public Stream

and in Stream.h noticing thatclass Stream : public Print

(and you'll see the write() methods only in the classes)

#include<SoftwareSerial.h>
SoftwareSerial mySerial(10,9); // are we creating an object to SoftwareSerial class?

void setup(){
mySerial.begin(115200);
Serial.begin(9600);
//more statements
}

SoftwareSerial will not work at 115200 baud rate and 38400 can be problematic. I would use 9600 as the baud rate for SS.

li16:
so where does Serial.print(variable) appear and where does the mySerial.print(variable) appear

Serial.print() sends text to whatever's connected to the Arduino's hardware serial interface. Normally this means the serial monitor on your computer (via the USB interface) but there are other ways to set this up (eg you could connect something to pins 0 and 1).

mySerial.print() sends text to the the software serial object, so the data is sent out of whatever pins you defined when you set up the software serial object.

GypsumFantastic:
Serial.print() sends text to whatever's connected to the Arduino's hardware serial interface. Normally this means the serial monitor on your computer (via the USB interface) but there are other ways to set this up (eg you could connect something to pins 0 and 1).

mySerial.print() sends text to the the software serial object, so the data is sent out of whatever pins you defined when you set up the software serial object.

If I'm not mistaken, I think OP's ask was where is the code for the print() method - and how does this work. if that's the case answer #7 should help

J-M-L:
If I'm not mistaken, I think OP's ask was...

Ah, my mistake. Thanks for picking that up.