1 of 32

K-12 Microelectronic Workshop�Arduino Programming�(2)

1 struct instructor {

2 String name = “Yi Liu”;

3 String department = “Mechanical Engineering”;

4 String university = “University of Vermont”;

5 }

2 of 32

Contents

  • 2. First Arduino Program
    • 2.1 Arduino sketch structure
    • 2.2 Uploading
    • 2.3 Using delay — a blinking LED
    • 2.4 Debugging with Serial
    • 2.5 Summary

2

3 of 32

2.1 First Arduino program

1 void setup() {

2 // put your setup code here, to run once:

3 }

4

5 void loop() {

6 // put your main code here, to run repeatedly:

7 }

3

0_BasicStructure.ino

4 of 32

2.1 First Arduino program

1 void setup() {

2 // put your setup code here, to run once:

3 }

4

  • initializes configurations
    • sets pin modes
    • starts serial communications
  • only execute once

5 of 32

2.1 First Arduino program

5 void loop() {

6 // put your main code here, to run repeatedly:

7 }

5

  • called immediately after setup()
  • runs repeatedly

6 of 32

2.1 First Arduino program

  1. const int ledPin = 13;

2

3 void setup() {

4 pinMode(ledPin, OUTPUT);

5 }

6

7 void loop() {

8 digitalWrite(ledPin, HIGH);

9 }

6

1_LightUpLED.ino

7 of 32

2.1 First Arduino program

1 const int ledPin = 13;

7

  • declare constant integer value ledPin
  • assign 13 to ledPin

8 of 32

2.1 First Arduino program

8

4 pinMode(ledPin, OUTPUT);

  • set ledPin to OUTPUT mode

1_LightUpLED.ino

9 of 32

2.1 First Arduino program

pinMode(pin, mode);

9

  • pin: int, the pin number you want to configure

  • mode: the mode of operation of the pin.
    • INPUT: set the pin to input mode
    • OUTPUT: set the pin to output mode

  • returns: void

Syntax:

10 of 32

2.1 First Arduino program

10

8 digitalWrite(ledPin, HIGH);

  • send 5V (or 3.3V on 3.3V board) to ledPin

11 of 32

2.1 First Arduino program

11

  • pin: int, the pin number you want to write the value

  • mode: the mode of operation of the pin.
    • HIGH: set the pin to high voltage level
    • LOW: set the pin to low voltage level

  • returns: void

Syntax:

digitalWrite(pin, mode);

12 of 32

Contents

  • 2. First Arduino Program
    • 2.1 Arduino sketch structure
    • 2.2 Uploading
    • 2.3 Using delay — a blinking LED
    • 2.4 Debugging with Serial
    • 2.5 Summary

12

13 of 32

2.2 Uploading

  • select correct board
  • click verify
  • click upload

13

14 of 32

Contents

  • 2. First Arduino Program
    • 2.1 Arduino sketch structure
    • 2.2 Uploading
    • 2.3 Using delay — a blinking LED
    • 2.4 Debugging with Serial
    • 2.5 Summary

14

15 of 32

2.3 Using delay — a blinking LED

1 const int ledPin = 13;

2

3 void setup() {

4 pinMode(ledPin, OUTPUT);

5 }

6

7 void loop() {

8 digitalWrite(ledPin, HIGH);

9 delay(1000);

10

11 digitalWrite(ledPin, LOW);

12 delay(1000);

  1. }

15

2_BlinkLED.ino

16 of 32

2.3 Using delay — a blinking LED

9 delay(1000);

16

  • pauses the program for a specific amount of time
  • in milliseconds

17 of 32

2.3 Using delay — a blinking LED

delay(milliseconds);

17

  • milliseconds: int, time (in milliseconds) to pause
    • 1000 milliseconds = 1 second

  • returns: void

Syntax:

18 of 32

Contents

  • 2. First Arduino Program
    • 2.1 Arduino sketch structure
    • 2.2 Uploading
    • 2.3 Using delay — a blinking LED
    • 2.4 Debugging with Serial
    • 2.5 Summary

18

19 of 32

2.4 Debugging with Serial

  • Serial Monitor can display real-time textual data from Arduino board
  • is a good way to output
    • variables
    • parameters
    • states

19

20 of 32

2.4 Debugging with Serial

1 const int ledPin = 13;

2

3 void setup() {

4 pinMode(ledPin, OUTPUT);

5 Serial.begin(9600);

6 Serial.print(“Starting LED Blink on pin ”);

7 Serial.println(ledPin);

8 }

20

3_SerialPrint_BlinkLED.ino setup()

21 of 32

2.4 Debugging with Serial

5 Serial.begin(9600);

21

  • start serial communication between the Arduino board and the computer
  • sets the data rate in bits per second

22 of 32

2.4 Debugging with Serial

22

Serial.begin(speed);

  • speed: int, it refers to baud rate and defines how quickly data is sent and received between devices.
    • the common value is 9600
    • 115200 for large amounts of sensor data
    • more options can be found in Serial Monitor after the board is connected

  • returns: void

Syntax:

23 of 32

2.4 Debugging with Serial

6 Serial.print(“Starting LED Blink on pin ”);

7 Serial.println(ledPin);

23

  • send data to the Serial Monitor via serial

24 of 32

2.4 Debugging with Serial

Serial.print(value)

Serial.println(value)

24

  • value: can be any type of data
    • int
    • float
    • long
    • char
  • Serial.println() adds “\n” at the end
    • next printed data will appear on a new line

Syntax:

25 of 32

2.4 Debugging with Serial

10 void loop() {

11 digitalWrite(ledPin, HIGH);

12 Serial.println(“LED is ON”);

13 delay(1000);

14

15 digitalWrite(ledPin, LOW);

16 Serial.println(“LED is OFF”);

17 delay(1000);

18 }

25

3_SerialPrint_BlinkLED.ino loop()

26 of 32

Contents

  • 2. First Arduino Program
    • 2.1 Arduino sketch structure
    • 2.2 Uploading
    • 2.3 Using delay — a blinking LED
    • 2.4 Debugging with Serial
    • 2.5 Summary

26

27 of 32

2.5 Summary

pinMode(pin, mode)

digitalWrite(pin, value)

27

delay(milliseconds)

Serial.begin(speed)

Serial.print(value)

Serial.println(value)

digital I/O

time

communication

28 of 32

2.5 Summary

28

how to structure an Arduino script?

29 of 32

2.5 Summary

29

constant declarations

variable declarations

how to structure an Arduino sketch?

e.g. pins

30 of 32

2.5 Summary

30

constant declarations

variable declarations

void setup() {

initializations

}

how to structure an Arduino sketch?

e.g. pins

e.g. pin mode, serial

31 of 32

2.5 Summary

31

constant declarations

variable declarations

void setup() {

initializations

}

void loop() {

main logics

}

how to structure an Arduino sketch?

e.g. pins

e.g. pin mode, serial

32 of 32

K-12 Microelectronic Workshop�Arduino Programming

1 Serial.println(“Thank you!”);