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.

Advertisements