arduino 101arduino-info.wikispaces.com/.../arduino-101-part2-shed-oct-2012.pdf · arduino 101 part...

5
The Shed October/November 2012 67 Arduino 101 GND, INPUT, OUTPUT, etc. Arduino is powered by +5.0 Volts of DC (Direct Current).We show the +5.0 Volts connected HIGH on the top. Look closely at the YourDuino nd the PIN marked "5V". That's the one +5.0V power is connected to. Where does it come from? In the rst place it comes from the USB cable from your computer to Arduino. There is provision for a dedicated power supply, however. We show GND (Ground) connected LOW on the bottom of the Arduino. Find the Pin marked "GND". There are actually 3 "GND" pins. Rails The parallel lines of +5V (HIGH) on the diagram, and GND (LOW) on the diagram are called Rails. Like railroad rails across the top and bottom. Almost everything that happens on Arduino is between the +5V (High) rail and the GND (Low) (0.0V) rail. S o far we have begun to get acquainted with the Arduino and IDE, the “sketches” or programs that make it work, and we have got it working blinking an LED on and off. In this article we will delve a little deeper preparatory to diving right in with a fully edged project with some real- world applications in the next issue. We have set up a page on The Shed website to hold all the arduino sketches we are discussing here. You can simply copy and past them into your own IDE. To begin you need to understand a bit about electricity (see panel) and how that translates into the digital scheme of things. Circuit diagrams Often an actual circuit (like the YourDuino and Breadboard hookup) gets to be a confusing bunch of wires and components going in all directions. To keep it simple we draw Circuit Diagrams to show what we're trying to do. Notice the symbols used in the diagram above for things like: Switch, Resistor, LED and there are labels on connections, like Arduino 101 Part 2 by Terry King 5V +5 Volts Rail HIGH LOW 3 13 GND GND 0.0 Volts Ground Rail Arduino Current limiting Resistor Pulldown resistor LED Switch It's easy to connect many things to YourDuino. There are many pins for Ground and +5V. The two pairs of pins on the right side are handy for connecting to your breadboard. Three-pin cables with the standard pattern of Ground- Voltage-Signal (usually Black-Red-White) plug right in. Circuit diagram terminology. Some of the conventions used in circuit drawings.

Upload: duongphuc

Post on 17-Sep-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arduino 101arduino-info.wikispaces.com/.../Arduino-101-Part2-SHED-Oct-2012.pdf · Arduino 101 Part 2 by Terry King 5V +5 Volts Rail HIGH LOW 313 GND GND ... Ist Digit 2nd Digit3rd

The Shed October/November 2012 67

Arduino 101

GND, INPUT, OUTPUT, etc.

Arduino is powered by +5.0 Volts of

DC (Direct Current).We show the +5.0

Volts connected HIGH on the top. Look

closely at the YourDuino fi nd the PIN

marked "5V". That's the one +5.0V

power is connected to.

Where does it come from? In the fi rst

place it comes from the USB cable from

your computer to Arduino. There is

provision for a dedicated power supply,

however. We show GND (Ground)

connected LOW on the bottom of the

Arduino. Find the Pin marked "GND".

There are actually 3 "GND" pins.

RailsThe parallel lines of +5V (HIGH) on

the diagram, and GND (LOW) on the

diagram are called Rails. Like railroad

rails across the top and bottom. Almost

everything that happens on Arduino

is between the +5V (High) rail and the

GND (Low) (0.0V) rail.

So far we have begun to get

acquainted with the Arduino and

IDE, the “sketches” or programs

that make it work, and we have got it

working blinking an LED on and off. In

this article we will delve a little deeper

preparatory to diving right in with a

fully fl edged project with some real-

world applications in the next issue.

We have set up a page on The Shed

website to hold all the arduino sketches

we are discussing here. You can simply

copy and past them into your own IDE.

To begin you need to understand a bit

about electricity (see panel) and how

that translates into the digital scheme

of things.

Circuit diagramsOften an actual circuit (like the

YourDuino and Breadboard hookup)

gets to be a confusing bunch of wires and

components going in all directions. To

keep it simple we draw Circuit Diagrams

to show what we're trying to do. Notice

the symbols used in the diagram above

for things like: Switch, Resistor, LED

and there are labels on connections, like

Arduino 101 Part 2by Terry King

5V

+5 Volts RailHIGH

LOW

3 13

GND GND

0.0 Volts Ground Rail

Arduino

Current limiting Resistor

Pulldown resistor

LED

Switch

It's easy to connect many things to YourDuino. There are many pins for Ground and +5V. The two pairs of pins on the right side are handyfor connecting to your breadboard. Three-pin cables with the standard pattern of Ground-Voltage-Signal (usually Black-Red-White) plug right in.

Circuit diagram terminology. Some of the conventions used in circuit drawings.

Page 2: Arduino 101arduino-info.wikispaces.com/.../Arduino-101-Part2-SHED-Oct-2012.pdf · Arduino 101 Part 2 by Terry King 5V +5 Volts Rail HIGH LOW 313 GND GND ... Ist Digit 2nd Digit3rd

Digital signalYou will hear Digital signals described

three or four ways: but 0, OFF and LOW

mean the same thing. And 1, ON and

HIGH mean the same thing.

When a Pin (or wire or connection)

changes from 0 to 1, or 1 to 0, we say it

is a SIGNAL, like someone raising or

lowering a fl ag. Each signal is referred

to as a BIT (a Binary InTeger) A bit is

a number which has only two possible

values: 0 (Low) and 1(High).

Bits & BytesA group of 8 bits is called a BYTE. 1024

bytes (8192 bits) is one Kilobyte(It’s

based on 2 to the power of 10 (210).

1048576 bytes (2 to the power of 20) is

1 Megabyte (MB) and 1 Gigabyte (GB is

230.) Note that this convention is not

the same as a kilogram which is 1000

grams.

Output signals: An LED or Buzzer

connected to an Arduino OUTPUT

can "signal" you that something has

happened.

Input Signals: If you push a button

that changes an INPUT, you "signal"

Arduino that something should be done.

Time to hook real things up to those

INPUTS and OUTPUTS. Take a couple

of minutes to look at the Arduino board

closely. All regular Arduino boards have

the same overall size and the same long

black connector strips across the top

and bottom edges. These are female

sockets that pins can plug into. Let's

look at the details.

First the top connector: The sockets

are numbered 0 to 13 from right to

left. These are the DIGITAL INPUT/

OUTPUT connections. You can push

wires or the pins on the end of wires

into those "Black Holes" and connect

them to many different devices. We

then tell the arduino how to treat them:

as Input (digital Read) devices or output

(digitalWrite) devices.

Digital inputOur circuit diagram shows a very simple

circuit using a pushbutton to control

the outputs of two LEDs. Make up the

circuit shown on your breadboard.

The pushbutton switch causes the INPUT

to change from LOW to HIGH, which is

a "signal" to the arduino. Arduino can

change the OUTPUT on one LED from

LOW to HIGH, and the other from HIGH

to LOW. Copy and paste the sketch into

your IDE and load it.

This sketch adds three constants:

two LEDs on pins 10 and 11 and a

new INPUT, the BUTTON on pin 3.

This sketch introduces a new concept

essential to all microprocessor and

computer programs the “If” statement.

If statementThe “If” statement makes the computer

ask a question and take an action based

on the answer to that question. In this

case we have asked it to digitalread the

buttonPin and compare it to a reference.

The == symbol is used when we need to

The Shed October/November 201268

5V

+5 Volts Rail

3 11

GND

0.0 Volts Ground Rail

Arduino

Switch

10KΩ220Ω220Ω

10

The circuit diagram for the button input sketch.

The Shed October/November 2012

Electricity basics

We presume that you are familiar with the basics of electricity. But in case you’re not here’s a quick summary. An electrical circuit can be viewed as being similar to a plumbing system. The stream of water is the current and the pressure to drive the stream is the voltage. Imagine a stream flowing through a hose. Kinking the hose or adding a valve will cause the stream to be throttled; the more you narrow the hose the more constricted is the flow. This is resistance which constricts the flow of electricity and converts it into heat, lessening the current. Too much current can

be bad for some components, for example LEDs. In the case of electricity the battery represents the pressure in the system, the flow is current of course and the constriction is resistance. These three elements are linked through the fundamental formula known as Ohms law. Often called the most important formula in electricity and the only formula you really need to know.In summary it is:R (Resistance)= V (Voltage) / I (Current)V = R x 1I = V/R

V

I R

Ohms Law triangle shows the relationship between Voltage, current and resistance. Put your fi nger on the value you want to see how to calculate it.

Page 3: Arduino 101arduino-info.wikispaces.com/.../Arduino-101-Part2-SHED-Oct-2012.pdf · Arduino 101 Part 2 by Terry King 5V +5 Volts Rail HIGH LOW 313 GND GND ... Ist Digit 2nd Digit3rd

SHED Magazine Arduino Sketch Button Input - Reads state of Pushbutton, changes state of 2 LEDS - SEE the comments after "//" on each line below - CONNECTIONS: - Pushbutton Switch from +5 to Pin 3 - 10K Resistor from Pin 3 to ground - LED and 220 ohm resistor in series from pins 10 and 11 to Gnd Questions: [email protected] */

/*-----( Declare Constants and Pin Numbers )-----*/#define buttonPin 3 // Pins to connect to#define ledPin1 10#define ledPin2 11

void setup() /****** SETUP: RUNS ONCE ******/{ pinMode (ledPin1,OUTPUT) ; // PIN 10 is an OUTPUT pinMode (ledPin2, OUTPUT) ; // PIN 11 is an OUPUT pinMode (buttonPin, INPUT) ; // PIN 3 is an INPUT}//--(end setup )---

void loop() /****** LOOP: RUNS CONSTANTLY ******/{ if (digitalRead(buttonPin)==HIGH) // IF button is pushed { digitalWrite(ledPin1, HIGH) ; // LED1 ON digitalWrite(ledPin2, LOW) ; // LED2 OFF } else { digitalWrite(ledPin1, LOW) ; // LED1 OFF digitalWrite(ledPin2, HIGH) ; // LED2 ON } delay(50); // Switch may be "bouncing". Wait a bit

} //--(end main loop )---

Aurdino 101

compare a value against a reference you

supplied (that’s what HIGH means). It

returns an answer of either true or false.

If TRUE then the program carries

out the actions described in the curly

brackets immediately following. If

FALSE then it runs the instructions in

the “else” condition. The else condition

is optional the default is to do nothing

and make no change.

Feel free to make additions to this

sketch and try different combinations.

Add more pushbuttons for example or

more LEDS.

ResistorsYou may be wondering what the

purpose of the 10kΩ resistor connected

to the GRD rail is. This is what is called

a pulldown resistor. In this case it ties

the output to ground when the button is

open. Remove it and see how the circuit

performs.

Without the resistor the arduino gets

no signal to the pin so it tends to pick

up stray electromagnetic signals called

noise and these can cause the pin to

fl uctuate between its two states rapidly

and generate an inconsistent response.

The resistor ties the circuit to ground

with a high resistance when the circuit

is open so the pin has a LOW signal

rather than NO signal.

The lower resistance of the closed

circuit (when the button pushed) will

activate the pin high as more current

will fl ow directly to the pin overriding

the pulldown resistor. The switch

will PULLUP the pin against the

PULLDOWN resistor. These resistors

are important in digital circuits. They

SHED Magazine Arduino Sketch Button Input - Reads state of Pushbutton, changes state of 2 LEDS - SEE the comments after "//" on each line below - CONNECTIONS: - Pushbutton Switch from +5 to Pin 3 - 10K Resistor from Pin 3 to ground- LED and 220 ohm resistor in series from pins 10 and 11 to Gnd

Aurdino 101

5V

+5 Volts Rail

GND

0.0 Volts Ground Rail

ArduinoA0 S9Pot

Servo

Layout for the ButtonInput sketch.• Pushbutton switch from +5V and

Pin 3• 10k resistor from pin 3 to ground• LED and 220Ω resistor in series

from Pins 10 and 11 to Gnd

The diagram for the Potentiometer-controlled Servo.

You can copy this from The Shed website page at www.theshedmag.co.nz/online/arduino and paste it directly in to your IDE.

The Shed October/November 2012 69

Page 4: Arduino 101arduino-info.wikispaces.com/.../Arduino-101-Part2-SHED-Oct-2012.pdf · Arduino 101 Part 2 by Terry King 5V +5 Volts Rail HIGH LOW 313 GND GND ... Ist Digit 2nd Digit3rd

The Shed October/November 201270

The Shed October/November 2012 25

Resistors

Resistors are the most common electronic component; they are also the simplest. They have one job to do and that is to restrict the flow of current. Resistance is measured in Ohms with the Ω symbol. They range in value from 1 ohm to megohms (MΩ). Resistors are non polar so they have no specific orientation. They are usually marked with coloured bands and these bands have a specific code. It’s worth knowing the code and you will probably get to know it with time especially the more common values. You can also check a resistor’s value with a multimeter set to read resistance but being able to read the code is a serious time saver. There is much more to know about resistors and how they operate in a circuit and Make:Electronics is a recommended source for more information.

RESISTORS COLOUR CODE Most resistors have a colour code to represent their value. There are 4 or 5 coloured bands on the resistor. To one end is a stripe of either gold or silver usually alone. If you orient this to be on the right side then the first two bands from the left represent the value of the resistor. The third band indicates how many zeros to add or its exponent for those that know their maths.

2

3

4

5

6

7

9

8

2

3

4

5

6

7

9

8

2

1 11

3

4

5

6

7

9

8

100

1000

10000

100000

1000000

0.01 Silver

0.1 Gold

Ist Digit 2nd Digit Multiplier3rd Digit

10% Silver

5% Gold

Tolerance

1%

2 %

1 0 000 ±5%10K Ohm ±5%

4 7 0 00 ±1%47K Ohm ±1%

0 00 0

10

Layout for the ButtonInput sketch.• Pushbutton switch from +5V

and Pin 3• 10k resistor from pin 3 to ground• LED and 220Ω resistor in series

from Pins 10 and 11 to Gnd

Page 5: Arduino 101arduino-info.wikispaces.com/.../Arduino-101-Part2-SHED-Oct-2012.pdf · Arduino 101 Part 2 by Terry King 5V +5 Volts Rail HIGH LOW 313 GND GND ... Ist Digit 2nd Digit3rd

Aurdino 101

The Shed October/November 2012 71

LED

LEDs or Light Emitting Diodes have become universally used in every electronic or electrical device even as replacements for incandescent light. They come in a wide variety of colours from infrared to ultraviolet and most of the visible spectrum in between. There are also special LEDs that can be made to run Red, Green or Blue and we have included one in The Shed starter kit.

A diode is a device that allows electricity to flow in one direction only. Power can run through a diode easily but only in one direction. They are also sensitive to current.

Too much current can destroy them. Ideally LEDs prefer a current of only around 20mA (20 Milliamps or 0.02 amps) at around 2.5 Volts. Any more and you risk destroying them. That’s why we usually apply a current limiting resistor to one leg of the LED. In this case we use a 220Ω resistor. To see what current flow 220Ω provides on 5V use Ohms Law where I=V/R = 5V/220Ω =0.022A or 22mA.

LED’s have a positive and a negative leg or anode and cathode. The positive leg that is connected to the +5v line is the longer leg. The negative leg is shorter. It is also identified by a flat spot ground on the side of the casing rim.

can also be used as pullup resistors

keeping the circuit high when open.

You will also notice the addition of the

two 220Ω current limiting resistors for

the LEDs (see LED panel). We didn’t need

this previously because the Yourduino

has one built in to Pin 13.

Analog InSo far we have covered digital signals

out (digitalWrite) and digital in (digital

Read). But Arduino also has analog

inputs. Analog inputs are far more

common than digital (although that is

rapidly changing). We have incorporated

several into the starter kit. Digital has

two values either on or off but analog

has an infi nite number of values. One

example of an analog in the starter kit is

the potentiometer. A potentiometer (or

Pot) is a variable resistor.

The Pot has three connections, the outer

two left and right are connected to the

+5V and GND respectively and the

centre connection is the signal. From

the schematic you can see that the pot is

basically a surround of resistive material

and a wiper that contacts it.

As we move the pot, the output voltage

going to Arduino varies from 0 to 5 Volts

and all the values in between. Arduino

reads these as values from 0-1023 where

0 is the GRD and 1023 is +5V.

In this sketch we will use the input signal

from the pot to control a servomotor.

A servomotor is motor that can move

to any position through 180̊ . They are

widely used for controlling things like

steering, for example, but any purpose

that requires incremental operation of a

mechanical device is suitable for a servo.

The stepper motor in The Shed Start

Kit has three wires running to a three-

pin female connector. These three

connections are GND (Brown) +5V (Red),

and Signal (Orange) respectively. You will

see on the Yourduino board that there are

a number of three pin connectors in both

the digital and analog connectors.

The digital connections in the top block

are coloured white, red and black. The

black is GDN, the red +5V and the white

is the Signal. This is the connection

for the three-pin connectors. If you

have a different arduino you will have

to improvise this connection on the

breadboard.

Find the sketch called ServoPot in the

sketch depository in The Shed website.

Copy and paste it directly into your IDE.

Set up the circuit as described on your

breadboard and the arduino and upload

the sketch to YourDuino. As you move

the pot the servo should turn back and

forth.

What’s happening here? An Analog

Input Device (the Pot) is feeding a

varying voltage into an Arduino Analog

input. The Arduino sketch is making

decisions based on that value to send

an Output Signal to the Servomotor.

Arduino is reading the position of the Pot

wiper scaling that into a digital signal

and sending that signal to the servo. To

ensure that the servo actually has time

to move to the position there is a delay of

25 milliseconds before it reads again.

ED

LEDs come in every colour, shape and size imaginable.

The anode is the longer leg. The cathode side is shorter and has a fl attened edge.