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

Arduino Lecture2

arduino

Uploaded by

lovelivemaki905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Arduino Lecture2

arduino

Uploaded by

lovelivemaki905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

제 1 장 Introduction

Design Lab on Vibration and


Dynamic Systems

School of Mechanical Engineering


2024 Fall Semester

- SKKU ME
제 1 장 Introduction

Lecture 2

Serial Communication, Serial Monitor,


Processing

- SKKU ME
Contents
1. Arduino Serial Communication

2. Data print by Serial Communication

3. Arduino Communication

4. Handle Desktop Window


Objectives
• RS-232, USB, USART serial communication
• Arduino USB-serial communication
• print data by print() function
• Change data output format
• Check received data by echo
• Understand character(char)and integer(int) difference
• Control LED by serial data
• PC-Arduino Communication by Processing program
• Control Arduino by Processing program
Class Assignment

 Compose a circuit and a program that explained in Example 6-5


 Compose a circuit and a program that explained in Example 6-6
 How to show your work
 Make your own circuit and program
 Demonstration video(.mp4) that shows your circuit and program must be
uploaded on I-campus
 Due date is 09/29(Sun)
Required parts
01. Arduino serial 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.

 USART (Universal Synchronous/Asynchronous Receiver/Transmitter)


 Communication technology standard used as a single line data transmission.
 A typical communication port is USB.
01. Arduino serial communication

 RS-232 serial port


 Data transmission standard through serial communication
 It was originally used to connect terminal and modem
 The first Arduino board was equipped with an RS-232C serial port to connect
the Arduino to a computer using a 9-pin serial cable.
01. Arduino serial communication

 Communication between USB and Serial communication(RS-


232)
 They are not directly compatible with each other.

 So you can connect like as follows.


• Use I2C which communicate between USB and serial.
– Arduino Uno corresponds to this.

• Put USB-serial communication function in the microcontroller.


– Arduino Leonardo ATMega 32U4 MCU corresponds to this.
01. Arduino serial communication

 Arduino Uno serial communication


 Microcontrollers such as the ATMega 328p mounted on the Arduino Uno have a built-in
hardware serial port.
 This serial port is composed of transmitter (TX) and receiver (RX) with Arduino digital
input/output pins 0 and 1.

 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

 Arduino board using MCU with built-in USB function


 Some Arduino boards, including Leonardo, use a MCU that can communicate directly
via USB without going through a converter.
 If you use a MCU like this
• Product manufacturing parts are reduced.
• One programming step can also be reduced, reducing the cost of the
board.
• It is easier to use to emulate an Arduino to a USB device.
• There is no need to multiplex USB programmers
– The host computer and an external serial device can be connected and communicated simul-
taneously.
02. Data print by Serial Communication

 Data print by print() function


 The most basic serial communication function of Arduino is data value out-
put using Serial.println() function.
 To output data to the terminal, you need to use three functions:
• Serial.begin(baud rate)
– The Serial.begin() function is called only once in setup() at the beginning of the
program so that the serial port can prepare for communication.
• Serial.print(“Message string”)
• Serial.println(“Message string”)
– The Serial.println() function automatically adds a newline function at the end of the
output message.

void setup(){
Serial.begin(9600);
}

void loop(){
Serial.println(“Hello World.”);
}
02. Data print by Serial Communication

 Reading the variable resistance value and outputting in ter-


minal
 Let's make a program that reads the variable resistance value and outputs
the raw data (read value) and the value converted to percentage (%) to the
serial terminal.
02. Data print by Serial Communication

Example 6-1 Outputting variable resistance valute to serial


terminal
//Simple serial output program using variable resistor

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

 Using special characters


 In Arduino serial communication, special characters can be output by using a
backslash (\) and special character symbols together, and mainly used to
change the output format of the data to be displayed.
 There are many types of special characters, but the most common are line
breaks (\ n) and tabs (\ t).
 \ is a command to print special characters. If you want to print the character it-
self with a backslash like \n and \t, you must enter the backslash one more
time, like ‘\ \ n’ or ‘\ \ t’.

 Change the data representation format


 If you use Serial.print() and Serial.println() functions, you can print by specify-
ing the data type without adding additional code.
 The default setting is ASCII code of decimal or adding additional arguments,
you can specify various output data format in hexadecimal, octal, binary, etc.
02. Data print by Serial Communication

 Output data in tabular format using special characters


Example 6-2 Output data in tabular format using special
characters

//Outputting variable resistance values ​


in tabular form

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

 Output data in tabular format using special characters


