KEMBAR78
Arduino- Serial communication | PDF
Arduino Based Applications
Part-2
By
Jawaher A.Fadhil
B.SC in Electronics Engineering
M.Tech in Computer Engineering
University of Duhok
College of Science
CS Department
Serial Communication
2
Introducing Serial Communication
The word serial comes from the way data is transmitted;
serial devices send bits one at a time on a single wire. This is
something that you have seen before; it is like a telephone
call. Both users pick up the telephone and a single wire
connects them together.
While serial devices send bits on a single wire, parallel
devices send multiple bits on multiple wires. Although
parallel communications can be faster than serial, they were
often more expensive, requiring more wires. There are also
speed limitations due to physical limitations of conductive
wiring. The next figure shows the difference between serial
and parallel communications.
3
Introducing Serial Communication
4
Serial versus parallel
Introducing Serial Communication
Serial ports are extremely simple. This simplicity is one reason
why they are used so often. Data is sent on one wire, the
transmit wire (TX), and received on another, the receive wire
(RX). On the other side of the cable, it is connected to another
computer with a TX pin and an RX pin. Inside the cable itself, the
TX and RX wires are inverted. The TX pin on one side is
connected to the RX pin on the other side. This is illustrated in
Figure.
5
Transmit and receive wires
Introducing Serial Communication
Arduinos use serial ports for communicating with computers
and other devices. The USB port of an Arduino is used for
serial communication with a computer, with the added
advantage that USB can also be used to power the device.
USB also has the advantage of auto-configuring most of the
parameters.
Some Arduinos have other hardware serial ports, enabling
communication with other devices. The USB communication
is sent to Arduino pins 0 and 1, meaning that those pins are
reserved if your device must communicate with a computer.
6
Starting a Serial Connection
To Start a Serial Connection you must first do some basic
configuration. To do this, you use the begin function of the
Serial object. Typically, 9,600 is an appropriate speed for
communicating. You are free to use any speed you want as long
as both devices are operating at the same speed.
void setup()
{
Serial.begin(9600); // Opens the serial port
}
Serial configuration is normally done in setup() because devices
tend to not change the speed at which they communicate over
time.
7
Sending Text
To send data to the serial device , use the function print(). The
data to be printed can be in any format.
Serial.print("Hello, world"); // Output an entire string
Serial.print('!'); // Output a single character
8
Serial Communication-writing
You need the following components:
• Arduino Uno
• USB cable
Serial Communication-writing
void setup( )
{
pinMode(13, OUTPUT);
Serial.begin(9600); // sets data rate to 9600 bps
}
void loop()
{
digitalWrite(13, HIGH);
Serial.println("LED is On");
delay(1000);
digitalWrite(13, LOW);
Serial.println("LED is Off");
delay(1000);
} 9
Serial Communication-Reading
Arduinos can also receive data. Receiving data can be used for
many projects; computers can send data, for example, to control
the brightness of an LED. To read data from the serial device , use
the function Serial.read( );
int ledPin=13;
int value;
void setup()
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
}
10
Serial Communication-Reading
void loop ()
{value = Serial.read();
if (value == '1')
{
digitalWrite(ledPin,HIGH);
}
else if (value == '0')
{
digitalWrite(ledPin,LOW);
}
}
11
Reference
James A. Langbridge,” Arduino sketches tools and
techniques for programming wizardry”,2015.
12
13

Arduino- Serial communication

  • 1.
    Arduino Based Applications Part-2 By JawaherA.Fadhil B.SC in Electronics Engineering M.Tech in Computer Engineering University of Duhok College of Science CS Department
  • 2.
  • 3.
    Introducing Serial Communication Theword serial comes from the way data is transmitted; serial devices send bits one at a time on a single wire. This is something that you have seen before; it is like a telephone call. Both users pick up the telephone and a single wire connects them together. While serial devices send bits on a single wire, parallel devices send multiple bits on multiple wires. Although parallel communications can be faster than serial, they were often more expensive, requiring more wires. There are also speed limitations due to physical limitations of conductive wiring. The next figure shows the difference between serial and parallel communications. 3
  • 4.
  • 5.
    Introducing Serial Communication Serialports are extremely simple. This simplicity is one reason why they are used so often. Data is sent on one wire, the transmit wire (TX), and received on another, the receive wire (RX). On the other side of the cable, it is connected to another computer with a TX pin and an RX pin. Inside the cable itself, the TX and RX wires are inverted. The TX pin on one side is connected to the RX pin on the other side. This is illustrated in Figure. 5 Transmit and receive wires
  • 6.
    Introducing Serial Communication Arduinosuse serial ports for communicating with computers and other devices. The USB port of an Arduino is used for serial communication with a computer, with the added advantage that USB can also be used to power the device. USB also has the advantage of auto-configuring most of the parameters. Some Arduinos have other hardware serial ports, enabling communication with other devices. The USB communication is sent to Arduino pins 0 and 1, meaning that those pins are reserved if your device must communicate with a computer. 6
  • 7.
    Starting a SerialConnection To Start a Serial Connection you must first do some basic configuration. To do this, you use the begin function of the Serial object. Typically, 9,600 is an appropriate speed for communicating. You are free to use any speed you want as long as both devices are operating at the same speed. void setup() { Serial.begin(9600); // Opens the serial port } Serial configuration is normally done in setup() because devices tend to not change the speed at which they communicate over time. 7
  • 8.
    Sending Text To senddata to the serial device , use the function print(). The data to be printed can be in any format. Serial.print("Hello, world"); // Output an entire string Serial.print('!'); // Output a single character 8 Serial Communication-writing You need the following components: • Arduino Uno • USB cable
  • 9.
    Serial Communication-writing void setup() { pinMode(13, OUTPUT); Serial.begin(9600); // sets data rate to 9600 bps } void loop() { digitalWrite(13, HIGH); Serial.println("LED is On"); delay(1000); digitalWrite(13, LOW); Serial.println("LED is Off"); delay(1000); } 9
  • 10.
    Serial Communication-Reading Arduinos canalso receive data. Receiving data can be used for many projects; computers can send data, for example, to control the brightness of an LED. To read data from the serial device , use the function Serial.read( ); int ledPin=13; int value; void setup() { Serial.begin(9600); pinMode(ledPin,OUTPUT); } 10
  • 11.
    Serial Communication-Reading void loop() {value = Serial.read(); if (value == '1') { digitalWrite(ledPin,HIGH); } else if (value == '0') { digitalWrite(ledPin,LOW); } } 11
  • 12.
    Reference James A. Langbridge,”Arduino sketches tools and techniques for programming wizardry”,2015. 12
  • 13.