Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
31 views

1-Introduction To Arduino

The document provides information about Arduino boards, software, and code structure. It discusses the Arduino UNO board, Arduino IDE software, code structure including the setup() and loop() functions, and basic code examples using functions like pinMode(), digitalWrite(), and delay(). It also covers uploading code to the Arduino board and running a simple blink example.

Uploaded by

sebas.lopez1202
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

1-Introduction To Arduino

The document provides information about Arduino boards, software, and code structure. It discusses the Arduino UNO board, Arduino IDE software, code structure including the setup() and loop() functions, and basic code examples using functions like pinMode(), digitalWrite(), and delay(). It also covers uploading code to the Arduino board and running a simple blink example.

Uploaded by

sebas.lopez1202
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 82

these notes borrow from . . .

– Arduino web site


• http://arduino.cc/en/Guide/Environment
• http://arduino.cc/en/Tutorial/HomePage

– Adafruit tutorial #1 and 2


• http://www.ladyada.net/learn/arduino/lesson2.html

– Leah Buechley’s Introduction to Arduino


• http://web.media.mit.edu/~leah/LilyPad/03_arduino_intro.html

1
2
3
4
Arduino UNO Board with USB Connectivity:
Arduino Mega 2560 R3
Ether Mega 2560 R3
Arduino Mega 1280
Arduino Software: Sketcher
Arduino IDE
IDE = Integrated
Development
Environment

http://www.arduino.cc/en/Guide/Environment

10
code structure: header

header provides information and


can also contain code

11
code structure: setup function

setup function is executed


only once at the start

12
code structure: loop function

loop function is
repeated indefinitely

13
code

pinMode(13, Output)
prepares pin 13 for
outputs of voltage

digital I/O functions:


• pinMode
• digitalWrite
• digitalRead
14
code

digitalWrite(13, HIGH)
sets pin 13 to a voltage that
means “on” (five volts in this case)

digital I/O functions:


• pinMode
• digitalWrite
• digitalRead
15
code

delay(1000);
tells microcontroller to do
nothing for 1000 ms = 1 s

digital I/O functions:


• pinMode
• digitalWrite
• digitalRead
16
code

digitalWrite(13, LOW)
sets pin 13 to voltage
that means “off” or zero volts

digital I/O functions:


• pinMode
• digitalWrite
• digitalRead
17
Arduino Hardware
The Arduino board is where the code you write is executed.
The board can only control and respond to electricity, so specific
components are attached to it to enable it to interact with the real
world.
These components can be sensors, which convert some aspect of the
physical world to electricity so that the board can sense it, or actuators,
which get electricity from the board and convert it into something that
changes the world.
Examples of sensors include switches, accelerometers, and ultrasound
distance sensors. Actuators are things like lights and LEDs, speakers,
motors, and displays.
There are a variety of official boards that you can use with Arduino
software and a wide range of Arduino-compatible boards produced by
members of the community.
The most popular boards contain a USB connector that is used to
provide power and connectivity for uploading your software onto the
board. Figure 1-1 shows a basic board that most people start with, the
Arduino Uno.
Arduino Hardware
The Arduino Uno has a second microcontroller onboard to handle all USB communication; the small surface-
mount chip (the ATmega8U2) is located near the USB socket on the board.

The Arduino-compatible Teensy and Teensy+ boards from PJRC (http://www.pjrc.com/teensy/) are also capable
of emulating USB devices.

Older boards, and most of the Arduino-compatible boards, use a chip from the FTDI company that provides a
hardware USB solution for connection to the serial port of your computer.
Binary sketch size:
1026 bytes (of a 32256 byte
maximum)

Maximum Size: 32256 byte


Arduino Software:
Software programs, called sketches, are created on a computer using the
Arduino integrated development environment (IDE).

The IDE enables you to write and edit code and convert this code into
instructions that Arduino hardware understands.

The IDE also transfers those instructions to the Arduino board (a process
called uploading

The Arduino software for Windows, Mac, and Linux can be downloaded from
http://arduino.cc/en/Main/Software.

The Windows download is a ZIP file. Unzip the file to any convenient
directory—Program Files/Arduino is a sensible place.
Uploading and Running the Sketch

Transfer your compiled sketch to the Arduino board and see it working.
Connect your Arduino board to your computer using the USB cable. Load the Blink sketch
into the IDE.

Next, select Tools → Board from the drop-down menu and select the name of the board
you have connected (if it is the standard Uno board, it is probably the first entry in the
board list).

Now select Tools → Serial Port. You will get a drop-down list of available serial ports on
your computer. Each machine will have a different combination of serial ports, depending
on what other devices you have used with your computer.

On Windows, they will be listed as numbered COM entries. If there is only one entry,
select it. If there are multiple entries, your board will probably be the last entry.
Creating and Saving a Blink LED Sketch
To open an editor window ready for a new sketch, launch the, go to the File
menu, and select New. Paste the following code into the Sketch Editor window
(it’s similar to the Blink sketch, but the blinks last twice as long):
Program in C :

const int ledPin = 13; // LED connected to digital pin 13


void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(2000); // wait for two seconds
digitalWrite(ledPin, LOW); // set the LED off
delay(2000); // wait for two seconds
}
To better understand procedures, lets use an analogy to the
kinds of procedures we're used to

clean cat wash the cat (dirty cat) // a procedure for washing the cat
{ turn on the shower. find the cat.
grab the cat.
put cat under shower.
wait 3 minutes. // wait for cat to get clean.
release cat. }
The Arduino Environment
Board Type
Serial Port / COM Port
The Environment
Parts of the Sketch
Comments
• Comments can be anywhere
Comments
• Comments can be anywhere
• Comments created with // or /*
and */
Comments
• Comments can be anywhere
• Comments created with // or /*
and */
• Comments do not affect code
Comments
• Comments can be anywhere
• Comments created with // or /*
and */
• Comments do not affect code
• You may not need comments, but
think about the community!
Operators
The equals sign

= is used to assign a value

== is used to compare values


Operators
–And & Or

–&& is “and”

–|| is “or”
Variables
Basic variable types:

Boolean
Integer
Character
Declaring Variables
Boolean: boolean variableName;
Declaring Variables
Boolean: boolean variableName;

Integer: int variableName;


Declaring Variables
Boolean: boolean variableName;

Integer: int variableName;

Character: char variableName;


Declaring Variables
Boolean: boolean variableName;

Integer: int variableName;

Character: char variableName;


String: stringName [ ];
Assigning Variables
Boolean: variableName = true;
or variableName = false;
Assigning Variables
Boolean: variableName = true;
or variableName = false;
Integer: variableName = 32767;
or variableName = -32768;
Assigning Variables
–Boolean: variableName = true;
–or variableName = false;
–Integer: variableName = 32767;
–or variableName = -32768;
–Character: variableName = ‘A’;
–or stringName = “SparkFun”;
Variable Scope
Where you declare your variables matters
Setup
void setup ( ) { }

The setup function comes before


the loop function and is necessary
for all Arduino sketches
Setup
void setup ( ) { }

The setup header will never change,


everything else that occurs in setup
happens inside the curly brackets
Setup
void setup ( ) {
pinMode (13, OUTPUT); }

Outputs are declare in setup, this is


done by using the pinMode function
This particular example declares digital pin # 13 as an output,
remember to use CAPS
Setup
void setup ( ) { Serial.begin;}

Serial communication also begins in


setup
This particular example declares Serial communication at a
baud rate of 9600. More on Serial later...
Setup, Internal Pullup Resistors
void setup ( ) {
digitalWrite (12, HIGH); }

You can also create internal pullup resistors in setup, to do so


digitalWrite the pin HIGH
This takes the place of the pullup resistors currently on your
circuit 7 buttons
Setup, Interrupts
void setup ( ) {
attachInterrupt (interrupt, function, mode) }

You can designate an interrupt


function to Arduino pins # 2 and 3

This is a way around the linear


processing of Arduino
Setup, Interrupts
void setup ( ) {
attachInterrupt (interrupt, function, mode) }
Interrupt: the number of the interrupt, 0 or 1,
corresponding to Arduino pins # 2 and 3
respectively
Function: the function to call when the
interrupt occurs
Mode: defines when the interrupt should be
triggered
Setup, Interrupts
void setup ( ) {
attachInterrupt (interrupt, function, mode) }

•LOW whenever pin state is low


•CHANGE whenever pin changes value
•RISING whenever pin goes from low to
high
•FALLING whenever pin goes from low to
high
–Don’t forget to CAPITALIZE
Returns Example
none
int pin = 13;
volatile int state = LOW;
void setup()
{
Also See:
http://www.arduino.cc/en/Reference/Interrupts
pinMode(pin, OUTPUT);
attachInterrupt(0, blink, CHANGE); noInterrupts()
}
attachInterrupt()
void loop()
{ detachInterrupt()
digitalWrite(pin, state);
}
void blink()
{
state = !state;
}
Using Interrupts:
Interrupts are useful for making things happen automatically in microcontroller programs,
and can help solve timing problems.
A good task for using an interrupt might be reading a rotary encoder, monitoring user
input.
If you wanted to insure that a program always caught the pulses from a rotary encoder,
never missing a pulse, it would make it very tricky to write a program to do anything else,
because the program would need to constantly poll the sensor lines for the encoder, in
order to catch pulses when they occurred.

Other sensors have a similar interface dynamic too, such as trying to read a sound sensor
that is trying to catch a click, or an infrared slot sensor (photo-interrupter) trying to catch
a coin drop.

In all of these situations, using an interrupt can free the microcontroller to get some other
work done while not missing the doorbell.
If Statements
if ( this is true ) { do this; }
If
if ( this is true ) { do this; }
Conditional
if ( this is true ) { do this; }
Action
if ( this is true ) { do this; }
Else
else { do this; }
Basic Repetition

• loop

• For

• while
Basic Repetition

void loop ( ) { }
Basic Repetition

void loop ( ) { }
Basic Repetition

void loop ( ) { }

The “void” in the header is what the


function will return (or spit out)
when it happens, in this case it
returns nothing so it is void
Basic Repetition

void loop ( ) { }

The “loop” in the header is what the function


is called, sometimes you make the name up,
sometimes (like loop) the function already
has a name
Basic Repetition

void loop ( ) { }

The “( )” in the header is where you declare


any variables that you are “passing” (or
sending) the function, the loop function is
never “passed” any variables
Basic Repetition

void loop ( ) { }
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
//this could be anything
}
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
}
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
}
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
}
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
}
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
}
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
}
Basic Repetition

while ( count<10 )
{
//while action code goes here
}
Basic Repetition

while ( count<10 )
{
//while action code goes here
//should include a way to change count
//variable so the computer is not stuck
//inside the while loop forever
}
Basic Repetition

while ( count<10 )
{
//looks basically like a “for” loop
//except the variable is declared before
//and incremented inside the while
//loop
}
Basic Repetition
Or maybe:

while ( digitalRead(buttonPin)==1 )
{
//instead of changing a variable
//you just read a pin so the computer
//exits when you press a button
//or a sensor is tripped
}
Serial Communication:
Used for communication between the Arduino board and a computer or other devices.
Serial Functions: http://arduino.cc/en/Reference/Serial

if (Serial) available() parseInt() peek()


begin() end() print() println()
find() findUntil() read() readBytes()
flush() parseFloat() readBytesUntil() setTimeout()
serialEvent() write()
Example:
int incomingByte = 0; // for incoming serial data
Start serial Port communication
void setup()
{

}
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps Availability of data at serial port
void loop() {
Read Serial Port and store data to
// send data only when you receive data: variable call incomingByte
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read(); Print data to the serial port
// say what you got:
Serial.print("I received: ");
Prints data to the serial port as
Serial.println(incomingByte, DEC); human-readable ASCII text followed
}
}
by carriage return character
Arduino library
http://arduino.cc/en/Tutorial/HomePage
Stepper Motor Library:
Stepper myStepper = Stepper(steps, pin1, pin2, pin3, pin4)
stepper.setSpeed()
stepper.step()
/ * MotorKnob
* A stepper motor follows the turns of a potentiometer (or other sensor) on analog input 0.
* http://www.arduino.cc/en/Reference/Stepper
* This example code is in the public domain.
/

#include <Stepper.h>
#define STEPS 100 // change this to the number of steps on your motor
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 8, 9, 10, 11); // create an instance of the stepper class, specifying

// the previous reading from the analog input


int previous = 0;
void setup()
{
// set the speed of the motor to 30 RPMs
stepper.setSpeed(30);
}
void loop()
{
// get the sensor value
int val = analogRead(0);
// move a number of steps equal to the change in the
// sensor reading
stepper.step(val - previous);

// remember the previous value of the sensor


previous = val;
}
Questions ?

You might also like