1

I am using an Arduino Uno board with IDE 1.6.7.

I have tried to implement a tweaked version of the basic example (SoftwareSerial example) from the Software Serial library. The code is attached.

It should blink the light and print the message on the serial monitor. But I am not getting anything.

In fact, I also tried the example as it is. It did not work either.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 9); // RX, TX

void setup() {
    pinMode(13,OUTPUT);
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
        ; // wait for serial port to connect. Needed for native USB port only
    }
    Serial.println("Goodnight moon!");
    // set the data rate for the SoftwareSerial port
    mySerial.begin(9600);
    delay(1000);
}

void loop() { // run over and over
    mySerial.println("hell");
    if (mySerial.available() > 0) {
        digitalWrite(13,HIGH);
        Serial.write("working..");
        delay(3000);
        digitalWrite(13,LOW);
        delay(2000);
    }
}
dda
  • 1,595
  • 1
  • 12
  • 17
rohit
  • 11
  • 1
  • 1
  • 2

3 Answers3

2

SoftwareSerial cannot transmit and receive at the same time. So even if you connected pin 8 to 9 with a wire, your sketch will not receive what was transmitted. That's a guess without your wiring diagram.

NeoSWSerial can simultaneously transmit and receive. It is a drop-in replacement for SoftwareSerial.

However, AltSoftSerial is even better, and it only works on pins 8 & 9. Use that instead!

This answer compares all 4 serial types, including HardwareSerial.

slash-dev
  • 2,029
  • 13
  • 12
1

I built two scetch for testing. One for Arduino Nano no1 and another for Arduino Nano no2. They are tested and working, not only written to this page.

No2 is sending "hello" and No1 is listening and sending it to PC via Serial. The speed (1200bps) is slow, but so it is easier to see what is happening.

Connect Pin10 of no1 to Pin11 of no2 and gnd to gnd. Connect the Nanos to the USB-ports of the PC and set a monitor program (1200, 8N1) to listen no1.

This seems to be a good way to test Serial.print, Serial.println and Serial.write commands. You will be surprised.

/*No1:/

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
  Serial.begin(1200);
  Serial.println("Testing");
  mySerial.begin(1200);
}
void loop() { // run over and over
 if (mySerial.available()) //If this is missing, you get fast repeating carpage
{
    Serial.write(mySerial.read());
    Serial.print(".");
 }
}

/*No2:/

#include <SoftwareSerial.h>
 SoftwareSerial mySerial(10, 11); // RX, TX
 void setup(){
  mySerial.begin(1200);
}
void loop(){
    mySerial.print("Hello");
}
Mistofeles
  • 21
  • 2
0
if(mySerial.available()>0)

mySerial.available() returns the number of bytes available in your serial receive buffer. Unless you are sending commands via serial in pins 8 and 9, this will remain 0 and you will never enter the if statement.

Also, you have two declarations for serial here:

  1. Serial.begin() initialized the built-in UART USB module. Here you will be able to send/receive commands with a program such as Putty.

  2. SoftwareSerial mySerial(8, 9); // RX, mySerial.begin() initializes serial communications with pins 8 and 9 on the Arduino board. This would be if you wanted to send/receive commands with digital I/O instead of, or in addition to, the UART USB module.

dda
  • 1,595
  • 1
  • 12
  • 17