22
Sungmin Kim SEOUL NATIONAL UNIVERSITY Smart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in their most basic form A tiny computer which is the brain of the project It is so small that it can live in a cloth or other wearable things In this Chapter... Arduino and Arduino compatible products will be introduced Developed by IDII (Interaction Design Institutelvera, Italy) in 2005 An open source electronics prototyping platform Intended to be used by anyone who are not engineers but wants to make a physical interactive project » Artists » Designers » Educators » Hobbyists The name ‘Arduino’ refers to both the hardware and the software 2

Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

  • Upload
    vukhanh

  • View
    232

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Sungmin Kim

SEOUL NATIONAL UNIVERSITY

Smart Garment Design

7. Microcontrollers

Introduction� Microcontrollers

� Basics

� Computers in their most basic form

� A tiny computer which is the brain of the project

� It is so small that it can live in a cloth or other wearable things

� In this Chapter...

� Arduino and Arduino compatible products will be introduced

– Developed by IDII (Interaction Design Institutelvera, Italy) in 2005

– An open source electronics prototyping platform

– Intended to be used by anyone who are not engineers but wants to make a physical interactive project

» Artists

» Designers

» Educators

» Hobbyists

– The name ‘Arduino’ refers to both the hardware and the software

2

Page 2: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Hardware� Arduino Board

� A printed circuit board that contains a microcontroller and related components

� Pin breakouts, status LEDs, reset button, and so on

3

Status LEDs

Hardware� Arduino Boards

� Currently a variety of configurations are available

� http://Arduino.cc/en/Main/Products

4

Page 3: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Hardware� Arduino Boards

� Entry level

� Enhanced features

5

Shield stacking for various I/O modules

Hardware� Arduino Boards

� Internet of things (IoT)

� Wearables

6

Page 4: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Hardware� Arduino Boards

� Most Common Boards

� Arduino Uno

– A reasonable amount of functionality all in an accessible package– Quite bulky from a wearable electronic perspective

� LilyPad Arduino

– Arduino in a LilyPad package– Has ‘petals’ or ’sewtabs’ placed around the edge of the circuit board– Easy to make electrical connections using conductive thread– Has the same microcontrollers and number of inputs and outputs as the Arduino Uno

7

Hardware� E-Textile Toolkits

� Definitions

� E-Textile

– A category of electronic parts that can be used in combination with textiles and other soft materials

� Toolkit

– A set of tools that can be used for any number of purposes

– Different from a ‘kit’ as a kit’s parts are usually meant to be used together to assemble a single thing

– Other terms you might run into that mean roughly the same thing are ‘platform’ or ‘system’

� Module

– A discrete unit that is part of a toolkit

– Printed circuit boards that contain electronic components and their necessary connections

– Can be connected together using conductive materials to create a complete circuit

8

Page 5: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Hardware� E-Textile Toolkits

� Microcontroller

� LilyPad Arduino 328 (SparkFun, 2007)

– The first set of widely available electronic components

– Specifically intended for integration with nontraditional conductive materials

9

Sewable

• Modules are designed to enable electrical connections made with hand sewn conductive thread

• Connection points are situated around the perimeter of the boards (sew tabs, sew holes, petals)

Rounded edges • Works well in the context of floppy flexible fabric substances

Thin • A bit thinner than a traditional circuit board• Easier to incorporate into a lining, pocket, or seam

Purple • Make them friendlier, a lot more attractive

Hardware� E-Textile Toolkits

� Microcontroller

� LilyPad Arduino Simple

10

FTDI (Future Technology Devices International)Headers for USB connection

JST Connector for battery

Ground (-)

Power (+)

Analog Inputs

Programmable ports :Digital I/Os or Analog Outputs

Page 6: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Hardware� E-Textile Toolkits

� Microcontroller

� LilyPad Simple Snap

– A rechargeable lithium-polymer battery is integrated into the board– Pins on the board are outfitted with female snaps

» A breakout board with matching male snaps can mate with it» Arduino and battery portion can be removed when a project is washed

11

Hardware� E-Textile Toolkits

� Module

� Sensors

� Actuators

12

Light Sensor Temperature Sensor Accelerometer Button

Buzzer Vibrating Motor Micro LED LED Tri-Color LED Lily Pixel

Page 7: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Hardware� E-Textile Toolkits

� Module

� Others

13

Protoboards Battery holder Battery connector

XBee

Hardware� E-Textile Toolkits

� Module

� Special boards

14

Twinkle Tiny

• Lights LEDs at random

• Works well for ambient lights

• Lights LEDs at random

• Used to breathing fade (0), heart beat (1), steady blink

(2), random fade (3)

0

1

2

3

Page 8: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Hardware� E-Textile Toolkits

� Flora

� Created by Adafruit Industries in New York

– Newest e-textiles toolkit to hit the wearables scene– Makes some significant leaps forward from an engineering perspective– Offers an exciting selection of new modules

� Microcontroller

– Main board » Slightly smaller than a LilyPad Arduino and has a built-in USB support

– Gemma» Even smaller board that offers some of the functionality of the main board

15

Hardware� E-Textile Toolkits

� Flora

� Sensors

� Actuators

16

Neo Pixel

Lux Sensor Color Sensor Accelerometer/Compass GPS

Page 9: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Hardware� E-Textile Toolkits

� Aniomagic

� Graphical programming interface

– You can determine the behavior of the LEDS with drop-down menus and sliders rather than code

� Optical programming

– Program is transmitted onscreen through the display of a rapid sequence of shapes– No need for a USB cable

17

Sparklefor

LEDs only

Chicletfor

Sensors

Light board Sound Sensor

Hardware� E-Textile Toolkits

� Batteries

� Characteristics

– Convert chemical energy to electrical energy– There are two types of batteries : primary (single use) and secondary (rechargeable)

18

Round Nonround Coin Cell Lithium-ion/Lithium-ion Polymer

Cylindrical shape and usually provide 1.5V depending on their chemistry

9V batteries are the type form this category that are most likely to be used for wearables

Small and thin that excellent for low-current wearable applications

A bit more expensive but can be used again and again in a variety of projects

Page 10: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Software� IDE (Integrated Development Environment) Basics

� Blank Sketch

19

Verify :Check the code and indicates if there are any errors in syntax

Upload :Compiles the code and uploads it to the Arduino board

New :Opens a new sketch

Open :Opens an existing sketch

Save :Saves your sketch

Serial Monitor :Opens the serial monitor where you can view data that is being sent and received

Where you write your code

Where you will see status updates

Where you will see information about errors

Software� IDE Basics

20

There are lots of helpful example sketches

Page 11: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Software� IDE Basics

� Core Codes

21

/*AnalogReadSerialReads an analog input on pin 0, prints the result to the serial monitor.Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.This example code is in the public domain.*/

void setup(){

// initialize serial communication at 9600 bits per second:Serial.begin(9600);

}

void loop(){

// read the input on analog pin 0:int sensorValue = analogRead(A0);// print out the value you read:Serial.println(sensorValue);delay(1); // delay in between reads for stability

}

Multilinecomment

Commands that are to happen only when the program first begins

Single linecomment

Commands that will happen over and over again

I/O Connections� Digital Output

� The Circuit

� Connect LilyPad LED to pin 11

� Connect a through hole LED to pin 11

� Connect multiple LEDs in parallel to pin 11

22

Page 12: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

I/O Connections� Digital Output

� The Code

� Variables

– Provide a way to name and store values

» Reading from a switch or sensor

» Constant value like a particular pin number

– There are many different variable types

» Integer, double, character...

� Functions

– pinMode(pin, mode)

» Sets a digital pin as either an input or an output

» mode can be either INPUT or OUTPUT

» This command is included in the setup so that the pin’s behavior is determined at the beginning

– digitalWrite(pin, value)

» value can be either HIGH (V+ at 40mA) or LOW (off)

– delay(time)

» Pauses program for some time in milliseconds

23

int led=11;

void setup() {

pinMode(led, OUTPUT);}

void loop(){

digitalWrite(led, HIGH);delay(1000); digitalWrite(let, LOW);delay(1000);

}

I/O Connections� Digital Input

� The Circuit

� Connect a switch from any digital input pin to either power(+) or ground (-)

– To power, the pin will read “HIGH” when the switch is closed and “LOW” when it’s open– To ground, the logic will be reversed

� Connect a pull up resistor

– When the switch is open, the input pin will float» A floating pin has no reliable reference and can produce erratic values» A pull up resistor of a large enough value (>10kΩ)will prevent floating pin

24

power

ground

input

Switch is connected to power Switch is connected to ground Use an internal pull up resistor

Page 13: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

