Introduction to Arduino Microcontrollers Jeff McRaven [email protected].

39
Introduction to Introduction to Arduino Arduino Microcontrollers Microcontrollers Jeff McRaven Jeff McRaven [email protected] [email protected]

Transcript of Introduction to Arduino Microcontrollers Jeff McRaven [email protected].

Introduction to Introduction to Arduino Arduino

MicrocontrollersMicrocontrollersJeff McRavenJeff McRaven

[email protected]@yahoo.com

OverviewOverview

BackgroundBackground Microcontroller defined/Why Arduino's?Microcontroller defined/Why Arduino's? Types of Arduino microcontrollersTypes of Arduino microcontrollers

What To Get (Hardware and What To Get (Hardware and Software)Software)

Arduino CArduino C Electronic CircuitsElectronic Circuits ProjectsProjects

Blinking light(s)Blinking light(s) Reading inputs (variable resistors)Reading inputs (variable resistors)

Microcontrollers – One Microcontrollers – One DefinitionDefinition

Programmers work in the virtual world.Programmers work in the virtual world. Machinery works in the physical world.Machinery works in the physical world. How does one connect the virtual world How does one connect the virtual world

to the physical world?to the physical world? Enter the microcontroller.Enter the microcontroller. A microcontroller is basically a small-A microcontroller is basically a small-

scale computer with generalized (and scale computer with generalized (and programmable) inputs and outputs.programmable) inputs and outputs.

The inputs and outputs can be The inputs and outputs can be manipulated by and can manipulate the manipulated by and can manipulate the physical world.physical world.

Arduino – Official Arduino – Official DefinitionDefinition

Taken from the official web site Taken from the official web site (arduino.cc):(arduino.cc): Arduino is an open-source electronics Arduino is an open-source electronics

prototyping platform based on flexible, prototyping platform based on flexible, easy-to-use hardware and software. It's easy-to-use hardware and software. It's intended for artists, designers, intended for artists, designers, hobbyists, and anyone interested in hobbyists, and anyone interested in creating interactive objects or creating interactive objects or environments.environments.

Why Arduino?Why Arduino?

For whatever reason, Arduino For whatever reason, Arduino microcontrollers have become the de microcontrollers have become the de facto standard.facto standard. Make Magazine features Make Magazine features manymany projects using projects using

Arduino microcontrollers.Arduino microcontrollers. Strives for the balance between ease of Strives for the balance between ease of

use and usefulness.use and usefulness. Programming languages seen as major Programming languages seen as major

obstacle.obstacle. Arduino C is a greatly simplified version of Arduino C is a greatly simplified version of

C++.C++. Inexpensive ($35 retail).Inexpensive ($35 retail).

Arduino TypesArduino Types

Many different versionsMany different versions Number of input/output channelsNumber of input/output channels Form factorForm factor ProcessorProcessor

LeonardoLeonardo DueDue MicroMicro LilyPadLilyPad EsploraEsplora UnoUno

LeonardoLeonardo Compared to the Uno, a slight upgrade.Compared to the Uno, a slight upgrade. Built in USB compatibilityBuilt in USB compatibility

Bugs?Bugs?

Presents to Presents to PC as a mouse PC as a mouse or keyboardor keyboard

DueDue

Much faster processor, many more Much faster processor, many more pinspins

Operates on 3.3 voltsOperates on 3.3 volts Similar to the MegaSimilar to the Mega

MicroMicro

When size matters: Micro, Nano, When size matters: Micro, Nano, MiniMini

Includes all functionality of the Includes all functionality of the LeonardoLeonardo

Easily usable on a breadboardEasily usable on a breadboard

LilyPadLilyPad

LilyPad is popular for clothing-based LilyPad is popular for clothing-based projects.projects.

EsploraEsplora

Game controllerGame controller Includes joystick, four buttons, Includes joystick, four buttons,

linear potentiometer (slider), linear potentiometer (slider), microphone, light sensor, microphone, light sensor, temperature sensor, three-axis temperature sensor, three-axis accelerometer.accelerometer.

Not the standard set of IO pins.Not the standard set of IO pins.

Arduino Uno Close UpArduino Uno Close Up

The pins are in three groups:The pins are in three groups: Invented in 2010Invented in 2010 14 digital pins14 digital pins 6 analog pins6 analog pins powerpower

Where to StartWhere to Start

Get an Arduino (starter kit)Get an Arduino (starter kit) Download the compilerDownload the compiler Connect the controllerConnect the controller Configure the compilerConfigure the compiler Connect the circuitConnect the circuit Write the programWrite the program Get frustrated/Debug/Get it to workGet frustrated/Debug/Get it to work Get excited and immediately start next Get excited and immediately start next

projectproject(sleep is for wimps)(sleep is for wimps)

Arduino Starter KitsArduino Starter Kits

Start with a combo pack (starter kit)Start with a combo pack (starter kit) Includes a microcontroller, wire, LED's, sensors, Includes a microcontroller, wire, LED's, sensors,

etc.etc. www.adafruit.comwww.adafruit.com

adafruit.com/products/68 ($65)adafruit.com/products/68 ($65) www.sparkfun.comwww.sparkfun.com

https://www.sparkfun.com/products/11576 ($99.95)https://www.sparkfun.com/products/11576 ($99.95) Radio ShackRadio Shack

Make Ultimate Microcontroller Pack w/ Arduino Kit Make Ultimate Microcontroller Pack w/ Arduino Kit ($119.99)($119.99)

www.makershed.comwww.makershed.comhttp://www.makershed.com/http://www.makershed.com/

Ultimate_Arduino_Microcontroller_Pack_p/msump1.htm ($150)Ultimate_Arduino_Microcontroller_Pack_p/msump1.htm ($150)

What to Get – My What to Get – My RecommendationRecommendation

Required:Required: Arduino (such as Arduino (such as

Uno)Uno) USB A-B (printer) USB A-B (printer)

cablecable BreadboardBreadboard Hookup wireHookup wire LED'sLED's ResistorsResistors SensorsSensors SwitchesSwitches

Good Idea:Good Idea: CapacitorsCapacitors TransistorsTransistors DC motor/servoDC motor/servo RelayRelay

Advanced:Advanced: Soldering iron & Soldering iron &

soldersolder Heat shrink tubingHeat shrink tubing 9V battery adapter9V battery adapter Bench power Bench power

supplysupply

Arduino CompilerArduino Compiler

Download current compiler from:Download current compiler from:arduino.cc/en/Main/software

Arrogantly refers to itself as an IDE Arrogantly refers to itself as an IDE (Ha!).(Ha!).

Run the software installer.Run the software installer. Written in Java, it is fairly slow.Written in Java, it is fairly slow.

Visit playground.arduino.cc/Main/ DevelopmentTools for alternatives to the base arduino IDE

Configuring the Arduino Configuring the Arduino CompilerCompiler

Defaults to COM1, will probably Defaults to COM1, will probably need to change the COM port setting need to change the COM port setting (my work PC uses 7).(my work PC uses 7).

Appears in Device Manager (Win7) Appears in Device Manager (Win7) under Ports as a Comm port.under Ports as a Comm port.

Arduino Program Arduino Program DevelopmentDevelopment

Based on C++ without 80% of the Based on C++ without 80% of the instructions.instructions.

A handful of new commands.A handful of new commands. Programs are called 'sketches'.Programs are called 'sketches'. Sketches need two functions:Sketches need two functions:

void setup( )void setup( ) void loop( )void loop( )

setup( ) runs first and once.setup( ) runs first and once. loop( ) runs over and over, until power loop( ) runs over and over, until power

is lost or a new sketch is loaded.is lost or a new sketch is loaded.

Arduino CArduino C

Arduino sketches are centered Arduino sketches are centered around the pins on an Arduino around the pins on an Arduino board.board.

Arduino sketches always loop.Arduino sketches always loop. void loop( ) {} is equivalent to while(1) void loop( ) {} is equivalent to while(1)

{ }{ } The pins can be thought of as global The pins can be thought of as global

variables.variables.

Arduino C Specific Arduino C Specific FunctionsFunctions

pinMode(pinMode(pinpin, , modemode))Designates the specified pin for input or outputDesignates the specified pin for input or output

digitalWrite(digitalWrite(pinpin,, value value))Sends a voltage level to the designated pinSends a voltage level to the designated pin

digitalRead(digitalRead(pinpin))Reads the current voltage level from the Reads the current voltage level from the

designated pindesignated pin

analog versions of aboveanalog versions of above analogRead's range is 0 to 1023analogRead's range is 0 to 1023

serial commandsserial commands print, println, writeprint, println, write

Compiler FeaturesCompiler Features

Numerous sample Numerous sample sketches are sketches are included in the included in the compilercompiler

Located under Located under File, ExamplesFile, Examples

Once a sketch is Once a sketch is written, it is written, it is uploaded by uploaded by clicking on File, clicking on File, Upload, or by Upload, or by pressing <Ctrl> Upressing <Ctrl> U

Arduino C is Derived Arduino C is Derived from C++from C++

avr-libcavr-libc#include <avr/io.h>#include <avr/io.h>#include <util/delay.h>#include <util/delay.h>

int main(void) {int main(void) {while (1) {while (1) {

PORTB = 0x20;PORTB = 0x20;_delay_ms(1000);_delay_ms(1000);PORTB = 0x00;PORTB = 0x00;_delay_ms(1000);_delay_ms(1000);

}}return 1;return 1;

}}

