Sei sulla pagina 1di 82

Working with Arduino:

Lesson #1: Getting Acquainted with the Kit

JorgeCute F. Resurreccion
What is Arduino?
Arduino is a popular “open source” single board
microcontroller. It is designed to make the
process of using electronics in
multidisciplinary projects more accessible.
This idea began in Italy and its initial
purpose was to make STUDENT design
projects more affordable than other
prototyping projects at the time.
Arduino Board
◼ “Strong Friend” Created in Ivrea, Italy
◼ in 2005 by Massimo Banzi & David Cuartielles

◼ Open Source Hardware

◼ Processor
◼ Coding is accessible & transferrable → (C++, Processing, java)
Arduino…

◼ is the go-to gear for artists, hobbyists,


students, and anyone with a gadgetry dream.

◼ rose out of another formidable challenge:


how to teach students to create electronics,
fast.

http://spectrum.ieee.org/geek-life/hands-on/the-making-of-arduino
PWR IN USB
(to Computer)

RESET

SCL\SDA
(I2C Bus)

POWER
5V / 3.3V / GND
Digital I\O
PWM(3, 5, 6, 9, 10, 11)

Analog
INPUTS
PWR IN USB
(to Computer)

RESET

SCL\SDA
(I2C Bus)

POWER
5V / 3.3V / GND
Digital I\O
PWM(3, 5, 6, 9, 10, 11)

Analog
INPUTS
Go ahead and plug your board in!
Arduino Shields
PCB Built Shield Inserted Shield
Arduino Shields

Micro SD MP3 Trigger LCD


SIK Components
Name Image Type Function Notes
Push Button Digital Input Switch - Closes Polarized, needs
or opens circuit resistor
Trim Analog Input Variable resistor Also called a
Trimpot.
potentiometer
Photoresistor Analog Input Light Dependent Resistance varies
Resistor (LDR) with light.
Relay Digital Output Switch driven by Used to control
a small signal larger voltages
Temp Sensor Analog Input Temp Dependent
Resistor
Flex Sensor Analog Input Variable resistor

Soft Trimpot Analog Input Variable resistor Careful of shorts

RGB LED Dig & Analog 16,777,216 Ooh... So pretty.


Output different colors
SIK Components
SIK Components
SIK Components
Arduino & Arduino Compatible Boards
Fritzing View of Breadboard Circuit

◼What happens
when you break
the circuit?
◼What if you
wanted to add
more than one
LED?
Arduino Software
The Arduino programming
platform was designed in
JAVA to help newcomers
become familiar with
programming. The language
used to write code is C/C++
and only uses TWO functions
to make a functionable
program.
Adding control – let’s use the Arduino
and start programming!!!
Concepts: INPUT vs. OUTPUT
Referenced from the perspective of the microcontroller (electrical board).

Inputs is a signal / information Output is any signal exiting the


going into the board. board.

Almost all systems that use physical computing will have some form of
output

What are some examples of Outputs?


Concepts: INPUT vs. OUTPUT
Referenced from the perspective of the microcontroller (electrical board).

Inputs is a signal / information Output is any signal exiting the


going into the board. board.

Examples: Buttons Switches, Examples: LEDs, DC motor,


Light Sensors, Flex Sensors, servo motor, a piezo buzzer,
Humidity Sensors, Temperature relay, an RGB LED
Sensors…
Concepts: Analog vs. Digital
◼Microcontrollers are digital devices – ON or OFF.
Also called – discrete.

◼analog signals are anything that can be a full


range of values. What are some examples? More
on this later…

5V 5V

0V 0V
Arduino Integrated Development Environment (IDE)
Part of the Sketch

Two required functions /


methods / routines:

void setup()
{
// runs once
}

void loop()
{
// repeats
error & status messages }
Settings: Tools → Serial Port

◼Your computer
communicates to the Arduino
microcontroller via a serial
port → through a USB-Serial
adapter.

◼Check to make sure that the


drivers are properly installed.
Settings: Tools → Board

◼Next,double-check that the proper board is selected under the


