SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig,...
-
Upload
brianna-lamb -
Category
Documents
-
view
219 -
download
2
Transcript of SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig,...
![Page 1: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/1.jpg)
SAMI
MAKERSPACE
MA
K E : A
N E
L E CT R
ON
I CS W
OR
K S HO
P
![Page 2: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/2.jpg)
ARDUINO
BASICS
Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson,
Tai Johnson, and Eli Santistevan
![Page 3: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/3.jpg)
THIS PRESENTATION:Part One: Hardware
• Getting around and Arduino
• Getting connected
• The breadboard
• A simple circuit
Part Two: Software
• The integrated development environment
Part Three: Programming
• Inputs and outputs
• Some conventions
• First Program: Blink
• Variables
• Analog versus digital
• Pulse wave modulation
• Second Program: Fade
• Third Program: Button
• Conditional statements
• Serial communication
![Page 4: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/4.jpg)
PART
ONE: T
HE
HARDWARE
AR
DU
I NO
, U
SB
CA
BL E
, L A
PT
OP
, B
RE
AD
BO
AR
D
![Page 5: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/5.jpg)
GETTIN
G AROUND
AN ARDUIN
O
TH
E “
UN
O” M
OD
EL
![Page 6: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/6.jpg)
![Page 7: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/7.jpg)
Analog INPUTS
Digital I\OPWM(3, 5, 6, 9, 10,
11)
PWR IN USB (to Computer)
SCL\SDA(I2C Bus)
POWER 5V / 3.3V /
GND
RESET
![Page 8: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/8.jpg)
LET’
S GET
CONNECTED!
![Page 9: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/9.jpg)
PLUG IN YOUR ARDUINO
![Page 10: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/10.jpg)
BREADBOARDS
RA
PI D
AN
D S
OL D
ER
- F RE
E P
RO
TO
TY
PI N
G!
![Page 11: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/11.jpg)
PROTOTYPING CIRCUITS SOLDERLESS BREADBOARDOne of the most useful tools in an
engineer or Maker’s toolkit. The three most important things:
• A breadboard is easier than soldering• A lot of those little holes are connected, which ones?• Sometimes breadboards break
![Page 12: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/12.jpg)
WHAT’S A BREADBOARD?
![Page 13: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/13.jpg)
SOLDERLESS BREADBOARD
Each row (horiz.) of 5 holes are connected.
Vertical columns – called power bus are connected vertically
![Page 14: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/14.jpg)
A SIM
PLE
CIRCUIT
L I GH
T U
P A
N L
ED
US
I NG
TH
E A
RD
UI N
O
![Page 15: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/15.jpg)
USE THE BREADBOARD AND ARDUINO TO BUILT A SIMPLE CIRCUIT
Use the breadboard to wire up a single LED with a 330 Ohm Resistor (Orange-Orange-Brown).Note: the longer leg on the LED is the positive leg and the shorter leg is the negative
![Page 16: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/16.jpg)
![Page 17: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/17.jpg)
![Page 18: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/18.jpg)
PART
TW
O: THE
SOFTW
ARE
I NT
EG
RA
TE
D D
EV
EL O
PM
EN
T E
NV
I RO
NM
EN
T
![Page 19: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/19.jpg)
THE IN
TEGRAT
ED
DEVELOPM
ENT
ENVIRONMENT
![Page 20: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/20.jpg)
OPEN UP “ARDUINO” APPLICATION
Look for the icon on the desktop of the white clam-shell computers.
You can also download the application for free to your own computers (you may need to also install the proper drivers)
![Page 21: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/21.jpg)
ARDUINO: INTEGRATED DEVELOPMENT ENVIRONMENT (IDE)
Two required functions / methods / routines:
void setup()
{
// runs once
}
void loop()
{
// repeats
}
error & status messages
![Page 22: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/22.jpg)
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.
![Page 23: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/23.jpg)
SETTINGS: TOOLS BOARD
Next, double-check that the proper board is selected under the ToolsBoard menu.
![Page 24: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/24.jpg)
PART
3:
PROGRAMMIN
G
!C
OD
E F
OR
TH
E M
AS
SE
S
![Page 25: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/25.jpg)
CONCEPT: I
NPUTS
VS.
OUTPUTS
![Page 26: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/26.jpg)
CONCEPTS: INPUT VS. OUTPUTReferenced from the perspective of the microcontroller
(electrical board).
Inputs is a signal / information going into the board.
Output is any signal exiting the board.
Almost all systems that use physical computing will have some form of output.What are some examples of Outputs?
![Page 27: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/27.jpg)
CONCEPTS: INPUT VS. OUTPUT
Inputs is a signal / information going into the board.
Output is any signal exiting the board.
Examples: Buttons Switches, Light Sensors, Flex Sensors, Humidity Sensors, Temperature Sensors…
Examples: LEDs, DC motor, servo motor, a piezo buzzer, relay, an RGB LED
![Page 28: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/28.jpg)
YOU DON’T NEED TO SPEAK BINARY!
The Arduino IDE allows you to work in a “high-level programming language.” The code you write is readable to humans (with a little training)
The IDE does the hard work of making your code something that the chip on the Arduino understands:
![Page 29: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/29.jpg)
SUB-ROUTINES/FUNCTIONS/COMMANDS
Arduino uses pre-made sub-routines (small programs) to make your life easier.
Programs like:
• delay()
• if()
• loop()
Most programs need an input (what they are acting on) and give an output (usually a numerical value)
The names of these programs (like most things in Arduino) are case sensitive, so type carefully!
digitalWrite() is not the same as digitalwrite()
Can you guess what these do?
![Page 30: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/30.jpg)
pinMode(pin, INPUT/OUTPUT);
ex: pinMode(13, OUTPUT);
digitalWrite(pin, HIGH/LOW);
ex: digitalWrite(13, HIGH);
delay(time_ms);
ex: delay(2500); // delay of 2.5 sec.
// NOTE: -> commands are CASE-sensitive
![Page 31: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/31.jpg)
digitalWrite()
analogWrite()
digitalRead()
if() statements / Boolean
analogRead()
Serial communication
BIG
6 C
ON
CEP
TS
![Page 32: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/32.jpg)
SOME CONVENTIONS: CURLY BRACKETSWhen you call a sub-routine every thing that happens in the program should be contained in curly brackets:
Everything between the curly brackets {..} is part of the loop function.
![Page 33: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/33.jpg)
SOME CONVENTIONS: SEMICOLONS
Any line of code that isn’t calling a sub-routine, or is a sub-routine that is only one line long should always end with a semicolon (;)
If you forget these, you will get errors when you try to “compile” the code!
![Page 34: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/34.jpg)
SOME CONVENTIONS: COMMENTS
It’s easier to get lost in your own code than you may think.
Leave comments to yourself, especially before you do something “tricky.”
They are not needed to make the program work, but can save you a lot of trouble in the long run.
![Page 35: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/35.jpg)
FIRST
PROGRAM: B
LINK
L ET
’ S M
AK
E A
N L
ED
BL I N
K!
![Page 36: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/36.jpg)
PLAN YOUR ATTACK WITH A FLOWCHART
Initialize
Turn LED ON Wait
Turn LED OFF
Wait
![Page 37: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/37.jpg)
1. PUT IN THE “BARE MINIMUM”
The “setup” function runs once and will lock in any settings you want.
The “loop” function will run for as long as the Arduino has power!
![Page 38: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/38.jpg)
2. INITIALIZE THE LED PIN AS A “OUTPUT”
Use the “pinMode” command. This requires two inputs (or arguments). You must tell it what pin to act on (13 in this case) and whether that pin is an OUTPUT or INPUT (this is case sensitive).
![Page 39: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/39.jpg)
3. TURN ON THE LED FOR ONE SECOND
![Page 40: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/40.jpg)
4. TURN OFF THE LED FOR ONE SECOND
![Page 41: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/41.jpg)
5. REPEAT!
Once the end of the program is reached, it will go to the beginning of loop() and run it again. And again. And again….
![Page 42: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/42.jpg)
SOME EXTRA CHALLENGES…
• Make the light blink with a 200 ms interval.
• Change the blink to mimic a heart beat.
• What’s the fastest blink your eye can detect?
• Add two, three, or four LED’s. (each LED will need its own 330 Ohm resistor).
![Page 43: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/43.jpg)
CONCEPT: V
ARIABLE
S
![Page 44: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/44.jpg)
CONCEPT: VARIABLES
• Declaring variables• Types of variables• Variable scope• Initializing variables• Variable rollover
![Page 45: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/45.jpg)
TYPES OF VARIABLES
8 bits 16 bits 32 bits
byte char
intunsigned int
longunsigned longfloat
![Page 46: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/46.jpg)
DECLARING VARIABLES AND SCOPE OF VARIABLES
Variable Scope
Global Vs.
Function-level
![Page 47: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/47.jpg)
INITIALIZING A VARIABLE: EXAMPLE
Compare this code our early “blink” program.
There is an “int” type variable declared. • An integer (no decimal)
The variable is named “ledPin”
ledPin is initialized to the value “5”
This is a “global” variable since it was declared outside of a function.• All other functions are able to “call”
ledPin and they can change its value
Notice how easy it would be to change the pin on your circuit that has the LED connected to it?
![Page 48: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/48.jpg)
CONCEPT: D
IGITA
L VERSUS
ANALOG
![Page 49: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/49.jpg)
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?
How can we get 3V from a microcontroller that can only be on (5V) or off (0V)?5 V
0 V
5 V
0 V
![Page 50: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/50.jpg)
CONCEPT: P
WM
PU
L SE
WA
VE
MO
DU
L AT
I ON
![Page 51: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/51.jpg)
CONCEPT: PULSE WAVE MODULATION
To create an analog signal, the digital microcontroller uses a technique called PWM. By varying the duty cycle, we can mimic an “average” analog voltage.
Pulse Width Modulation (PWM)
![Page 52: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/52.jpg)
SECOND PROGRAM: F
ADE
L ET
’ S D
I M T
HA
T L
ED
!
![Page 53: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/53.jpg)
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
PROJECT #2 – FADINGINTRODUCING A NEW COMMAND…
![Page 54: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/54.jpg)
THIR
D PROGRAM:
BUTTON
CO
NT
RO
L TH
AT
LE
D W
I TH
DI G
I TA
L IN
PU
T!
![Page 55: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/55.jpg)
USE AN EXAMPLE PROGRAM
In Arduino, open up:
File Examples 02.Digital Button
![Page 56: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/56.jpg)
DIGITAL INPUT: BUTTON
to Digital Pin 2
![Page 57: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/57.jpg)
DIGITAL INPUT: BUTTON
This is just like our 1st circuit!
![Page 58: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/58.jpg)
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)
![Page 59: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/59.jpg)
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
![Page 60: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/60.jpg)
![Page 61: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/61.jpg)
USING C
ONDITIO
NAL
STATE
MENTS
I F T
HI S
, T
HE
N T
HA
T!
![Page 62: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/62.jpg)
CONDITIONAL STATEMENTS: IF()
![Page 63: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/63.jpg)
void loop()
{
int buttonState = digitalRead(5);
if(buttonState == LOW)
{ // do something
}
else
{ // do something else
}
}
CONDITIONAL STATEMENTS: IF()
DIG INPUT
![Page 64: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/64.jpg)
BOOLEAN OPERATORS
<Boolean> Description
( ) == ( ) is equal?( ) != ( ) is not equal?( ) > ( ) greater than( ) >= ( ) greater than or equal( ) < ( ) less than( ) <= ( ) less than or equal
![Page 65: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/65.jpg)
SERIAL C
OMMUNICAT
ION
KE
EP
I NG
AN
EY
E O
N Y
OU
R P
RO
GR
AM
…
![Page 66: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/66.jpg)
USING SERIAL COMMUNICATIONMethod used to transfer data between two
devices.
Arduino dedicates Digital I/O pin # 0 to receiving and Digital I/O pin #1 to transmit.
Data passes between the computer and Arduino through the USB cable. Data is transmitted as a string of zeros (‘0’) and ones (‘1’).
![Page 67: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/67.jpg)
SERIAL MONITOR & ANALOGREAD()
Initializes the Serial Communication
9600 baud data rate
prints data to serial bus
![Page 68: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/68.jpg)
SERIAL MONITOR & ANALOGREAD()
Opens up a Serial Terminal Window
![Page 69: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/69.jpg)
EXAMPLE
void loop ( )
{
Serial.print(“Hands on “) ;
Serial.print(“Learning ”) ;
Serial.println(“is Fun!!!”) ;
}
Serial.print adds to the same line. Serial.println adds to the
line, then “returns” (starts a new line).
![Page 70: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/70.jpg)
![Page 71: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,](https://reader030.fdocuments.in/reader030/viewer/2022020200/5697c0231a28abf838cd3e3f/html5/thumbnails/71.jpg)
USING SERIAL COMMUNICATION TO DEBUG AND TROUBLESHOOTLet’s use serial communication to watch what is happening in our “button” program.