Arduino Logo

Arduino Beginner’s Course Lesson 3– Using the Serial Library





You can download our e-book ‘Learn Arduino’ from this link

The Serial library is one of the most widely used library in Arduino development. The serial communication provides a simple communication interface to the PC or any other device that accepts connections over serial protocol. Throughout this course and some of the projects we post, Serial will be used to output either debug data or factual data. Debug data is when we need to output particular data to know where the execution of the program is at a particular time, or to output variable values at different intervals. It can also used as the main data output of your device, case in point the Lightning Detector, where data is being gathered by the PC using the serial communication.

The aim of this lesson is to give you the opportunity to learn how to set up the Serial library, how to use it in different situations and explain the common function in serial communication. For an in depth knowledge of how the library works, please visit Arduino.cc serial page.

Hardware Serial

Every Arduino board has at least 1 hardware serial (UART). It communicates over RX digital pin 0 and TX digital pin 1. These pins are also connected to the USB port on the device.

Software Serial

Is another library almost identical to Serial but allows for all digital pins to act as TX and RX. This means that the device can communicate to a PC over pin 0 and 1, an to another device over pin 2 and 3.

In the previous lesson, we already saw how to print some text to the Serial port and read it with the serial monitor provided with the Arduino IDE. Today we are going to write and read data over Serial, interpret it and act accordingly.

Writing to Serial

Something we saw last time was Serial.begin. This initializes the serial listener for any communications. This line needs to go in the setup() function, not mandatory but highly recommended. So our first piece of code today

void setup(){

Serial.begin(9600);

}

The parameter ‘9600’ is the speed at which the data will be sent, currently 9600 kbps. Now we can greet the user by adding some println functions in the setup.

void setup() {

Serial.begin(9600);

Serial.println("Hello, Runtime Projects here");

Serial.println("This is lesson 3");

}

The println function will send the text over to the other end of the serial communication, and appends the new line character ‘\n’. This means that the cursor will move to a new line after printing the text on the serial monitor.

Other uses of println are Serial.println(x) where x can be a byte, integer, float or char, or even char array. Play with this function for a while and see how you might use it to stream your data.




Reading from Serial

Serial communication is needed when you need to instruct your Arduino to perform a task on demand. For example you would like to switch an LED on/off at will, or drive a motor to the left by 10 degrees. The possibilities are endless. In order to do this, we need to make the Arduino listen for messages we send over the serial line. Imagine for simplicity’s sack, we want the LED on when we send ‘H’ and off when we send ‘L’ over the serial. Let’s do this.

char incomingMessage;

int ledPin = 13;

void setup() {

  Serial.begin(9600);

  pinMode(ledPin,OUTPUT);

  Serial.println("Hello, Runtime Projects here");

  Serial.println("This is lesson 3");

}

void loop() {

  if (Serial.available() > 0) {

    incomingMessage = Serial.read();

    if (incomingMessage == 'H') {

      digitalWrite(ledPin,HIGH);

    } else if (incomingMessage == 'L') {

      digitalWrite(ledPin,LOW);

    }

  }
}

Now upload the sketch and go to the serial monitor. You should receive the 2 lines we set in the setup. Then send ‘H’ in the serial monitor and check your LED, send ‘L’ and check it again 🙂 Note, the ‘H’ and the ‘L’ are case sensitive.

Stay tuned for the next lesson, we will deal with pins and how to control them 😀

Go to Lesson 4 here

Tags:

Leave a Reply

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