Slide 1

Slide 1 text

en-1.3 Arduino 101 First steps in hardware hacking

Slide 2

Slide 2 text

Program • Introductions • What can you do with Arduinos? • First circuit: Blink • Coding Dojo with Arduino • ABCofE: ABC of Electronics • Tra!c light circuit • Wrapping up 2 ☈ ☈ ☈

Slide 3

Slide 3 text

Introductions 3

Slide 4

Slide 4 text

Luciano Ramalho • Consultant, teacher and speaker specializing in the Python language 4

Slide 5

Slide 5 text

Luciano Ramalho • Self-taught programmer since 1978 • BASIC, ASM Z-80, Pascal, C, C++, Smalltalk, Perl, Java, Python, JavaScript, Ruby... • Bachelors degree in Library and Information Sciences from ECA/USP • Art and Programming Workshop (PSI-2615) at Poli/USP with Prof. Etienne Delacroix 5

Slide 6

Slide 6 text

Luciano Ramalho • Owner and teacher of O"cinas Turing • Partner and instructor at Python.pro.br • Founding member of Garoa Hacker Clube Turing.com.br oficinas 6

Slide 7

Slide 7 text

Introduce yourselves • Find out the name, interests and hobbies of the people near you • Ask and tell your neighbors: • Why are you interested in Arduinos? • Do you have a project in mind? 5 minutes 7

Slide 8

Slide 8 text

Census • Who knows a lot about electronics? • Who knows something about electronics? • Who knows a lot about programming? • Who knows something about programming? 8

Slide 9

Slide 9 text

Census nothing some a lot 3 2 2 0 2 3 0 1 0 LR Electronics Programming nothing some a lot 9

Slide 10

Slide 10 text

What can you do with Arduinos? 10

Slide 11

Slide 11 text

RepRap 3D printer Open hardware Self-replicating!

Slide 12

Slide 12 text

Open hardware • Open hardware: free and open schematics and software + o#-the-shelf or home made components • Arduino is open hardware: copying is legal • Use of the Arduino brand: only under license

Slide 13

Slide 13 text

Biker turn signal Arduino Lilypad Leah Buechley of MIT

Slide 14

Slide 14 text

• Breathalyzer-Microphone www.instructables.com/id/Breathalyzer-Microphone/ • Guitar Pedal www.instructables.com/id/Lo-"-Arduino-Guitar-Pedal/ • Balancing Robot (inspired by Segway) hacknmod.com/hack/make-a-mini-segway-using-the-arduino/ • High-speed photography trigger hacknmod.com/hack/high-speed-photography-how-to-trigger- using-arduino/ • Home automation www.makeuseof.com/tag/how-to-build-home-automation-system- raspberry-pi-and-arduino/ Simple projects 14

Slide 15

Slide 15 text

15 That’s why the logo is ∞

Slide 16

Slide 16 text

Parts 16

Slide 17

Slide 17 text

A kit to get started ☐ LEDs ☐ 7-segment display ☐ 9g micro-servo ☐ Arduino compatible board ☐ USB cable Controller ☐ Breadboard ☐ Male-male jumpers ☐ Assorted resistors ☐ Push-button ☐ Potentiometer ☐ LDR (light sensor) ☐ Re!ective optical sensor ☐ Temperature sensor Basic parts Output devices Input devices 17

Slide 18

Slide 18 text

Arduino Uno R3 • Development board with ATmega328 microcontroller and auxliliary circuits • Client USB interface (device) • On-board voltage regulator for 7-12V (recommended) 18

Slide 19

Slide 19 text

Arduino: power in • From USB: 5V • 7 to 12V DC adapter • Vin and GND : 7 to 12V DC 5V via USB 7 to 12V DC 19 GND, Vin

Slide 20

Slide 20 text

Arduino: power out • 3.3 V and 5 V (+) • GND: ground (-) • Vin: directly from external supply 7 a 12 V (+) 20 Power pins The word “pin” is often used to refer to holes where pins are connected

Slide 21

Slide 21 text

Input/output pins 14 digital input/ output pins: 0...13 6 analog input pins: A0...A5 6 with Pulse Width Modulation: PWM ʙ 21

Slide 22

Slide 22 text

Inputs × outputs • Inputs: buttons, knobs, sensors etc. • Outputs: lights, motors, displays, buzzers etc. 22 ☐ LEDs ☐ 7-segment display ☐ 9g micro-servo ☐ Push-button ☐ Potentiometer ☐ LDR (light sensor) ☐ Re!ective optical sensor ☐ Temperature sensor Output devices Input devices

Slide 23

Slide 23 text

