introduction to microcontrollers: arduino skylar roebuck and lucas libraro

42
Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Upload: sharon-oliver

Post on 25-Dec-2015

234 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Introduction to Microcontrollers: Arduino

Skylar Roebuck and Lucas Libraro

Page 2: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro
Page 3: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

What we will learn you…• Presentation learnin

– The Arduino– Microcontroller basics– Introduction to programming

• Variables• Binary

– Introduction do the digital ‘1’ and ‘0’

• Data types• Anatomy of a program• Sketches- setup and loop• Logic AND, OR, NOT

– Circuit Construction• What’s in the kit?• Ohm’s Law• Resistor Color Coding• Pull Up and Pull Down

Then we get building…

Page 4: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

The Arduino

Page 5: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

What is an Arduino

The Arduino is first and foremost a microcontroller.

Page 6: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

So what is a microcontroller?

• Essentially, a microcontroller is a small computer on a single integrated circuit.

• The microcontroller has a simple CPU comprised of clock timers, input/output ports, and memory.

Page 7: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Uses:

• automatically controlled products and devices• automobile engine control systems • implantable medical devices • remote controls• office machines• appliances• power tools• And more and more and more

Page 8: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Personal Project Uses:

• Microcontrollers are great at handling inputs and outputs for example:

• Using a temperature sensor and outputting the temperature on a screen.

• Using an infrared sensor and using it to guide a robot by controlling motors.

• Using servos to move a robotic arm• Possibilities are endless.

Page 9: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

So why the Arduino?

Vs.

Page 10: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Arduino• Expandability

– Easy to attach Ethernet shields, sensors, etc.

• Easy Setup– All you need is a standard USB cable for programming and power

• Breakout board– Circuit creation is made easy by not having to do any soldering

• Inexpensive

• Powerful– Fueled by the Atmega328 microcontroller

Page 11: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

The learnin

Pencils and Paper Time!

Page 12: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Variables

• A variable is how you store a value within a program

• Examples: • Hackrva = “awesome”;

– Assigns the string (a data type) Awesome to hackrva

• Hackrva = 2;– Assigns the integer 2 to the variable hackrva.– Further: hackrva = hackrva + 2;

» NOW: hackrva = 4

Page 13: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Data types

• Data types are an important foundation to programming. For example, if you want a program to count to 10 then you want:– 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

• You do NOT want– 1.00000, 1.00001, 1.00002, etc.

• This is solved by using data types properly

Page 14: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Data types mark 2

• In the example before, counting to 10, we would want that to be an integer so that it ignores all other values.

• This is done in conjunction with the variable and this process is known as defining or initializing variables:

• Int counter;– -this just tells the compiler that counter is an integer and

it doesn’t need to be concerned with any other data.

Page 15: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Data types mark 3

• Further there are many data types and the variable needs to be assigned according to what it will be used for.

Common data types are:• String = “hack.rva rocks”;• Char = “A”;• Int = 1;• Float = 1.5675;

– Binary types• Boolean = 0; (only 0 or 1)• Byte = “00001111”;

Page 16: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

The nitty gritty• The foundation of programming is • the ‘0’ and the ‘1’

• Boolean• ‘0’ – No• ‘1’ – Yes

• Binary• 000 - 0• 001 - 1• 010 - 2• 011 - 3• 100 - 4• 101 - 5• 110 - 6• 111 - 7

This is what the microcontroller understands

Seems very polar but there are shades of gray we will discuss later

Page 17: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

LOGIC Gates

• Logic comes into play when working and thinking on a binary level. They are essentially operations like +, -, *, / except on a binary numbers.

• In fact using logic is how you CREATE +,-,*,/ well…you also need about 5000 transistors.

• The foundation basic gates are:• AND, OR, NOT

Page 18: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

ANDINPUT OUTPUT

A B A AND B

0 0 0

0 1 0

1 0 0

1 1 1

Example: We want LED3 to come on when LED1 and LED2 are both on:

If LED2 = 1 AND LED1 = 1 then LED3 = 1

Page 19: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

OR

Example: We want LED3 to come on when either LED1 and LED2 are both on:

If LED2 = 1 OR LED1 = 1 then LED3 = 1

INPUT OUTPUT

A B A OR B

0 0 0

0 1 1

1 0 1

1 1 1

Page 20: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

NOT (invert)

Example: We want LED3 to be the opposite of its current state (if off then on and if on then off):

LED3 = NOT LED3;

INPUT OUTPUT

A NOT A

0 1

1 0

Page 21: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Anatomy of a Program

Page 22: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

How do we make it do what we want: Program it!

• High level languages– C++, C, Java – closest to natural human understanding– Arduino’s Programming Language

• Low level languages– Assembly Code– a little more difficult to interpret for a person but still

possible

• Lowest (if you write this you have too much free time)– Machine Code – all patterns of ‘0’s and ‘1’s– The microcontroller might see:

0001001010010100101001001010001010101010– And from this it will know a specific command.

Page 23: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

A program.

• Basic life of a program:– User writes a program in C (a high level language)– A compiler will then take that code and converts it into

Assembly language– Then an assembler will take that code and turn it into machine

code.

– We are using an Arduino IDE, or integrated development environment, so we will write code using Arduino’s language and it will take care of the rest!

– Easy Peazy

Page 24: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Programming Languages• Obviously, not every programming language is the same. Each language

differs in a way that makes it particularly good for something.

• Arduino is no different. Arduino has been simplified specifically to make it easy to program the Arduino microcontroller.

• Anatomy of an Arduino Program:– Arduino calls every program a Sketch (maybe they feel like that makes them edgy

to be different)– Sketches are comprised of a SETUP and a LOOP section– Setup: Only Runs once and is used to set necessary registers and pins– Loop: This is the rest of the sketch and by default when a sketch finishes in this

language it repeats the entire sketch. Essentially looping-a concept we will cover later.

Page 25: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

CIRCUIT CONSTRUCTION

Page 26: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

What’s in the kits?

Page 27: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

What’s in the kits?Half-sized breadboard- 400 connection points, plenty of room for beginner projects, with 2 power rails on the side.

Page 28: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

What’s in the kits?1K & 10K potentiometer - these pots have 0.1" spacing and fit very nicely into a breadboard without modification

Page 29: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

What’s in the kits?2 small pushbuttons - Snap into the breadboard for button inputs

Page 30: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

What’s in the kits?5 bright red diffused LEDs (250mcd) - indicators, blinkies, bright enough to see in the day, but diffused so that they are visible from all angles.

Page 31: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

What’s in the kits?Red, green and blue ultra-bright LED - Can be used on their own, or color-mixed to make nearly any color in the rainbow!

Page 32: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

What’s in the kits?5 100 ohm resistors - They can be used to protect pin outputs when starting out5 1K resistors - Good for use as LED limiting resistors5 10K resistors - Great for pullups & pulldowns

Page 33: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

A few more concepts before building:

• Ohm’s Law!– Georg Simon Ohm was kind of the Originial

Gangster of circuit construction– He developed the simple relationship

Or current = voltage / resistance

Page 34: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Why is this important?

• Today we will be using LEDs or Light Emitting Diodes .• LEDs are essentially switches that emit light.• When hooked up correctly to a power source, the

current will flow through the switch in the right direction and turn it on.

• BUT, LEDs are often delicate! With too much current the LED will blow and become unusable.

• SOLUTION: We use a resistor to buffer the current through the LED.

Page 35: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Which resistor?

• Using Ohm’s we can determine exactly which resistor is best but it is always best to be safe than sorry.– The higher the resistance the dimmer the LED will

• We will be using the 1K resistors.– …and which one is that?

Page 36: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Resistor Color Coding

Page 37: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Resistor Color Coding

100ohm

100k ohms

Page 38: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Pull up/pull down resistors• Pull up/pull down is a way of setting up buttons so that they are reliable.

• When we use buttons today we will add a pull down resistor. This resistor ensures that the button registers as a ‘0’ or off whenever it is not being pushed.

• This is the function of a pull down resistor—it makes sure inputs are what we expect them to be.

Page 39: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Time to build!

Page 40: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Picture of build schematic

Page 41: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Program 1

This program introduces everybody to the general setup of a typical sketch

Page 42: Introduction to Microcontrollers: Arduino Skylar Roebuck and Lucas Libraro

Program 2