Serial Communications
Serial Communications
Communicatio
ns
What is Serial communication?
● Serial is used for communication between the Arduino board and a computer or
other devices.
● Serial data transfer is when we transfer data one bit at a time, one right after
the other.
● Information is passed back & forth between the computer and Arduino by,
essentially, setting a pin high or low. Just like we used that technique to turn an
LED on and off, we can also send data.
● When Arduino receives data, RX blinks while TX blinks while transmitting data.
● When you upload the data to the Arduino, the bits are shoved out one at a time through the USB cable to the
Arduino where they are stored in the main chip.
Different functions of Serial Communication
1. Serial.begin(9600)
● It is a library procedure call where Serial is the library and inside that library is a procedure named begin.
● Begin tells the serial object to perform initialization steps to send and receive data on the Rx and Tx (pins 1
and 0).
● 9600 defines the baud rate. It is the rate at which data is being transferred in bits per second.
2. Serial.print()
● The serial.print ( ) in Arduino prints the data to the serial port.
● The printed data will be visible in the serial monitor.
● The Serial.print( ) is declared in two formats, which are shown below:
○ print( value )
○ print( value, format)
value: It signifies the value to print, which includes any data type value.
format: It consists of number base, such as OCT (Octal), BIN (Binary), HEX (Hexadecimal), etc. for
the integral data types. It also specifies the number of decimal places.
3. Serial.println()
● It is same as Serial.print() but with a slight difference.
● Syntax:
○ Serial.println(val)
○ Serial.println(val, format)
4. Serial.read()
● Serial.read() is a function of the Serial library. What it does is read out the
first available byte from the serial receive buffer.
● Say you had sent the phrase “Sub Sandwich” to your Arduino. This means
you had put 12 bytes into your serial receive buffer.
● Then Serial.read() will return the first value available in the serial receive
buffer, which in this case is “S”, and it will leave “ub Sandwich” in the
Serial receive buffer.
Syntax
● Let's say you need to send the number 217. The binary (1's and 0's) representation of this
number is 11011001.
● Using the command Serial.write(217) will literally just send 11011001 across the line.
7. Serial.parseInt()
● It reads the integer values stored in serial buffer and returns them.
● If you had “I ate 314 tacos” in the serial receive buffer, you’d only get 314 returned the first time
you call Serial.parseInt().
● A common method of using Serial.parseInt() is to pair it with a while loop and Serial.available(),
so that the only time you check for a new integer is when data has actually arrived at the serial
port.
analogWrite()
Writes an analog value to a pin. Can be used to light a LED at varying brightnesses or drive a motor
at various speeds. After a call to analogWrite(), the pin will generate a steady rectangular wave of the
specified duty cycle until the next call to analogWrite().
Syntax
analogWrite(pin, value)