Arduino Lecture2
Arduino Lecture2
- SKKU ME
제 1 장 Introduction
Lecture 2
- SKKU ME
Contents
1. Arduino Serial Communication
3. Arduino Communication
Serial port
It is a single-line data transmission technology and consists of one reception
strand and one transmission.
The most used USB(Universal Serial Bus) consists of a total of four strands,
one transmission and one reception and two 5V power supplying strands.
Since the Arduino's built-in hardware serial port is not compatible with USB, a separate
MCU is used as the USB-Serial converter.
If it has separate MCU built-in as a USB-Serial converter, a driver is required to connect
to the computer, and when the Arduino IDE is installed, the driver is automatically in-
stalled.
01. Arduino serial communication
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.println(“Hello World.”);
}
02. Data print by Serial Communication
const int POT = 0; //POT constant definition using analog input pin 0
void setup()
{
Serial.begin(9600); //9600 rate communication start
}
void loop()
{
int val = analogRead(POT); // Read variable resister
int per = map(val, 0, 1023, 0, 100); //convert variable resister
into percentage
Serial.print("Analog Reading: ");
Serial.print(val); //output variable resister in serial
Serial.print("Percentage: ");
Serial.print(per); //percentage value output
Serial.println("%"); //'%’ print, newline
delay(1000); //1sec delay and repeat
}
02. Data print by Serial Communication
const int POT = 0; //POT constant definition using analog input pin 0
void setup()
{
Serial.begin(9600); //9600 rate Start serial communication
}
void loop()
{
Serial.println(" \ nAnalog Pin \ tRaw Value \ tPercentage");
02. Data print by Serial Communication
The Serial object uses the following two functions to process the echo.
• Serial.available() function
– Returns the next character of the data in the buffer, one byte at a time.
– Returns the number of data currently stored in the Arduino serial port receive buf-
fer. If the number of data is more than 0, it means that the data remains in the Se-
rial.read() function.
• Serial.read() function can be read by 1 byte and should be used only when
the return value of Serial.available() function is greater than 0.
03. Arduino Communication
void setup()
{
Serial.begin(9600); //9600 rate serial communication start
}
void loop()
{
//Output only when data is received
if(Serial.available() > 0)
{
data = Serial.read(); //Save incoming text
Serial.print(data); //Serial output of received text
}
}
03. Arduino Communication
ASCII code
The ASCII code is a 7-bit set containing a total of 128 characters and com-
mands.
When sending alphanumeric characters to a serial monitor, it is not sending
the characters themselves, but sending bytes to the computer to interpret
them.
03. Arduino Communication – Differences between
character type(char) and integer type(int)
Number representation
Processes data sent from PC as character type
• Even you enter numbers on serial monitor(PC), the Arduino reads the value
as character type.
Can get integer easily with a function parseInt() provided by Arduino IDE
• Int numFromChar = Serial.parseInt();
03. Arduino Communication – Controlling LED with a
single character
03. Arduino Communication – Controlling LED with a
single character
void setup()
{
Serial.begin(9600); //start serial communication at baud rate 9600
pinMode(LED, OUTPUT);
}
void loop()
{
//run when received data
if(Serial.available() > 0)
{
data = Serial.read();
03. Arduino Communication – Controlling LED with a
single character
int rval = 0;
int gval = 0;
int bval = 0;
void setup()
{
Serial.begin(9600); //start serial communication at baud rate 9600
void loop()
{
// run when received data
while(Serial.available() > 0)
{
rval = Serial.parseInt(); //read the first integer value
gval = Serial.parseInt(); //read the second integer value
bval = Serial.parseInt(); //read the third integer value
if(Serial.read() == '\n') //finished sending
{
//set RGB values to each LED colors
analogWrite(RED, rval);
analogWrite(GREEN, gval);
analogWrite(BLUE, bval);
}
}
}
04. Desktop Application Control – Processing
Processing
Processing is an open-source programming language developed to teach ba-
sic computer programming based with graphics.
Arduino IDE was developed based on Processing.
Can program GUI applications communicating with Arduino using Processing.
Can make programs to control Arduino using programming languages includ-
ing serial communication libraries such as Python, PHP, Visual Basic, C.
04. Desktop Application Control – Processing
Installing Processing
Visit Processing.org/download and download compressed file correspond to
OS
IDE program(similar with Arduino IDE) will be executed by double-clicking
processing.exe file.
04. Desktop Application Control
const int POT = 0; //declare POT constant using analog input pin 0
void setup()
{
Serial.begin(9600); //start serial communication
}
void loop()
{
val = map(analogRead(POT), 0, 1023, 0, 255); //read variable resis-
tance value and map data
Serial.println(val); //print variable resistance value
delay(50); //wait 50ms to avoid buffer overflow
}
04. Desktop Application Control
void setup()
{
size(500, 500); //set window size
port = new Serial(this, "COM3", 9600); //start serial communication
port.bufferUntil('\n'); //save received characters until Newline
}
void draw()
{
background(0, 0, brightness); //change window background color
}
Example 6-8 Processing program set the color of RGB LED con-
nected to Arduino
void setup()
{
size(640, 256); //set window size
img = loadImage("hsv.jpg"); //load image to use as background
port = new Serial(this, "COM9", 9600); //start serial communication
}
04. Desktop Application Control– Processing program
Example 6-8 Processing program set the color of RGB LED con-
nected to Arduino
void draw()
{
background(0); //set background color as black
image(img, 0, 0); //set program background image
}
void mousePressed()
{
color c = get(mouseX, mouseY); //find color value of the point that
mouse clicked
String colors = int(red(c))+","+int(green(c))+","+int(blue(c))+"\n";
//get selected RGB value
print(colors); //print RGB value for debugging
port.write(colors); //send RGB value to Arduino
}
04. Desktop Application Control– Processing program