microcontrollers (rex st. john)

45
Microcontrollers Microcontrollers by @rexstjohn (for web developers)

Upload: future-insights

Post on 03-Aug-2015

494 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Microcontrollers (Rex St. John)

MicrocontrollersMicrocontrollers

by @rexstjohn

(for web developers)

Page 2: Microcontrollers (Rex St. John)

Rex St. JohnRex St. John

Internet of Things Evangelist at Intel

Page 3: Microcontrollers (Rex St. John)

Our goal today is to understand what is going on here

Page 4: Microcontrollers (Rex St. John)

Why Microcontrollers?Why Microcontrollers?

Page 5: Microcontrollers (Rex St. John)

By 2020, there will be 50 billionBy 2020, there will be 50 billioncomputers on earthcomputers on earth

Moore's Law: They will keep gettingsmaller, cheaper and faster

Page 6: Microcontrollers (Rex St. John)

Many of these devices will Many of these devices will lacklack user userinterfacesinterfaces

Often communicating via low-energy wirelessprotocols

Page 7: Microcontrollers (Rex St. John)

They will rely on sensors to understandThey will rely on sensors to understandtheir environmenttheir environment

Heat, temperature, sound, light, smoke, steam,LiDAR...

Page 8: Microcontrollers (Rex St. John)

And form "the edge" of large internetAnd form "the edge" of large internetconnected systems...connected systems...

Page 9: Microcontrollers (Rex St. John)

These computers will often take theThese computers will often take theform of form of MicrocontrollersMicrocontrollers

Small computers specializing in real-timeoperations

Page 10: Microcontrollers (Rex St. John)

Increasingly we are using webIncreasingly we are using webdevelopment tools and techniques todevelopment tools and techniques to

program these devicesprogram these devices

GitHub, Containers, Node.js etc

Page 11: Microcontrollers (Rex St. John)

"Hybrid" developers will be needed who"Hybrid" developers will be needed whoare capable of understanding theseare capable of understanding these

systemssystems

Are you up for the task?

Page 12: Microcontrollers (Rex St. John)

Full-Stack Developer, n: Person who isFull-Stack Developer, n: Person who iscomfortable programming both frontcomfortable programming both front

and back-end systemsand back-end systems

Full-Stack Developer, n: Person who isFull-Stack Developer, n: Person who ispaid once to do the work of four peoplepaid once to do the work of four people

(actual definition)

(technical definition)

Now that we can run Linux on our IoT devices, full-stackdeveloper takes on a whole new meaning!

Page 13: Microcontrollers (Rex St. John)

Example ProjectsExample ProjectsWhat can we do with microcontrollers?

Page 14: Microcontrollers (Rex St. John)

RamBotRamBot

Gamification of drones and robots

Page 15: Microcontrollers (Rex St. John)

Sign++Sign++

Sign language translation glove

Page 16: Microcontrollers (Rex St. John)

Smart Chicken CoopSmart Chicken Coop

Smart chicken tender

Page 17: Microcontrollers (Rex St. John)

TinyCheeseTinyCheese

Makes tiny cheese

Page 18: Microcontrollers (Rex St. John)

A Microcontroller (MCU) is a A Microcontroller (MCU) is a smallsmallcomputercomputer with a with a processorprocessor, , memorymemory,,

and and programmable input/outputprogrammable input/outputperipherals.peripherals.

They specialize in real-time behaviorThey are used everywhere (automotive, industrial etc)

They are contained on an integrated circuit

Page 19: Microcontrollers (Rex St. John)

MicroprocessorMicroprocessor MicrocontrollerMicrocontrollerMicroprocessorPlus other stuff*On integrated circuits (IC)Often on a PCB*

Programmable deviceAccepts digital IOProcesses dataProvides results as output

*Other stuff may vary*Printed Circuit Board

Page 20: Microcontrollers (Rex St. John)

PCB with microcontroller pinout

Page 21: Microcontrollers (Rex St. John)

Example UsesExample UsesHome appliancesIndustrialAutomotiveDronesEverything, really

Anywhere you need cheap, highly reliable, event-driven computers

Page 22: Microcontrollers (Rex St. John)

Sensors attached to PCB boards are the "userinterface" for microcontrollers, lets learn how that

works

Page 23: Microcontrollers (Rex St. John)

Web DevelopmentWeb DevelopmentFor ThingsFor Things

Page 24: Microcontrollers (Rex St. John)

LibMRAALibMRAA FirmataFirmataFirmata is a protocol forcommunicating withmicrocontrollers from softwareon a computer (based on MIDI).

LibMRAA is a C/C++ librarywith bindings to javascript &python to interface with theIO on Galileo, Edison etc.

We can now talk to our microcontrollers without writingassembly language!

Page 26: Microcontrollers (Rex St. John)

Digital and Analog SensorsDigital and Analog Sensors

Communication for basic data

Page 27: Microcontrollers (Rex St. John)

Digital I/ODigital I/OGPIO: General purpose input / outputHigh or Low depending on voltage level (1 or 0)Frequently come in "ports" of 8 (a byte)Can alternate as analog pins (but not both!)Good for LEDs, buttons, buzzers, relays

Page 28: Microcontrollers (Rex St. John)

Example: LED with MRAAExample: LED with MRAAvar m = require('mraa'); //require mar//write the mraa version to the consoleconsole.log('MRAA Version: ' + m.getVersion()); //LED hooked up to digital pin 13 (or built in pin on Galileo Gen1 & Gen2)var myLed = new m.Gpio(13); myLed.dir(m.DIR_OUT); //set the gpio direction to outputvar ledState = true; //Boolean to hold the state of Led

