What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type...

29
What is the Arduino?

Transcript of What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type...

Page 1: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

What is the Arduino?

Page 2: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Anatomy

USBPower Supply

Power Out & GND Pins

Analog Input Pins Digital Input/Output Pins

Communication Pins

Reset Button

Built in LED with Resistor (Pin 13)

*Older revisions may have some parts located in different places

Page 3: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

They can also look like this...

Arduino MEGA

Arduino Pro Mini Arduino Lilypad

Page 4: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

What can an Arduino do?

Respond to input froma person or the

environment.

Automate tasks.

What can’t an Arduino do?

Replace a desktopcomputer

Page 5: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Arduino & Power

Operates at 5V DC.

Power from USB, power supply, or battery.

Can only provide 40mA of current to a circuit.

Page 6: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Power Adapters

This power adapter is…

- 9V DC from 120V AC

- Provides up to 200 mA

- “Center Positive” plug

For the Arduino you want...

- 9 to 12V DC

- At least 250mA

- 2.1mm Center Positive plug

Page 7: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

The Arduino IDE

Verify

Upload New Open Save

Serial Monitor

Board Type Serial Port

The software used to program the Arduino is called the Arduino IDE (Integrated Development Environment).

From the IDE, we can write instructions for the Arduino called “code” and upload it to the device to interpret.

Page 8: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

VerifyUsed to check for errors when compiling a sketch to machine-readable code.

UploadAfter compiling the sketch, it sends it to the Arduino over USB.

New, Open, SaveCreate new sketch. Open an

existing sketch.

Save a sketch to your sketchbook folder (in Documents/Arduino)

Serial MonitorCan be used to read/send messages from/to the Arduino.

Page 9: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Sketches

The list of instructions (“code”) written in the IDE is

called a “sketch”.

A sketch will be interpreted, line by line, in order by the

Arduino.

Before the Arduino can understand the sketch, it has to

be compiled into a language the Arduino can

understand.

Page 10: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Saving Sketches

Blink.ino

Sketches saved from the current version of the IDE are

saved with an .ino file extension

Blink.pde

Sketches saved from the older version of the IDE are

saved with an .pde file extension

Page 11: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Sketch Structure

Page 12: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

void setup(){ }

This code is only run once, when the Arduino is powered on, reset, or uploaded to.

Page 13: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

void loop() { }

- This code is looped, line by line, from top to bottom.

Page 14: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Code Blocks: { }Any instructions we want to give the Arduino need to be within the

setup or loop code blocks of our sketch.

A code block starts with a…

{

(opening curly bracket)

and ends with a...

}

(closing curly bracket)

Page 15: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Functions

Blocks of code like setup() or loop() are called “functions”.

Functions allows us to easily reuse blocks of code by calling its

name, like this:

name(optional arguments);

Page 16: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Functions

Meanwhile, elsewhere…void digitalWrite(int pin, int state){

... some code ...}

void pinMode(int pin, int mode){... some code ...

}

void delay(int time){... some code ...

}

Page 17: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

;

Use a semicolon at the end of each instruction in a code block to tell the Arduino to execute it.

void loop(){function(100);

}

Page 18: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

;

Use a semicolon at the end of each instruction in a code block to tell the Arduino to execute it.

Page 19: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

// This is a comment

It is possible to write notes which will be ignored by the Arduino.

Anything following a // will be ignored when the sketch is compiled and uploaded to the

Arduino.

You can also write multi-line comments like this:

/*This is amulti-line comment*/

It’s a good idea to use comments to keep track of you are doing!

Page 20: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Well Commented

Using comments like this will make your code easier to read… BY YOU!

Page 21: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Digital Output

Something from the Arduino to us.

Digital Output has only 2 states: HIGH or LOW

HIGH = on

LOW = off

Page 22: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Uploading Code

(1) Make sure your Arduino is plugged in and that the board type/serial port match what you

are working with.

(2) Verify the sketch to check for problems. (optional)

(3) Upload to the Arduino.

RXTX LEDs will flash as the code is being uploaded. When they are done, your code should begin running.

Page 23: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Errors?Sometimes you will see an error at the bottom of the Arduino window like this:

Here are some common sources of errors:

- Syntax errors (look for missing semicolons/curly brackets/parenthesis,

misspellings, or wrong capitalization)

- Arduino not plugged in- Wrong serial port selected

Page 24: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Capitalization Matters

pinmode(13, OUTPUT);

is not the same as

pinMode(13, OUTPUT);

or

Pinmode(13, OUTPUT);

Be careful to follow the EXACT capitalization of function names.

Page 25: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Digital Output Functions

pinMode(pin number, OUTPUT);

- Sets the selected digital pin as either INPUT or OUTPUT

- This should be called within setup()

- Do this for each pin you will use as digital outputdigitalWrite(pin number, HIGH or LOW);

- Sets the selected digital pin either HIGH (on) or LOW(off)

- This should be called within loop()

Page 26: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Another Useful Function

delay(amount of time in milliseconds);

- Pauses the sketch at the point it was called for a selected amount of time.

- 1000 milliseconds equals 1 second

- Use after digitalWrite to maintain a HIGH or LOW state for a period of time.

Page 27: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Setting up the LED pin

void setup(){// code will only run when the Arduino

startspinMode(13, OUTPUT); // set pin 13 as digital output

}

Page 28: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Blinking LED pin

void loop(){

digitalWrite(13, HIGH); //turn on LED delay(1000); // wait 1 second (LED is still on) digitalWrite(13, LOW); // turn off LED delay(1000); // wait 1 second (LED is still off)// start loop() over again

}

Page 29: What is the Arduino? … · The Arduino IDE Verify Upload New Open Sav e Serial Monitor Board Type Serial Port The software used to program the Arduino is called the Arduino IDE (Integrated

Connecting the LED

Remember to check the direction of the LED and use a resistor!

(Connected to Pin 12)