Example 6-2 Output data in tabular format using special
characters
Serial.println("------------------------------------------");
for(int i = 0; i < 10; i++)
{
int val = analogRead(POT); //read variable resistance
int per = map(val, 0, 1023, 0, 100); //Convert variable resis-
tance value to percentage (%)
Serial.print("A0 \ t \ t");
Serial.print(val);
Serial.print(" \ t \ t");
Serial.print(per); //Output of values ​
converted to percentages
(%)
Serial.println("%"); //'%' Symbol output, line break
delay(1000); //Repeat code after waiting for 1 second
}
}
02. Data print by Serial Communication
03. Arduino Communication

 Check the data received on Arduino


 Arduino is observing the serial input buffer and must print all the characters
it receives.

 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

Exercise 6-3 Testing Arduino cereal echo

//Testing Arduino serial echo

char data; //Declare string variable data to store received characters

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.

 If PC sent number 5 to the Arduino, serial monitor shows number 5 as 53


• because character ‘5’ corresponds to 53 on ASCII code table

 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

Example 6-4 Controlling LED with a single character

//Controlling LED with a single character

const int LED = 9;


char data; //declare character variable ‘data’ to save received letter

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

Example 6-4 Controlling LED with a single character

//turn on LED if received 1


if(data == '1')
{
digitalWrite(LED, HIGH);
Serial.println("LED ON");
}
//turn off LED if received 0
else if(data == '0')
{
digitalWrite(LED, LOW);
Serial.println("LED OFF");
}
}
}
03. Arduino Communication – Controlling RGB LED
with multiple variables

 CSV(Comma Separated Value)


 CSV mean text data representing multiple fields separated by a comma (,)
 Can control multiple devices by send variables separated by comma
 To control RGB LED, send 8bit variables correspond to each 3 colors to Ar-
duino

 Send RGB data separated by comma to Arduino, then read


characters as integer by using Serial.parseInt() function
 Serial.parseInt() function can read data separated by comma received
through serial port
03. Arduino Communication – Controlling RGB LED
with multiple variables
03. Arduino Communication – Controlling RGB LED
with multiple variables

Example 6-5 Controlling RGB LED with multiple variables


//Sending multiple values at the same time

//declare RGB LED pin constant values


const int RED = 11;
const int GREEN = 10;
const int BLUE = 9;

//declare integer variables to save RGB values

int rval = 0;
int gval = 0;
int bval = 0;

void setup()
{
Serial.begin(9600); //start serial communication at baud rate 9600

//set LED pins to output


pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
03. Arduino Communication – Controlling RGB LED
with multiple variables

Example 6-5 Controlling RGB LED with multiple variables

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

 Controlling Processing with Arduino


04. Desktop Application Control

 Controlling Processing with Arduino


 Change color of the application window by sending variable resistance value
from desktop.
 Send variable resistance value through desktop serial port per 50ms.
• At higher speed, an error will occur because of serial buffer overflow.
04. Desktop Application Control

Example 6-6 Sending data to desktop (Arduino code)


//Sending variable resistance value to desktop

const int POT = 0; //declare POT constant using analog input pin 0

int val; //declare integer type variable to save variable resistance


value

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

 Controlling Processing with Arduino


 Program Processing processes serial data sent from Arduino.
 Fix the serial port ‘COM3’ of following code to real serial port that Arduino is
connected.
04. Desktop Application Control

Example 6-7 Changing window background color with the data


//Processing
from Arduinoprogram changing
(Processing window background color as variable resis-
code)
tance value changes

//import serial communication library and declare object


import processing.serial.*;
Serial port;

float brightness = 0; //declare real number variable ‘brightness’ to


save variable resistance value

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
}

void serialEvent(Serial port)


{
brightness = float(port.readStringUntil('\n')); //read and save
variable resistance value
}
04. Desktop Application Control

 Running Processing program


 Run program by clicking RUN button( ) on Processing IDE.
Picture 6-13 Changing color of the window as resistance value changes

analog value increasing →


 SudoGlove processing debugger
 Can check Processing GUI based debugger in the project driving RC car with
glove (www.sudoglove.com) by Jeremy Blum.
 Can download the source code of the debugger at www.jeremyblum.com .
04. Desktop Application Control – Controlling Arduino

 Controlling Arduino with Processing


 Make program changing the color of RGB LED connected to Arduino by click-
ing color pallete on desktop.
 Use hsv.jpg file as color pallete.
 Copy hsv.jpg file into ‘data’ folder of Processing to use in the program.
04. Desktop Application Control – Controlling Arduino

 Connecting RGB LED to Arduino


04. Desktop Application Control– Processing program

Example 6-8 Processing program set the color of RGB LED con-
nected to Arduino

//Processing program changing LED color to the color selected on color


pallete

import processing.serial.*; //import serial communication library


PImage img; //declare image object
Serial port; //declare serial communication object

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

 Running Processing program

You might also like