Arduino CArduino Cvoid setup( ) {void setup( ) {

pinMode(13, OUTPUT);pinMode(13, OUTPUT);}}

void loop( ) {void loop( ) {digitalWrite(13, HIGH);digitalWrite(13, HIGH);delay(1000);delay(1000);digitalWrite(13, LOW);digitalWrite(13, LOW);delay(1000);delay(1000);

}}

These programs blink an LED on pin These programs blink an LED on pin 1313

Basic Electric CircuitBasic Electric Circuit

Every circuit (electric or electronic) Every circuit (electric or electronic) must have at least a power source must have at least a power source and a load.and a load.

The simplest circuit is a light.The simplest circuit is a light. Plug in the light, and it lights up.Plug in the light, and it lights up. Unplug it, the light goes out.Unplug it, the light goes out. Electricity flows from the power Electricity flows from the power

source, through the load (the light) source, through the load (the light) and then back to the power source.and then back to the power source.

Basic LED CircuitBasic LED Circuit

Connect the positive (+) lead of a Connect the positive (+) lead of a power source to the long leg of an power source to the long leg of an LED.LED.

Connect other leg of the LED to a Connect other leg of the LED to a resistor.resistor. High resistance means a darker light.High resistance means a darker light. Low resistance means brighter light.Low resistance means brighter light. No resistance means a burned out LED.No resistance means a burned out LED.

Connect other leg of the resistor to Connect other leg of the resistor to the negative lead of the power the negative lead of the power source.source.

Let the Good Times Roll!Let the Good Times Roll!

At this point we have:At this point we have: Purchased a starter kit, including the Purchased a starter kit, including the

ArduinoArduino Connected and configured the ArduinoConnected and configured the Arduino Connected a simple LED circuitConnected a simple LED circuit

Let's write some code!Let's write some code!

Blink SketchBlink Sketch

void setup( ) {void setup( ) {

pinMode(13, OUTPUT);pinMode(13, OUTPUT);

}}

void loop( ) {void loop( ) {

digitalWrite(13, HIGH);digitalWrite(13, HIGH);

delay(1000);delay(1000);

digitalWrite(13, LOW);digitalWrite(13, LOW);

delay(1000);delay(1000);

}}

Connected to one end of the circuit

Connected to other end of the circuit

4 LED Blink Sketch4 LED Blink Sketchvoid setup( ) {void setup( ) { pinMode(1, OUTPUT);pinMode(1, OUTPUT); pinMode(3, OUTPUT);pinMode(3, OUTPUT); pinMode(5, OUTPUT);pinMode(5, OUTPUT); pinMode(7, OUTPUT);pinMode(7, OUTPUT);}}

void loop( ) {void loop( ) { digitalWrite(1, HIGH);digitalWrite(1, HIGH); delay (200);delay (200); digitalWrite(1, LOW);digitalWrite(1, LOW);

digitalWrite(3, HIGH);digitalWrite(3, HIGH); delay (200);delay (200); digitalWrite(3, LOW);digitalWrite(3, LOW);

digitalWrite(5, HIGH);digitalWrite(5, HIGH); delay (200);delay (200); digitalWrite(5, LOW);digitalWrite(5, LOW);

digitalWrite(7, HIGH);digitalWrite(7, HIGH); delay (200);delay (200); digitalWrite(7, LOW);digitalWrite(7, LOW);}}

So What?So What?

Great. Blinking lights. Not Great. Blinking lights. Not impressed.impressed.

Only covered output thus far.Only covered output thus far. Can use analog inputs to detect a Can use analog inputs to detect a

physical phenomena.physical phenomena.

InputsInputs

Digital inputs will come to the Arduino as Digital inputs will come to the Arduino as either on or off (HIGH or LOW, either on or off (HIGH or LOW, respectively).respectively). HIGH is 5VDC.HIGH is 5VDC. LOW is 0VDC.LOW is 0VDC.

Analog inputs will come to the Arduino as Analog inputs will come to the Arduino as a range of numbers, based upon the a range of numbers, based upon the electrical characteristics of the circuit.electrical characteristics of the circuit. 0 to 10230 to 1023 .0049 V per digit (4.9 mV).0049 V per digit (4.9 mV) Read time is 100 microseconds (10,000 a Read time is 100 microseconds (10,000 a

second)second)

Analog InputAnalog Input

A potentiometer A potentiometer (variable resistor) is (variable resistor) is connected to analog connected to analog pin 0 to an Arduino.pin 0 to an Arduino.

Values presented to Values presented to pin 0 will vary pin 0 will vary depending upon the depending upon the resistance of the resistance of the potentiometer.potentiometer.

