how to hack electronics

111
~Henri Matisse Drawing is putting a line round an idea

Upload: planning-ness

Post on 01-Nov-2014

764 views

Category:

Devices & Hardware


6 download

DESCRIPTION

In this session IOT expert Bob Gallup walked us through the basics of electronics and taught us how to do some basic wiring and programming on an Adafruit Trinket micro processor.

TRANSCRIPT

Page 1: How to hack electronics

~Henri Matisse

Drawing is putting a line round an idea

Page 2: How to hack electronics

Hacking is giving form to an idea

Page 3: How to hack electronics

Hack to Learn

Page 4: How to hack electronics

Hack to Communicate

Page 5: How to hack electronics

Hack to Do

Page 6: How to hack electronics

Hack to Do Think

Page 7: How to hack electronics

How to Hack Electronics

PLANNING-NESS 2014

Page 8: How to hack electronics

Hi. I’m Robert Gallup

Page 9: How to hack electronics

Agenda

Mad Cap First Hacks Electricity + Electronics

Hacking Switches + Knobs + Smart LEDs Project? Wrap Up

Page 10: How to hack electronics

Feel Free to Wander

Page 11: How to hack electronics

?

Page 12: How to hack electronics

EYour Hacker’s Kit

BREADBOARDSTEALTH CASE RESISTOR LED

SMA

RT LED

BUTTON KNOBJUMPER WIRES TRINKET

Page 13: How to hack electronics

Microcontroller

Page 14: How to hack electronics

>Put the Trinket in the Breadboard

Page 15: How to hack electronics

>Connect the Trinket to your Computer

Glowing at FirstPower Indicator

Page 16: How to hack electronics

>Download Software

http://www.robertgallup.com/download/planningness2014-mac.zip

Unzip Arduino Software

Note: There are also downloads for “-windows”, and “-linux”

Page 17: How to hack electronics

Start Arduino

Page 18: How to hack electronics

>Mac OS X: Security Settings

Open Preferences Unlock, and change “Allow apps …” to Anywhere

Run IMPORTANT: Reset security settings

1 2 3 4

Page 19: How to hack electronics

>Configure the Software for Your Board

Page 20: How to hack electronics

>Configure the Programmer

Page 21: How to hack electronics

>Add the Planning-ness Examples

1

2 Select planningness2014 in the Libraries folder from the class software download

Page 22: How to hack electronics

>If it works, …

Page 23: How to hack electronics

?

Page 24: How to hack electronics

>Load the LEDBlink Example

Page 25: How to hack electronics

>The Sketch Window

Sketch (program)

Page 26: How to hack electronics

>Run the Sketch on your Trinket

1 2

Press Reset

Press the Run button

LED glows

Page 27: How to hack electronics

Wait. What?!

Page 28: How to hack electronics

>The “Blink”

Blinking

Page 29: How to hack electronics

ESketch Structure (Recipe)

//  Svoid  setup()  {                                    pinMode(1,  OUTPUT);        }  !//  Loopvoid  loop()  {      digitalWrite(1,  HIGH);        delay(500);                              digitalWrite(1,  LOW);          delay(500);                          }

Statements

Comment

Page 30: How to hack electronics

ESketch Walkthrough (See the Comments)

//  Setup  runs  once  at  the  beginning  void  setup()  {                                    pinMode(1,  OUTPUT);         //  Pin  #1  is  output        }  !//  Loop  runs  over  and  over  again  forever  void  loop()  {      digitalWrite(1,  HIGH);    //  turn  the  LED  on      delay(1000);                        //  wait  for  a  second      digitalWrite(1,  LOW);      //  turn  the  LED  off      delay(1000);                        //  wait  for  a  second  }

Page 31: How to hack electronics

