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?
-
\$\begingroup\$ Parity and stop bits? I don't see how they're configured on either side. \$\endgroup\$pjc50– pjc502012年10月02日 10:17:01 +00:00Commented Oct 2, 2012 at 10:17
2 Answers 2
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.
-
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– Connor Wolf2013年01月21日 11:54:10 +00:00Commented Jan 21, 2013 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– Toby Jaffey2013年01月22日 00:42:35 +00:00Commented Jan 22, 2013 at 0:42
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.
-
\$\begingroup\$ Man, when did RS-232 become obsolete? \$\endgroup\$abdullah kahraman– abdullah kahraman2012年10月02日 14:49:32 +00:00Commented Oct 2, 2012 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– stevenvh2012年10月02日 15:03:43 +00:00Commented Oct 2, 2012 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– embedded.kyle2012年10月02日 20:29:24 +00:00Commented Oct 2, 2012 at 20:29