Arduino Tutorial Series : Lesson 08 - Serial Communication



Arduino Tutorial Series : Lesson 08 - Serial Communication

So far, Our arduino projects were good at doing thing alone, all by itself. But what about transfering data or control signals between two Arduinos or between an arduino and PC ?

Yes, we can use serial communication to communicate between devices.

Serial Communication between Arduino and PC

Serial communication between Arduino and PC is pretty much easy. There is a serial communication link through the USB port we use to program the arduino to the PC. We can use it to send and receive data.

Turn ON/OFF Led through PC commands

Let's create a project that switch ON and OFF Led connected to Arduino by commands sent by PC.

Hardware


Hardware setup is same as we used in Blinky.

Code

Download the arduino code serialLed.ino

In order to use serial communication we need to initialize serial port first.

Serial.begin(9600);

The parameter Serial.begin() takes is the data rate.(normally called Baud rate)
Then we can use following functions in Serial library,

read() - reads incomming serial data
write() - writes serial data/ send binary data stream
print() - print in the serial monitor console

Upload the sketch to arduino and open the Serial Monitor window of Arduino IDE


You can type 1 and enter to turn ON the Led and type 0 and enter to turn OFF the Led.

NOTE :

Serial print function is very useful for debugging arduino code. By using this we can keep track on what is happening inside the arduino when it runs. If your arduino code isn't working as you wanted, you can use serial print to find out whats the problem.  

Serial communication between two Arduinos

Let's make two Arduinos talk to each other.

Hardware setup


To make things little exciting, lets connect a servo motor to one arduino and a potentiometer to the other Arduino. So we can control a motor connectrd to Arduino 1 using Arduino 2.


Locate the TX and RX pins on the Arduino. In order to establish a two way serial connection, TX pin of Arduino 1 needs to be connectrd to RX od Arduino 2, and RX of Arduino 1 to TX of Arduino 2.

Code

Download the project files serialServo.rar

Now we can communicate between Arduinos and many other devices. This will be useful for most of your future projects!

See you with the next tutorial ;)

No comments:

Powered by Blogger.