6
\$\begingroup\$

I'm trying to send ASCII characters from Arduino UNO to a computer serial port. I'm using a cable with a male COM connector, attached to the computer's serial port, and three wires (TX, RX and Ground) on the Arduino side. I used pins 12, 13 and Gnd as shown in the picture:

Arduino and COM interface

And I am running this piece of code to send a string every second on the serial port:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(13, 12); // RX, TX

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

void loop()
{
  mySerial.println("Hello world");
  delay(1000);
}

But, when I read that port (at the right 9600 speed), instead of "Hello world" I get strange characters instead:

enter image description here

I thought I had set the pin erroneusly, so I swapped RX with TX, but I get a different but still wrong output:

enter image description here

What am I doing wrong?

\$\endgroup\$
  • \$\begingroup\$ Parity and stop bits? I don't see how they're configured on either side. \$\endgroup\$ – pjc50 Oct 2 '12 at 10:17
10
\$\begingroup\$

The Arduino UART produces TTL level signals, that is 5V for high and 0V for low. A PC's RS232 port expects full RS232 voltages which can be -9V to +9V and are inverted.

Either use a TTL level serial adapter (such as those from FTDI) to interface to the PC. Or use a level converter like the MAX232.

\$\endgroup\$
  • 2
    \$\begingroup\$ Note that this is not always true. Some modern PCs use 0-5V signalling. You shouldn't assume your computer uses either. Measure it! \$\endgroup\$ – Connor Wolf Jan 21 '13 at 11:54
  • 1
    \$\begingroup\$ Even if the PC uses 0-5V signalling, it's likely still inverted to RS232 signals (1=low voltage, 0=high voltage), so you still need a MAX232 or similar. \$\endgroup\$ – Toby Jaffey Jan 22 '13 at 0:42
7
\$\begingroup\$

Like Toby says you need an EIA-232 (the name RS-232 is obsolete) transceiver. The Arduino's UART will output +5 V when idle and for a logical "1", and 0 V for a logical "0". EIA-232 works with inverted levels, so the +5 V becomes typically -12 V, and the 0 V becomes +12 V.

If you connect the UART directly to the PC's EIA-232 port it may see the +5 V as a low, but the 0 V will be undefined, so it may interpret your data any way.

But the received data is a much bigger problem, and you're very lucky that the AVR's I/O pins have protection diodes:

enter image description here

The RxD line from the PC will be -12 V when idle, and +12 V when it's sending a logic "0". Both levels are way beyond the maximum allowed values for the AVR, but the diodes will clamp them, and the limited drive current from the PC's EIA-232 will be lower than the maximum allowed 40 mA. Without the protection diodes it's very likely that that I/O pin (and maybe more) would be damaged.

\$\endgroup\$
  • \$\begingroup\$ Man, when did RS-232 become obsolete? \$\endgroup\$ – abdullah kahraman Oct 2 '12 at 14:49
  • 5
    \$\begingroup\$ @abdullah - eons ago! :-) The "RS" is for "Recommended Standard", but it has been accepted as standard by EIA and TIA ages ago. But everybody still uses the "RS", except me :-). \$\endgroup\$ – stevenvh Oct 2 '12 at 15:03
  • 2
    \$\begingroup\$ @abdullahkahraman October 1, 1997. Or shortly thereafter. That was the last revision of TIA-232-F. And about the time USB began it's crusade to replace it in PCs. \$\endgroup\$ – embedded.kyle Oct 2 '12 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.