Analog Input-ApplicationAnalog Input-Application

The variable resistor can be replaced The variable resistor can be replaced with a sensor.with a sensor.

For example, a photo resistor.For example, a photo resistor. Depending upon the light level at the Depending upon the light level at the

photo resistor:photo resistor: Turn on a lightTurn on a light Increase or decrease the brightness of an Increase or decrease the brightness of an

LED (or an LED array)LED (or an LED array)

Most sensors are simply variable Most sensors are simply variable resistors, but vary their resistance resistors, but vary their resistance based on some physical characteristic.based on some physical characteristic.

SensorsSensors

Sensors can be both binary or a range.Sensors can be both binary or a range. Usually, sensors that measure a range Usually, sensors that measure a range

of values vary their resistance to of values vary their resistance to reflect their detection.reflect their detection.

Arduinos can only sense voltages, not Arduinos can only sense voltages, not resistances.resistances.

Sensors that only vary their Sensors that only vary their resistances require a circuit called a resistances require a circuit called a voltage divider to provide the Arduino voltage divider to provide the Arduino a voltage.a voltage.

Common SensorsCommon Sensors

Dials on a radio are Dials on a radio are simply simply potentiometerspotentiometers

TemperatureTemperature LightLight AngleAngle SwitchesSwitches

did the user throw a did the user throw a switch or push a switch or push a button?button?

Accelerometer Accelerometer (measures motion (measures motion and tilt)and tilt)

Infrared sensor & Infrared sensor & lightlight

Hall effect sensor Hall effect sensor and magnetand magnet

Ball tilt sensor (for Ball tilt sensor (for measuring measuring orientation)orientation)

ForceForce

““Competitors”to the Competitors”to the ArduinoArduino

PIC controllerPIC controller Microcontroller programmed with C or Microcontroller programmed with C or

assemblerassembler Alternatives to the Arduino lineAlternatives to the Arduino line

Pinguino – PIC controllerPinguino – PIC controller MSP430 – Texas Instruments; $4.30MSP430 – Texas Instruments; $4.30 Others: customs, Teensy, etc.Others: customs, Teensy, etc.

NetduinoNetduino ComputersComputers

Raspberry PiRaspberry Pi BeagleBones – TI; has computer and BeagleBones – TI; has computer and

controllercontroller

NetduinoNetduino

Microcontroller and development tools Microcontroller and development tools created by Microsoft to work with the .NET created by Microsoft to work with the .NET Micro Framework.Micro Framework.

VASTLYVASTLY better development environment. better development environment. visualmicro.comvisualmicro.com Other alternativesOther alternatives

DifferencesDifferences Pins on a Netduino are 3.3V (not 5).Pins on a Netduino are 3.3V (not 5). Netduinos have a much faster processor.Netduinos have a much faster processor. 60K of RAM (versus an Uno's 2K).60K of RAM (versus an Uno's 2K).

Largely compatible with the Arduino, but it Largely compatible with the Arduino, but it is not a drop-in replacement (can fry it).is not a drop-in replacement (can fry it).

Raspberry PiRaspberry Pi

Low end computer, not a controllerLow end computer, not a controller Uses Debian LinuxUses Debian Linux

Arch Linux ARM, Fedora, FreeBSD, Arch Linux ARM, Fedora, FreeBSD, Slackware…Slackware…

Programmed with PythonProgrammed with Python BBC BASIC, C, PerlBBC BASIC, C, Perl

As it is a computer and not a controller, As it is a computer and not a controller, its role in these projects is different.its role in these projects is different.

Hierarchy: computers control Hierarchy: computers control controllers, controllers control controllers, controllers control hardware.hardware.

ShieldsShields

Shields are circuit boards that plug Shields are circuit boards that plug into the top of an Arduino.into the top of an Arduino.

They extend the capabilities of an They extend the capabilities of an Arduino.Arduino.

Examples:Examples: EthernetEthernet GPSGPS MotorMotor PrototypePrototype

shieldlist.orgshieldlist.org

ConclusionConclusion

The Arduino microcontroller is a low cost The Arduino microcontroller is a low cost way to enter into the hobby of robotics.way to enter into the hobby of robotics.

The Arduino has two plusses over any The Arduino has two plusses over any other:other: The user communityThe user community Extensive online library of code and projectsExtensive online library of code and projects

Viewed as the "base" system, upon which Viewed as the "base" system, upon which all other microcontrollers are built. all other microcontrollers are built. Compatibility.Compatibility.

So get a kit, and start ushering in the So get a kit, and start ushering in the inevitable takeover of our robotic inevitable takeover of our robotic overlords!overlords!

Introduction to Introduction to Arduino Arduino

MicrocontrollersMicrocontrollersJeff McRavenJeff McRaven

[email protected]@yahoo.com