I have been attempting to use the Serial1 line of an Arduino Zero to send and receive data. When I hook up the TX line to send data everything works fine, but I was having problems hooking up the RX line. Every time I hooked it up, it would work for a few seconds and then the whole Arduino would freeze up and have to be reset. I thought at first that this was my circuitry causing this and did a bunch of tests along those lines. Then I tried to just "go to the basics", so I took all of my stuff out of the equation and used the really simple sketch below. I had the TX line unconnected to anything, and the RX line directly connected to 3.3V, and it did the same thing. It worked for about a second and then froze. I tried ground, and same thing. I totally disconnected everything from the RX line and reset. I could see the LED blinking and was getting output from the Serial line. I then tried to hook up to the RX line and as soon as either the unconnected wire, or even my finger, touched the RX pin it would freeze up. So I thought maybe I had a messed up board, or had somehow damaged that board. I bought a new one that I had never used before and did the bare bones test with just the RX line and it did the exact same thing. Does anyone have any ideas on what could be causing this? Can anyone else reproduce this on their board? I have googled for this, but have not found anything that matches. If the RX serial line does not work correctly I would think this would be a big deal.
Have you tried echoing the receiver back out to the transmitter using the console connected to Serial1? This requires either a 3.3V USB to Serial (FTDI) converter, or like I've used a 5V USB to Serial converter plus an I2C logic level shifter, so as not to damage the 3.3V processor.
Here's the code:
void setup()
{
Serial1.begin(115200); // Begin Serial1
}
void loop()
{
if (Serial1.available()) // Check if incoming data is available
{
byte byteRead = Serial1.read(); // Read the most recent byte
Serial1.write(byteRead); // Echo the byte back out on the serial port
}
}
I used this method to test the Zero's other UARTs on SERCOM1 and 2. They worked fine. See here.
It is your
while( !Serial1 );
that is hanging up. Take that line out. I have used
delay(200);
in its place to give it a bit of time so the first characters printed are not garbled.
So I decided to try both suggestions, just a little modified. Since I had two Zeros and I connected the serial ports of one to the other. I connected ground and Vin on both, and then connected Tx of one to Rx of the other, and vice-versa.
Here was the Zero test code:
char c = 0; // for incoming serial data
void setup() {
Serial.begin(57600); // start serial for output
delay(500);
Serial.println("Starting setup");
Serial1.begin(57600); // opens serial port, sets data rate to 9600 bps
delay(500);
Serial.println("Setup Finished");
}
void loop() {
delay(2000);
Serial.println("Printing");
Serial1.print("ABC");
String s = "";
while (Serial1.available() > 0) {
// read the incoming byte:
c = Serial1.read();
s += c;
}
if(s.length() > 0) {
Serial.print("Read: ");
Serial.println(s);
}
}
I was able to get the zeros to send data to each other, so the ports are working. I am not sure why my other simple test locked the zero up, but this points back to my circuit. However, I have been able to hook the exact same circuit board up to the Serial1 line of an Arduino Due and it works perfectly fine. The circuit connects the RX and TX lines up to a level shifter so I get 5V out on it. I am using a GLT2010 chip and the circuit shown in the attachment with 1k pull-up resistors on the lines. I have also tried to put 1k pull-ups on the Arduino side as well, but it made no difference. I am stumped as to why this works for the Due, but not for the Zero.