112
Intro to Arduino The Basics of Open Hardware and Circuits Jason Griffey Evenly Distributed LLC http://evenlydistributed.net Code4LibDC 2014

Introduction to Arduino and Circuits

Embed Size (px)

DESCRIPTION

Workshop I delivered at Code4LibDC Unconference 2014 on open hardware and arduino for libraries and archives.

Citation preview

Page 1: Introduction to Arduino and Circuits

Intro to ArduinoThe Basics of Open Hardware and Circuits

Jason Griffey Evenly Distributed LLC

http://evenlydistributed.net

Code4LibDC 2014

Page 2: Introduction to Arduino and Circuits
Page 3: Introduction to Arduino and Circuits

SSID: LibraryBox - Free Content!

Folder: Code4LibDC

http://librarybox.us

Connect to wifi signal, then launch/refresh any browser

Page 4: Introduction to Arduino and Circuits
Page 5: Introduction to Arduino and Circuits
Page 6: Introduction to Arduino and Circuits
Page 7: Introduction to Arduino and Circuits

Software install?

Page 8: Introduction to Arduino and Circuits

Let’s Plug In

Page 9: Introduction to Arduino and Circuits

Open Source Hardware

Page 10: Introduction to Arduino and Circuits

Moore’s Law

Photo by Marcin Wichary - http://flic.kr/p/4v4DSq

Page 11: Introduction to Arduino and Circuits

Koomey’s Law

Photo by LisaW123 - http://flic.kr/p/7eiqaq

Page 12: Introduction to Arduino and Circuits

Photo by sobczak.paul - http://flic.kr/p/aDD8ep

Page 13: Introduction to Arduino and Circuits

Arduino

Page 14: Introduction to Arduino and Circuits

Photo by bjepson - http://flic.kr/p/8EVHqm

Page 15: Introduction to Arduino and Circuits
Page 16: Introduction to Arduino and Circuits

Why Arduino?

Page 17: Introduction to Arduino and Circuits

Open Hardware

Page 18: Introduction to Arduino and Circuits

Photo by dam - http://flic.kr/p/8hLXBM

Page 19: Introduction to Arduino and Circuits

Photo by dam - http://flic.kr/p/8hLXwT

Page 20: Introduction to Arduino and Circuits

Photo by dam - http://flic.kr/p/8hQcJs

Page 21: Introduction to Arduino and Circuits

Photo by dam - http://flic.kr/p/8hQcDG

Page 22: Introduction to Arduino and Circuits

Photo by dam - http://flic.kr/p/8hQcDG

Page 23: Introduction to Arduino and Circuits

Photo by Nathan Chantrell - http://flic.kr/p/aALqhd

Page 24: Introduction to Arduino and Circuits

Photo by Nathan Chantrell - http://flic.kr/p/aALqhd

Page 25: Introduction to Arduino and Circuits
Page 26: Introduction to Arduino and Circuits
Page 27: Introduction to Arduino and Circuits

Photo by antonw - http://flic.kr/p/91daJe

Shields

Page 28: Introduction to Arduino and Circuits
Page 29: Introduction to Arduino and Circuits
Page 30: Introduction to Arduino and Circuits
Page 31: Introduction to Arduino and Circuits

A La Mode

Page 32: Introduction to Arduino and Circuits

http://learn.adafruit.com/system/assets/assets/000/000/426/original/FSR402_MED.jpg?1340650766

Sensors

Page 33: Introduction to Arduino and Circuits
Page 34: Introduction to Arduino and Circuits
Page 35: Introduction to Arduino and Circuits

Arduino Software

Page 36: Introduction to Arduino and Circuits
Page 37: Introduction to Arduino and Circuits
Page 38: Introduction to Arduino and Circuits
Page 39: Introduction to Arduino and Circuits
Page 40: Introduction to Arduino and Circuits
Page 41: Introduction to Arduino and Circuits
Page 42: Introduction to Arduino and Circuits
Page 43: Introduction to Arduino and Circuits
Page 44: Introduction to Arduino and Circuits

Our Hardware

