arduino serial communication

Arduino Serial Communication Tutorial

Sometimes you really need to know what’s going on inside your device, sometimes you want to use more than one device, or maybe you want to add some external modules to your device. In all these cases, you need to use Serial communication. Today we are going to build a very simple system where you push a button that sends a message to the computer asking if the light should be ON or OFF, and turn an LED on and off based on the answer.

Today’s recipe

  • Arduino UNO board
  • 2 LED’s (preferably different colors)
  • 2x 220 Ohm resistors
  • 10 kW resistor
  • 1 push button (2 pin or 4 pin are both fine)

The code for today is available on github.

What is serial communication?

Serial communication is the most commonly used method to send information between two devices in the simplest way: sending it bit by bit. Essentially the information is passed back and forth by setting a pin high or low with a predefined frequency, called baud rate. The Arduino UNO board comes with a predefined serial port with two pins. These two pins are available on digital pin 0 (RX) and 1 (TX), but it is important to keep in mind: The USB port is also using the same two pins, so if you want to use the Serial Monitor, keep these two pins unconnected. Additionally, if you need it, you can also set up extra serial channels with the SoftwareSerial library.

Setting up our system

arduino serial communication

Figure 1: Prototype setup

arduino serial communication

Figure 2: Connection diagram

Our simple example of the Arduino serial communication system will consist of a push button and two LEDs: one to indicate if the system is working, and one that we are going to control through the Serial Monitor. Both LEDs are connected to a 5V digital output pin and they are also serially connected to a 220 Ohm resistor. This allows us to limit the amount of current flowing across the LEDs.

Connecting the button circuit

We are going to use a very simple push button on the digital pin 2 for this project. By default the button circuit is open which indicates that the input pin is high. When the button is pushed, the input pin connects to the ground (LOW).

arduino serial communication

Figure 3: Button circuit

Arduino code

Now, let’s write some code. First, we need to define our pins that we will be using and write the setup function. In the setup function, first initialize the pins and open the serial communication. Upload and run the following codes on your Arduino.

Once your Arduino serial communication code is uploaded, open the Serial Monitor by going to Tools, Serial Monitor. Set the baud rate to 9600, then choose Both NL & CR option on the bottom right corner. If you did everything correctly, you should see a welcome message like this:

arduino serial communication

Figure 4: Serial Monitor

Adding a button interrupt

In the setup function, you can see the following line:

attachInterrupt(digitalPinToInterrupt(btn), btn_ISR, LOW);

So, what does it really do? Well, basically this command line prevents further execution on Arduino and calls the btn_ISR function each time the btn pin is LOW (each time you press the button). Keep in mind that on an Arduino Uno interrupts can only be attached to Pin 2 and 3.

Writing the loop function

We have now reached the most important part of our code: the loop function. Arduino needs to run the following three things repeatedly in a given order:

  1. Wait for the button to be pushed
  2. Send out the message to the computer
  3. Receive the answer back

With a global variable (systemstate), we can indicate whichever state we are in. When the systemstate is 0, your Arduino waits for an interrupt to happen (the red LED blinks slowly). When the systemstate is 1, your Arduino sends out the question and then automatically changes to state 2, where it stays until there is an answer available on the serial line. When the answer arrives, we read it to a local string, and compare it to our two expected answers: “onrn” or “offrn”.

So, why do we have this “rn“ at the end of the expected strings? When we chose Both NL & CR in the Serial Monitor, we instructed the computer to automatically add a “r” (carriage return) and a “n” (new line) characters to the end of our message. Therefore, when you type in “on”, your Arduino is going to receive the message as “onrn”.

Now, you just need to upload the full code below, open the Serial Monitor and you can start talking to your Arduino.

arduino serial communication

Figure 5: Arduino blinking red light

arduino serial communication

Figure 6: Serial Monitor with 9600 baud rate and Both NL & CR option

The full code: