Software serial query

I am using an atmega328p chip and need two extra serial ports, so was thinking to use a software serial library such as the arduino software serial. So my question is: is it possible to use the Arduino softwareSerial outside of the Arduino coding environment, and if so, how would I set this up in platformio?

If not the arduino softwareserial, is there a different serial library which could provide two extra ports (they’re not used simultaneously).

I’ve programmed arduino for a few years, but now looking to program the 328 without the arduino platform.

thanks for help,

A bit of googling turns up the project GitHub - blalor/avr-softuart: Software UART implementation for the AVR platform which supports ATMega328P chips. The implementation is just two files, softuart.c and .h. It has code to support one further serial port at PD1 and PD2 (configurable though), but not two, and uses up the TIM0_COMPA_vect interrupt, so TIM0 is used for that. So you have to slightly re-code it. If the two software UARTs are never used at the same time, you might get away with just making some constants to variables, e.g. regarding the pin and port macros, and exchange their value at the right time in the code.

Otherwise, rewrite ArduinoCore-avr/libraries/SoftwareSerial/src at master · arduino/ArduinoCore-avr · GitHub to not use Arduino functions (or copy the needed ones into the code). From what I see it just uses some conversion functions like digitalPinToPort to map a pin number to the needed port and pin register macros, and a digitalWrite and pinMode. You can substitute that with your implementation or copy the Arduino one.

Unfortunately, it also does using Print::write which is the Arduino class that provides all the serial output stuff.

That might be a pain to rewrite. :frowning_face:


Hmm the way I see it it does that to auto-include the other implementations of the Print class which do e.g. a write with a buffer. The class then does however only implement the fundamental primitive, the write-single-byte, which all other derivatives use

So that should not be the dealbreakinger, since it’s not calling into implementations of the Print::write functions – it implements them.

1 Like

Ah, yes, of course. Bit of a brain fadebthere I think, sorry. :frowning_face:

Thank @maxgerhardt.


thanks for replies. I might be better off finding a pin compatible chip which has two uarts perhaps. I could use spi as one of the three comm links I need.


is there a good place to look for pin compatible alternatives? I found 328PB which has 2 uarts, but as far as I can find googling its not available as a dip…

Perhaps there’s a features comparison site?? I’ll see what I can find.

Just had a quick look at the files (I started programming Attiny82 years ago before switching to the Arduino framework. In the End, I converted to follow the Arduino road, saving myself from re-writing libraries to my special application all the time…). Max’ choice looks like it would be working pretty much out of the box for you. All you need to do, is to wrap the contents in two classes (one “worker class” which does the bit-banging and one “config” class where you’ll finally construct the objects with…) But be reminded that this will be some evenings worth of work; if you want to go down that path, off you go :wink: