0% found this document useful (0 votes)
31 views

Embedded Systems: Assist. Prof. Rassim Suliyev - SDU 2018 Week 3

The document discusses serial communication with Arduino boards. It explains that Arduino uses serial communication to interface with computers via USB and with other devices. It describes the TX and RX pins for transmitting and receiving serial data and how serial data is sent as bits one after another. The document also discusses baud rates, sending data from Arduino to the computer using Serial.print and Serial.println, receiving data in Arduino using Serial.available and Serial.read, and provides an example of controlling an LED using serial input.

Uploaded by

indrahermawan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Embedded Systems: Assist. Prof. Rassim Suliyev - SDU 2018 Week 3

The document discusses serial communication with Arduino boards. It explains that Arduino uses serial communication to interface with computers via USB and with other devices. It describes the TX and RX pins for transmitting and receiving serial data and how serial data is sent as bits one after another. The document also discusses baud rates, sending data from Arduino to the computer using Serial.print and Serial.println, receiving data in Arduino using Serial.available and Serial.read, and provides an example of controlling an LED using serial input.

Uploaded by

indrahermawan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

EMBEDDED SYSTEMS

Week 3 Assist. Prof. Rassim Suliyev - SDU 2018


Communicating with Others
 Arduino can use same USB cable for programming
and to talk with computers
 Talking to other devices uses the “Serial” commands
 TX – sending to PC
 RX – receiving from PC
Serial Communications
 Sends
“Hello
world!” to
your
computer

 Click on
“Serial
Monitor”
button to see
output
Arduino Communications
 Is just serial communications
 Arduino doesn’t really do USB
 It really is “serial”, like old RS-232 serial
 All microcontrollers can do serial
 Not many can do USB
 Serial is easy, USB is hard

serial terminal from the old days


Serial Communications
 “Serial” because data is broken down into bits, each sent
one after the other down a single wire.
 The single ASCII character ‘B’ is sent as:

 Toggle a pin to send data, just like blinking an LED


 You could implement sending serial data with
digitalWrite() and delay()
 A single data wire needed to send data. One other to
receive.
Arduino & USB-to-serial

 A standard Arduino has a single hardware serial port


 But serial communication is also possible using software
libraries to emulate additional ports
Arduino Mini
 Arduino Mini separates the two circuits

Arduino Mini USB adapter Arduino Mini


Arduino Mega
 The Arduino Mega has four hardware serial ports
 Only one of these has a USB adapter built in
Arduino to Computer

 USB is totally optional for Arduino, but it makes things easier


 Original Arduino boards were RS-232 serial, not USB
 All programs that talk to Arduino (even the Arduino IDE) think that they’re
talking via a serial port
Arduino & USB
 Since Arduino is all about serial, and not USB,
Interfacing to things like USB flash drives, USB
hard disks, USB webcams, etc. is not possible
 Also, USB is a host/peripheral protocol. Being a
USB “host” means needing a lot of processing
power and software, not something for a tiny 8kB
microcontroller. It can be a peripheral. In fact, there
is an open project called “AVR-USB” that allows
AVR chips like used in Arduino to be proper USB
peripherals
Serial Message Protocol
 Where each message begins and ends?
 Sides must agree how information is organized in
the message (communications protocol)
 Header - one or more special characters that
identify the start of message
 Footer - one or more special characters that identify
the end of message
Sending Debug Information from
Arduino to Your Computer
 This sketch prints sequential numbers on the Serial Monitor:
void setup(){
Serial.begin(9600); // send and receive at 9600 baud
}
int number = 0;
void loop(){
Serial.print("The number is ");
Serial.println(number); // print the number
delay(500); // delay half second between numbers
number++; // to the next number
}

 Output is:
The number is 0
The number is 1
The number is 2
Baud rate

First call the


Serial.begin()

The function takes a


single parameter: the
desired
communication speed
(baud).

You must use the


same speed for the
sending side and the
receiving side.

baud is a measure of the number of bits transmitted per second


Sending information
 You can display text using the Serial.print()or
Serial.println() function

 println() – prints the data followed by a carriage return character and a


newline character
 These commands can take many forms
 Numbers are printed using an ASCII character for each digit
 Floats are similarly printed as ASCII digits, defaulting to two decimal places
 Bytes are sent as a single character
 Characters and strings are sent as is
Strings
 String message = "This string"; //C++ type strings
 message.length()
//provides thenumber of characters) in the string
 message.concat(anotherMessage)
//appends the contents of anotheMessage to message (also + operator)
 message.substring(s, e);
//returns a substring starting from s till e
 You can use the indexOf and lastIndexOf functions to find an instance of a particular character in
a string

 char message[8] = "Arduino"; //C type string


 int length = strlen(message);
// return the number of characters in the string
 strcpy(destination, source);
// copy string source to destination
 strcat(destination, source);
// append source string to the end of the destination string
 if(strcmp(str, "Arduino") == 0)
// do something if the variable str is equal to "Arduino"
Comparing C type Strings

char str1[ ] = "left";


char str2[ ] = "right";
if(strcmp(str1, str2) == 0)
Serial.print("strings are equal)

strcmp("left", "leftcenter") == 0)
// this will evaluate to false

strncmp("left", "leftcenter", 4) == 0)
// this will evaluate to true
String Object
 charAt(n) or [n] - Access a particular character of the String
 concat(parameter) or + - Appends the parameter to a String
 endsWith(string2) - Tests whether or not a String ends with string2
 equals(string2) or == - Compares two strings for equality (case sensitive)
 indexOf(val, [strt]) – locates val in a String by searching forward starting from strt
index. To search backward use lastIndexOf(val,[strt])
 length() - Returns the length of the String, in characters
 remove(index,[count]) – remove all characters (or count caracters if given) from a
String starting from index
 replace(substring1, substring2) – Replace all instances of substring1 in a String to
substring2
 setCharAt(index, c) - Sets a character to c at index of the String
 startsWith(string2) - Tests whether or not a String starts with the string2
 substring(from, [to]) - Get a substring of a String, from - inclusive, to – exclusive
 toInt() or toFloat() - Converts a valid String to an integer or float
 toLowerCase() or toUpperCase() - Get a lower-case or upper-case version of a String
 trim() - Get a version of the String with any leading and trailing whitespace removed
Mathematical Operators
Comparing Character and Numeric
Values
Logical and Bitwise operators

 Logical
operators

 Bitwise
operators
Combining Operations and
Assignment
Advanced Mathematical Operators
Other Useful Operators
Functions
type functionName(parameters)
{
statements;
}

Functions are declared by first declaring the function type. This is the type of
value to be returned by the function such as 'int' for an integer type function. If
no value is to be returned the function type would be void. After type, declare the
name given to the function and in parenthesis any parameters being passed to the
function.

int delayVal()
{
int v; // create temporary variable 'v'
v = analogRead(pot); // read potentiometer value
v /= 4; // converts 0-1023 to 0-255
return v; // return final value
}
Receiving Serial Data in Arduino
 Serial.available() - Get the number of bytes
(characters) available for reading from the serial
port.
 This is data that's already arrived and stored in the
serial receive buffer (which holds 64 bytes)
 Serial.read() - Reads incoming serial data
 Serial.readBytes(buffer, length) - reads characters
from the serial port into a buffer. The function
terminates if the determined length has been read,
or it times out
Controlling Arduino
int ledPin = 13; // choose a pin for LED
int val = 0; // variable to store the data received via Serial port
void setup() {
pinMode(ledPin,OUTPUT); // make ledPin an output
Serial.begin(9600); // initialize the Serial port
}
void loop () {
// Serial.available() – is a method to see whether something is
// received or not via Serial port without pausing the main program
if( Serial.available() ) {
val = Serial.read(); // read the value received via Serial port
if( val == 'H' ) { // if ‘H’, then blink
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
}
}
}

You might also like