High baud rate serial communication problem

I've been trying to use my external DAC with an Arduino Nano to reproduce 8-bit audio coming from the serial port.

void setup() {
  Serial.begin(528000, SERIAL_8N1);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
void loop() {
  byte inByte = Serial.read();
  digitalWrite(2, bitRead (inByte, 0));
  digitalWrite(3, bitRead (inByte, 1));
  digitalWrite(4, bitRead (inByte, 2));
  digitalWrite(5, bitRead (inByte, 3));
  digitalWrite(6, bitRead (inByte, 4));
  digitalWrite(7, bitRead (inByte, 5));
  digitalWrite(8, bitRead (inByte, 6));
  digitalWrite(9, bitRead (inByte, 7));

Despite the enormous baud rate, it actually kind of works. The biggest problem I have is that the 7th bit is stuck high. Reducing the baud rate kind of helps, but it makes a bunch of other things worse so I'm not considering it yet. I've tried outputting this bit on other pins, makes no difference. Any ideas on what's going on here?

Did some research on 328P baud rates and reducing it to exactly 500000 seems to have fixed the 7th bit issue. Maybe there is also a way to stabilize the execution speed? Despite the code being pretty straightforward digital outputs slightly stutter and degrade the audio.

If you look in the Atmega 328 datasheet you will see tables of typical baud rates and the timing errors associated with them. There is no timing error at 500,000 baud - or 1,000,000 baud. I use 500,000 baud regularly.


There is no timing error at 500,000 baud - or 1,000,000 baud.

Well that's what i'm getting at 500 000. Should be a straight triangle.

I wonder if all your digitalWrite()s are slowing things down for the high baud rate.

Try the digitalWriteFast.h library - it really is a lot faster but it must know the I/O pins at compile time - which you do.

Read about direct port adressing for even faster I/O. If you can choose your I/O pins to suit the I/O Ports on the nano you could probably write all 8 values with 2 or 3 instructions and without even needing bitRead(). Unfortunately none of the I/O ports on a nano has all 8 bits available so you would need to use at least 2 Ports.


Or it could interrupts to service millis() and micros() and whatever goes on with loop().

Try putting all the writes inside while (1) { }.

Thanks everyone!
Nothing worked, and it really shouldn't have, because all along the real problem was Tera Term. As soon as I switched to the command line "copy" the glitches stopped.

Try the digitalWriteFast.h library

Thanks for the info! Even though regular digitalWrite would probably work fine, I ended up using digitalWriteFast anyways, just to be sure.