Page 45: Introduction to Arduino and Circuits
Page 46: Introduction to Arduino and Circuits
Page 47: Introduction to Arduino and Circuits
Page 48: Introduction to Arduino and Circuits
Page 49: Introduction to Arduino and Circuits
Page 50: Introduction to Arduino and Circuits
Page 51: Introduction to Arduino and Circuits
Page 52: Introduction to Arduino and Circuits
Page 53: Introduction to Arduino and Circuits
Page 54: Introduction to Arduino and Circuits
Page 55: Introduction to Arduino and Circuits
Page 56: Introduction to Arduino and Circuits
Page 57: Introduction to Arduino and Circuits
Page 58: Introduction to Arduino and Circuits
Page 59: Introduction to Arduino and Circuits
Page 60: Introduction to Arduino and Circuits
Page 61: Introduction to Arduino and Circuits
Page 62: Introduction to Arduino and Circuits
Page 63: Introduction to Arduino and Circuits
Page 64: Introduction to Arduino and Circuits

Fritzing

Page 65: Introduction to Arduino and Circuits
Page 66: Introduction to Arduino and Circuits

ArduinoIDE

Page 67: Introduction to Arduino and Circuits

Our Sketch

Page 68: Introduction to Arduino and Circuits

int val; !void setup() { pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); Serial.begin(9600); } !void loop() { Serial.println(analogRead(0)); val = analogRead(0); !if (val > 256) digitalWrite(2,HIGH); if (val < 256) digitalWrite(2,LOW); if (val > 512) digitalWrite(3,HIGH); if (val < 512) digitalWrite(3,LOW); !if (val > 768) digitalWrite(4,HIGH); if (val < 768) digitalWrite(4,LOW); !if (val > 769) digitalWrite(5,HIGH); if (val < 1020) digitalWrite(5,LOW); }

Globals

Setup

Loop

Page 69: Introduction to Arduino and Circuits

int val; !void setup() { pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); Serial.begin(9600); } !void loop() { Serial.println(analogRead(0)); val = analogRead(0); !if (val > 256) digitalWrite(2,HIGH); if (val < 256) digitalWrite(2,LOW); if (val > 512) digitalWrite(3,HIGH); if (val < 512) digitalWrite(3,LOW); !if (val > 768) digitalWrite(4,HIGH); if (val < 768) digitalWrite(4,LOW); !if (val > 769) digitalWrite(5,HIGH); if (val < 1020) digitalWrite(5,LOW); }

Globals

Setup

Loop

Page 70: Introduction to Arduino and Circuits

int val; !void setup() { pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); Serial.begin(9600); } !void loop() { Serial.println(analogRead(0)); val = analogRead(0); !if (val > 256) digitalWrite(2,HIGH); if (val < 256) digitalWrite(2,LOW); if (val > 512) digitalWrite(3,HIGH); if (val < 512) digitalWrite(3,LOW); !if (val > 768) digitalWrite(4,HIGH); if (val < 768) digitalWrite(4,LOW); !if (val > 769) digitalWrite(5,HIGH); if (val < 1020) digitalWrite(5,LOW); }

Globals

Setup

Loop

Page 71: Introduction to Arduino and Circuits

int val; !void setup() { pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); Serial.begin(9600); } !void loop() { Serial.println(analogRead(0)); val = analogRead(0); !if (val > 256) digitalWrite(2,HIGH); if (val < 256) digitalWrite(2,LOW); if (val > 512) digitalWrite(3,HIGH); if (val < 512) digitalWrite(3,LOW); !if (val > 768) digitalWrite(4,HIGH); if (val < 768) digitalWrite(4,LOW); !if (val > 769) digitalWrite(5,HIGH); if (val < 1020) digitalWrite(5,LOW); }

Globals

Setup

Loop

Page 72: Introduction to Arduino and Circuits
Page 73: Introduction to Arduino and Circuits

Can you:

Page 74: Introduction to Arduino and Circuits

Can you:

• Change one of the Pin Numbers?

Page 75: Introduction to Arduino and Circuits

Can you:

• Change one of the Pin Numbers?

• Re-wire the ground wires?

Page 76: Introduction to Arduino and Circuits

Can you:

• Change one of the Pin Numbers?

• Re-wire the ground wires?

• Change the order the LEDs light up?

Page 77: Introduction to Arduino and Circuits

Can you:

• Change one of the Pin Numbers?

• Re-wire the ground wires?

• Change the order the LEDs light up?

• Light up 2 LEDs at once?

Page 78: Introduction to Arduino and Circuits

Can you:

• Change one of the Pin Numbers?

