dpnm lab., postech 1/25 cs490k - internet of things (iot) jonghwan hyun dpnm lab. department of...

17
NM Lab., POSTECH 1/2 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH [email protected] Oct. 16, 2014 Introduction to Arduino #2

Upload: alexis-rosamond-eaton

Post on 25-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 1/25CS490K - Internet of Things (IoT)

Jonghwan Hyun

DPNM Lab.Department of Computer Science and Engineering, POSTECH

[email protected]

Oct. 16, 2014

Introduction to Arduino #2

Page 2: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 2/25CS490K - Internet of Things (IoT)

Installing Arduino What do we need?

(Optional)

Page 3: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 3/25CS490K - Internet of Things (IoT)

Installing Arduino1. Download & install the Arduino environment (IDE)

2. Connect the board to your computer via the USB cable

3. Install the drivers

4. Launch the Arduino IDE

5. Select your board

6. Select your serial port

7. Write codes

8. Upload the program

Reference: http://arduino.cc/en/Guide/HomePage

Page 4: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 4/25CS490K - Internet of Things (IoT)

Installing Arduino Download & install the Arduino environment (IDE)

Official homepage: http://arduino.cc/en/Main/Software

Link for faster download• http://server.noraki.net/Arduino/

No installer needed, except for Windows

Java needs to be installed

(Windows) Install drivers during IDE installation

(Linux) $ sudo apt-get install arduino

Prerequisite for LinuxAssumes running Ubuntu

$ sudo apt-get remove brltty

Page 5: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 5/25CS490K - Internet of Things (IoT)

Installing Arduino Connect the board to your computer via the USB cable

USB A type

USB B type

USB A type

USB B type

Page 6: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 6/25CS490K - Internet of Things (IoT)

Installing Arduino Connecting Arduino

MAC• Click “Network Preferences”, and in the window that appears, click “Apply”.

Linux• $ ls /dev/tty* • SHOULD have device name like ttyACM*, ttyUSB* or ttyS*

Windows

Page 7: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 7/25CS490K - Internet of Things (IoT)

Programming Arduino Launch the Arduino IDE

Page 8: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 8/25CS490K - Internet of Things (IoT)

Programming Arduino Select board

Page 9: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 9/25CS490K - Internet of Things (IoT)

Programming Arduino Select your serial port

(Windows) Device manager (Win + R “devmgmt.msc” <Enter>)

(MAC) tty.usbmodem

(Linux) /dev/tty/USB* or /dev/tty.USB*

Page 10: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 10/25CS490K - Internet of Things (IoT)

Programming Arduino Programming Reference

Page 11: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 11/25CS490K - Internet of Things (IoT)

Sample Program – LED Blink LED Blink

Blinking LED on Arduino board

CodesLoad the “Blink” example

• FileExamplesBasicsBlink

Page 12: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 12/25CS490K - Internet of Things (IoT)

Sample Program – LED Blink Verify & Compile

Once it compiles, you must see the following messages in the Sta-tus bar and the Program notification Area

Page 13: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 13/25CS490K - Internet of Things (IoT)

Sample Program – LED Blink Upload to the board

Once it upload, you must see the following messages in the Status bar and the Program notification Area

Page 14: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 14/25CS490K - Internet of Things (IoT)

Sample Program – Serial Communication

Serial communicationArduino uses serial ports to communicate with PC

WritingSerial.begin (baud)

• Initialize serial port for communication• Sets baud rate• Example

– Serial.begin(9600); // 9600 baud

Serial.print (val), Serial.print (val, fmt)• Prints data to the serial port• Examples

– Serial.print(“Hi”); // print a string– Serial.print(78); // works with numbers, too– Serial.print(variable); // works with variables– Serial.print(78,BIN); // will print 1001110

Serial.println(val)• Same as Serial.print(), but with line-feed

Page 15: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 15/25CS490K - Internet of Things (IoT)

Sample Program – Serial Communication

ReadingSerial.available()

• Returns the number of bytes available to be read, if any• Example

– if (Serial.available() > 0) data = Serial.read();

Reading data from serial port• letter = Serial.read()• letters = Serial.readBytesUntil(character, buffer, length)• number = Serial.parseInt()• number = Serial.parseFloat()

Serial MonitorSend / Receive serial data

Included in the IDE• Ctrl – Shift – M

Page 16: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 16/25CS490K - Internet of Things (IoT)

Sample Program – Serial Communication

EchoArduino will echo anything you send to it until you send , (a comma)

Source codes

/* Use a variable called byteRead to temporarily store the data coming from the PC */byte byteRead;

void setup() { // Turn the Serial Protocol ON Serial.begin(9600);}void loop() { /* check if data has been sent from the computer: */ if (Serial.available()) { /* read the most recent byte */ byteRead = Serial.read();

/*Listen for a comma which equals byte code # 44 */ if(byteRead==44){ Serial.println(); }else{ /*ECHO the value that was read, back to the serial port. */ Serial.write(byteRead); } }}

Page 17: DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH noraki@postech.ac.kr

DPNM Lab., POSTECH 17/25CS490K - Internet of Things (IoT)

Announcement Assignment #6 - Programming on Arduino

Implement a simple calculator on Arduino board

Serial communication interface can be used to communicate with the board

Try to figure out the maximum value of integer that the Arduino can handle

Hand in your source code and running screenshots

http://dpnm.postech.ac.kr/cs490/asn6