39
Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM tp://scn.sap.com/community/developer-center/hana/blog/2014/12/08/iot-with-sap-hana-dev

Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Embed Size (px)

Citation preview

Page 1: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Beginning ArduinoSession 2

AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

http://scn.sap.com/community/developer-center/hana/blog/2014/12/08/iot-with-sap-hana-dev

Page 2: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

2.0 – Welcome and Introduction2.1 – Components used in this Project.2.2 – Exploring a Switch and its effects in a digital system.

2.3 – De-bouncing a switch 2.3.1 – Blink Without Delay 2.4 – Exploring the 4 main uses for a resistor 2.4.1 – Current Limiting 2.4.2 – Voltage Division 2.4.3 – Pull Down 2.4.4 – Pull Up

Today’s Agenda

Page 3: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

2.5 – Exploring Speakers and Piezo Buzzers 2.5.1 – How a speaker works vs a Piezo speaker 2.6 – PWM outputs, what are they? A detailed look into Pulse width Modulation

2.7 – The RGB LED 2.8 – Exploring Libraries. 2.9 – Using Tabs, creating our own Lib 2.10 – Using integer Arrays 2.11 – Bringing it all together: Name That Tune 2.12 – Serial Feedback 2.13 - Shields

Today’s Agenda (cont.)

Page 4: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

TAC Switch x 4Peizo Buzzer x 1RGB Breakout x 1

Components used in this Project.

Page 5: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Circuit:

Page 6: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

When a switch is pressed in a circuit it casues the line voltage to “bounce”

Bouncing

Page 7: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Sample of a switch press

Page 8: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

When timing is crucial or multiple things need to happen during the void loop(), the delay() command can become a big problem.

Open example > Basic > Blink

Blink

Page 9: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

void setup() { pinMode(13,OUTPUT); pinMode(9,OUTPUT); pinMode(2,INPUT); }

Modify the Blink Sketch setup

Page 10: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

void loop() { digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(13, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second if(digitalRead(2) == LOW) digitalWrite(9,HIGH); else digitalWrite(9,LOW); }

Modify the Blink Sketch loop

Page 11: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Open sketch examples > Digital > Blink without delay

Blink Without delay()

Page 12: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Modify the sketch in the same way, what happens differently?

Modify the sketch

Page 13: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Current Limiting Voltage Division Pull Down Pull Up

Four Main uses for a Resistor

Page 14: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Current Limiting Resistor

Page 15: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Voltage Divider

Page 16: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Pull-UP

Page 17: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

void setup() { pinMode(6,OUTPUT); }

void loop() { if(digitalRead(2) == LOW) digitalWrite(6,HIGH); else digitalWrite(6,LOW); }

Example of INPUT_PULLUP

Page 18: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

The Piezo Buzzer

Page 19: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Pulse Width ModulationIs a DC voltage that is turned ON and OFF at different rates. The voltage has 2 states, 0 and Vcc

PWM

Page 20: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

PWM

Page 21: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

A Low pass Filter will smooth a PWM signal back to a DC voltage.

Low Pass Filter

Page 22: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

• Single Color• Bi-Color• Tri-Color• RGB

Types of LEDs

Page 23: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

The RGB LED provided in your kit is mounted on a Breakout board.

A breakout board provides all the nessesary supporting electronics so it can be used easily and quickly for proto typeing.

The RGB LED with your Kit

Page 24: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

void setup() { pinMode(6,OUTPUT); pinMode(9,OUTPUT); pinMode(10,OUTPUT); }

void loop() { digitalWrite(9,HIGH); delay(300); digitalWrite(9,LOW); digitalWrite(10,HIGH); delay(300); digitalWrite(10,LOW); digitalWrite(6,HIGH); delay(300); digitalWrite(6,LOW); }

RGB LED test

Page 25: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Libraries are chunks of code writen for specific, and often repetitive task.

# include <Lib_Name>or

# include “Lib_Name”

Libraries

Page 26: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Go to – https://github.com/shirriff/Arduino-IRremote/releases

Download the IRremote.zip file

Download and install Libraries.

Page 27: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Method 1 Automatic – Go to Sketch > Input Library > Add Library. Navigate to the file just downloaded and select it.

Method 2 Manually extract the Library and copy the folder to the Lib folder within the Arduino sketch folder.

Two ways to install a Library

Page 28: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

// Our Lib of Pitches

#define NOTE_G1 49 #define NOTE_B2 123 #define NOTE_F3 175 #define NOTE_G4 392

Create a Library

Page 29: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

An array is a collection of variables that are accessed with an index number.

Arrays are Zero indexed.

Arrays

Page 30: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

// Call my Lib #include "pitches.h"

// Define my Array int BP[] = {NOTE_G4, NOTE_B2, NOTE_F3, NOTE_G1};

Bringing it all together

Page 31: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

// Declare my variabls int INDEX = 2; // Start button

int redLEDPin = 9; int greenLEDPin = 10; int blueLEDPin = 6;

Bringing it all together

Page 32: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

// Set my pin modes void setup() { pinMode(2,INPUT_PULLUP); pinMode(3,INPUT_PULLUP); pinMode(4,INPUT_PULLUP); pinMode(5,INPUT_PULLUP); pinMode(13,OUTPUT); pinMode(redLEDPin,OUTPUT); pinMode(greenLEDPin,OUTPUT); pinMode(blueLEDPin,OUTPUT); }

Bringing it all together

Page 33: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

// My loop void loop() { if(digitalRead(INDEX) == LOW) { tone(8,BP[INDEX-2]); RGBWrite(random(100),random(100),random(100)) ; while(digitalRead(INDEX) == LOW) { } RGBWrite(0,0,0); noTone(8); } INDEX=INDEX+1; if(INDEX > 5) INDEX = 2; }

Bringing it all together

Page 34: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Press keys in this order

1 – 2 – 3 – 4 – 2

Name that tune

Page 35: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

There is 1 UART (universal asynchronous receiver/transmitter) on the Arduino Uno

The Serial port uses Pin 0 and 1

Pin 0 = RX or Data Receive Pin 1 = TX or Data Transmit

The USB connection uses the same UART

Serial Feedback

Page 36: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

int R = random(100); int G = random(100); int B = random(100); if(digitalRead(INDEX) == LOW) { Serial.print(INDEX); Serial.print("|"); Serial.print(R); Serial.print("|"); Serial.print(G); Serial.print("|"); Serial.println(B); // Serial.println() adds a line feed to the end tone(8,BP[INDEX-2]); RGBWrite(R,G,B) ;

Add to the script

Page 37: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

A Shield is a supportive board that plugs into the Arduino to extend the Arduino’s ability and reduces the amount of bread boarding needed.

There’s a Shield for that…

Page 39: Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM

Challenge #1 – Use a switch-case and make specific colors light up based on the tone being played.

Challenge #2 – Team up and use 8 buttons to make a 8 Key Keyboard

Challenge #3 Use the Ir Lib to play sounds from your Remote.

Challenge