• Re-wire the ground wires?

• Change the order the LEDs light up?

• Light up 2 LEDs at once?

• Add another LED?

Page 79: Introduction to Arduino and Circuits

Can you:

• Change one of the Pin Numbers?

• Re-wire the ground wires?

• Change the order the LEDs light up?

• Light up 2 LEDs at once?

• Add another LED?

• Swap the sensor?

Page 80: Introduction to Arduino and Circuits

Structure/* Each Arduino sketch must contain the following two functions. */void setup(){/* this code runs once at the beginning of the code execution. */}void loop(){/* this code runs repeatedly over and over as long as the board is powered. */}

Comments// this is a single line/* this isa multiline */

SetuppinMode(pin, [INPUT \ OUTPUT \ INPUT_PULLUP]);/* Sets the mode of the digital I/O pin. It can be set as an input, output, or an input with an internal pull-up resistor. */

Control Structuresif(condition){// if condition is TRUE, do something here}else{// otherwise, do this}

for(initialization; condition; increment){// do this}/* The ‘for’ statement is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. */

Digital I/OdigitalWrite(pin, val); /* val = HIGH or LOW. Sets the digital pin to either ON or OFF. */int var = digitalRead(pin);/* Reads the voltage from a digital pin. Returns either HIGH or LOW. */int var = digitalRead(pin);/* Reads the value from a specified digital pin, either HIGH or LOW. */

Analog I/OanalogWrite(pin, val); /* Writes an analog voltage to a pin.val = integer value from 0 to 255 */int var = analogRead(pin);/* Reads the voltage from the specified analog pin. */int var = analogRead(pin);/* Reads the value from the specified analog pin. */

Timedelay(time_ms);/* Pauses the program for the amount of time (in milliseconds). */delayMicroseconds(time_us);/* Pauses the program for the amount of time (in microseconds). */millis();/* Returns the number of milliseconds since the board began running the current program. max: 4,294,967,295 */micros();/* Returns the number of microseconds since the board began running the current program. max: 4,294,967,295 */

Serial CommunicationSerial.begin(baudrate);/* Sets the data rate in bits per second (baud) for serial data transmission. */Serial.print("");/* Sets the data rate in bits per second (baud) for serial data transmission. */Serial.print("Hello World!!");/* Sends a string "Hello World!!" to the serial bus. This will be seen on the Serial Monitor in Arduino. */Serial.println("Hello World!");/* Identical to Serial.print(), but this also adds a carriage-return / line-feed to advance to the next line. */Serial.println("");/* Identical to Serial.print(), but this also adds a carriage-return / line-feed to advance to the next line. */boolean Serial.available()/* Serial.available() returns TRUE if there is data available on the Serial bus. */int Serial.read();/* Returns a single byte of data available from the Serial buffer. */int Serial.read();/* Returns a single byte of data available from the Serial buffer. */

Data Typesvoid // nothing is returnedboolean // 0, 1, false, truechar // 8 bits: ASCII characterbyte // 8 bits: 0 to 255, unsignedint // 16 bits: 32,768 to 32,767, signedlong /* 32 bits: 2,147,483,648 to 2,147,483,647, signed */float // 32 bits, signed decimal

ConstantsHIGH \ LOWINPUT \ OUTPUTtrue \ false

Mathematical Operators= // assignment+ // addition- // subtraction* // multiplication/ // division% // modulus

Logical Operators== // boolean equal to!= // not equal to< // less than> // greater than<= // less than or equal to>= // greater than or equal to&& // Boolean AND|| // Boolean OR! // Boolean NOT

Bitwise Operators& // bitwise AND| // bitwise OR^ // bitwise XOR~ // bitwise INVERTvar << n // bitwise shift left by n bitsvar >> n // bitwise shift right by n bits

Libraries#include <libraryname.h>/* this provides access to special additional functions for things such as servo motors, SD card, wifi, or bluetooth. */

Advanced I/Otone(pin, freq);/* Generates a square wave of the specified frequency to a pin. Pin must be one of the PWM (~) pins. */tone(pin, freq, duration);/* Generates a square wave of the specified frequency to a pin for a duration in milliseconds. Pin must be one of the PWM (~) pins. */noTone(pin);// Turns off the tone on the pin.

LilyPad ProtoSnap Simple:

RedBoard:

ATmega328Microcontroller

TemperatureSensor(Pin A1)

Button(Pin A5)

Vibe Motor(Pin 3)

RGB LED(Pins R=9,G=11, B=10)

Switch(Pin 2)

LightSensor(Pin A6)

Buzzer/Speaker(Pin 7)

LEDs(Light Emitting Diodes)(Pins 5, 6, A2, A4, A3)

Power In

Power5V / 3.3 / GND

USB to ComputerReset

AnalogInputs

SCL/SDA(I2C Bus)

ATmega328Microcontroller

Digital I/OPWM(3,5,6,9,10,11)

Page 81: Introduction to Arduino and Circuits

Building Your Own Hardware

Page 82: Introduction to Arduino and Circuits

Patron Counters

Page 83: Introduction to Arduino and Circuits

$300

Page 84: Introduction to Arduino and Circuits

$280

Page 85: Introduction to Arduino and Circuits
Page 86: Introduction to Arduino and Circuits

Build One Yourself

Page 87: Introduction to Arduino and Circuits

Build One Yourself

• Arduino - $35

Page 88: Introduction to Arduino and Circuits

Build One Yourself

• Arduino - $35

• Data Logger Shield - $20

Page 89: Introduction to Arduino and Circuits

Build One Yourself

• Arduino - $35

• Data Logger Shield - $20

• SD Card - $10

Page 90: Introduction to Arduino and Circuits

Build One Yourself

• Arduino - $35

• Data Logger Shield - $20

• SD Card - $10

• Sensor (infrared, laser tripwire, ultrasonic, or PID) - $10

Page 91: Introduction to Arduino and Circuits

Build One Yourself

• Arduino - $35

• Data Logger Shield - $20

• SD Card - $10

• Sensor (infrared, laser tripwire, ultrasonic, or PID) - $10

• TOTAL COST - $70

Page 92: Introduction to Arduino and Circuits

Temperature/Humidity Loggers

Page 93: Introduction to Arduino and Circuits

$240

Page 94: Introduction to Arduino and Circuits

$415

Page 95: Introduction to Arduino and Circuits
Page 96: Introduction to Arduino and Circuits
Page 97: Introduction to Arduino and Circuits
Page 98: Introduction to Arduino and Circuits
Page 99: Introduction to Arduino and Circuits

Build One Yourself

Page 100: Introduction to Arduino and Circuits

Build One Yourself

• Arduino - $35

Page 101: Introduction to Arduino and Circuits

Build One Yourself

• Arduino - $35

• Data Logger Shield - $20

Page 102: Introduction to Arduino and Circuits

Build One Yourself

• Arduino - $35

• Data Logger Shield - $20

• SD Card - $10

Page 103: Introduction to Arduino and Circuits

Build One Yourself

• Arduino - $35

• Data Logger Shield - $20

• SD Card - $10

• Temperature/Humidity Sensor - $5-10

Page 104: Introduction to Arduino and Circuits

Build One Yourself

• Arduino - $35

• Data Logger Shield - $20

• SD Card - $10

• Temperature/Humidity Sensor - $5-10

• LCD Display screen - $15

Page 105: Introduction to Arduino and Circuits

Build One Yourself

• Arduino - $35

• Data Logger Shield - $20

• SD Card - $10

• Temperature/Humidity Sensor - $5-10

• LCD Display screen - $15

• TOTAL COST - $85

Page 106: Introduction to Arduino and Circuits

Other Possibilities

Page 107: Introduction to Arduino and Circuits

Other Possibilities

• RFID Reader

Page 108: Introduction to Arduino and Circuits

Other Possibilities

• RFID Reader

• Barcode Scanner

Page 109: Introduction to Arduino and Circuits

Other Possibilities

• RFID Reader

• Barcode Scanner

• Seat usage detector

Page 110: Introduction to Arduino and Circuits

Other Possibilities

• RFID Reader

• Barcode Scanner

• Seat usage detector

• What else?

Page 111: Introduction to Arduino and Circuits

Go Build Stuff!

Page 112: Introduction to Arduino and Circuits

Email: [email protected]

Site: jasongriffey.net

gVoice: 423-443-4770

Twitter: @griffey

http://librarybox.us

http://evenlydistributed.net

!Founder & Principal Consultant

Evenly Distributed LLChttp://pinboard.in/u:griffey/

Jason Griffey