periodicActivity(); //call the periodicActivity function

function periodicActivity(){ //if ledState is true then write a '1' (high) otherwise write a '0' (low) myLed.write(ledState?1:0); ledState = !ledState; //invert the ledState //call the indicated function after 1 second (1000 milliseconds) setTimeout(periodicActivity,1000); }

Page 29: Microcontrollers (Rex St. John)

Analog I/OAnalog I/OGPIO pins for digital can often be used for analogGood for sensing light, temperature, sound etcWe must translate digital signals to analog signals(PWM)

Page 30: Microcontrollers (Rex St. John)

Pulse Width ModulationPulse Width Modulation

Pulse Width Modulation, orPWM, is a technique forgetting analog results withdigital means. Digital controlis used to create a squarewave, a signal switchedbetween on and off.

Useful for servos (robotics, drones), audio and more

Let us turn a jaggy thing into a squiggly thing

Page 31: Microcontrollers (Rex St. John)

Example: 3-Axis AccelerometerExample: 3-Axis Accelerometer

Has a ground wire, power wire and 3-analog wires

Page 32: Microcontrollers (Rex St. John)

// "ADXL335"var five = require("johnny-five");var Edison = require("edison-io");

var board = new five.Board({ io: new Edison()});

board.on("ready", function() { var accelerometer = new five.Accelerometer({ controller: "ADXL335", pins: ["A0", "A1", "A2"] }); accelerometer.on("change", function() { console.log(" x : ", this.x); console.log(" y : ", this.y); console.log(" z : ", this.z); });});

Johnny-Five SyntaxJohnny-Five Syntax

Page 33: Microcontrollers (Rex St. John)

Serial CommunicationsSerial CommunicationsCommunication for fancy data

Page 34: Microcontrollers (Rex St. John)

"Serial" aka "Time Division Multiplexed""Serial" aka "Time Division Multiplexed"Data is sent one chunk at a time based on a clock signal

Errors and noise occur when sending data via wirerequiring safeguards.

Page 35: Microcontrollers (Rex St. John)

: Inter-integrated circuit: Serial peripheral communication

(SCI): Universal asynchronous receiver/ transmitter

I²CSPIUART

Want more? Read this.

Common Serial CommunicationsCommon Serial Communications

Page 36: Microcontrollers (Rex St. John)

Example: I2C LCDExample: I2C LCD

Page 37: Microcontrollers (Rex St. John)

// How to write to the Seeed LCD Screen// NOTE: You *MUST* plug the LCD into an I2C slot or this will not work!var Cylon = require('cylon');

function writeToScreen(screen, message) { screen.setCursor(0,0); screen.write(message);}

Cylon .robot({ name: 'LCD'}) .connection('edison', { adaptor: 'intel-iot' }) .device('screen', { driver: 'upm-jhd1313m1', connection: 'edison' }) .on('ready', function(my) { writeToScreen(my.screen, "Ready!"); }).start();

Cylon SyntaxCylon Syntax

Page 38: Microcontrollers (Rex St. John)

SPISPI“ A master sends a clock signal, and

upon each clock pulse it shifts one bit outto the slave, and one bit in, coming fromthe slave. Signal names are therefore SCKfor clock, MOSI for Master Out Slave In,and MISO for Master In Slave Out.

I²CI²C“ SCL and SDA. SCL is the clock line.

It is used to synchronize all datatransfers over the I2C bus. SDA is thedata line. The SCL & SDA lines areconnected to all devices on the I2Cbus.

Common Synchronous ProtocolsCommon Synchronous Protocols

Page 39: Microcontrollers (Rex St. John)

SPISPIFaster, 1-20MHzRequires 3+ physical linesLess worry about "noise"All lines are unidirectionalChip-select lines required

I²CI²CSlower, 100 - 400MHz2 lines only, easier to wireNoise an issueAll lines are bi-directionalFor chaining devices

Common Synchronous Protocols ComparedCommon Synchronous Protocols Compared

Many microcontrollers support bothprotocols.

Page 40: Microcontrollers (Rex St. John)

(or SCI) uses two wires (TX / RX) to transmit data (or SCI) uses two wires (TX / RX) to transmit dataasynchronously at an agreed-upon baud rateasynchronously at an agreed-upon baud rate

UARTUART

Uses start and stop bits for primitive error correctionBuad Rate: Transmission speed (9600, 115200 etc)

Long history, common on PCs before USB

Page 41: Microcontrollers (Rex St. John)

UART NotesUART NotesUART is a hardware module, not a protocolCan run in a Slow: 9 - 56 kHz (vs. 1 - 20 MHz for SPI)Used for sending ASCII charactersAlso: Keyboard, LCD monitor data

synchronous mode called USART

Page 42: Microcontrollers (Rex St. John)

Test Time: ATmega32U4Xadow Main Board with Lets find: SPI, UART, I2C, Analog I/O

Page 43: Microcontrollers (Rex St. John)

Test Time: Find GPIO, Analog, PWM, UART, SPI

Page 44: Microcontrollers (Rex St. John)

Test Time: Find SPI, UART, I2C, USB, PWM, GPIO, Clock lines

Page 45: Microcontrollers (Rex St. John)

Further ReadingFurther ReadingIntroduction to Microcontrollers