Arduino Serial Ports

UART0 is the default serial port which is used for serial programming.

Some arduino’s have more than one serial port and these are called Serial1, Serial2, etc

Setup Serial Port

  //----- SETUP SERIAL PORT -----
  Serial.begin(9600);
  while(!Serial)
    ;           //Leonardo/Micro should wait for serial init

  //or

  Serial2.begin(9600);

  //or

  Serial.begin(9600, SERIAL_8N2);    //SERIAL_8E1, SERIAL_8O1, etc

Sending ASCII Text

  Serial.print("Hello");        //Write string no new line
  Serial.println(my_variable);  //Write a value with line break at end

Sending Variables As ASCII

    Serial.print(MyVariable, DEC);     //int value
    Serial.print(MyVariable, HEX);    //hex value
    Serial.println(1.23456, 2)       //Limit decimal places (gives "1.23")

Others see here.

Write Binary Data

  Serial.write(0x01);

Receive Binary Data

  int DataRx;
  if (Serial.available() > 0)    //Gets the number of bytes available for reading from the serial port (buffer holds up to 64 bytes)
  {
    DataRx = Serial.read();      //Read next byte (returns -1 if no data available)
  }
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *