18
Hello World! Intro to Arduino Use the SparkFun RedBoard (Arduino) to introduce code to your circuits. Tags: Classic Circuits Arduino Grades: 5 to 12+ | Duration: 1/2 Hour - 1 Hour Supplies: SparkFun RedBoard (or any programmable board), 5 connector cables, 1 Red-Blue LED, Slider, Circuit Scribe Pen Products: Connector Cable Circuit Scribe Pen Slider LED

Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

Hello World! Intro toArduino

Use the SparkFun RedBoard (Arduino) to introduce code to your circuits.

Tags: Classic Circuits Arduino

Grades: 5 to 12+ | Duration: 1/2 Hour - 1Hour

Supplies: SparkFun RedBoard (or anyprogrammable board), 5 connector cables,1 Red-Blue LED, Slider, Circuit Scribe Pen

Products:

Connector CableCircuit Scribe PenSliderLED

Page 2: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

Step #1

Intro: Using theRedBoard (or Arduinoor Other)

Microcontrollers are

programmable boards, or

essentially tiny

computers, which you can

tell what to do!

Programmable boards,

like the RedBoard,

Arduino, and many

others, let you take your

circuits to the next level

using code. With code you

can change how your

circuits behave using

conditional statements,

logic, variables, and more.

In this project we'll be

using the SparkFun

RedBoard, but most

microcontroller boards

operate in exactly the

same way.

Step #2

Intro to the Pins

Page 3: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

Intro to the Pins

Along the left and right

sides of the RedBoard are

series of pins. These pins

will play a key role when

you interact with the

board.

Most of the pins can be

set up to as inputs or

outputs for either analog

or digital signals. The

signals are read or written

as voltages.

Input - takes

information in, like

amount of light from

a photoresistor or

whether a button is

pushed.

Output - sends

information out, like

lighting up an LED or

spinning a motor.

Digital - takes on

one of two states,

like a light switch,

which can only be

ON or OFF.

Analog - takes on a

range of values, like

a TV's volume

setting. The

AnalogWrite

command uses

values anywhere

from 0 to 255.

Page 4: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

You can have pins set to

be digital outputs, analog

inputs, etc. The pins are

what you use to connect

the RedBoard to all of

your physical

components, like your

LEDs, switches, motors,

etc. In the code you refer

to pins by number. There

are also several special

pins with specific

functions. For example,

Vin allows you to supply

power to the board via a

battery so you can use

your board away from

your computer.

Step #3

Understanding theCircuit: Overview

This diagram shows how

your circuit should look

when you're done

hooking it up to your

RedBoard.

Page 5: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

Button (input): The state

of the button (on/off) is

read by digital pin 2.

Start by connecting one

side of the button to that

pin. That’s not all though!

For this circuit to work we

connect the button to

ground through the slider

(as a resistor) to force the

voltage at pin 2 to LOW

(0V, gnd) when the button

is not pressed.The

opposite side of the

button is connected to

the 5V pin. The signal at

pin 2 will jump up to HIGH

(or 5V) when you press

the button.

LED (output): Our LED is

connected to two digital

pins (4 and 6).The LED

will turn on when one pin

is HIGH and one is LOW,

creating a voltage drop

across the LED. We’ll

alternate them to change

the color of the bi-

directional LED module.

Page 6: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

Group Leader Tip:

Using the slider this

way is called a

“pulldown resistor”

because it pulls the

voltage of pin 2 to

ground when the

button is not pressed.

Without this, your LED

will flicker a lot. This is

because pin 2 is now a

"floating" input, which

is super sensitive to

changes in capacitance

from nearby wires, your

fingers, and even the

air. So if you or your

students have a

flickering LED, check

this part of the circuit!

Step #4

Draw it Out

Remember the Squishy

button from the Intro to

Switches project? We're

bringing it back!

When designing

your button, draw

two shapes

(rectangles,

triangles, circles)

with a gap in

between.

Create another

Page 7: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

piece of paper in

any shape, and draw

a patch of silver ink

on the back side.

This will bridge the

gap in the circuit.

Finally, stick the

paper over the gap

using two rolled up

pieces of scotch

tape on the edges.

The tape will lift the

button off the page

until you press it!

After designing your

button, finish drawing the

rest of the circuit:

You'll need to draw

out 5 points for the

RedBoard to

connect the

following pins; 5V,

ground, the button

pin (2), and both LED

pins (4 and 6).

Use the circuit

stencil to draw out

two 2-pad

connections for the

LED and the slider.

Draw the connecting

wires as shown.

Page 8: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

Step #5

Intro to the Code

The RedBoard uses the

Arduino IDE for its code.

You can download the IDE

for free from here.

When you open up the

IDE for the first time, you

probably see these lines:

Copy

Page 9: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

As you can see there are

two main functions; the

setup function and the

loop function. We'll

explore each in more

detail in the following

steps.

Usually, when working

with programmable

boards your code will

generally follow and/or

repeat this pattern.

1. Read in some

inputs.

Ex: Look at

whether or not

a button is

pressed.

2. Make some

decisions based on

the data you read.

Ex: If the

button is

pressed,

increment a

counter.

void setup() {

// put your setup code here,

//to run once:

}

void loop() {

// put your main code here,

//to run repeatedly:

}

1

2

3

4

5

6

7

8

9

Page 10: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

3. Take some action

based on the

decision you made.

Ex: If the

counter

reaches some

value, tell an

LED to turn on.

Next, we'll start working

with the code for this

project! Download it from

the link below and it will

automatically open in IDE.

Attachments:Hello_World.ino

Page 11: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

Step #6

Intro to the Code:Declare Variables

Before we get into any of

the action, every program

needs to start with some

definitions. Above the

setup function, you will

declare and define all of

the variables that you'll

use in the code.

int const pinName =

pinNumber;

In the first three lines we

are assigning names to

the pins that we're using.

The word int means that

the pin number is an

integer, and const means

that it remains constant

for the whole program.

int bVal;

Creating a variable to

store the button state

looks similar, except we

don't need to assign a

value to it yet. We also

use the word int because

the value will either be

always be an integer -- a 1

or a 0.

Page 12: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

Step #7

Intro to the Code:Setup

The setup function is the

first function to run and it

only runs once. It does

exactly what the name

implies, you use it to set

up everything you'll need

for your project. You use

the setup function to

declare which pins you'll

be using and how they'll

work.

For this project you'll use

the following lines in your

setup function:

pinMode(pinNumber ,

Mode);

This line defines how your

pins will work. The Mode

will either be INPUT or

OUTPUT depending on

whether you intend on

reading in or writing out

information.

digitalWrite(pinNum

ber , Value);

This line initializes the pin

to the value you want.

Since this digitalWrite,

the value can only be

either HIGH or LOW.

Page 13: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

When a pin is set to HIGH

it's voltage is set to 5V (or

3.3V for certain boards).

When a pin is set to LOW

it's voltage is set to 0V

(ground).

Group Leader Tip:

As you can see in the

examples above, you

will often need to refer

to pins by their

number. There may be

some cases where you

want to switch the pin

you're using for a

certain component.

Rather than having to

scroll through all of

your code, changing

each place you used

that pin number, it may

be helpful to name

your pins before the

setup function. Then

throughout your code

you can just refer to the

pin by name and if you

want to change

something you just

need to change the

number associated

with the name. This is

what we do in the

example code!

Step #8

Intro to the Code:Loop

Page 14: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

Loop

The loop function is

where most of the action

happens. It comes after

the setup function and

then runs again and

again, repeating until you

turn off the RedBoard.

For this project you'll use

the following lines in your

loop function:

bVal =

digitalRead(buttonPi

n);

This line looks at the pin

within the parentheses (in

our case the pin

connected to our push

button), reads the value,

and then writes it to the

variable bVal. Since this is

a digitalRead this

function will only return

either a 1 (HIGH) or 0

(LOW).

digitalWrite(pinNum

ber , Value);

This line works just like it

did in the setup function.

Except since this is

happening repeatedly it

helps to think of it as

setting or changing the

value of your pin rather

than "initializing it."

Page 15: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

Remember, current flows

from HIGH to LOW

voltage points. In our

example, if pin 4 is HIGH

and pin 6 is LOW the

current will flow in one

direction through the LED,

lighting it up. In the

program we press the

button to flip which pin is

HIGH and which is LOW,

and as a result we switch

the direction the current

flows through the LED.

That changes its color!

if (bVal == Value)

{}

This line is a conditional

"if" statement. It looks at

the value of the variable

bVal to determine

whether the statement

inside the "()" is true. If

the statement is true the

RedBoard will run the

code inside the "{}"

brackets. In this case,

we're checking whether

the button is pressed, and

then setting the LED

color.

Step #9

Putting it Alltogether!

Page 16: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

Uploading your Code

Before you upload

your code you'll

want to try

compiling it. This

checks for syntax

errors. So make sure

to check your

parentheses, semi-

colons, spelling, etc.

A quick way to do

most of this is to

click the "verify"

button. It's the blue

checkmark in the

top left corner of the

IDE.

Once you're sure

that your code

compiles make sure

to check that you

have the correct

COM port and board

selected. They are

both under the Tools

menu.

When you are finally

ready click the

Upload button, the

blue arrow in the

top left of the IDE.

Your RedBoard will

blink a bit. Once

you're done you're

all set to try it out!

Try it out!

Page 17: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

Try out your project by

pressing the squishy

paper button. See if your

LED changes color!

Page 18: Hello World! Intro to Arduino - Direct National · Hello_World.ino. Step #6 Intro to the Code: Declare Variables Before we get into any of the action, every program needs to start

Step #10

More Ideas

Make your own

Lightshow: Try editing

the code yourself. Add in

some extra digitalWrite

commands to your loop

and see what cool

flashing light patterns you

can make.

Morse Code: Check out

the morse code project

where you use a similar

circuit (and a lot more

code!) to interpret coded

messages.

More Arduino: For a

more detailed intro to

coding on Arduino-like

boards check out

Arduino's tutorials.