Breadboard • Known in Brazil as “protoboard” • Available in di#erent sizes: • 830 points • 400 points • etc. pictured: 400 points 23

Slide 24

Slide 24 text

Conections inside a regular breadboard power lines numbered columns 24

Slide 25

Slide 25 text

25

Slide 26

Slide 26 text

Note: each part connects to two or more columns ...or connects one column to a power line

Slide 27

Slide 27 text

Quiz: can I plug it like this? live examples by the teacher using the projection screen... 27

Slide 28

Slide 28 text

LED • Light-emitting diode • Polarized component: must be connected in the right direction 28

Slide 29

Slide 29 text

LED: light-emitting diode • + lead (longer) connects to power source • % lead (shortest) connects to ground • Connect to resistor in series to protect from excessive current notch 29

Slide 30

Slide 30 text

LED: basic circuit Current-limiting resistor according to LED specs 30

Slide 31

Slide 31 text

LED RGB • 3 LEDs (red, green, blue) in a single component 31

Slide 32

Slide 32 text

Resistors • Sold in tapes to feed machines in assembly lines 32

Slide 33

Slide 33 text

What is a resistor? • It reduces current • No polarity: may be connected either way • Rated in Ohms (Ω) • Symbols in schematics: 33

Slide 34

Slide 34 text

A small detour to understand why we need resistors 34

Slide 35

Slide 35 text

Voltage, current & resistance 35

Slide 36

Slide 36 text

Feel the power 36 Chap. 1, p. 5

Slide 37

Slide 37 text

Voltage • Unit: Volt (V) • Symbol in formulae: V 9 V 37

Slide 38

Slide 38 text

Resistance • Unit: Ohm (Ω) • Symbol in formulae: R 7mm of tongue ≈ 330 Ω 38

Slide 39

Slide 39 text

Current • Unit: Ampère (A) • Symbol in formulae: I • 6.241 × 1018 electrons per second 9 V ÷ 330 Ω ≈ 0.027 A 27 mA 39

Slide 40

Slide 40 text

Examples of current Hearing aid 0.7 mA 0.0007 A Cheap cell phone charger 500 mA 0.5 A Tablet charger 2.1 A 2.1 A Toaster (@120V) 16 A 16 A Automobile starter motor 120 A 120 A 40

Slide 41

Slide 41 text

Ohm’s Law V = I × R I = V / R R = V / I I V R 41

Slide 42

Slide 42 text

Back to the resistor... 42

Slide 43

Slide 43 text

Resistor: color code 33 x 101 = 330 Ω 43

Slide 44

Slide 44 text

Resistor: color code 330Ω with 5% tolerance: from 313Ω to 346Ω 44

Slide 45

Slide 45 text

Resistor: standard values E12 series of IEC 60063 standard (for 10% tolerance) 45

Slide 46

Slide 46 text

Resistor: examples 330 Ω 1 kΩ 10 kΩ 1 MΩ 46

Slide 47

Slide 47 text

Suitable resistors for LEDs • In practice, to protect a simple LED in a temporary circuit, a 500 Ω resistor will do • To determine the ideal resistor for an LED, use Ohm’s Law (source_voltage − led_voltage_drop) R = led_current

Slide 48

Slide 48 text

Formula terms • Source voltage: circuit voltage; for Arduino digital pins that is 5V • LED voltage drop: voltage drop between LED leads, given as “forward voltage”, “voltage drop” or “forward supply” • LED current: “forward current”; 20mA is a common value (20/1000 or .02 in formula) (source_voltage − led_voltage_drop) R = led_current

Slide 49

Slide 49 text

Example 49 (3 − 2.2) R = = 40 20/1000 LED (green 570nm): fwd. voltage = 2.2V current = 20mA

Slide 50

Slide 50 text

RadioShack RGB 5mm LED • Arduino UNO voltage = 5V • Red LED voltage drop: 2V • Red LED current: 50mA • Ideal resistor: 60Ω • Real resistor: 68Ω 50 (5 − 2) R = = 60 50/1000

Slide 51

Slide 51 text

First circuit 51

Slide 52

Slide 52 text

First circuit: blink 52

Slide 53

Slide 53 text

Longer lead (+) To pin 13 GND pin “ground” (-) 53 First circuit: blink

Slide 54

Slide 54 text

First circuit: blink

Slide 55

Slide 55 text

55

Slide 56

Slide 56 text

Programming 56

Slide 57

Slide 57 text

Arduino IDE • Easy to use editor, based on Processing.org, designed for artists 57 arduino.cc/en/Main/Software

Slide 58

Slide 58 text

Language • C++, simpli"ed 58

Slide 59