I/O Connections� Digital Input

� Floating

25

Digital InputHIGH

+5V

Digital InputLOW

GND

Digital InputHIGH ? LOW ?

+5V

Electric field(Motor, Hair dryer, Vacuum cleaner..)

Digital InputHIGH ? LOW ?

GND

Digital InputHIGH

Digital InputLOW

+5V

GND

+5V

GND

Digital InputLOW

+5V

GND

Pull-up

Pull-down

I/O Connections� Digital Input

� The Code

� Functions

– pinMode(pin, mode)

» mode can be either INPUT or INPUT_PULLUP (to use an internal pull up resistor)

– digitalRead(pin)

» The opposite of digitalWrite function

» Reads the voltage coming into a pin

» Needs a variable to store the information that is read

� Serial Monitor functions

– A way to monitor the values you’re getting into Arduino

– Serial.begin(speed)

» Initializes the serial connection and sets the speed of communication in baud

– Serial.println(val)

» Transmits a value followed by a carriage return

26

Page 14: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

I/O Connections� Digital Input

� The Code

27

int buttonPin=5;int buttonValue=0;

void setup(){

Serial.begin(9600);pinMode(buttonPin, INPUT_PULLUP);

}

void loop(){

buttonValue=digitalRead(buttonPin);Serial.println(buttonValue);delay(100);

}

if (buttonValue==HIGH){

digitalWrite(led, HIGH);}else{

digitalWrite(led, LOW);}

Example : Button as Controller

ground

input

output

I/O Connections� Analog Input

� Digital vs Analog

� Digital

– Refers to a binary state» On or off, high or low, voltage flowing or not flowing, 1 or 0

– There are only two possible states

� Analog

– Refers to a range of values» Light sensor can tell you if it’s light, dark, or somewhere in between» Brightness of an LED can be controlled continuously

28

Page 15: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

I/O Connections� Analog Input

� The Circuit

� Connect a LilyPad light sensor

– Power, ground, and analog input– Sensor will output between 0 and V+ depending on the light level it senses

» 0V indicates the darkest» V+ indicates the brightest

29

analog in

ground

power

I/O Connections� Analog Input

� The Code

� Functions

– analogRead(pin)» Reads the value of a specified analog pin» pin can either be ‘2’ or ‘A2’ for analog input» pinMode does not need to be set for an analog input

30

int lightSensorValue=0;int lighSensorPin=A2;

void setup(){

Serial.begin(9600);}

void loop(){

lightSensorValue=analogRead(lightSensorPin);Serial.println(lightSensorValue);delay(100);

}

if (buttonValue==HIGH){digitalWrite(led, HIGH);}

else{digitalWrite(led, LOW);}

Example : Button as Controller

if (lightSensorValue>500){digitalWrite(led, HIGH);}

else{digitalWrite(led, LOW);}

Page 16: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

I/O Connections� Analog Output

� Use

� Provides a range of values rather than simply turning something on or off

– Brighten or dim an LED with subtlety

– Control the speed of a motor

� The Circuit

� Pulse Width Modulation (PWM)

– Analog output pin does not produce a range of voltages

» Simulates an analog effect by pulsing 5 volts in differing duty cycles

» If the pin is quickly switched back and forth between

0V and 5V, it creates the 2.5V effect

– Digital output pins can function as PWM pins

31

digital outputas PWM

I/O Connections� Analog Output

� The Code

� Functions

– analogWrite(pin, value)» value can be between 0 and 255 (0~V+)

32

int LEDpin=11;

void setup(){

pinMode(LEDpin, OUTPUT);}

void loop(){

analogWrite(LEDpin,0);delay(100);analogWrite(LEDpin,64);delay(100);analogWrite(LEDpin,128);delay(100);analogWrite(LEDpin,255);delay(100);

}

ground

power

analog in

analog out

Experiment : Sensitive System

Page 17: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Wireless Communication� Basics

� Importance

� Wired communication can easily be accomplished, but not practical in the wearable context

� Wires physically tether the wearable to whatever external system it is communicating with

� Nobody wants to get tangled up in wires when they’re going for fun or walking around

� Applications

� Design wearable systems that communicate beyond the body

� Use gestures, biometric data, or body language to control what happens on a screen

� Log body-generated data to a shared database

� Send a signal from one wearable to another

33

Wireless Communication� Basics

� Types of Wireless Communication Protocol

34