Tools→Board menu.
digitalWrite()
BIG 6 CONCEPTS
analogWrite()

digitalRead()

if() statements / Boolean

analogRead()

Serial communication

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 United States License.
Programming - Routines
Each Arduino program is called a SKETCH and
has two required functions, called
ROUTINES.

void setup ( ) { } - All of the code within the curly braces will be
run ONCE when the program first runs.

void loop ( ) { } - This function is run AFTER setup has finished.


All of the code within the curly braces will be run again, and again, until the
power is removed.
Programming - Syntax

// - Single line comment


/* */ - Multiline comment
{ } – used to define a block of code that starts and ends.
; - used to define the end of a line of code.
Programming - Variables
Again the variables used are similar to ROBOTC, with a few
exceptions.
int (integer) – this stores a number in 2 bytes(16 bits) and has no decimal
places. The value must be between -32,768 and 32,768.

long(long) – Used when the integer is NOT large enough. This takes 4
bytes(32 bits) of RAM and has a range of -2,147,483,648 and
2,147,483,648.

boolean(boolean) – A simple true and false variable. It is useful because it


only takes up 1 bit of RAM.

float (float) – Used for floating decimals. It takes 4 bytes of RAM and has
a range of -3.4028235E+38 and 3.4028235E+38

char(character) – Stores one character using ASCII code (“A” = 65). Uses
1 byte of RAM
Programming – Math Operators
These are used for manipulating numbers.

= (assignment) makes something equal to something else.


For example, x = 10*2, thus x = 20.

% (modulo) – this gives the remainder when one number is


divided by another. For example 12 % 10 gives 2.

+ (addition)
- (subtraction)
* (multiplication)
/ (division)
Comparison Operators
These are used to make logical comparisons.

== (equal to) - For example 12==10 is FALSE and 12


==12 is TRUE.

!= (not equal to) - For example 12!=10 is TRUE and


12!=12 is FALSE.

< (less than)


> (greater than)
Programming – Control Structures
These execute code based on CONDITIONS.
Here are just a few.
if(condition) { }
else if (condition) { }
else(condition) { }

This will execute the code between the curly braces if the
condition is true, and if not test the condition of the “else if”. If that
is false , the “else” code will execute.

