Serial Communication with Arduino

This article contains everything you need to know to implement serial communication on your next arduino project. Just as usual, I have included example sketches (code) and wiring diagram.

In this lesson I will go into opening and closing serial communications, setting up your baud rate, and sending and receiving basic texts and numbers

Serial communication

Serial communication is used to communicate with electronic devices in this case, an arduino uno. Serial communication can be used for getting valuable data from the arduino such as sensor data or it can be used to send commands to the arduino. It also helps debug faster since you have more data.

Wiring

This is very self explanatory since all we have to do is to plug in the arduino to the computer with the 2.0 cable.

Programming

In this piece of code, I have begun the serial communication with begin() function and passed in the desired baud rate as my parameter. Then, it checks for incoming data with available() function, if incoming data available then, I used the readString() to read the complete string instead of I character that you get with the read() function. Later on, it checks if the incoming data said quit and if it did then It stops the serial communication and if it didn't then it prints the incoming data using the println() function which prints on a new line instead of on the same line which you can achieve using the print() function instead of println().


NOTE: Configure the serial monitor with no line ending. This way it can detect the quit message properly.


String serial_input = "qqqqq";


void setup() {

Serial.begin(9600); //Setting the baud rate to 9600 and starting serial communication

}


void loop()

{

if(Serial.available()>0) // checks if serial data is available

{

serial_input = Serial.readString(); // if incoming data then read it

if(serial_input == "quit")

{

Serial.println("Ending communication link");

Serial.end(); // if incoming data says quit then end serial communication

}

else

{

Serial.println(serial_input); //if incoming data doesn't say quit then print on a new line data

}

}

}

  • Arduino does not power up?

You may have a faulty arduino

  • Arduino doesn't quit when entered "quit"?

Make sure your serial monitor is configure with no line ending.


My Other Tutorials: