1-Introduction To Arduino
1-Introduction To Arduino
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
11
code structure: setup function
12
code structure: loop function
loop function is
repeated indefinitely
13
code
pinMode(13, Output)
prepares pin 13 for
outputs of voltage
digitalWrite(13, HIGH)
sets pin 13 to a voltage that
means “on” (five volts in this case)
delay(1000);
tells microcontroller to do
nothing for 1000 ms = 1 s
digitalWrite(13, LOW)
sets pin 13 to voltage
that means “off” or zero volts
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)
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 :
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 “and”
–|| is “or”
Variables
Basic variable types:
Boolean
Integer
Character
Declaring Variables
Boolean: boolean variableName;
Declaring Variables
Boolean: boolean variableName;
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 ( ) { }
void loop ( ) { }
void loop ( ) { }
void loop ( ) { }
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
}
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