Slide 59 text

Structure comments 59 Consider the comments in Portuguese a bonus: they make it easy to tell what is C++ and what is not

Slide 60

Slide 60 text

comments 60 Structure variable declaration

Slide 61

Slide 61 text

comments 61 Structure variable declaration function definitions

Slide 62

Slide 62 text

comments variable declaration 62 function definitions function call Structure

Slide 63

Slide 63 text

Logic 63 Arduino runs the setup() function once after a reset

Slide 64

Slide 64 text

Logic 64 The led pin is configured for output

Slide 65

Slide 65 text

Logic 65 Arduino runs the loop() function over and over, as fast as it can

Slide 66

Slide 66 text

Logic 66 digitalWrite changes the state of a digital pin: HIGH is 5V, LOW is 0V

Slide 67

Slide 67 text

Logic 67 Where would you change the code to control an LED connected to pin #8?

Slide 68

Slide 68 text

Logic 68 Where would you change the code to change the flashing speed?

Slide 69

Slide 69 text

Syntax 69 instruction blocks are delimited by {...}

Slide 70

Slide 70 text

70 editor highlights pairs of {...}, (...) and [...] Syntax

Slide 71

Slide 71 text

71 editor colorizes the code to help reading and debugging it Syntax

Slide 72

Slide 72 text

C++ syntax tips • Every statement ends with ; (semicolon) • Each comma and semicolon counts, but spaces and line breaks are ignored • Upper case ≠ lower case • ex. Alpha ≠ alpha ≠ ALPHA 72

Slide 73

Slide 73 text

Coding Dojo with Arduino Slides at http://bit.ly/dojo-mozilla 73

Slide 74

Slide 74 text

Arduino & co. 74

Slide 75

Slide 75 text

Arduino Family Nano Uno Mega etc... 75 Lilypad

Slide 76

Slide 76 text

The UNO microcontroller: Atmel ATmega328 • “AVR” family • clock: 16 MHz • SRAM: 2 KB • EEPROM: 1 KB • Flash: 32 KB 76

Slide 77

Slide 77 text

Inputs and outputs of the ATmega328 • 28 pins total • 23 multi-function pins 77 datasheet diagram This is why the holes in the Arduino connectors are called “pins”: each hole connects to a real pin in the microcontroller.

Slide 78

Slide 78 text

• Pin functions: standardized and simpli"ed • Easier to learn • Easier to design compatible expansions 78 Inputs and outputs of the Arduino UNO

Slide 79

Slide 79 text

Shields 79 Arduino with two stacked shields • Expansion boards • Some examples: • Ethernet, Wi-Fi, motor driver, acelerometer, GPS, LCD touch screen...

Slide 80

Slide 80 text

Shields 80 5 stacked shields! • More examples: • GPRS, NFC/RFID, MIDI sequencer, MP3 decoder, XBee radio, photo camera controller... • Don’t miss: shieldlist.org a database with 317 shields from 127 makers as of March 12, 2014

Slide 81

Slide 81 text

Clones and derivatives • Some examples: • Severino • Garagino • Sanguino • Program-ME Severino

Slide 82

Slide 82 text

Vídeo: http://www.youtube.com/watch?v=S4nIV99RMtg 82 Arduino clone on breadboard video: http:/ /www.youtube.com/watch?v=S4nIV99RMtg

Slide 83

Slide 83 text

83

Slide 84

Slide 84 text

84 Reset button Voltage regulator LED connected to “pin 13” ATMega328 Power-on LED connects to FTDI adapter for programming via USB 16MHz crystal

Slide 85

Slide 85 text

More electronic components 85

Slide 86

Slide 86 text

Potentiometer • A.k.a: “pot” • Adjustable resistor • Symbol in schematics: 86

Slide 87

Slide 87 text

Potentiometer: how to use 87 Vout • Connect side leads to source and ground • Connect central lead Vout to an analog pin on the Arduino

Slide 88

Slide 88 text

LDR or photoresistor • Light Dependent Resistor • Use with resistor to make a voltage divider 88

Slide 89

Slide 89 text

Voltage divider circuit 89 Vout

Slide 90

Slide 90 text

Reading an LDR • 5V to LDR • 10k Ω resistor in series, to ground • between them: Vout connected to an analog pin (ex. A0) 90

Slide 91

Slide 91 text

Tra!c light 1 91

Slide 92

Slide 92 text

Tra!c light 1 • Parts: • LEDs: green, yellow, red • 3 suitable resistors • 1 potentiometer • Code: gist.github.com/ramalho/6202074 92

Slide 93

Slide 93 text

93 Tra!c light 1

Slide 94

