Blogroll

Powered by Blogger.

Saturday, 12 July 2014

Convert Digital Pins of Arduino Board to Transmitter (Tx) and Receiver (Rx)

by realfinetime  |  in Arduino Uno at  12:39

          Normally 0th and 1st pin is the inbuilt Receiver and Transmitter in most of the Arduino boards ( Uno and Deumilanove ). Serial monitor displays the data transmitted from and received to these pins. Only one set of transmitter and receiver is provided in most of the Arduino boards. But in certain occasions, more than one set of transmitter and receiver is required.

          If you want to connect an X-Bee and a GSM modem to same Arduino board, one set of transmitter and receiver become insufficient. So in newer versions of arduino, a library is provided to convert Digital Output pins to transmitter as well as receiver. A library named SoftwareSerial is used for this purpose. SoftwareSerial library must be included in the Program. To demonstrate the working of SoftwareSerial library, complete the circuit as shown in following diagram.


          We are going to convert 10th and 11th pins of Arduino board to RX and TX respectively. Inbuilt RX ( Pin 0 ) is connected to the new TX ( Pin 11 ) of Arduino. Similarly Inbuilt TX ( Pin 1 ) is connected to the new RX ( Pin 10 ) of Arduino.


          After completing the circuit, upload the following program to send data from new TX ( Pin 11 ) to inbuilt RX ( Pin 0 ). If the uploading is successfull, open serial monitor to see the output. Baud rate should be changed to 2400 in the serial monitor.


#include <SoftwareSerial.h>         // Include the SoftwareSerial Library 

SoftwareSerial mySerial(10, 11);   // RX, TX ( Creates a new SoftwareSerial object )

void setup()
{
  Serial.begin(2400);                      //   Initialise the Serial port at 2400 baud.
  mySerial.begin(2400);                 //   Initialise the mySerial object at 2400 baud rate.

  mySerial.write("Send from new TX to inbuilt RX");  //   Send data through new TX ( Pin 11 ).
  delay(1000);
  while(1)
  {
    if(Serial.available())
    {
      Serial.write(Serial.read());
    }
  }
}

void loop()
{

}

Explanation of the code.

          mySerial.write("Send from new TX to inbuilt RX") command will transmit the text "Send from new TX to inbuilt RX" through the newly generated TX ( pin 11 ). This pin is connected to the inbuilt RX ( Pin 0 ) of Arduino board. Inbuilt receiver ( Pin 0 ) will receive this data using Serial.read() command and will print in the serial monitor using Serial.write(Serial.read()) command.


          Now upload the following program to send data from inbuilt TX ( Pin 1 ) to new RX ( Pin 10 ). If the uploading is successfull, open serial monitor to see the next output. Baud rate should be changed to 2400 in the serial monitor.


#include <SoftwareSerial.h>           // Include the SoftwareSerial Library 

SoftwareSerial mySerial(10, 11);     // RX, TX ( Creates a new SoftwareSerial object )

void setup()
{
  Serial.begin(2400);                        //   Initialise the Serial port at 2400 baud.
  mySerial.begin(2400);                   //   Initialise the mySerial object at 2400 baud rate.

  Serial.write("Send from inbuilt TX to new RX");   //   Send data through inbuilt TX ( Pin 1 ).
  delay(1000);
  while(1)
  {
    if(mySerial.available())
    {
      Serial.write(mySerial.read());
    }
  }
}

void loop()
{

}

Explanation of the code.

          Serial.write("Send from inbuilt TX to new RX") command will transmit the text "Send from inbuilt TX to new RX" through the inbuilt TX ( pin 1 ). This pin is connected to the new RX ( Pin 10 ) of Arduino board. New receiver ( Pin 11 ) will receive this data using mySerial.read() command and will print in the serial monitor using Serial.write(mySerial.read()) command. 

3 comments:

  1. Pin number above 22 can also be converted into transmitter or receiver

    ReplyDelete
  2. The second program that is while sending data from inbuilt Transmitter to my receiver contains a fault...that is the Transmitter is sending data for infinite time.. The correction is 'change- while(1) to while(0).

    ReplyDelete
  3. How i use Rx and Tx how an out / enter digital to HIGH /LOW an led or um relay ?

    ReplyDelete

IMPORTANT NOTICE

All the circuits, published in this blog is only after testing and getting proper results in my private lab. When you try these circuits, you should check the supply voltage, polarity of components, presence of childrens nearby and shorts in the circuits. This website will not be responsible for any harm happened to you or your components caused by your carelessness.

For More Electronic Tips

Blog Archive

Proudly Powered by Blogger.