intro to hardware programming with the arduino uno

71
Getting Started with the Arduino Uno by Eric J. Richardson and Vui Nguyen September 2017

Upload: vui-nguyen

Post on 23-Jan-2018

291 views

Category:

Technology


8 download

TRANSCRIPT

Page 1: Intro to Hardware Programming with the Arduino Uno

Getting Startedwith the

Arduino Uno

by Eric J. Richardson and Vui NguyenSeptember 2017

Page 2: Intro to Hardware Programming with the Arduino Uno

Legal Disclaimer

Arduino is a trademark of somebody, certainlyBut not us

Page 3: Intro to Hardware Programming with the Arduino Uno

Why this Presentation?

Last year, we started doing IoT when we entered the Intel IoT Roadshow“Save the Water Pipes” was an automated faucet dripper – handy to prevent pipes from freezing and breaking in cold climates like Colorado!

Vui: software architect Eric: hardware engineer Crystal: web developer

Page 4: Intro to Hardware Programming with the Arduino Uno

Why this Presentation?

We entered our project in the US-China Young Maker Competition (Sponsored by Intel) and competed in China! Vui presented our “Save the Water Pipes” project to Women Who Code Several of the Women Who Code had questions about the Intel Edison board which powered our project

Page 5: Intro to Hardware Programming with the Arduino Uno

Why this Presentation?

There was a lot of interest in the Intel Edison The Edison is cool, but at $100 it’s a bit pricey

to get started with an embedded computing project

so Eric did a follow up presentation at WWC on the Raspberry Pi

Why the Raspberry Pi?

Page 6: Intro to Hardware Programming with the Arduino Uno

Why this Presentation?

Raspberry Pi is a great value, single-board computer, and can be used as the basis for many projects

At Raspberry Pi presentation, Eric also provided a good overview of electronics and hardware concepts

But there is a lot of setup with Raspberry Pi just to get started With Arduino Uno, you could get up and running much more

quickly, for as much money, with less equipment and setup

Page 7: Intro to Hardware Programming with the Arduino Uno

Who is this Presentation For?

This presentation is for software devs interested in learning about hardware

You should, by the end, know enough to get an Arduino Uno up and running, and writing Arduino sketches

And, hopefully, driving electronic circuits

We hope that we cover at least one thing you already know, and at least one thing you don't

If you have a question about something, please ask! We may wave it off as something we cover later

Page 8: Intro to Hardware Programming with the Arduino Uno

Personal Disclaimer

For those of you who are already familiar with all of this, Eric apologizes if he sounds like he's “mansplaining”! He now has

Vui co-presenting to help with this.

Page 9: Intro to Hardware Programming with the Arduino Uno

Who Are We?

Eric J. RichardsonLockheed Martin Systems EngineerColorado State University

B.S. Electrical & Computer Engineering, 2004

LIKES: DISLIKES:

Antique Cars Dogs Shadowrun Cajun Food Old Video Games Hand Tools Cool Boots

Cancer Fascism Lasagna TV Commercials

Which Assume I'm Familiar with Sports Stars

Page 10: Intro to Hardware Programming with the Arduino Uno

Who Are We?

Vui NguyenSimplyE iOS DeveloperColorado School of Mines

B.S. Computer Science, 1999University of Denver

M.S. Computer Science, 2006

LIKES: DISLIKES:

Fishing Eating Fishing Traveling Fishing Dogs Fishing

Not Fishing Enough

Sexism Doing Dishes Negativity Being asked,

“Where are you from?”

Page 11: Intro to Hardware Programming with the Arduino Uno

Who Are We Not?

We are not graphic artists!

We are not full-fledged Arduino experts!

We fully expect someone here to ask us a question we don't know the answer to

Page 12: Intro to Hardware Programming with the Arduino Uno

What is “Arduino”?

3 things:

– Arduino is an Italian company that designs low-cost computers designed to interact with physical hardware

These are open-source hardware designs– “Arduino-compatible” boards have special I/O ports that

allow the boards to be expanded with plugin-in modules called shields

– It’s also the Arduino-specific programming language (based on C++) and IDE

We will be working with the Arduino Uno board

Page 13: Intro to Hardware Programming with the Arduino Uno

Arduino Uno: What Does It Have?

Arduino Uno

14 Digital Input / Output pins (6 of these can be used as Analog outputs) 6 Analog inputs Atmel 8-bit microcontroller USB or plug-in power Arduino-compatible I/O; can plug in additional “shields” Ability to run Arduino sketches without any additional software installation Low-low price of ~$25