Bluetooth Zigbee Wi-Fi NFC

Distance ~10m ~100m ~100m ~20cm

Speed ~24Mbps ~250Kbps 11M / 54Mbps 106~848Kbps

Maximum number of channels 7 32000 14 1

Power Consumption Medium Very Low High Low

Complexity Low Low High Low

Cost Low Low High Low

Page 18: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Wireless Communication� Bluetooth

� Basics

� Initiated in 1989

– By Dr. Nils Rydbeck, CTO at Ericsson Mobile in Lund, and Dr. Johan Ullman

– Wireless technology standard for exchanging data over short distances

– Originally conceived as a wireless alternative to RS-232 data cables

� The name was suggested by Kim Kardach

– He developed a mobile phone–computer communication system in 1997

– He was reading a novel “The Long Ships” about King Harald Bluetooth who united Scandinavia

– The implication is that Bluetooth does the same with communications protocols

– The Bluetooth logo is a bind rune of the Scandinavian alphabet ᚼ and ᛒ, Harald's initials

� ‘Bluetooth Special Interest Group (SIG)’ was formed in 1998

– Ericsson, Intel, Lenovo, Microsoft, Apple, Nokia, Toshiba, IBM

35

Wireless Communication� Bluetooth

� The Circuit

� Bluetooth Mate Silver by SparkFun

36

light sensor

analog input

power board

ground power

battery

RN-42 Chip basedBluetooth module

Page 19: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Wireless Communication� Bluetooth

� The Code

� After pairing, serial output will be transferred via Bluetooth connection

37

int lightSensorPin=A0;int lightSensorValue=0;

void setup(){

Serial.begin(115200);}

void loop(){

int lightSensorValue;analogRead(lightSensorPin);Serial.println(lightSensorValue);delay(200);

}

Wireless Communication� XBee

� Basics

� The brand name of a family of form factor compatible radio modules from Digi International

� The first XBee radios were introduced under the MaxStream brand in 2005

– Standard designed for point-to-point and star communications at over-the-air baud rates of 250 kbit/s

� Two models were initially introduced

– A lower cost 1 mW XBee and the higher power 100 mW XBee-PRO

� The XBee radios can all be used with the minimum number of connections

– Power (3.3 V), ground, data in and data out– A version called the programmable XBee has an additional on-board processor for user’s code– The programmable XBee and a surface-mount version of the XBee radios were both introduced in 2010

38

Page 20: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Wireless Communication� XBee

� Basics

� Available topologies

39

Wireless Communication� XBee

� Basics

� Configuring XBees

– XBees need to be configured for communication

– CoolTerm (http://freeware.the-meiers.org) is used to communicate with serial devices

– XBee radious are configured with ‘AT’ commands rather than programmed

� Two XBees will be used for an example

40

Page 21: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Wireless Communication� XBee

� Configuring XBee A

41

+++OK

ATRE

OK

ATIDB0D1

OK

ATMYA

OK

ATDLB

OK

ATWR

OK

Type “+++” into the CoolTerm window but do not press Enter

This will wipe any previous settings from the radio and do a factory reset

This sets the PAN ID, which is the channel on which the two radios will communicate

This will set the identity of this radio to “A”

This sets the address of the radio this radio talking to radio “B”

This saves these settings to the radio’s firmware

PAN IDs• In order for two XBees to communicate with each other, they must have the

same PAN ID.• If you are in an environment where many pairs of XBees are in use, each pair

must use a different PAN ID so that they do not interfere with each other

Wireless Communication� XBee

� Configuring XBee B

42

+++OK

ATRE

OK

ATIDB0D1

OK

ATMYA

OK

ATDLB

OK

ATWR

OK

A

+++OK

ATRE

OK

ATIDB0D1

OK

ATMYB

OK

ATDLA

OK

ATWR

OK

B

A

B

Page 22: Smart Garment Design - SNUfashiontech.snu.ac.kr/note/SmartGarment/07-Microcontrollers.pdfSmart Garment Design 7. Microcontrollers Introduction Microcontrollers Basics Computers in

Tools� Electronics

43

Soldering iron Desoldering tool Safety glass Helping hands

Wire stripper Flat-nosed plier Small snips Multimeter Heat gun

Tools� Sewing

44

Needle Needle Threader Seam Ripper Craft Knife

Basic Iron Scissors

Craft Iron Pinking Shears Embroidery Hoop