Arduino Serial Communication

Leave a comment

In the last post we covered the basics of establishing serial communications between your PC and Arduino. We used only two specific methods to accomplish our simple Hello World! program. Arduino’s official website has a lot more information about the Serial class. However, what I do not like is just the way things are organized. Alphabetically listing all available methods does not really help anyone to understand and implement specific instances. I believe that is the case with any index or dictionary or help documentation, they have all necessary information but no one can venture to read it cover to cover. Ok, enough of complaining. I believe we can crack this Serial class soon enough.

So why so much emphasis on the Serial communications you ask. It is one of the ways the microcontroller can communicate with other chips, sensors and external systems. Also, it is very useful in assisting us with debugging our application. Arduino in its basic form such as the Uno class has one serial port marked as TX/RX on the board. The MEGA series has 3 additional serial ports compared to the Uno class. I am using Arduino MEGA and so the serial port are marked as TX0/RX0, TX1/RX1, TX2/RX2 and TX3/RX3 (numbering of ports starts with 0 instead of 1). The TX0/RX0 would correspond to TX/RX in case of Uno class boards.

The first port TX0/RX0 or TX/RX as the case might be, also share the digital pins 0 and 1 of Arduino. So effectively these pin functions are multiplexed. This means that these pins can be switched by configuration or by calling specialized functions that in-turn do the necessary configuration changes to behave differently. In effect the digital pins 0 and 1 can be used as digital inputs or outputs or as serial communication channels among other things (such as interrupts for example). Your Arduino already uses this channel to communicate with the IDE so as to load your sketch. The Serial Monitor screen we have seen being used as part of the previous post also uses this channel for communication.

There are couple of on board LEDs marked as TX/RX that light up when communication is attempted through this channel. During programming you should have seen these LEDs blinking. Also when you tried the Hello World! program as described in the previous post, you should have seen that the LED marked TX should blink in your board. We introduced a delay of 500 micro seconds as part of our code and so the TX LED would blink at the rate of twice a second. So whats up with the RX LED on board? It never lights up. That is because we programmed Arduino to keep sending the message “Hello Word!” but never attempted to listen to what the computer or Serial Monitor has to say. Also we never tried to send anything from Serial Monitor to our Arduino.

Try this now, open the Serial Monitor and if you have already loaded the Hello World! sketch, then, you should see the appropriate text displayed on screen and TX LED blinking. Type some text (could be any thing, but, be polite, after all Arduino just said hello to you) in the Serial Monitor (in the text box area to the left of the SEND button) and then click on the SEND button. Your RX button should light up momentarily. You need to type some text each time before your click on SEND to see the RX LED light up. So you can also communicate with Arduino, but, we have not instructed Arduino to expect your commands and interpret the results. Rest assured that it is something we are going to attempt soon.


Only Arduino

Leave a comment

In most cases for a beginner’s project, the hardware connections to the Arduino board is pretty basic. The microcontroller being the brains of the overall project could take readings from a sensor and perform some calculations and conversions and output the result to a display or drive something such as a motor.

In essence bulk of the time and effort is spent on coding. So it is important that one gets comfortable with the range of variables (microcontroller brings in its own limitations), the wealth of libraries and most importantly bit wise operations.

So here is what we will do. We will just use Arduino for learning. I mean there is no external components to wire up and so no hardware circuitry involved. Your computer’s keyboard will serve as the input and the output will be displayed on screen. Basically we will establish serial communication between computer and Arduino to try out some examples.

Let’s first start with the conventional Blink program for Arduino.

// Arduino's usual LED Blink program
// Arduino's internal LED is connected to digital pin 13
int led = 13;

// we want to use the digital pin 13 as output
void setup()
 pinMode(led, OUTPUT);

void loop()
 // turn on the LED for 250 milli seconds
 digitalWrite(led, HIGH);

 // turn off the LED for the next 250 mill seconds
 digitalWrite(led, LOW);

 // loop through this behavior so that the LED
 // turns on and off to create the blink effect

In most cases the above program comes pre-loaded as part of your Arduino and so you can see that the on board LED blinking once you power your board. Since Arduino did not come with a built-in display, there is no way to show the text familiar to C programmers “Hello World!”.

Our objective here is to display the welcome greeting “Hello Word!” on the computer screen. We will make use of Arduino’s serial communication library to do so. And, guess what, this program is even simpler.

// Arduino Hello World! program

// we want to initialize serial communications
void setup()
 // we will setup Arduino to communicate with the PC
 // at 9600 baud (9600 bits per second)

void loop()
 // let's print the welcome text Hello World!
 Serial.println("Hello World!");

 // we will pause for 500 milli seconds

 // loop through this behavior so that the text
 // repeats forever

That’s it!

Now compile and run the program in Arduino IDE and be sure to open the Serial Monitor under Tools menu to see the output. You may also press Ctrl+Shift+M to get to the Serial Monitor display screen. Hopefully, you will see the welcome message.

Serial Monitor Output

Effectively we wrote only 2 lines of code.
(1) To setup serial communications and
(2) To write out the text we want to display

This is a good starting point. Please make sure that you can type this code in your IDE and not copy and paste. The serial class starts with capital S, but, the function println starts with the lower case p. Once you can get the initial program to work, try experimenting by changing the text to say your name and see whether your program works as expected.

We will continue this exploration in the next few posts. Perhaps we can get Arduino to interact with us.

Arduino circuit examples

Leave a comment

Alberto Pighixxx has once again surprised us by compiling many useful circuits for Arduino enthusiasts around the globe. Please visit his site for a downloadable version of most frequently used Arduino circuit examples.

Sample circuit image of the work done by Pighixxx

Arduino Circuit Examples

Look for the most latest content named Arduino Basic Connections. It does not come with any Arduino code associated with the individual circuits. But, one could easily find code samples from other resources such as Arduino’s official site.

Thanks Alberto Pighixxx!

Older Entries Newer Entries