Page 14: Intro to Hardware Programming with the Arduino Uno

Arduino Uno: What Does It NOT Have?

The Arduino Uno does not have one of everything. Some things you can add, and some things you have to live without:

Networking capability Multi-computing Native graphics or sound

capability Case Huge Processing Power Full-blown O/S

Page 15: Intro to Hardware Programming with the Arduino Uno

Arduino Uno: Required Equipment

Arduino Uno ($25)

USB A/B (male / male) cable

9V or 12V, center-pin-positive,

500mA (or more) power

supply (optional)

Page 16: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #1: Voltage, Current, Power, and Charge

Direct-Current electricity has 4 dimensions:

Voltage: How Hard The Electricity is Pushing

– measured in Volts, or V (also mV) Current: How Much Electricity is Flowing Right Now

– measured in Amperes, or Amps, or A (also mA) Power: How Much Work Electricity is Doing Right Now

– measured in Watts, or W (also mW, kW, MW, etc.) Charge: How Much Electricity There Is

– measured in Coulombs, or more usefully, Amp-Hours (also mAh)

Page 17: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #1: Voltage, Current, Power, and Charge

What we call a power supply is a constant-voltage source

– That is, a power supply will deliver its rated output voltage up to its rated current output

– So, a 19.5V power supply, rated for 6.7A of output, can deliver up to and including 6.7A without ill effect

Page 18: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #1: Voltage, Current, Power, and Charge

Power supplies can also deliver less than their rated current output!

– They only deliver the current that the circuit “needs” (more on this later)

– But, they always deliver the voltage that they are rated for (unless they are overloaded)

So, you can use a power supply rated for more current than you need

BUT, DO NOT use a power supply rated for more Voltage than you need!

Page 19: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #1: Voltage, Current, Power, and Charge

Electrical Power is always calculated by this formula:

Power = Voltage x Current

Or, if you prefer:

Watts = Volts x Amps

So, if you know the voltage and the current, you know the power being delivered. Back to our earlier example, the 6.7A @ 19.5V power supply is capable of delivering:

19.5V x 6.7A = 130W

Page 20: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #1: Voltage, Current, Power, and Charge

Batteries, as far as circuits are concerned, work the same as power supplies

– A battery will deliver its rated voltage up to its rated current, though battery manufacturers do not always tell you the rated current

The biggest difference is that you also have to worry about the charge of a battery, which you do not have to worry about with a power supply

– A battery with a greater charge capacity (in amp-hours, or Ah) will “last” longer between chargings

Page 21: Intro to Hardware Programming with the Arduino Uno

Arduino Uno: Optional Equipment

breadboard ($5, but get 2, or 1 big one)

Single Pole/Single Throw

Normally Open Switch ($2)

1kΩ resistors ($1)

Page 22: Intro to Hardware Programming with the Arduino Uno

Arduino Uno: Optional Equipment

LED Assortment ($6)

Male-to-Male Jumpers ($8)

1MΩ Potentiometers ($3)

Page 23: Intro to Hardware Programming with the Arduino Uno

Install Arduino IDE

Download and install Arduino IDE for your OS

– https://www.arduino.cc/en/Guide/HomePage Follow instructions to “Install the board drivers”:

– https://www.arduino.cc/en/Guide/ArduinoUno

– And, from the same page, “Select your board type and port”

Sample code to get started:

– https://www.arduino.cc/en/Tutorial/BuiltInExamples

Page 24: Intro to Hardware Programming with the Arduino Uno

Hardware Building Blocks

We will going to focus the rest of this presentation on using the Arduino Uno as the basis of a hardware project

This will take the form of the “Four Building Blocks” of physical computing:

– Digital Output

– Digital Input

– Analog Output

– Analog Input

Page 25: Intro to Hardware Programming with the Arduino Uno

Hardware 100 – Blink an LED

Before that, let's make sure the darn thing turn on

Making sure you can get an LED to blink should always be the first thing you do with any new hardware project

– It's the “Hello World” of hardware

Page 26: Intro to Hardware Programming with the Arduino Uno

blinkOneLED

Wire the green LED up like this:I/O 3

1kΩ

Page 27: Intro to Hardware Programming with the Arduino Uno

blinkOneLED

Physically, it will look something like this:

Page 28: Intro to Hardware Programming with the Arduino Uno

Demonstration: blinkOneLED

Let's make sure everything works!

Page 29: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #2: Circuits, Open Circuits, and Short Circuits

First, some terminology.

This is a circuit diagram:

+

_Vin

Voltage Source

Load(resistor)

+

_

Current

Page 30: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #2: Circuits, Open Circuits, and Short Circuits

Current flows out of the positive (+) terminal of the Voltage Source, through the circuit, and into the (-) terminal of the Voltage Source

The amount of current that flows is given by Ohm's Law:

Where E is Voltage, I is current, and R is the Resistance of the load, in Ohms (Ω)

Current I can also be express as:

E = I x R

I = E R

Page 31: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #2: Circuits, Open Circuits, and Short Circuits

So, for example, if Vin were 3.3V, and the load was 1kΩ, the current in this circuit would be 3.3V / 1000Ω = 3.3 mA

What if we were to take the load out of the circuit?

+

_Vin

Page 32: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #2: Circuits, Open Circuits, and Short Circuits

In this case, current cannot flow, because there is no electrical path from the positive terminal of the voltage source to the negative one.

This is called an open circuit.

Think of it as an infinite-resistance load.

– So, 3.3V / ∞Ω = 0A But what happens if we replace the load resistor with a

wire?

Page 33: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #2: Circuits, Open Circuits, and Short Circuits

+

_VinCurrent?

Wires have a resistance of 0Ω So, the current through the system is: 3.3V / 0Ω That would be infinite current, if the voltage source could

supply infinite current!

Page 34: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #2: Circuits, Open Circuits, and Short Circuits

Instead, the voltage source will attempt to supply as much current as it can – enough to trip a circuit breaker, or more likely, burn up a wire, or the voltage source itself

This is called a short circuit

Do not wire up a short circuit! That's how you burn up components, and destroy circuit boards

Page 35: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #3: GND and Vin

Second, some shorthand.

This takes time to draw:

+

_Vin

Page 36: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #3: GND and Vin

So, we will use two convenient shorthands.

The positive terminal of the voltage source, and anything connected to it, we call Vin, or we just name it whatever the input voltage is (such as 3.3V)

We denote it with this symbol:

The negative terminal of the voltage source, and anything connected to it, we call GND (“ground”).

We denote GND with this symbol:

Vin

Page 37: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #3: GND and Vin

So:

+

_Vin

Vin

==

Page 38: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #4: Breadboards

Breadboards are useful for wiring up circuit prototypes without soldering

There are different types, but they mostly look something like this:

The magic of a breadboard is that you can plug one wire into one hole, and have it be electrically connected to another wire plugged into another hole

Page 39: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #4: Breadboards

The electrical connections are arranged like so:

It is helpful to plug Vin into the + rail, and GND into the – rail

On either side of the channel are 5 pins all connected to each other

Page 40: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #4: Breadboards

Breadboards cannot carry infinite current without burning up!

– If your circuit needs to carry more than about 500mA, I recommend not using a breadboard and jumpers: get some thicker-gauge, dedicated wires

Breadboards cannot handle infinite voltage without arcing internally!

– If your circuit needs to deliver more than about 24V, I recommend not using a breadboard and jumpers: get some wires with thicker insulation

These numbers give a wide safety margin

Page 41: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #5: LEDs

“LED” stands for “Light Emitting Diode”

They do not behave like resistors! We can model them like this:

+

_VD

RD

Page 42: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #5: LEDs

VD is around 1.8V, while R

D is not very much – on the order

of 2Ω or so

So, if you were to attach a 3.3V voltage source directly to the LED, you would get:

This might not sound like much, but the output pins of the Arduino Uno can only output about 40mA or so apiece– if you were to “ask” them for 750mA, it could shut down or even damage the Arduino Uno

(3.3V – 1.8V) / 2Ω = 750mA

Page 43: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #5: LEDs

This is why each LED needs a 1kΩ resistor in series with it: Vin

1kΩ

If the LED is too dim, try a 470Ω resistor If the LED is too bright, try a 2kΩ resistor

Page 44: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #5: LEDs

Finally, LEDs are diodes, and the magic of a diode is that they only allow current to flow in one direction

The flat part of the diode bulb, or the shorter of the two legs, should be oriented towards GND

See http://media.bemyapp.com/control-led-lights/

Page 45: Intro to Hardware Programming with the Arduino Uno

digitalOutputs.py

Wire the green, yellow, and red LED up like this:I/O 3

1kΩ

I/O 5

1kΩ

I/O 6

1kΩ

Page 46: Intro to Hardware Programming with the Arduino Uno

digitalOutputs

Physically, it will look something like this:

Page 47: Intro to Hardware Programming with the Arduino Uno

Demonstration: digitalOutputs

Prepare to have your mind BLOWN!

What it does: lights up a series of LEDs, one at a time, in round robin fashion

The Magic:

– Set LED pin mode to OUTPUT: we’re sending signals out to the LEDs, not receiving from them

– Set digital write to HIGH to turn LED on

– Set digital write to LOW to turn LED off

– State is whether LED is currently on or off

– Check state of current LED in loop to determine which LEDs need to be turned off/on

Page 48: Intro to Hardware Programming with the Arduino Uno

digitalInput

Leaving the LEDs in place, wire the switch like this:I/O 12

This is a single-throw switch. It only has one position in which the circuit is completed. For a “normally open” switch, the connection to GND will be made when you close the switch (press the button)

Page 49: Intro to Hardware Programming with the Arduino Uno

digitalInput

Physically, it will look something like this:

Page 50: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #6: Pull-Ups / Pull-Downs

When the switch is closed, the circuit will look like this:

Now, the I/O pin is connected to GND, which the Arduino Uno will read as “LOW.”

I/O 12

Page 51: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #6: Pull-Ups / Pull-Downs

What is I/O 12 connected to when the switch is open?

It's not connected to LOW anymore, but it's also not connected to HIGH

A digital input only has two options, so the Arduino Uno must read it as LOW or HIGH, but you don't know which one it will “pick”

This is called a “floating input,” and it is bad, because you never know what you're going to get!

I/O 12

Page 52: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #6: Pull-Ups / Pull-Downs

We can avoid floating inputs with “pull-up” and “pull-down” resistors

A pull-up will leave the input pin to read HIGH in the absence of any other connection, while a pull-down will leave a floating input to read LOW

The Arduino Uno can implement pull-ups (but NOT pull-downs) in code, as you will see

Double-throw switches may not actually need pull-ups or pull-downs, but it won't hurt anything if you implement them anyway

I/O 12

Vin

Page 53: Intro to Hardware Programming with the Arduino Uno

Demonstration: digitalInput

Pretty much the same as digitalOutputs, until you press the button

What it does: Light up a series of LEDs, one at a time, in round robin robin. Press a button to make the LEDs light up in the opposite direction and at a faster speed.

The Magic:

– Same concepts as digitalOutputs, but this time uses an array of LED pins

– For each LED pin in array, turns LED on, pauses, turns LED off

– If button is not pressed, continue moving “forward” in turning LEDs on / off

– If button is pressed, move backwards to turn LEDs on /off

Page 54: Intro to Hardware Programming with the Arduino Uno

analogOutputs

Wire the green, yellow, and red LED up like this:

(same as digitalOutputs)

I/O 3

1kΩ

I/O 5

1kΩ

I/O 6

1kΩ

Page 55: Intro to Hardware Programming with the Arduino Uno

Arduino Uno I/O

By the way, why did we pick pins 3, 5, and 6, of all the pins that we could have picked?

Because those pins are 3 of the Input / Output pins that can also act as analog outputs (PWMs)

These pins are denoted by the ~ symbol

Page 56: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #7: Pulse Width Modulation

Because of the nature of field-effect transistors, digital outputs – either HIGH or LOW – are easy for a computer to generate

Analog outputs, which may have a value between HIGH and LOW – say, 1.7V, or 2.2V, or 3.1V – are NOT easy to generate

However, since we mostly only need analog outputs for physical devices, which cannot instantly turn on and off, we can simulate an analog input by rapidly turning a digital signal off and on, and varying the percentage of time the signal is “on”

Page 57: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #7: Pulse Width Modulation

0V

3.3V

0 ms

1 ms

2 ms

3 ms

4 ms

5 ms

A 1kHz waveform, “off” for 50% of the time, and “on” for 50% of the time.The “duty cyle” is 50%.The output voltage is effectively half of the maximum, or 1.65V

Page 58: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #7: Pulse Width Modulation

0V

3.3V

0 ms

1 ms

2 ms

3 ms

4 ms

5 ms

This 1kHz waveform is “off” for 75% of the time, and “on” for 25% of the time.The “duty cyle” is 25%.The output voltage is effectively one-quarter of the maximum, or .825V

