0

I'm debugging a project which includes SoftwareSerial. I'm not convinced the serial connection behaves as expected, so I came up with the following simple test:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); // RX, TX

void setup() {
  Serial.begin(4800);

  // starting the software serial
  mySerial.begin(4800);
}

void loop() {
  for (int i=0; i <= 10; i++){
    delay(10);
    mySerial.write(i); 
  }
}

I connected pin 11 to pin 1 (arduino Uno), hence forwarding the data of the software serial connection via the default (hardware) serial connection to my computer. But no data is coming through.

Am I missing something?

  • Shouldn't be pin 11 to pin 0? Also, you're simply "sending" data from the Software serial, but you're not reading it anywhere... – Talk2 Oct 6 '16 at 21:49
  • 1
    Try mySerial.print(i) instead. – Mikael Patel Oct 6 '16 at 21:57
1

If you are hoping to see the results in the serial monitor, you need to read it in and send it back (so it ends up on the USB port). Like this:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  Serial.begin(4800);

  // starting the software serial
  mySerial.begin(4800);
}

void loop() {
  for (int i = 0; i <= 10; i++) {
    delay(10);
    mySerial.println(i);

    while (Serial.available ())
      Serial.write (Serial.read ());
  }
}

Now you have to connect pin 11 to pin 0 (not pin 1) because that will now be the Serial Rx pin.

I think your method failed because with Serial active the Serial hardware was trying to drive pin 1 high (ie. idle) which was conflicting with the data from pin 11.


An alternative is to not do a Serial.begin() - which therefore does not initialize the serial hardware and it doesn't conflict any more. This also works:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  // starting the software serial
  mySerial.begin(4800);
}

void loop() {
  for (int i = 0; i <= 10; i++) {
    delay(10);
    mySerial.println(i);
  }
}

Now you connect pin 11 to pin 1 as you originally wanted.


Notice I changed your mySerial.write(i); to mySerial.println(i); so you get correct data output (numbers, not control characters).

  • @ChrisStratton - what do you mean "unlikely to help"? I tested both of my solutions and they do work. There is no doubt about it. In the first case (using Serial to echo pin 11) the signal on pin 11 will "fight" the input from the ATmega16U2 - via a 1k resistor. That means that pin 11 has to sink 5 mA, which it is quite capable of doing. The second method, not using the hardware serial at all, means that pin 11 is connected to the ATmega16U2 (via the 1k resistor) and that pins D0 and D1 are high-impedance, and will not affect anything. – Nick Gammon Oct 7 '16 at 20:45
  • Yes, seems I misread the arduino schematic for positioning of the resistors as it is essentially drawn backwards, with the peripheral ATmega16u2 in the center and the main processor ATmega8 off to the side... – Chris Stratton Oct 7 '16 at 21:02
  • Those schematics take some getting used to! The 16U2 is drawn somewhat larger than the 328P. :) – Nick Gammon Oct 7 '16 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.