SoftwareSerial works only on pins PDx ?

Hi dudes,

I have to receive a serial line at 9600 with SoftwareSerial lib on an Arduino NANO.

The following code is working fine only if I use some on pin from PDx as RX (here PD7):

#include <SoftwareSerial.h>
SoftwareSerial mySerial(PD7, PB1);  // (RX, TX)

void setup() {
  Serial.begin(9600);
  Serial.println("Start serial monitoring");
  mySerial.begin(9600);
}

void loop() {
  if (mySerial.available() > 0)
    Serial.write(mySerial.read());    
}

If I try to run it with PB0 as RX it doesn't work at all.
And I have tried various RX pins under PBx and PCx port, it's the same problem. Only PDx works as RX like a charm.

Is there a known restriction or something to do fix this problem ?

Cheers,

Phil

Why not use the recommended pin numbers (PORTD bit7 is pin 7, PORT B bit 0 is pin 8 )?
The SoftwareSerial tutorial uses 10,11; those are PORTB bits 2,3.

Well, it is supposed to work on any pin that supports pcints....

On Uno/Genuino/Duemilanove (and therefore Nano, too - it's still a 328P) pin 13 is the only exception.

SoftwareSerial mySerial (7,8);
SoftwareSerial mySerial (7,9);

Pretty sure 13 supports PCINT as well.

PD[7:0], PC[6:0], PB[7:0] -> PCINT[23:0] per the datasheet.
D13 is PB[5].

SoftwareSerial mySerial(PD7, PB1);  // (RX, TX)

Is equivalent to

SoftwareSerial mySerial(7, 1);  // (RX, TX)

which puts the Rx line on D7, and the Tx line on D1.

The PDx and PBx notation defines the bit position within Port D and Port B, respectively. For most sketches, you would use 0 through 13 to specify ports D0 through D13, and A0 through A7 for the nano analog ports (although 0 through 7 will work for analogRead instructions, since it can be assumed the reference is to an analog input).

On a Nano/Uno/Promini, PB1 is D9.

PortD0 to D7 are D0 to D7
PortB0 to B5 are D8 to D13
PortC0 to C5 are D14 to D19.

A Nano and some Prominis will have C6 and C7 as Analog6 and Analog7. (input only)
D0/D1 are the hardware UART pins that connect back to the PC for serial downloads/serial prints.
Don't use D0/D1 for software serial.

there is nothing to speculate or discus.
USE ARDUINO PIN NUMBERS!

Hi,

Thanks to all of you for your reply .

As I came from the AVR world, that's maybe my second design with the Arduino's IDE, I've made a mistake with naming the I/O pins as I used to do: PBx etc.

I've downloaded a ref card "NANO PINOUT" and understood the mapping:

D0-D7 are pins 0 to 7 and are mapped to PD0-PD7
D8-D13 are pins 8 to 13 and are mapped to PB0-PB5
A0-A5 are pins 14 to 19 and are mapped to PC0-PC5

So, this evening I'll try to use "8" instead of PB0 and so, then I'll let you know if it rocks.

Cheers,

Phil

Phil242:
Hi,

Thanks to all of you for your reply .

As I came from the AVR world, that's maybe my second design with the Arduino's IDE, I've made a mistake with naming the I/O pins as I used to do: PBx etc.

I've downloaded a ref card "NANO PINOUT" and understood the mapping:

D0-D7 are pins 0 to 7 and are mapped to PD0-PD7
D8-D13 are pins 8 to 13 and are mapped to PB0-PB5
A0-A5 are pins 14 to 19 and are mapped to PC0-PC5

So, this evening I'll try to use "8" instead of PB0 and so, then I'll let you know if it rocks.

Cheers,

Phil

use the pin numbers printed on PCB

Yes, the Arduino functions take the Arduino Pin Numbers (0-19 or thereabouts, for a Nano.)
The various PDn and PBn definitions still exist via <avr/io.h> (that the Arduino Core includes for you), and they're both integers, so the core can't detect mis-use. But PD0..PD7 is just 0..7 and PB0..PB7 is ALSO 0..7, so the only reason it's working on PDn for you is that the Pin numbers and the PD values just happen to be the same. (that won't be true on some other boards!)

So, with the correct naming, 8 for pin D8 it works like a charm.
And I've tried with 13, the built-in LED, it works fine too, no restriction on this pin.

Problem solved!

Thanks to all the community for the kind answers.

Cheers,

Phil