Page 59: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #7: Pulse Width Modulation

0V

3.3V

0 ms

1 ms

2 ms

3 ms

4 ms

5 ms

This 1kHz waveform is “off” for 25% of the time, and “on” for 75% of the time.The “duty cyle” is 75%.The output voltage is effectively three-quarters of the maximum, or 2.48V

Page 60: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #7: Pulse Width Modulation

This seems stupid, but it actually works very well for a variety of applications

– Your car's fuel injectors

– Throttling the electric motors in a quadcopter

– Anti-lock brakes A PWM signal has two properties: the frequency and the

duty cycle

– The frequency should be fast enough that whatever you're driving operates smoothly, and slow enough to avoid high-frequency impedence

Page 61: Intro to Hardware Programming with the Arduino Uno

Electronics Mini-Lesson #7: Pulse Width Modulation

The PWM pins on the Arduino Uno are either 980 Hz (pins 5 and 6), or 490 Hz (pins 3, 9, 10, and 11)

– Both of these frequencies are much too fast for the eye to notice an LED flickering

The duty cycle can be anywhere between 0 (0%) and 255 (100%)

– If you want a percentage duty cycle, you must multiply by 255 and divide by 100 to set the PWM

Page 62: Intro to Hardware Programming with the Arduino Uno

Demonstration: analogOutputs

Behold the wonders of Pulse Width Modulation!

What It Does: Lights up a group of LEDs all at once, at different intervals/steps, from dimmest to brightest and back to dim again

The Magic:

– Instead of just turning on the LEDs all the way on or all the way off, analog output lets us light up the LED within a range of dimmer settings

– The settings are in large intervals/steps

Page 63: Intro to Hardware Programming with the Arduino Uno

Demonstration: analogOutputPulse

This program shows what's possible with small changes in PWM duty cycle

What It Does: Gradually lights up a group of LEDs all at once, from dimmest to brightest and back to dim again

The Magic:

Much like analogOutputs, except with PWM, the dimmer settings can be much smaller intervals / steps

Changes in the dim settings can be much more gradual

Page 64: Intro to Hardware Programming with the Arduino Uno

analogInput

Finally, we have analog inputs

Many times you wish to know not merely whether something is happening, but how much it is happening

Many sensors – temperature sensors, light sensors, pressure sensors, etc. – will deliver an output voltage as a reaction to whatever they're sensing

We will model this voltage with a potentiometer (variable resistor)

Page 65: Intro to Hardware Programming with the Arduino Uno

analogInput

Wire the green, yellow, and red LED up like this:

(same as digitalOutputs)

I/O 3

1kΩ

I/O 5

1kΩ

I/O 6

1kΩ

Page 66: Intro to Hardware Programming with the Arduino Uno

analogInput

And, wire the potentiometer up like this:+5V

10MΩ A0

The end terminals can be wiredup in any direction, but the center terminal should point to the analoginput pin

Page 67: Intro to Hardware Programming with the Arduino Uno

analogInput

Physically, it will look something like this:

Page 68: Intro to Hardware Programming with the Arduino Uno

Demonstration: analogInput

This program converts an analog input into an LED readout of sorts

Page 69: Intro to Hardware Programming with the Arduino Uno

Demonstration: analogInput

This effect would require a somewhat complicated circuit to implement if we weren't using the microcontroller

What It Does: Gradually light up each LED, one at a time, as we turn up the knob on the potentiometer, and then gradually dim each LED, one at a time, as we turn down the knob on the potentiometer.

The Magic:

– Cycling through an array of LEDs, based on the value of the potentiometer, turn the current LED all the way on, all the way off, or somewhere in between

– The “in between” is the analog value

Page 70: Intro to Hardware Programming with the Arduino Uno

Where to go from Here?

https://github.com/vuinguyen/ArduinoUnoBasics

What types of projects would you like to build?

Interest in hands-on workshop?

– For nominal fee through GDI (‘cause these workshops take a lot of work to create!)

– We can help you build that cool thing you’ve been wanting to build

– Sign up for our email list to give us input Educators

– We can help you create a course for your students – just ask us! Please take our survey by Friday, September 22:

https://goo.gl/forms/7dn6avLltAg28StD2

Page 71: Intro to Hardware Programming with the Arduino Uno

The End

Thanks for Coming!

https://github.com/vuinguyen/ArduinoUnoBasics

Sunfishempire.com

@sunfishgurl

@infoGuideApps