Go Down

Topic: SoftwareSerial works only on pins PDx ? (Read 361 times) previous topic - next topic

Phil242

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):

Code: [Select]

#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

runaway_pancake

#1
Oct 14, 2019, 01:03 am Last Edit: Oct 14, 2019, 01:04 am by runaway_pancake
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.
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

DrAzzy

Well, it is *supposed* to work on any pin that supports pcints....
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

runaway_pancake

#3
Oct 14, 2019, 01:53 am Last Edit: Oct 14, 2019, 04:40 am by runaway_pancake
https://www.arduino.cc/en/Reference/softwareSerial

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);





"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

CrossRoads

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].
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

david_2018

#5
Oct 14, 2019, 03:02 am Last Edit: Oct 14, 2019, 03:04 am by david_2018
Code: [Select]

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

Is equivalent to
Code: [Select]

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).

CrossRoads

#6
Oct 14, 2019, 03:08 am Last Edit: Oct 14, 2019, 03:09 am by CrossRoads
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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Juraj

#7
Oct 14, 2019, 07:26 am Last Edit: Oct 14, 2019, 07:31 am by Juraj
there is nothing to speculate or discus.
USE ARDUINO PIN NUMBERS!


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

Juraj

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

westfw

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!)

Phil242

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

Go Up