How to serial out on any arduino pin.


Linux-SWAT

Forum Addict!
Joined
Feb 13, 2010
Messages
9,172
Hi,

I made a little test program.
This range works for me on a Uno.

Code:
// GPL3 license, Linux-SWAT, 2021
// Distributed as is.

// 16 bits, 9600 bauds, start bit low
int serialPin = 8;
short num = 0xa55a;
byte state;

void setup()
{ 
  pinMode(serialPin, OUTPUT);
  digitalWrite(serialPin, HIGH);
  delay(100);
}

void loop()
{
  for (int F=97; F <= 100; F++){ // test range
    //delay(500);
    digitalWrite(serialPin, LOW); // start bit low
    delayMicroseconds(F);               
      for (int i=0; i <= 15; i++){
      
        state = bitRead(num, i);
        digitalWrite(serialPin, state);
      
        delayMicroseconds(F);
      }
    digitalWrite(serialPin, HIGH); // end bit high
    delayMicroseconds(F); 
  }
}
 
Out of interest, what advantage did you have in mind over using the SoftwareSerial library?

I can think of a few reasons for manually bit-bashing serial out from a GPIO, but also reasons not to. Is this part of a larger project?
 
AFAIK, the serial library uses only the RX pin, but maybe I missed something huge ?
I want the arduino to have many serial out.
 
The page at https://www.arduino.cc/en/Reference/SoftwareSerialWrite gives this example:

Code:
SoftwareSerial mySerial(10, 11);

void setup()
{
  mySerial.begin(9600);
}

void loop()
{
  mySerial.write(45); // send a byte with the value 45

   int bytesSent = mySerial.write(“hello”); //send the string “hello” and return the length of the string.
}

The docs do point out that receiving may not work on all pins on all Arduino boards, because it needs interrupts to catch incoming data. By the same token, if data starts coming in on two serial ports at once, you may well struggle to handle both.

Also bear in mind that SoftwareSerial.write() is blocking, because you haven't got a hardware serial interface to take care of buffering etc. It's not really a limitation of the library so much as a limitation of the concept.
 
Back
Top