(Change the Blinking SpeedHack 01:

1. Change the blink speedHint: edit the blink sketch to change the blink delay from 1 second to 1/4 second (note: 1 second = 1000 milliseconds). Then run the sketch again.

2. Stretch: Make a short on, longer off blink Hint: the delay times don’t have to be the same.

Page 32: How to hack electronics

EYour First Circuit

Jumper Wire

LEDResistor

Page 33: How to hack electronics

>Wire Up an LED

5V#2

#1

#0USB

RST#3

#4

GNDBAT

JUM

PER

WIR

E

Page 34: How to hack electronics

EA Note about Breadboards

The “rift” separates the two sides

All of the contact points in the same row are connected

Page 35: How to hack electronics

(An LED on Another PinHack 02:

1. Rewire your hack to move the LED to Trinket pin #2.

2. In the “blink” sketch, change the LED pin to match. Hint: You may have to change more than one line

Page 36: How to hack electronics

(A SolutionHack 02:

//  Setup  runs  once  void  setup()  {                                    pinMode(2,  OUTPUT);         //  Pin  #2  is  output        }  !//  Loop  repeats  forever  void  loop()  {      digitalWrite(2,  HIGH);    //  turn  the  LED  on      delay(1000);                        //  wait  for  a  second      digitalWrite(2,  LOW);      //  turn  the  LED  off      delay(1000);                        //  wait  for  a  second  }

Page 37: How to hack electronics

(Another Way, “Variable”Hack 02:

//  Declarations  int  pinLED  =  2;  !//  Setup  runs  once  at  the  beginning  void  setup()  {                                      pinMode(pinLED,  OUTPUT);         //  Pin  is  output        }  !//  Loop  runs  over  and  over  again  forever  void  loop()  {      digitalWrite(pinLED,  HIGH);       //  turn  the  LED  on      delay(1000);                             //  wait  for  a  second      digitalWrite(pinLED,  LOW);         //  turn  the  LED  off      delay(1000);                               //  wait  for  a  second  }

pinLED = 2

Declare Variable

Page 38: How to hack electronics

EVariable Declaration

TYPE VALUENAME

int pinLED = 2;

Page 39: How to hack electronics

Libraries Make It Easier

Page 40: How to hack electronics

>Add the BobaBlox Library

1

2 Select BobaBlox Folder from the class software download

Page 41: How to hack electronics

>Load and Run the LEDBlinkPinLibrary Example

Page 42: How to hack electronics

EWhat’s Different About This Sketch?

//  Include  the  library  #include  <BobaBlox.h>  !//  Declarations  LED  boardLED(1);            //  Declare  an  LED  on  pin  #1  !//  Setup  runs  once  at  the  beginning  void  setup()  {                                  }  !//  Loop  repeats  forever.  Use  the  “blink”  method  to  blink  boardLED  void  loop()  {    boardLED.blink();      }

Page 43: How to hack electronics

ELED Variable Declaration

TYPE PINNAME

LED boardLED(1);

Page 44: How to hack electronics

EUsing a Function (method)

NAME METHOD NAME

boardLED.blink();

Page 45: How to hack electronics

EAnother Version of the Blink Method

# TIMES

boardLED.blink(1, 100);

DELAY

Page 46: How to hack electronics

(An LED on Another PinHack 03:

1. Change the library Blink sketch to use an LED on pin #3Hint: You can add a 2nd LED, or move the LED you connected in the previous hack.

Page 47: How to hack electronics

(Fast BlinkHack 04:

1. Change the library sketch to blink faster Hint: look at the example of the LED blink method that uses two parameters, times and delay.

Page 48: How to hack electronics

(Blink Morse CodeHack 05:

1. Load and run the SOS sketch from planningness2014 examples.Note: In Morse Code, “S” is dot-dot-dot, “O” is dash-dash-dash.

2. Change the sketch so it blinks “CQ”. Hint: In Morse Code, “C” is dot-dash-dot-dash, “Q” is dash-dash-dot-dash. Note: “CQ” is a little like “YO” in the ham radio world.

Page 49: How to hack electronics

EFor the Wanderer: Examples

Page 50: How to hack electronics

EFor the Wanderer: Reference

Page 51: How to hack electronics

?

Page 52: How to hack electronics

Chips + Circuits

Page 53: How to hack electronics

EA Microcontroller

!

!

microcontroller Input: temperature, motion, touch, …

ACTUATORS

SENSORS

PROGRAM

Output: light, motor, sound, heat, …

Page 54: How to hack electronics

E

+5V (Regulated)

0

1

2

Digital I/O

Ground

4

3

LED (Pin 1)

USB

Reset Button

Power LEDADAFRUITTRINKET

The Trinket Basic Connections (pinout)

I/O Pins connect the microcontroller to power, sensors, and actuators.

Page 55: How to hack electronics

Electronics

Page 56: How to hack electronics

EElectricity Starts with Electrons

Electrons are negative (but, nice)

-

-

Page 57: How to hack electronics

ESometimes Electrons Mingle

In conductive materials, electrons are free to float

--

---

-

-

- -

Page 58: How to hack electronics

EMix in Some Universal Forces

NegativePositive

+-

Like Yin+Yang, or Good+Evil, one comes with the other

Page 59: How to hack electronics

EElectricity = Motivated Electrons

-

-

-

- -

- -

-+-

Electrons are attracted by a positive force

Page 60: How to hack electronics

EVoltage (volts) = How Motivated

-

+

-

-

-

-

Page 61: How to hack electronics

ECurrent (amps) = How Many

+-

--

-

- -- -

-- -

-

--

-

-

Page 62: How to hack electronics

Volts Jolt! Current Kills!

Page 63: How to hack electronics

EActually, Current is a Little Twisted

+-

++

+

++

++

+ ++

+

++

+

+

Current flows positive to negative

Page 64: How to hack electronics

But, Not Without a Circuit

+

Page 65: How to hack electronics

This is a Schematic, BTW

+

Page 66: How to hack electronics

Circuits use Components

Page 67: How to hack electronics

EComponent: Resistor

+

HEAT

VOLTAGE DROP

+ + +++ ++

+++

Page 68: How to hack electronics

EComponent: LED (Light Emitting Diode)

CATHODE / -

+

ANODE / +

-

Page 69: How to hack electronics

?

Page 70: How to hack electronics

More Hacks

Page 71: How to hack electronics

EThe Button (Digital Input)

Page 72: How to hack electronics

EComponent: Button (Switch)

CONNECTED

1

2PIN GND

PIN

GND

Page 73: How to hack electronics

>Wiring Up a Single Button

5V#2

#1

#0USB

RST#3

#4

GNDBAT

JUM

PER

WIR

E JUM

PER WIRE

Page 74: How to hack electronics

ELoad / Run the ButtonLED Example

//  Declarations  LED  boardLED(1);                Button  pushButton(2);      int  buttonState;                !//  Setup  runs  once  at  the  beginning  void  setup()  {                                  }  !//  Loop  repeats  forever  void  loop()  {      //  Read  the  switch.  Set  LED  on/off  according  to  button  state      buttonState  =  pushButton.isDown();      if  (buttonState  ==  1)  {     boardLED.on();      }  else  {        boardLED.off();      }    }  

Page 75: How to hack electronics

EPushbutton Variable Declaration

TYPE PINNAME

Button pushButton(1);

Page 76: How to hack electronics

EPushbutton isDown Method

NAME METHOD NAME

pushButton.isDown();

Page 77: How to hack electronics

EOther Pushbutton Methods

pushButton.isDown();

pushButton.isUp();

pushButton.isPressed();

pushButton.isReleased();

Page 78: How to hack electronics

EBranching (One Thing or Another)

if  (buttonState  ==  1)  {     boardLED.on();  }  else  {      boardLED.off();  }  

Page 79: How to hack electronics

EThe If statement (if/then/else)

if  (condition)  {  

     •••  

}  else  {        •••  

}

if condition is true

if condition is false

conditions:(a == b)(a < b)(a > b)(a <= b)(a >= b)(a != b)

Page 80: How to hack electronics

(Reverse the LogicHack 06:

1. Reverse the logic in the buttonLED sketch. I.e. make the LED go OFF when the button is pressed. Hint: there are two ways to do this

2. Stretch: make the LED blink when the button is pressed.Hint: what other LED functions do you have to work with?

Page 81: How to hack electronics

EThe Knob (Analog Input)

Page 82: How to hack electronics

EComponent: Knob (Potentiometer)

+5V

GND

Analog Pin

+5V GND

Analog Pin

Page 83: How to hack electronics

EAnalog to Digital Converter (ADC)

ADCVoltage Values (0-1032)

0, 25, 100, 300, 100, 1023

Page 84: How to hack electronics

E

+5V (USB)

0

1

2

0

1

1

Digital I/O

Analog Output

Analog Input +5V (Regulated)

Battery

Ground

4

3 3

2

Reset

LED (Pin 1)

USB

Reset Button

Power LEDADAFRUITTRINKET

The Trinket: A More Complete Picture

Page 85: How to hack electronics

>Wiring Up a Knob

5V#2

#1

#0USB

RST#3

#4

GNDBAT

Page 86: How to hack electronics

ELoad / Run the knobDimLED Example

LED  boardLED(1);                Knob  bluKnob(1);                !int  knobValue;                    int  LEDBrightness;      !void  setup()  {                                  }  !void  loop()  {            //  Check  the  knob  value      //  Convert  the  knob  value  (0-­‐1023)  to  a  brightness  (0-­‐255).      //  Note:  see  the  Map  command  in  the  reference      knobValue  =  bluKnob.value();      LEDBrightness  =  map(knobValue,  0,  1023,  0,  255);      boardLED.setBrightness(LEDBrightness);        }

.setBrightness()(see reference)

Page 87: How to hack electronics

EKnob Variable Declaration

TYPE PINNAME

Knob bluKnob(1);

Page 88: How to hack electronics

EKnob Value Method

NAME METHOD NAME

bluKnob.value();

Page 89: How to hack electronics

(Change Knob DirectionHack 07:

1. Change the direction of the know. I.e. So the brightness goes down when the knob it turned up.

2. Stretch: Change the blink speed with the knob position.

Page 90: How to hack electronics

EThe Smart LED (Digital LED)

Unlike a standard LED, the Smart LED has a small processor inside that controls the light and communicates digitally with your microprocessor. They are also called NeoPixels. Generically, they are sometimes referred to by their “controller”, the WS2812 or WS2811.

Page 91: How to hack electronics

EComponent: Smart LED (NeoPixel)

Dig

ital In+

5V

GN

DD

igital O

ut

0 1 2 3 4 5 6 7

Digital In

Multiple of these LEDs can be “cascaded” from a singe pin on your micro process (but, you may need outside power).

Page 92: How to hack electronics

>Wiring Up a Smart LED (NeoPixel)

5V#2#1#0USB

RST#3#4GNDBAT

Page 93: How to hack electronics

(Hack the SmartPixel ExampleHack 08:

1. After wiring the SmartPixel up, load and run the SmartPixel sketch.

2. Change the rate the colors change in the sketch 3. The sketch currently uses red, green, and blue as it’s

colors. Change to three different colors Hint: look at the comments to figure out the parameters for the setPixelColor() method.

Page 94: How to hack electronics

(Hack the SmartPixelRainbow SketchHack 09:

1. Load and run the SmartPixelRainbow sketch.

2. Change the rate the colors change in the sketch. Hint: You can guess what to do (perfectly respectable), or look at the complicated code in the NeoPixelFunctions tab for a clue.

Page 95: How to hack electronics

(More Smart LED HacksHack 10:

1. BIG STRETCH: See if you can figure out how to control the rainbow color with a knob. Hint: look at the setPixelColor() and wheel() methods.

2. GIANT STRETCH: See you can cooperate with some of your table-mates and connect several smart LEDs together. Hint: one of the parameters when you declare the LED variable, is the number of LEDs in the strip.

Page 96: How to hack electronics

?

Page 97: How to hack electronics

Wax On. Wax Off.

Page 98: How to hack electronics

LEDs

Page 99: How to hack electronics

Buttons

Page 100: How to hack electronics

Knobs (potentiometers)

Page 101: How to hack electronics

Smart LED

Page 102: How to hack electronics

Team Project

Page 103: How to hack electronics

(Hack Digital PersonalityHack 99:

1. Team up and use everything you’ve learned to design a switch with a personality. Hint: use two LEDs and a switch. pressing the switch turns one of the LEDs on/off. The other LED flashes in some way with personality to indicate whether you’ve been successful. Note: could you use a knob as the switch?

2. Do anything else you want to try as an individual. Or, as a team.

Page 104: How to hack electronics

Wrap Up

Page 105: How to hack electronics

EMany Other Platforms

RPi BeagleBoneArduino

spark.io

pinocc.io

teensy

Page 106: How to hack electronics

EAdd-On Boards (Shields)

Page 107: How to hack electronics

ENext Steps: Online Tutorials

http://arduino.cc/en/Tutorial http://playground.arduino.cc https://learn.sparkfun.com !

http://learn.adafruit.com http://learn.adafruit.com/category/learn-arduino !

!

google “arduino tutorials”

Page 108: How to hack electronics

ENext Steps: Arduino Comic

http://www.jodyculkin.com/wp-content/uploads/2011/09/arduino-comic-latest3.pdf

Page 109: How to hack electronics

EOnline Resources

ARDUINO: www.arduino.cc, reference, tutorials SPARKFUN: www.sparkfun.com, components, devices, tutorials ADAFRUIT: www.adafruit.com, components, devices, tutorials MAKE MAGAZINE: www.makezine.com, magazine, books, stuff MAKER SHED: www.makershed.com, components, kits, stuff INSTRUCTABLES: www.instructables.com, tutorials RADIO SHACK: www.radioshack.com, components, devices, tutorials AMAZON: www.amazon.com, books, components, devices, tutorials O’REILLY: www.oreilly.com, books, tutorials, etc.

ELECTRONIC GOLDMINE: http://www.goldmine-elec-products.com, components JAMECO: www.jameco.com, components MOUSER: www.mouser.com, components DIGIKEY: www.digikey.com, components !

Page 110: How to hack electronics

Thank You!

Page 111: How to hack electronics

Robert Gallup [email protected] @robertgallup