for (int i =0; i < #repeats; i ++) { }


Used when you would like to repeat a line of code a specific # of times.
Often called a FOR LOOP.
Programming - Digital

pinMode (pin, mode) ; - Used to address the pin # on


the Arduino board you would like to use 0-19. The mode
can either be INPUT or OUTPUT.

digitalWrite (pin, value); - Once a pin is set to output


it can be set to either HIGH (5 Volts) or LOW(0 volts).
This basically means turn ON and OFF.

Note: There are ways to use the board as analog. Those will be explained later.
Lesson #1 – Blinking LED
What will you need? Arduino, breadboard, 4
wires, 10mm LED(large white), 560W resistor,
USB cable.

Longer Lead is POSITIVE


LEDs
An LED (light emitting diode) emits light when a
small current passes through it. You can
identify it as it looks like a small bulb

It only works in ONE


direction. So make sure you
attach it correctly. Also it
often needs a resistor to
limit the current going to it.

Schematic symbol
Resistors
Resistors restrict the amount of electrical
current that can flow through a circuit. The
color bands indicate the VALUE of the
resistor
Note: it is easy to grab the
WRONG one so be careful.
Also, it does not matter which
way the resistor is wired.

Schematic symbol
The schematic This is basically a SERIES circuit
where the resistor and LED are wired
one after another.

1. Run a red wire from the 5V on the


Arduino to the red strip on the BB.
2. Run a black wire from the
GROUND(GND) on the Arduino to
the blue strip on the BB.
3. Place the LED on H 22 and 21 with
the longer lead(+) of the LED in
H22.
4. Place a resistor on I21 and I11.
Notice that both the resistor and
LED share row 21.
5. Run a red wire from Digital 13 port
on Arduino to F22
6. Run a black wire from J11 to the
blue strip.
Writing the code - Integers
Load up the Arduino software.

Begin by using the variable “int” or integer and


let’s tell the Arduino which port the LED is in.
Writing the code - Setup

Remember that SETUP is used for things that only need


to be done once. Therefore we must tell the Arduino that
the LED in port 13 is an output. That means when we
input data it outputs an outcome or result.
Writing the code - Loop

The next steps is telling the Arduino what we want to do


with the LED. We first need to use the digitalWrite
command to turn the LED ON. We then use the “delay”
command to specify and amount of time in milliseconds.
We then use the same command to turn it OFF then wait
again. Since this is a loop the process will repeat forever
until the power is removed.
Compile
To compile your
sketch, click the
checkmark.

Make sure your


Arduino is plugged
into an available USB
port.

Click the arrow to


download the
program to Arduino. If
everything is attached
correctly. The LED
should blink.
Project #1: Wiring Diagram

Move the green


wire from the
power bus to pin
13 (or any other
Digital I/O pin on
the Arduino board.

Image created in Fritzing


A few simple challenges
Let’s make LED#13 blink!
◼ Challenge 1a – blink with a 200 ms second
interval.

◼ Challenge 1b – blink to mimic a heartbeat

◼ Challenge 1c – find the fastest blink that the


human eye can still detect…
◼ 1 ms delay? 2 ms delay? 3 ms
delay???

Try adding other LEDs


Programming Concepts: Variables

Variable Scope

◼Global
◼---

◼Function-
level
Programming Concepts: Variable
Types
◼ Variable Types:

8 bits 16 bits 32 bits

byte int long


char unsigned int unsigned long
float
Fading in and Fading Out
(Analog or Digital?)
◼ A few pins on the Arduino allow for us to
modify the output to mimic an analog signal.

◼ This is done by a technique called:


◼ Pulse Width Modulation (PWM)
Concepts: Analog vs. Digital

◼To create an analog signal, the microcontroller


uses a technique called PWM. By varying the duty
cycle, we can mimic an “average” analog voltage.

◼Pulse Width Modulation (PWM)


Project #2 – Fading
Introducing a new command…

◼analogWrite(pin, val);

◼pin – refers to the OUTPUT
pin (limited to pins 3, 5, 6, 9, 10,
11.) – denoted by a ~ symbol

◼val – 8 bit value (0 – 255).


◼ 0 => 0V | 255 => 5V
Move one of your LED pins over to
Pin 9
◼ In Arduino, open up:
◼ File → Examples → 01.Basics → Fade
Fade - Code Review
Fade - Code Review
Project# 2 -- Fading

◼ Challenge 2a – Change the rate of the fading


in and out. There are at least two different
ways to do this – can you figure them out?

◼ Challenge 2b – Use 2 (or more) LEDs – so


that one fades in as the other one fades out.
R G B

Color Mixing
Tri-color LED

◼ In the SIK, this is a standard –


Common Cathode LED

◼ This means the negative side


of the LED is all tied to Ground.
Project 3 – RGB LED

◼ Note: The
longest leg of
the RGB LED is
the Common
Cathode. This
goes to GND.

Use pins 5, 6, & 9


How many unique colors can you
create?

Use Colorpicker.com or
experiment on your
own.
Pick out a few colors that
you want to try re-
creating for a lamp or
lighting display...
Play around with this with
the analogWrite()
command.
RGB LED Color Mixing
◼ int redPin = 5;
◼ int greenPin = 6;
◼ int bluePin = 9;

◼ void setup()
◼ {
◼ pinMode(redPin, OUTPUT);
◼ pinMode(greenPin, OUTPUT);
◼ pinMode(bluePin, OUTPUT);
◼ }
RGB LED Color Mixing

◼ void loop()
◼ {
◼ analogWrite(redPin, 255);
◼ analogWrite (greenPin, 255);
◼ analogWrite (bluePin, 255);
◼ }
Project: Mood Lamp / Light Sculpture
Driving Motors or other High Current
Loads
◼ NPN Transistor (Common Emitter “Amplifier”
Circuit)

to Digital
Pin 9
Input

Input is any signal entering an electrical system.

• Both digital and analog sensors are forms of input


• Input can also take many other forms: Keyboards, a
mouse, infrared sensors, biometric sensors, or just plain
voltage from a circuit
Project #4 – Digital Input

◼ In Arduino, open up:


◼ File → Examples → 02.Digital → Button
Digital Sensors (a.k.a. Switches)
Pull-up Resistor (circuit)

to Digital Pin 2
Digital Sensors (a.k.a. Switches)
Add an indicator LED to Pin 13
This is just like our 1st
circuit!
Digital Input

• Connect digital input to your Arduino using Pins # 0 – 13


(Although pins # 0 & 1 are also used for programming)

• Digital Input needs a pinMode command:


◼ pinMode (pinNumber, INPUT);
◼ Make sure to use ALL CAPS for INPUT

• To get a digital reading:


◼ int buttonState = digitalRead
(pinNumber);

• Digital Input values are only HIGH (On) or LOW (Off)


Digital Sensors

• Digital sensors are more straight forward than Analog

• No matter what the sensor there are only two settings:


On and Off

• Signal is always either HIGH (On) or LOW (Off)

• Voltage signal for HIGH will be a little less than 5V on


your Uno

• Voltage signal for LOW will be 0V on most systems


http://opensourcehardwarejunkies.com/tutorial-03-digitalread-and-serial-port-
Programming: Conditional
Statements
if()
Programming: Conditional
Statements
if()
◼ void loop()
◼ {
◼ int buttonState =
digitalRead(5);
◼ if(buttonState == LOW) DIG
◼ { // do something INPUT
◼ }
◼ else
◼ { // do something else
◼ }
◼ }
Boolean Operators

<Boolean> Description
( ) == ( ) is equal?
( ) != ( ) is not equal?
( ) > ( ) greater than
( ) >= ( ) greater than or equal
( ) < ( ) less than
( ) <= ( ) less than or equal
Trimpot (Potentiometer)
Variable Resistor

fixed
end

wiper

fixed
end
Analog Sensors
3 Pin Potentiometer = var. resistor (circuit)
a.k.a. Voltage Divider Circuit

wiper

fixed
ends
1.0 V 1.0 V
Ohms Law… (just the basics)
Actually, this is the “voltage divider”

analogRead()

◼ Arduino uses a 10-bit A/D Converter:


• this means that you get input values from 0
to 1023
• 0V→0
• 5 V → 1023
◼Ex:
◼ int sensorValue = analogRead(A0);
Using Serial Communication

Method used to transfer data between two devices.

Data passes between the computer and Arduino


through the USB cable. Data is transmitted as zeros
(‘0’) and ones (‘1’) sequentially.

Arduino dedicates Digital I/O pin # 0 to


receiving and Digital I/O pin #1 to transmit.
Serial Monitor & analogRead()

Initializes the Serial


Communication

9600 baud data rate

prints data to serial bus


Serial Monitor & analogRead()

Opens up a Serial
Terminal Window
Analog Sensors
2 Pin Analog Sensors = var. resistor
◼Take two sensors -- Use
the Serial Monitor and find
the range of input values
you get for each sensor.

◼MaxAnalogRead = _________

◼MinAnalogRead = _________
Analog Sensors

Examples:
Sensors Variables
Mic soundVolume
Photoresistor lightLevel
Potentiometer dialPosition
Temp Sensor temperature
Flex Sensor bend
Accelerometer tilt/acceleration
Additional Serial Communication
Sending a Message
void loop ( )
{
Serial.print(“Hands on “) ;
Serial.print(“Learning ”) ;
◼ Serial.println(“is Fun!!!”) ;

}
Serial Communication:
Serial Debugging
void loop()
{
int xVar = 10;
Serial.print ( “Variable xVar is “ ) ;
Serial.println ( xVar ) ;
}
Serial Communication:
Serial Troubleshooting
void loop ( )
{
Serial.print (“Digital pin 9: “);
Serial.println (digitalRead(9));
}

Potrebbero piacerti anche