Serial.begin(9600);
The value 9600 specifies the Baud rate. Baud actually refers to symbols per second
but as there is no special encoding, the Baud rate is the same as the
number of bits per second i.e. 9600 bits per second or 9600Hz.
When you use the serial interface data is transmitted in both
directions and for the serial link the method is to use two wires - one
carries data to the PC, and one carries data from the PC.
When you send a character from the Arduino, the Arduino serial
transmit hardware module transmits the byte on the TX line. Similarly
when the Arduino
accepts a byte receives it on the RX line into the serial receive
hardware module. These two things are entirely separate pieces of
hardware (other than the baud rate clock which is applied to both).
The reason for separate Rx and Tx modules is that either end could start a transmission at any time -
so you can't use one wire. It also means there are two serial units in
the hardware serial module or two hardware buffers; one for transmit and
one for receive.
When you set the Baud rate to 9600 you are telling the hardware
module to set a bit speed of 9600Hz so that each bit sent out from the
transmit buffer is sent at 104.16us (1/9600). So every 104.16us, a bit is
sent from the buffer until it is empty. Similarly, the receiver looks for data bits separated in time by 104.16us.
The serial hardware module in the Arduino (or USART - Universal
Synchronous Asynchronous Receiver Transmitter) was really designed for
RS232 which is a physical protocol that allows transmission of data over
longish distances.
It's called a USART because you can use it for different serial
modes. You can change the number of bits, add a parity check, change the
number of stop bits, use a synchronous clock etc.
A real RS232 setup
uses a converter chip that takes the output of the USART and sets up
different voltage levels (logic 1
is -25V and logic 0 is +25V) so that a long cable can be driven. This
25V level is not mandatory so a common level used is ±12V as it easier
for logic chips to create. The
higher voltage is used to overcome cable resistance (the receivers can
accept levels of ±3V).
However the longer the cable, the more capacitance it has, so rise
and fall times are affected. The RS232 protocol uses an asynchronous
clock, meaning that the clock is determined from the transitions of the
signal.
Eventually the protocol stops working as the rise and fall times become
so slow you can't tell where a bit starts i.e it jitters around causing
false data.
However by reducing the speed of the RS232 signal the rise and fall
time can be mitigated i.e. the period of the bit will be large compared
to the rise/fall time of the signal.
So in a real system you use the maximum speed you can get away with for the cable length/type you are going to use!
When you use an Arduino these limitations do not apply and you can
actually get away with far higher Baud rates because there is no long
cable between the PC and the Arduino. 9600Baud is just the accepted
default rate used in most example programs - I usually change it to
115200 Baud in my programs! - If you have a lot of data to send to the
PC it makes program output quicker!
The Baud rate of 115200 is the one actually used by the IDE to upload
your program to the Arduino Board! You can see this enabling "Show
verbose output during upload" from Menu > File > Preferences.
So the IDE always uses 115200 for its own operation!
Actually you can go far faster - my older 3dprinter uses 250000Baud (0.25MBaud) (using an Arduino Mega3260 as the Arduino processor chip - and it never had communications problems). That's a bit period of 4us! - so 10 bits takes 40us to transmit.
One thing you must make sure of is that both ends; the Arudino and
the PC are setup the same. In terms of Arduino Software that means both
ends must use the same Baud rate because all other parameters are fixed
in Arduino software i.e.. number of bits, stop bit etc. If the
parameters don't match then one end will see rubbish - or nothing at
all.
Ensure that the Baud rates are the same as shown in the following
picture. If you change your program's baud rate then also change the PC
Baud rate.
Say you set the Arduino to 115200Baud and the PC to 9600Baud:
So in your Arudino program you use:
Serial.begin(115200);
...and in the IDE you leave it at the default of 9600.
For the Arduino it sends data at a period of 8.6us (1/115200). If
there are 10 bits transmitted then its complete in 86us. The ide will
not even see the bits as its receive period is 104us. Well, it
samples in the middle of the bit so may output some noise but it will be
rubbish.
This is one of the big problems with the RS232 protocol as there is
no way for one end to interrogate the other end to find out its
settings! It is entirely up to the programmer to set both ends
identical.
For me this happened on a new 3dprinter Ender 3 - with the old
printer the serial link worked fine but on the new one there was no
output. This is not too good if you are setting up a new system as you
won't know where the problem is. In fact the Ender3 used a different
baud rate in the Arduino code on the machine.
The serial hardware module can run at very high speed (up to 2Mbaud - but that is pushing it) but even a modest speed is too fast for the capabilities of a program in the microcontroller sampling pins (you can just about bit-bang a 9600Baud serial link but will struggle with higher speeds).
The Serial hardware module does it all for you and takes the pressure
off the main processor - only interrupting the processor if a complete
byte is received at the serial input. So all the processor needs to to is read a byte from the hardware module.
Deep within the vaults of Arduino code you will find the parameters
for the serial interface that are fixed and hidden from you. Ok it makes
the interface easy to use, as all you do is set is the Baud rate - the
rest is done for you.
There is one relevant class:
Class name |
Location |
Use |
HardwareSerial | HardwareSerial.h | Hardware specific |
HardwareSerial inherits the Steam class, so you can used methods defined in the Stream class.
In the class HardwareSerial we have:
void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }
void begin(unsigned long, uint8_t);
Your code invokes the top method when you write Serial.begin(baudrate) and the body of that code invokes the method below using two parameters. That second parameter sets up the internal hardware using the value SERIAL_8N1 (the actual value of this definition controls the serial hardware directly). These are the fixed hidden parameters and they - say:
8 - The hardware serial buffers use 8 bits - get or send 8 bits at a time.Note that a start bit is always used as this is how start of a data
transmission is detected. So the total number of bits transmitted or
received is (S)+8+1 = 10. The stop bit is actually an idle time
but must be accounted for to determine the total tx/rx time.
For Arduino boards with more than one serial port the HardwareSerial class will define numbered Serial objects;
For a single Serial port (Arduino Uno/Nano) you write:
Serial.begin(115200);
For multiple Serial ports you write
Serial.begin(115200);
Serial1.begin(9600);
When there are multiple ports Serial1 is the next label - provision is made for 4 ports in total (Arduino Mega has 4).
Arduino Serial begin is an essential piece of code for initialising
the internal serial hardware. You need the serial hardware so that the
processor is not overloaded. You can also increase the baud rate with no
effect on your code (except to make it operate faster!).
The two other functions that for a basic part of Serial code are:
Serial.available()
Serial.read()
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.