Search This Blog

Showing posts with label Serial Port. Show all posts
Showing posts with label Serial Port. Show all posts

Tuesday, 20 November 2012

Software Serial Arduino

Hello!

In the previous post I have spoken about the JY MCU Bluetooth Module, wiring using the Serial Port of our Arduino and a basic example but, how about if you want to use the Serial Port to another thing and you want to continue using the JY MCU? Can we emulate a Serial Port with digital I/O of our Arduino?. Of course, Yes!.

It's very easy. We only need to connect our JY MCU Bt following this schematic:


TX BT Module ------------------> Digital Pin 10 Arduino
RX BT Module ------------------> Digital Pin 11 Arduino
VCC BT Module <---------------> 3,3V Pin Arduino
GND BT Module <---------------> GND Pin Arduino

Note: Of course, you can choose the Digital I/O which you prefer. Then, in the code source, we configure them.



 Code:


#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
char lecturaSerie = Serial.read();
int led = 13;

void setup()  
{
   Serial.begin(9600); // Open serial communications and wait for port to open:
  
  mySerial.begin(9600); // set the data rate for the SoftwareSerial port
}

void loop() // run over and over
{
  if (mySerial.available())

   {
     if (mySerial.read() == '1')
      digitalWrite(led, HIGH);   // set the LED on
      Serial.println("LED ON");        
   }
   {
     if (mySerial.read() == '2')
      digitalWrite(led, LOW);   // set the LED on
      Serial.println("LED OFF");      
   }
  delay(100);                    // wait 100ms for next reading
}



In this code source, We use the library SoftwareSerial.h to emulate a Serial Port in the digital ports 10 and 11... and then, if Arduino receives de conditions "1" or "2", it switchs on or switchs off a Led and sends a message through the original Serial Port.


See you!!