how to hack electronics
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
~Henri Matisse
Drawing is putting a line round an idea
Hacking is giving form to an idea
Hack to Learn
Hack to Communicate
Hack to Do
Hack to Do Think
How to Hack Electronics
PLANNING-NESS 2014
Hi. I’m Robert Gallup
Agenda
Mad Cap First Hacks Electricity + Electronics
Hacking Switches + Knobs + Smart LEDs Project? Wrap Up
Feel Free to Wander
?
EYour Hacker’s Kit
BREADBOARDSTEALTH CASE RESISTOR LED
SMA
RT LED
BUTTON KNOBJUMPER WIRES TRINKET
Microcontroller
>Put the Trinket in the Breadboard
>Connect the Trinket to your Computer
Glowing at FirstPower Indicator
>Download Software
http://www.robertgallup.com/download/planningness2014-mac.zip
Unzip Arduino Software
Note: There are also downloads for “-windows”, and “-linux”
Start Arduino
>Mac OS X: Security Settings
Open Preferences Unlock, and change “Allow apps …” to Anywhere
Run IMPORTANT: Reset security settings
1 2 3 4
>Configure the Software for Your Board
>Configure the Programmer
>Add the Planning-ness Examples
1
2 Select planningness2014 in the Libraries folder from the class software download
>If it works, …
?
>Load the LEDBlink Example
>The Sketch Window
Sketch (program)
>Run the Sketch on your Trinket
1 2
Press Reset
Press the Run button
LED glows
Wait. What?!
>The “Blink”
Blinking
ESketch Structure (Recipe)
// Svoid setup() { pinMode(1, OUTPUT); } !// Loopvoid loop() { digitalWrite(1, HIGH); delay(500); digitalWrite(1, LOW); delay(500); }
Statements
Comment
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 }
(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.
EYour First Circuit
Jumper Wire
LEDResistor
>Wire Up an LED
5V#2
#1
#0USB
RST#3
#4
GNDBAT
JUM
PER
WIR
E
EA Note about Breadboards
The “rift” separates the two sides
All of the contact points in the same row are connected
(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
(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 }
(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
EVariable Declaration
TYPE VALUENAME
int pinLED = 2;
Libraries Make It Easier
>Add the BobaBlox Library
1
2 Select BobaBlox Folder from the class software download
>Load and Run the LEDBlinkPinLibrary Example
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(); }
ELED Variable Declaration
TYPE PINNAME
LED boardLED(1);
EUsing a Function (method)
NAME METHOD NAME
boardLED.blink();
EAnother Version of the Blink Method
# TIMES
boardLED.blink(1, 100);
DELAY
(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.
(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.
(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.
EFor the Wanderer: Examples
EFor the Wanderer: Reference
?
Chips + Circuits
EA Microcontroller
!
!
microcontroller Input: temperature, motion, touch, …
ACTUATORS
SENSORS
PROGRAM
Output: light, motor, sound, heat, …
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.
Electronics
EElectricity Starts with Electrons
Electrons are negative (but, nice)
-
-
ESometimes Electrons Mingle
In conductive materials, electrons are free to float
--
---
-
-
- -
EMix in Some Universal Forces
NegativePositive
+-
Like Yin+Yang, or Good+Evil, one comes with the other
EElectricity = Motivated Electrons
-
-
-
- -
- -
-+-
Electrons are attracted by a positive force
EVoltage (volts) = How Motivated
-
+
-
-
-
-
ECurrent (amps) = How Many
+-
--
-
- -- -
-- -
-
--
-
-
Volts Jolt! Current Kills!
EActually, Current is a Little Twisted
+-
++
+
++
++
+ ++
+
++
+
+
Current flows positive to negative
But, Not Without a Circuit
+
This is a Schematic, BTW
+
Circuits use Components
EComponent: Resistor
+
HEAT
VOLTAGE DROP
+ + +++ ++
+++
EComponent: LED (Light Emitting Diode)
CATHODE / -
+
ANODE / +
-
?
More Hacks
EThe Button (Digital Input)
EComponent: Button (Switch)
CONNECTED
1
2PIN GND
PIN
GND
>Wiring Up a Single Button
5V#2
#1
#0USB
RST#3
#4
GNDBAT
JUM
PER
WIR
E JUM
PER WIRE
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(); } }
EPushbutton Variable Declaration
TYPE PINNAME
Button pushButton(1);
EPushbutton isDown Method
NAME METHOD NAME
pushButton.isDown();
EOther Pushbutton Methods
pushButton.isDown();
pushButton.isUp();
pushButton.isPressed();
pushButton.isReleased();
EBranching (One Thing or Another)
if (buttonState == 1) { boardLED.on(); } else { boardLED.off(); }
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)
(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?
EThe Knob (Analog Input)
EComponent: Knob (Potentiometer)
+5V
GND
Analog Pin
+5V GND
Analog Pin
EAnalog to Digital Converter (ADC)
ADCVoltage Values (0-1032)
0, 25, 100, 300, 100, 1023
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
>Wiring Up a Knob
5V#2
#1
#0USB
RST#3
#4
GNDBAT
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)
EKnob Variable Declaration
TYPE PINNAME
Knob bluKnob(1);
EKnob Value Method
NAME METHOD NAME
bluKnob.value();
(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.
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.
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).
>Wiring Up a Smart LED (NeoPixel)
5V#2#1#0USB
RST#3#4GNDBAT
(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.
(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.
(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.
?
Wax On. Wax Off.
LEDs
Buttons
Knobs (potentiometers)
Smart LED
Team Project
(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.
Wrap Up
EMany Other Platforms
RPi BeagleBoneArduino
spark.io
pinocc.io
teensy
EAdd-On Boards (Shields)
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”
ENext Steps: Arduino Comic
http://www.jodyculkin.com/wp-content/uploads/2011/09/arduino-comic-latest3.pdf
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 !
Thank You!
Robert Gallup [email protected] @robertgallup