What is the Arduino?
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
They can also look like this...
Arduino MEGA
Arduino Pro Mini Arduino Lilypad
What can an Arduino do?
Respond to input froma person or the
environment.
Automate tasks.
What can’t an Arduino do?
Replace a desktopcomputer
Arduino & Power
Operates at 5V DC.
Power from USB, power supply, or battery.
Can only provide 40mA of current to a circuit.
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
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.
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.
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.
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
Sketch Structure
void setup(){ }
This code is only run once, when the Arduino is powered on, reset, or uploaded to.
void loop() { }
- This code is looped, line by line, from top to bottom.
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)
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);
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 ...
}
;
Use a semicolon at the end of each instruction in a code block to tell the Arduino to execute it.
void loop(){function(100);
}
;
Use a semicolon at the end of each instruction in a code block to tell the Arduino to execute it.
// 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!
Well Commented
Using comments like this will make your code easier to read… BY YOU!
Digital Output
Something from the Arduino to us.
Digital Output has only 2 states: HIGH or LOW
HIGH = on
LOW = off
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.
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
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.
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()
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.
Setting up the LED pin
void setup(){// code will only run when the Arduino
startspinMode(13, OUTPUT); // set pin 13 as digital output
}
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
}
Connecting the LED
Remember to check the direction of the LED and use a resistor!
(Connected to Pin 12)