Slide 94 text

gist.github.com/ramalho/6202074 code (needs fixing) Tra!c light 1

Slide 95

Slide 95 text

gist.github.com/ramalho/6202074

Slide 96

Slide 96 text

gist.github.com/ramalho/6202074

Slide 97

Slide 97 text

Tra!c light 2 97

Slide 98

Slide 98 text

Tra!c light 2 • Replace the potentiometer with an um LDR (light sensor) • To make this work, the LDR must be assembled with a resistor to make a voltage divider 98 LDR

Slide 99

Slide 99 text

Tra!c light 2 • Parts: • LEDs: green, yellow, red • 3 suitable resistors • 1 potentiometer • 1 LDR • 1 resistor for the voltage divider 99 LDR

Slide 100

Slide 100 text

gist.github.com/ramalho/6202074 same code, changes needed Tra!c light 1

Slide 101

Slide 101 text

Additional components 101

Slide 102

Slide 102 text

Re)ective optical sensor • Detects near objects (up to 25mm or 1in) • Emitter: infrared LED (invisible light) • Sensor: phototransistor protected against visible light • Use with voltage divider bezel 102

Slide 103

Slide 103 text

voltage divider 103 Re)ective optical sensor

Slide 104

Slide 104 text

104 reflective optical sensor voltage divider

Slide 105

Slide 105 text

7 segment display • 3 digits • model: CPS03631AB • typo: common anode • part number: CPS03631BR-11 pictured: CPS3631AR 105

Slide 106

Slide 106 text

11 digital pins needed to control: 8 for LED segments, 3 to select digit

Slide 107

Slide 107 text

Schematics from datasheet CPS03631AB 107 anode anode anode 7 segment display

Slide 108

Slide 108 text

7 segment display • “Common anode” means: all segments of each digit are attached to the same anode • To select a digit: 5V to the corresponding anode • To light a segment: GND to the corresponding cathode 108

Slide 109

Slide 109 text

• Anodes: 5V to pins 8, 9, 12 of the display select the active digit • GND to other pins light up segments A...G and decimal point (dp) 109 7 segment display

Slide 110

Slide 110 text

gist.github.com/ramalho/6566651 code: 7 segment display

Slide 111

Slide 111 text

Servomotor • Controlled movement in 180° arch • Easy programming using Arduino Servo library • examples included with the Arduino IDE 111

Slide 112

Slide 112 text

Servomotor 112 • You may need an external power source just for the servos External power source GND must connect to Arduino GND 5V DC Some servos draw more current than the Arduino can provide

Slide 113

Slide 113 text

113 TowerPro SG90 9g servo to external DC adapter 5V, 1A common ground Arduino powered by USB

Slide 114

Slide 114 text

Wrapping up 114

Slide 115

Slide 115 text

References • O!cial Arduino site: arduino.cc • Arduino Experimentation Kit: oomlout.com/a/products/ardx • Laboratório de garagem labdegaragem.com • Web: blogs, videos, wikis, diagrams... 115

Slide 116

Slide 116 text

arduino.cc • Seções do site 116

Slide 117

Slide 117 text

Fritzing.org • Fritzing software download and repository of projects 117

Slide 118

Slide 118 text

Some books • Arduino Básico, Novatec • Arduino Cookbook, O’Reilly • Make: Electronics, O’Reilly 118

Slide 119

Slide 119 text

Physical shops in São Paulo • Multcomercial, R. dos Timbiras, 257 • Side streets o# and parallel to Santa E"gênia, including junk stores • Laboratório de Garagem, Rua Berta, 60 (metrô Vila Mariana) • Neighborhood shops like Eletrônica Pinheiros, Rua Butantã, 133 119

Slide 120

Slide 120 text

Online shops • Laboratório de Garagem: labdegaragem.com • Farnell Newark: farnellnewark.com.br • Adafruit: adafruit.com • Sparkfun: sparkfun.com • Seeedstudio: seeedstudio.com • Many: dx.com, ebay.com (many sellers) seeedstudio with 3 “e”! 120

Slide 121

Slide 121 text

Seeedstudio Shield Bot • Line-following robot • Ready to go, only battery assembly is required • Fits an Arduino UNO under the chassics and shields above US $ 69.90 121

Slide 122

Slide 122 text

Visit a hackerspace! garoa.net.br 122

Slide 123

Slide 123 text

Credits Turing.com.br oficinas 123 • Slides created, compiled and organized by Luciano Ramalho (O"cinas Turing) with the help of many sources • License for use and copying: Creative Commons BY-SA • You may copy and use for any purpose as long as attribution is given and you share derivative works under the same terms