65
1 Arduino Basic Projects for Absolute Beginners Created as a companion manual to the Toronto Public Library Arduino Kits. Arduino Basic Projects for Absolute Beginners Copyright © 2018 Toronto Public Library. All rights reserved. The Toronto Public Library is not responsible for personal injury or damage to property that occurs with use of the Toronto Public Library’s Arduino Kits and the Arduino Kit Manual. Images created using Fritzing software.

for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

1

Arduino Basic Projects for Absolute Beginners

Created as a companion manual to the Toronto Public Library Arduino Kits.

Arduino Basic Projects for Absolute Beginners

Copyright © 2018 Toronto Public Library. All rights reserved.

The Toronto Public Library is not responsible for personal injury or damage to property

that occurs with use of the Toronto Public Library’s Arduino Kits and the Arduino Kit

Manual. Images created using Fritzing software.

Page 2: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

2

Table of Contents

PREFACE ................................................................................................................................................. 3

INTRODUCTION ..................................................................................................................................... 5

SAFETY TIPS ........................................................................................................................................... 9

ABOUT THE ARDUINO STARTER KIT ................................................................................................... 11

ARDUINO UNO TOUR .......................................................................................................................... 13

GETTING TO KNOW YOUR BREADBOARD .......................................................................................... 14

FIRST ELECTRONICS ............................................................................................................................. 16

GETTING TO KNOW THE ARDUINO SOFTWARE ................................................................................ 20

THE “MINIMUM” PROGRAM .............................................................................................................. 24

BLINK MY INTERNAL LIGHT: PROJECT 1 ............................................................................................. 28

BLINK AN EXTERNAL LED: PROJECT 2 ................................................................................................. 32

BRIGHTER LIGHT: PROJECT 3 .............................................................................................................. 36

TRAFFIC LIGHT SIMULATOR: PROJECT 4 ............................................................................................ 37

BUTTONS: PROJECT 5 .......................................................................................................................... 45

TONE CREATOR: PROJECT 6 ................................................................................................................ 49

MY FIRST INSTRUMENT: PROJECT 7 ................................................................................................... 51

RECOMMENDED RESOURCES ............................................................................................................. 63

Page 3: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

3

Preface Time Required: 5 minutes

Thank you for borrowing Toronto Public Library’s Arduino Kit. Please return this kit to

the Digital Innovation Hub from which it was borrowed.

Page 4: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

4

Borrowing Arduino Kits

Arduino Kits are available to Toronto Public Library customers with a valid Teen

(13-17), Adult – Under 25 (18 – 24), or Adult (25+) library card.

Holds cannot be placed on the Arduino Kits.

You can only borrow one Arduino Kit at a time. Each kit can be borrowed for 21

days and cannot be renewed.

Fines Per Day and Maximum Fines for Arduino Kits

CARD TYPE FINE AMOUNT

PER DAY

MAXIMUM YOU WILL

BE CHARGED FOR

EACH LOAN PERIOD

Adult $0.35 $14.00

Adult Under 25 (18-24) $0.15 $6.00

Teen (13-17) $0.15 $6.00

If you lose an Arduino Kit, you will be charged the purchase price of the Arduino

($50). The library does not accept a replacement Arduino or an item of equal value.

If the Arduino Kit is overdue by more than 40 days, the library considers it lost. If

you find the kit within 6 months of paying the replacement cost you can get a

refund, minus any overdue fines so please keep your receipt.

Please report damaged equipment or missing parts to the Digital Innovation Hub

staff from which it was borrowed. Damaged Arduino boards and kits are subject

to replacement purchase fees.

Page 5: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

5

Introduction Time Required: 5 minutes

An Arduino is a microcontroller; a small, simple computer on a single integrated circuit.

It is designed specifically for beginners who are new to coding and electronics. You can

learn more about the Arduino at https://www.arduino.cc/en/Guide/Introduction.

There are thousands of projects you can build with an Arduino.

Parts In This Kit

Your assembled kit includes all the parts you will need for the Arduino projects outlined

in this manual. When you are done with your projects, please return the parts to their

proper slots for the next person to enjoy.

There are different types of Arduinos. This kit uses a blue

Arduino Uno board. The different parts on the Arduino are

labelled in white.

Page 6: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

6

The USB cable is used to connect the Arduino to your

computer.

The breadboard lets you build circuits. It has a series of holes

where you can insert wires to create circuits. The magic of a

breadboard is that it’s reusable, and you don’t need to solder

(permanently joining components together to form a circuit

by melting metals).

Page 7: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

7

Jumper wires are used to create electric circuits and can be

inserted into the breadboard.

LEDs (light-emitting diodes) are electronic devices that emit

light when an electrical current passes through them.

Resistors help control the flow of electricity in your circuit by

limiting the current. They come in different values measured

in ohms (Ω). Using the correct value resistor is important

because they protect the LEDs and other parts from being

damaged by too much current. They have different colour

bands on them to tell you what kind of resistance it is.

Page 8: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

8

Before you can use the Arduino, you will need to set up the

Arduino IDE software on your computer. You’ll be using the

IDE (Integrated Development Environment) to write code

and upload it to your Arduino. The code for the Arduino is

called a Sketch.

Page 9: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

9

Safety Tips

Toronto Public Library’s Arduino Kits use low voltage electricity and are not inherently

dangerous. However, safety is always important when working with electrical circuits.

Please follow the safety tips and instructions in this manual at all times.

Expert Tip: Always treat electronic projects as if they could

have potentially dangerous voltages.

Each project has been planned and mapped out for you. Please take the time to read and

thoroughly review the project instructions from beginning to end before you begin.

Ensure that wires are connected accurately and in accordance with the diagrams

provided. Not following the instructions as specified may result in personal injury or

damage to the equipment.

Expert Tip: Turn off all power sources before modifying the

circuit. Keep your Arduino unplugged while you are

connecting wires and parts. Only connect it to the computer

after your setup matches the diagram provided.

Keep your work surface clear when using this kit and maintain an orderly and safe work

environment. Keep food and drinks away from the work area while working with your

Arduino kit. Always unplug the Arduino when not in use. After using the kit, return all

the parts to their proper storage place.

Expert Tip: Place the Arduino on a non-metal surface and

refrain from working on metallic surfaces.

Page 10: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

10

Warnings

This kit is not a toy and is not appropriate for small children. Small parts may present

a choking hazard. Not for children under 3.

Avoid touching the exposed end of ground and power wires when connected to the

Arduino.

Use only the materials provided in the Arduino Kit.

Do not make alterations or perform major repairs on the Arduino Kit.

No soldering with the TPL Arduino Kit.

Do not use lithium ion batteries, they may explode when shorted

Do not use on metallic surfaces, such as your Macbook. Place the Arduino on a non-

metal surface and refrain from working on the surface of your Macbook.

The library is not responsible for damage to any equipment and hardware used with

the kit, including personal computers, laptops or tablets.

Unplug the Arduino when not in use.

Turn off/disconnect all power sources before modifying a circuit. While you’re

connecting components, keep your Arduino unplugged. Only connect it to a computer

or power source after the circuit is complete.

Page 11: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

11

About the Arduino Kit Time Required: 10 minutes

The basic kit includes all the parts you’ll need to get you started in the wonderful world

of Arduino!

If you get stuck or have any issues or questions with this Arduino Kit, feel free to ask

any of our Digital Innovation Hub staff. We also have Arduino classes that will teach

you all the basics, and Arduino Clubs for free-form learning with staff support.

An Arduino is a low-cost microcontroller that is designed for

beginners and is easy to learn.

Now you’re ready to unpack your kit. Take a look inside and make sure you have all

the necessary parts, listed on the following page.

Page 12: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

12

Part Inventory for the Basic Kit

1x Arduino

The Arduino is the

microcontroller and

brains of our project.

It stores programs

and processes inputs

and outputs.

1x Breadboard

The breadboard is

used to temporarily

connect multiple

components and

wires together during

prototyping.

16x Jumper Wires

Jumper wires connect

the components

completing a circuit.

Note: The colour of

the wires do not

matter when building

the projects.

6x 220ohm

Resistors (red, red,

brown, gold bands

on resistor)

Resistors are used to

reduce the flow of

electric current in a

circuit. This prevents

damage to LEDs that

require a lower

voltage.

1x Piezo

The piezo is used to

create sound for the

projects.

6x 10kohm

Resistors (brown,

black, orange, gold

bands on resistor)

Used in a circuit for

the switches.

5x Pushbuttons

Used to interrupt a

circuit. When you

push and hold the

button, it completes

the circuit.

6x LEDs

LED stands for Light

Emitting Diode and

is used to create light.

These LEDs typically

require around 1 volt.

Page 13: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

13

Arduino Uno Tour Time Required: 10 minutes

Spend a few moments looking at the diagram below and compare it to the Arduino

included in your kit.

The Arduino has been labeled to help you learn all the different connectors and parts.

Digital pins (pins 2-13)

Use these pins with

digitalWrite(), digitalRead()

and analogWrite() functions.

analogWrite() works with

pins that have a ~ beside the

number.

Analog in

Used as inputs with the

analogRead() command.

Reset button

Used to reset the Arduino.

USB Port

Used to power the Arduino

with 5 volts. Also used to

upload code to the Arduino.

DC Power Jack

Used to power the

Arduino with an

external DC power

supply (7-12volts only).

5v and GND Power

Used to provide 5 volts of

positive power, and GND

to ground your circuit.

Power LED

When lit, it means the

Arduino is powered on.

RX and TX LED

Tells you when your Arduino

is sending or receiving

commands to your computer.

Usually will blink when

uploading a sketch or using

serial communication.

Built-in LED

A built-in LED that you

can control. It shares

digital pin 13. ATmega328P

This microcontroller is

the brains of the

Arduino.

Page 14: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

14

Power rails

All the power rails have invisible wires

under that run vertically.

Connectors Five holes in each of

the horizontal rows

are connected.

Getting to Know your Breadboard Time Required: 20 minutes

Video resources about breadboards: http://goo.gl/6HPHbg

In order for us to connect our tiny components together, we need our breadboard. A

breadboard is great for prototyping since it does not create a permanent connection

between components like soldering does. Everything is held together by friction when

you insert them into those tiny holes inside your breadboard.

Remember: If you have any questions, or need some extra

help, feel free to visit a Digital Innovation Hub at the Toronto

Public Library for classes or assistance.

What a breadboard looks like if we could see the wires under the breadboard

Page 15: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

15

Above is an example of how a breadboard usually looks; on the right is how a breadboard

would look if we could see the wires that connect all of the holes together. Those hidden

wires are used to connect all your components to each other while you prototype.

Take a look at your breadboard. You may have noticed that the breadboard holes are all

labeled A to J (vertically) and from 1-30 (horizontally). This is used to indicate where to

place your components.

Throughout this guide, we will be asking you to place your components in very specific

holes within your breadboard. For example, we might ask you to put a wire into hole 3E.

Now is the time to get familiar with the layout of your breadboard.

Page 16: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

16

First electronics Time Required: 30 minutes

For this first project, we’ll be creating a very basic circuit that can power an LED light

when a switch is pressed on the breadboard. We will not need to program our Arduino.

It does not matter what colour the wires you use compared to the wiring diagram. All

wires will conduct the same electricity.

Required Components

1x Arduino 1x

Breadboard

2x Jumper

Wires

1x 220ohm

Resistors

(red, red,

brown,

gold)

1x LED 1x

Pushbutton

Make sure you use the correct resistor! Each resistor has a

value, in ohms, that tells you how strongly it resists the flow

of current. To tell the resistors apart, note the coloured bands,

and the order in which they are arranged. The basic kit

includes three 220ohm resistors, and three 10kohm resistors.

Page 17: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

17

For this project we will only be using the Arduino to power for our LED. At this time, we

won’t need to write any Arduino code.

Step 1

Whenever you work on your Arduino, make sure you disconnect it from any power

source while connecting the components. Always double check that everything is

connected correctly, so you don’t short-circuit your Arduino.

Step 2

Time to plug our components into the breadboard!

Find a pushbutton and plug it into the breadboard as shown. There are four pins for this

component. Plug the pins from the switch into the breadboard holes E1, F1, E3 and F3.

Page 18: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

18

Step 3

Find the 220ohm resistor (the resistor with the colour bands red, red, brown and gold) included

in your kit. Plug one side of the resistor into the breadboard hole G3 and the other side

into the hole G7.

Step 4

Find your red LED. Take note that the LED has one leg that’s longer than the other. The

longer leg is our positive anode and the shorter is the negative cathode. Plug in the shorter

leg into the breadboard hole H7 and the longer leg into the hole the H8.

LED stands for Light Emitting Diode. What is a diode you might ask?

A diode is an electrical component that allows electricity to flow in only one

direction from positive to negative. That is why our LED has a positive (the longer

wire) and negative side wire.

Page 19: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

19

Step 5

To give the LED power, we will need to connect the jumper wire from the Arduino’s 5V

pin to the breadboard's hole F8.

Step 6

Lastly, we need to complete the circuit. Plug in a jumper wire from any of the Arduino’s

available GND pins to the breadboard at G1.

Step 7

That’s it! We are now done our first project. Just plug in your Arduino to your

computer using the USB cable (USB-A to USB-B).

Once powered, your LED will turn on whenever you push the switch. When you push

the button, it completes the circuit. Power flows from the 5V pin on the Arduino, to

the LED, then to the resistor, and then to the switch. When pushed, current flows

finally back to the Arduino’s GND pin.

Page 20: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

20

Getting to know the Arduino Software Time Required: 25 minutes

Arduino IDE (Integrated Development Environment)

The Arduino IDE is a software used to write, test, and upload programs to the Arduino.

Windows, Mac OS and Linux each have their own version of Arduino IDE available for

download at http://arduino.cc/en/main/software

Installing Arduino IDE

Windows

1. Open the Arduino downloads page at http://arduino.cc/en/main/software and

click the Windows link. This will download the .zip file of the Arduino application.

2. Unzip the downloaded folder by right clicking it and selecting “Extract All”.

3. To connect the Arduino to the computer, plug the square end of the USB into the

Arduino and the flat end into the computer. On the Arduino, the green LED will

turn on and Windows will try to find drivers. If drivers are found, skip to step

five.

4. If it fails to find drivers, close the window and install drivers manually.

a. Open the Start menu and in the Search Programs and Files box, type

devmgmt.msc. Press Enter to open the Device Manager window.

b. Click Browse to find your Arduino folder from where you saved it in Step

one.

c. In the Arduino folder, click the Drivers folder and then the Arduino Uno file

d. Click Next, and Windows completes the installation.

5. Now that you installed the software and drivers, you can open it by clicking the

arduino.exe file in the Arduino folder. You can also make a desktop shortcut for

convenient access to the Arduino IDE software.

6. Double click the icon to launch the application.

Page 21: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

21

Mac OS

1. Open the Arduino downloads page at http://arduino.cc/en/main/software and

click the MAC OS link.

2. Double click the app and place it in the applications folder.

3. To connect the Arduino to the computer, plug the square end of the USB into the

Arduino and the flat end into the computer.

4. A dialogue box will appear on screen with a message “A new network interface

has been detected”.

5. Click Network Preferences, then click Apply. Even if the left side of the window

displays the Arduino as “not configured”, it will work.

6. Close the Network Preferences window.

7. To launch the Arduino application, go to the Applications folder and select the

Arduino app.

Arduino IDE Software Environment

Programs written for Arduino are called “Sketches”. It is recommended to explore the

Arduino software environment before you start your first sketch.

The Arduino software is presented as a GUI (Graphical User Interface), which provides

a visual way of interacting with a computer. The turquoise coloured window is

Arduino’s GUI, and it is divided into four main areas.

Page 22: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

22

Arduino IDE as viewed in Mac OS 11

Menu Bar

The menu bar contains dropdown menus of all the tools, settings and

information relevant to the software. For Windows, the menu bar is located at

the top of the Arduino window and in Mac OS, the menu bar is located at the top

of the screen.

Toolbar

The toolbar contains common buttons for writing sketches for Arduino.

Verify – The Verify function is used to check that the written code does

not have obvious mistakes. However, it doesn’t guarantee the code works

as intended.

Page 23: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

23

Upload - The Upload function sends the sketch to the connected Arduino

board.

New - The New function is used to create a new sketch.

Open - The Open function is used to open an existing sketch.

Save - The Save function is used to save the current sketch.

Serial Monitor - The Serial Monitor function is used to view data sent to

or received by the Arduino board. Unlike other toolbar functions, it is

located on the far right of the toolbar.

Text Editor

The text editor displays the sketch as text. It has Arduino sketch feature like

colour codes and auto formatting.

Message Area

The message area notifies you of common errors with the Arduino.

Page 24: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

24

The “Minimum” Program Time Required: 15 minutes

Plug in your Arduino to the computer and launch the Arduino IDE software or web

interface.

Load the bare minimum example from the Arduino software to test if we can upload

this sketch.

Go to File -> Examples -> 01.Basics -> BareMinimum to load the bare minimum example

from the Arduino software to test if we can upload this sketch. First let us just go over

what is in the BareMinimum sketch.

Page 25: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

25

That is the bare minimum code you must have to be able to upload to the Arduino.

When you start a new project, always load the BareMinimium so you never forget the

required commands.

Now, upload the BareMinimium sketch to test if you can communicate with your

Arduino.

Under the menu “Tools”, double check that your Board setting is set to the Arduino Uno

and your serial port is selected correctly.

Any code inside void

setup() runs only when

the Arduino first turns

on or restarts.

Runs any code inside

void loop() after

running code inside

setup(). After running

all commands inside

void loop(), it will go

back to the first

command inside void

loop() and keep looping

until the Arduino is

turned off or new code

is uploaded to it. That’s

why it is called loop!

Page 26: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

26

Next, we have to select the Serial Port to say how to communicate with our Arduino.

Click the menu Tools -> Port.

On Windows machines, they will be listed as COM1, COM2, COM3, COM4 and etc.

Unfortunately, you have to guess which COM port it is so start with the highest

number. There is no harm in selecting the wrong COM port, so see if it will connect.

You could also disconnect the Arduino and see which COM port disappears and

reappears when you plug it back in.

On macOS, your Arduino Serial Port is usually called “dev/cu.usbmodem####” where the

# could be a random number. Like the Windows serial port if there is more than one,

there is no harm in guessing which “dev/cu.usbmodem####” it could be. In the example

above, it would be “/dev/cu.usbmodem1413” for the Arduino.

Now we can click the upload button to see if the sketch can be

uploaded to the Arduino.

Page 27: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

27

If all goes well, you should

see no errors on the bottom

debug screen. It should also

say “Done uploading”.

As you can see on the

example screenshot on the

left, an error has occurred.

The Arduino software gave

an “Problem uploading to

board” which means it was

unable to properly talk with

the Arduino.

In the debug information

you can see it also tells us

“avrdude: ser_open(): can't

open device "\\.\COM1": The

system cannot find the file

specified.”. This is telling us

our COM port was incorrect or the Arduino is not plugged in correctly. If you have any

issues call a TPL Digital Innovation Hub for assistance, come to one of our Arduino

courses or talk to staff directly at a Digital Innovation Hub.

Page 28: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

28

Blink my Internal Light: Project 1 Time Required: 20 minutes

Now that we have a basic understanding of what an Arduino is and how to get started,

let us program something very simple. This project is the one most people like to start

with, as it is simple and easy to understand. If you have any trouble getting started or

wish to have more assistance, we highly recommend you come to one of the Toronto

Public Library’s introductory Arduino courses.

For this project, we do not require you to wire anything as we will be using the built-in

LED.

Built-in LED

for this project.

Page 29: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

29

Coding the project

Time to code the project and enable the built-in LED to blink. In order to call on the

internal LED we will use the name LED_BUILTIN, which will automatically use the

correct Arduino pin on the board that controls that light. Below is the completed code we

will be typing in.

Line 1. void setup() { Line 2. pinMode(LED_BUILTIN, OUTPUT); Line 3. } Line 4. Line 5. void loop() { Line 6. digitalWrite(LED_BUILTIN, HIGH); Line 7. delay(1000); Line 8. digitalWrite(LED_BUILTIN, LOW); Line 9. delay(1000); Line 10. }

Inside our void setup() function we have a command on Line 2 called pinMode which

tells the Arduino if it is an input or output. Inside the brackets of the function pinMode

we pass two parameters. The first tells pinMode what pin to select (in our case

LED_BUILTIN), and the second tells the Arduino if it is an INPUT or an OUTPUT (we

have ours set as an output).

Coding Accuracy

Be careful typing in correctly. Forgetting to put in a semi-colon or

writing digtalwrite instead of digitalWrite is a big deal. Accuracy is

important making sure your code will run on the Arduino. Talk to

Digital Innovation Hub staff for assistance if you have any questions.

void setup() { pinMode(LED_BUILTIN, OUTPUT); } void loop() { }

Page 30: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

30

The Arduino will now process the command “pinMode(LED_BUILTIN, OUTPUT)”

whenever the device turns on and reboots only, as this command is in the setup()

function. This is only needed to run one time because our LED will never change from an

OUTPUT to an INPUT, so running the command again is not needed.

Inside our loop() function is where we will place the remainder of our code. Remember

any command you put inside the loop() function will repeat the command forever until

you turn off the device or reprogram it.

Line 6 uses the command digitalWrite(LED_BUILTIN, HIGH) which is used to tell the

built-in LED to turn on. Just like pinMode, digitalWrite also needs two parameters to be

passed to it. First parameter tells what Arduino pin to control, the second parameter says

what to do with the pin, if it is either on (HIGH) or off (LOW). We want to turn it on, so

we will use HIGH.

void setup() { pinMode(LED_BUILTIN, OUTPUT); } void loop() { digitalWrite(LED_BUILTIN, HIGH); }

Now upload this program to the Arduino, to see if our LED will turn on. If the LED

doesn’t turn on, double check that the program uploaded correctly and your code is

accurate. If it continues to not light up your LED and everything is connected accurately,

then your computer might be having issues uploading to your Arduino. Please refer to

the troubleshooting guide.

As you can see, the LED remains on when we upload or power our Arduino on with our

code. It is essentially asking the built-in LED to turn on a hundred or more times a second.

That is not very fun so we need to make our LED blink. We first need to tell the Arduino

to wait one second after turning on the LED, before we turn it off. To do this we will add

the command delay() on Line 7.

Page 31: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

31

The delay command tells the brains of our Arduino to stop and wait. We pass one

parameter with the command delay to say how many microseconds to pause. In our case

we used delay(1000) which will equal to a one second pause.

Next on Line 8 we add our digitalWrite command for the built-in LED, but this time

asking it to turn off (notice we are using LOW instead of HIGH). Adding the command

digitalWrite(LED_BUILTIN, LOW) will turn off our LED light.

Our last piece of code on line 9, is to pause the Arduino again, so it will turn off the LED

and wait one second.

Line 1. void setup() { Line 2. pinMode(LED_BUILTIN, OUTPUT); Line 3. } Line 4. Line 5. void loop() { Line 6. digitalWrite(LED_BUILTIN, HIGH); Line 7. delay(1000); Line 8. digitalWrite(LED_BUILTIN, LOW); Line 9. delay(1000); Line 10. }

Upload and we should now have a beacon light that turns on for one second and then

turns off for one second.

Page 32: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

32

Blink an External LED: Project 2 Time Required: 30 – 40 minutes

Use the LED included in the kit and make it blink. The code will be very similar to Project

1, but with some modifications.

Required Components

1x Arduino 1x Breadboard 2x Jumper

Wires

1x 220ohm

Resistors (red,

red, brown,

gold)

1x LED

Our goal is to tell our Arduino to blink the external LED forever (when powered on and

until we reprogram our Arduino). The LED light will turn on, wait for one second, turn

off the LED, wait for one second and repeat.

Let us start by connecting our components together on the breadboard and to the

Arduino. You should only power up your Arduino when you are confident it is

connected correctly.

Page 33: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

33

Wiring the project

Always make sure your Arduino is disconnected from any power source while

you work on it. This should always be your first step for every project, even

when the instructions do not tell you to do so.

Step 1

Connect the following as shown by starting with the LED (Light Emitting Diode).

Connect the short leg of the LED into the breadboard hole 1E and the longer leg of the

LED into 2E.

Page 34: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

34

Step 2

We need to connect our resistor into the breadboard. Unlike the LED which is a type of

diode, the resistor does not have a positive or negative leg.

Connect one leg of the resistor to the breadboard in the hole 2D and the other leg into 6D

Step 3

All that is left is connecting two jumper wires from the Arduino to the breadboard.

Connect a jumper wire from any of the available GND headers to the breadboard 1A (this

will connect the ground from the Arduino to the negative leg of the LED)

Page 35: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

35

Step 4

Connect a jumper wire from pin 9 of the Arduino to the breadboard hole 6A. This will

connect pin 9 of our Arduino to the resistor which then connects to the LED’s positive leg

to complete the circuit. We can control pin 9 of the Arduino by telling it to blink on and off.

Coding the project

Time to code the project and enable the external LED on the Arduino pin 9 to blink. Very

similar to blinking the internal LED, but now we replaced LED_BUILTIN with our

Arduino pin connected to our LED which is pin #9.

Line 1. void setup() { Line 2. pinMode(9, OUTPUT); Line 3. } Line 4. Line 5. void loop() { Line 6. digitalWrite(9, HIGH); Line 7. delay(1000); Line 8. digitalWrite(9, LOW); Line 9. delay(1000); Line 10. }

Upload the code to the Arduino to see the project blink the external LED.

Page 36: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

36

Brighter Light: Project 3 Time Required: 20-40 minutes

For this project, we need the same components as Project 2. If you have taken a break or

want to just jump into Project 3 please refer to Project 2 for wiring instructions, as the

code will be the only thing different in this project.

Coding

Take a quick review of the final code for a fade light. Instead of digitalWrite (used to turn

things on or off) we will be using analogWrite which allows us to control how bright the

LED is. You can set the value of analogWrite between 0-255 (0 turns off the pin, 255 turns

on full power, and 130 is about half the maximum brightness or speed of the motor).

Upload the code to the Arduino to see the LED fade in.

Line 1. int ledPin = 9; Line 2. Line 3. void setup() { Line 4. pinMode(ledPin, OUTPUT); Line 5. } Line 6. Line 7. void loop() { Line 8. analogWrite(ledPin, 0); Line 9. delay(300); Line 10. analogWrite(ledPin, 90); Line 11. delay(300); Line 12. analogWrite(ledPin, 130); Line 13. delay(300); Line 14. analogWrite(ledPin, 190); Line 15. delay(300); Line 16. analogWrite(ledPin, 255); Line 17. delay(300); Line 18. }

Create an integer variable called

ledPin which will hold the value 9.

Setup ledPin (Arduino pin #9) as

an output.

Output as an analog output to

ledPin (Pin #9). Set value to 0

which means turn the LED off.

Output as an analog output to

ledPin (Pin #9). Set value to 255

which mean maximum brightness.

Set the output of ledPin as 130.

That is 130 out of 255, which

would make it half the maximum

brightness.

Use delay to pause the code for

300 milliseconds so we can see

the different brightness.

Page 37: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

37

Traffic Light Simulator: Project 4 Time Required: 45 minutes

We only have had one LED connected at any time, so it is time to expand and make a

simple traffic light.

Required Components

1x Arduino 1x Breadboard 7x Jumper

Wires

3x 220ohm

Resistors

(red, red,

brown, gold)

3x LED (1x

Green LED, 1x

Yellow LED, 1x

Red LED)

Wiring the project

Page 38: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

38

Step 1

Connect your red LED shorter pin (cathode) into the breadboard hole E1 and

the longer LED pin (anode) into the breadboard pin E2.

Step 2

Connect one pin of the 220ohm resistor into the breadboard hole C1 and the

other side of the resistor into the breadboard hole C5.

Step 3

Connect your yellow LED short leg (cathode) into the breadboard hole E7 and

the long leg (anode) into the breadboard hole E8.

Page 39: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

39

Step 4

Connect another 220ohm resistor into the breadboard hole C7 and C11.

Step 5

Connect your green LED short leg (cathode) into the breadboard hole E13 and

the long leg (anode) into the breadboard hole E14.

Page 40: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

40

Step 6

Connect another 220ohm resistor into the breadboard hole C13 and C17.

Step 7

Connect a jumper wire from the breadboard hole A17 to any of the available

breadboard left negative power rail holes.

Page 41: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

41

Step 8

Connect a jumper wire from the breadboard hole A11 to any of the available

breadboard left negative power rail holes.

Step 9

Connect a jumper wire from the breadboard hole A5 to any of the available

breadboard left negative power rail holes.

Page 42: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

42

Step 10

Connect a jumper wire from the any of the Arduino GND pins to any of the

available breadboard left negative power rail holes.

Step 11

We now have all the lights grounded (connected by the negative power rail), so we now

need to send power to them. We will be using three digital output pins to control the

three lights.

Connect a jumper wire from the Arduino pin #2 to the breadboard hole A14

(this will power our green LED).

Page 43: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

43

Step 12

Connect a jumper wire from the Arduino pin #3 to the breadboard hole A8

(this will power our yellow LED).

Step 13

Connect a jumper wire from the Arduino pin #4 to the breadboard hole A2

(this will power our red LED).

Page 44: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

44

Coding

Now that everything is wired up, we can start to work on the code. We want our red

LED to turn on with the yellow and green off. Upload the code to the Arduino.

Line 1. int greenLED = 2; Line 2. int yellowLED = 3; Line 3. int redLED = 4; Line 4. Line 5. void setup() { Line 6. pinMode(greenLED, OUTPUT); Line 7. pinMode(yellowLED, OUTPUT); Line 8. pinMode(redLED, OUTPUT); Line 9. } Line 10. Line 11. void loop() { Line 12. digitalWrite(greenLED, HIGH); Line 13. digitalWrite(yellowLED, LOW); Line 14. digitalWrite(redLED, LOW); Line 15. delay(8000); Line 16. Line 17. digitalWrite(greenLED, LOW); Line 18. digitalWrite(yellowLED, HIGH); Line 19. digitalWrite(redLED, LOW); Line 20. delay(2000); Line 21. Line 22. digitalWrite(greenLED, LOW); Line 23. digitalWrite(yellowLED, LOW); Line 24. digitalWrite(redLED, HIGH); Line 25. delay(8000); Line 26. }

Set variables to store the

pin numbers of the green,

yellow and red LEDs.

Set the pins (2,3,4) used for

our LEDs as OUTPUTs

when the Arduino boots or

restarts since inside setup().

First the Arduino will turn

on the green LED, turn off

the yellow and red LED,

and pause for 8 seconds.

Then, turn off green and

red LED, turn on the

yellow LED, and pause.

e for 2

seconds.

Turn off the green and

yellow LED, turn on the red

LED, pause for 8 seconds.

Since no other commands

are here, repeat at the start

of the loop() function.

Page 45: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

45

Buttons: Project 5 Time Required: 30 minutes

So far, we have only created projects that use outputs but no inputs. The most basic way

to get user input is by using a pushbutton. We can have our Arduino read the state of a

pushbutton, which will be used to tell the internal LED of the Arduino to turn on when

it is pressed.

Required Components

1x Arduino 1x Breadboard 3x Jumper

Wires

1x 10kohm

Resistors

(brown, black,

orange, gold)

1x Pushbutton

Wiring the project

Step 1

Your pushbutton has

four legs that need to fit

into the breadboard.

Insert the button as

shown with the legs in

the breadboard holes

E1, E3, F1, F3.

Page 46: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

46

Step 2

Step 3

Insert your 10kohm

resistor into the

breadboard hole G3

and the other side

into G7.

Connect a jumper wire

from the Arduino’s 5v

pin to the breadboard

hole J1.

(The 5v pin of the

Arduino continuously

sends 5v power out. You

cannot turn it off or on

by code. Used for

powering external IC

chips, buttons, and

sensors.)

Page 47: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

47

Step 4

Step 5

Connect a jumper wire

from the Arduino’s GND

pin to the breadboard hole

J7.

Connect a jumper

wire from the

Arduino’s A0 pin to

the breadboard hole

J3.

(This will be an input

to detect the button

being pressed. It will

read that power is

flowing from 5V to

GND when it is

pressed.

We have a 10kohm

resistor so we don’t

short out the 5V to

GND connection.)

Page 48: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

48

Coding

Line 1. int buttonPin = A0;

Line 2. int buttonState;

Line 3.

Line 4. void setup() {

Line 5. pinMode(LED_BUILTIN, OUTPUT);

Line 6. pinMode(buttonPin, INPUT);

Line 7. }

Line 8.

Line 9. void loop() {

Line 10. buttonState = digitalRead(buttonPin);

Line 11. if(buttonState == HIGH) {

Line 12. digitalWrite(LED_BUILTIN, HIGH);

Line 13. } else {

Line 14. digitalWrite(LED_BUILTIN, LOW);

Line 15. }

Line 16. }

Upload the code to the Arduino and try the button.

Create an integer variable called buttonPin to store

the value A0 (the pin our button is connected to).

Create an integer variable called buttonState. This

will be used to store if the button is HIGH or LOW

when we check using digitalRead() function.

Set the built-in LED on the Arduino

called LED_BUILTIN as an output.

Set the button on the Arduino called

buttonPin (Pin A0) as an input.

Use digitalRead to see if

buttonPin (Pin A0) is pressed

or not and store if it is HIGH

or LOW in our variable

buttonState.

Check our variable buttonState

if the button is pressed. If it is,

turn on the built-in LED

(LED_BUILTIN).

If the button is not pressed

then turn off the LED

(LED_BUILTIN).

Page 49: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

49

Tone Creator: Project 6 Time Required: < 25 minutes

Required Components

1x Arduino 1x Piezo Speaker

Wiring the project

Step 1

Connect the Piezo speaker pins

directly to the GND pin and PIN

10 of the Arduino. (It does not

matter what pins go to GND or

pin 10. There is no positive or

negative on the Piezo included)

Be careful! The pins are

delicate and might bend if you

push too hard.

Page 50: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

50

Coding

Line 1. int SpeakerPin = 10; Line 2. Line 3. void setup() { Line 4. pinMode(SpeakerPin, OUTPUT); Line 5. } Line 6. Line 7. void loop() { Line 8. tone(SpeakerPin, 300); Line 9. delay(1000); Line 10. Line 11. tone(SpeakerPin, 700); Line 12. delay(1000); Line 13. Line 14. tone(SpeakerPin, 999); Line 15. delay(1000); Line 16. Line 17. noTone(SpeakerPin); Line 18. delay(1000); Line 19. }

Upload the code to the Arduino to hear the tones.

Setup SpeakerPin (Arduino pin

#10) as an output.

Create an integer variable called

SpeakerPin which will hold the

number 10.

Create a tone to our SpeakerPin, at

a tone of 300hz. Then wait for one

second.

Create a tone to our SpeakerPin, at

a tone of 700hz. Then wait for one

second.

Create a tone to our SpeakerPin, at

a tone of 999hz. Then wait for one

second.

Stop all tones for SpeakerPin (pin

#9) and wait for one second.

Page 51: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

51

My First Instrument: Project 7 Time Required: 45 minutes

In this project, we will combine our knowledge of switches and using the piezo speaker

to create a super basic instrument. We will assign a different tone() function to the

speaker whenever one of the switches is pressed.

Required Components

1x Arduino 1x

Breadboard

11x Jumper

Wires 3x Pushbuttons

3x 10kohm

Resistors

(brown, black,

orange, gold)

1x Piezo

Speaker

Page 52: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

52

Wiring the project

Step 1

Connect a jumper wire from

the Arduino’s 5v pin to one

of the breadboard right

positive power rail holes.

Page 53: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

53

Step 2

Step 3

Step 4

Connect a jumper wire from

the Arduino’s GND pin to

one of the breadboards right

negative power rail holes.

Your pushbutton has

four legs that need to fit

into the breadboard.

Insert the button as

shown with the legs in

the breadboard holes E1,

E3, F1, F3.

Connect a jumper wire from

an available breadboard

positive rail hole to the

breadboards hole J1.

Page 54: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

54

Step 5

Step 6

Insert your 10kohm

resistor into the

breadboard hole H3 and

the other side into H7.

Connect a jumper wire

from an available hole on

the breadboard right

negative power rail to the

breadboard J7 hole.

Page 55: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

55

Step 7

Step 8

Your pushbutton has

four legs that need to fit

into the breadboard.

Insert the button as

shown with the legs in

the breadboard holes E8,

E10, F8, F10.

Connect a jumper wire from

the Arduino’s A1 (analog in)

pin to the breadboard hole J3.

(This will be an input to

detect the button being

pressed. It will read that

power is flowing from 5V to

GND when it is pressed.

We have a 10kohm resistor so

it won’t short out the 5V to

GND connection.)

Page 56: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

56

Step 9

Step 10

Connect a jumper wire from

an available breadboard

positive rail hole to the

breadboard hole J8.

Insert your 10kohm resistor into

the breadboard hole H10 and the

other side into H14.

Page 57: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

57

Step 11

Step 12

Connect a jumper wire

from an available hole on

the breadboard right

negative power rail to the

breadboard J14 hole.

Connect a jumper wire from

the Arduino’s A2 (analog in)

pin to the breadboard hole

J10.

(This will be an input to

detect the button being

pressed. It will read that

power is flowing from 5V to

GND when it is pressed.

We have a 10kohm resistor

so it won’t short out the 5V

to GND connection.)

Page 58: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

58

Step 13

Step 14

Your pushbutton has

four legs that need to fit

into the breadboard.

Insert the button as

shown with the legs in

the breadboard holes

E15, E17, F15, F17.

Connect a jumper wire from

an available breadboard

positive rail hole to the

breadboards hole J15.

Page 59: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

59

Step 15

Step 16

Insert your 10kohm resistor into

the breadboard hole H17 and the

other side into H21.

Connect a jumper wire

from an available hole on

the breadboards right

negative power rail to the

breadboards J21 hole.

Page 60: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

60

Step 17

Connect a jumper wire from the Arduino’s A3 (analog in) pin to the breadboard

hole J17.

(This will be an input to detect the button being pressed. It will read that power is

flowing from 5V to GND when it is pressed.

We have a 10kohm resistor so don’t short out the 5V to GND connection.)

Page 61: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

61

Step 18

Upload the code to the Arduino and try pressing each button for a different tone.

Connect the Piezo speaker pins

directly to the GND pin and PIN

10 of the Arduino. (It does not

matter what pins go to GND or

pin 10. There is no positive or

negative on the Piezo included).

Be careful! The pins are

delicate and might bend if you

push too hard or misalign it

when inserting into the

Arduino.

Page 62: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

62

Coding

Line 1. int lowNotePin = A3;

Line 2. int medNotePin = A2;

Line 3. int highNotePin = A1;

Line 4.

Line 5. int highNote = 0;

Line 6. int medNote = 0;

Line 7. int lowNote = 0;

Line 8.

Line 9. int SpeakerPin = 10;

Line 10.

Line 11. void setup() {

Line 12. pinMode(lowNotePin, INPUT);

Line 13. pinMode(medNotePin, INPUT);

Line 14. pinMode(highNotePin, INPUT);

Line 15.

Line 16. pinMode(SpeakerPin, OUTPUT);

Line 17. }

Line 18.

Line 19. void loop() {

Line 20. highNote = digitalRead(highNotePin);

Line 21. medNote = digitalRead(medNotePin);

Line 22. lowNote = digitalRead(lowNotePin);

Line 23.

Line 24. if(lowNote == HIGH) {

Line 25. tone(SpeakerPin, 300);

Line 26. }

Line 27.

Line 28. else if(medNote == HIGH) {

Line 29. tone(SpeakerPin, 700);

Line 30. }

Line 31.

Line 32. else if(highNote == HIGH) {

Line 33. tone(SpeakerPin, 900);

Line 34. }

Line 35.

Line 36. else { noTone(SpeakerPin); }

Line 37. }

Create three integer variables to store

our analog input pins. (We will have

inputs for a low, medium and high

note the user can push.)

Create three integer variables that

will hold the state of our buttons. (0

means not pushed, 1 means pushed).

Create an integer variable called

SpeakerPin to store what Arduino

pin we will be using (pin #10).

Set our lowNotePin (A3),

medNotePin (A2) and highNotePin

(A3) as inputs.

Set SpeakerPin (#9) as an OUTPUT.

Store the states of our

buttons in our variables

called highNote, medNote

and lowNote.

If lowNote button is pressed

(HIGH) then make a tone to

SpeakerPin (#9) at 300 hz.

If lowNote was not pressed,

check if medNote is (HIGH).

If so make a tone to

SpeakerPin (#9) at 700hz.

If lowNote and medNote were

not pressed, check if highNote

is (HIGH). If so make a tone to

SpeakerPin (#9) at 900hz.

Else if no other button is pressed,

tell SpeakerPin (#9) to stop all

tones.

Page 63: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

63

Recommended Resources Get these for free at the Toronto Public Library

Want to learn more about Arduinos? Here is a list of our favourite Toronto Public

Library books and resources. When using the Arduino Kit, please stick to the projects

outlined in this manual. Additional projects found in the recommended resources are

for educational and entertainment purposes and are only intended for use with your

personal Arduino.

http://www.Arduino.cc

The official website has great tutorials and

reference resources. The website includes

information on all commands you can do for the

Arduino programming language and examples on

how to use them.

Learning Arduino with Peggy Fisher

This beginner course consists of two hours of

video and can be accessed for free from

Lynda.com (via tpl.ca/elearning with a valid Toronto

Public Library card).

Page 64: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

64

Adventures in Arduino by Becky Stewart

This book provides simple, easy-to-follow

introductions to the Arduino. It is written for 11 to

15 year olds, but we’ve found the concepts,

content, and language engaging and applicable to

adult Arduino users. Available from Safari (via

tpl.ca/elearning with a valid Toronto Public Library

card).

Arduino for Kids (2017) by Priya Kuber, Rishi Gaurav Bhatnagar, Vijay Varada

This book is intended for children (ages 9 and up)

and their parents. It includes a series of fun, easy

projects that don’t require any knowledge of

electronics. Available from Safari (via tpl.ca/elearning

with a valid Toronto Public Library card).

The Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi (2016) by Simon Monk

No one knows what the future holds, so we can’t

definitively say whether or not the Arduino

projects in this book will come in handy. What we

can guarantee is that you’ll have fun learning

about Arduinos in a unique and creative way.

Available in regular print.

Page 65: for Absolute Beginners - Toronto Public Library€¦ · Do not use lithium ion batteries, they may explode when shorted Do not use on metallic surfaces, such as your Macbook. Place

65

Make: Drones: Teach an Arduino to Fly by David McGriffy

Have you ever wondered how drones work? This

book reveals drone building secrets and explains

how you can get your Arduino to fly. Available in

regular print and as an eBook from Safari (via

tpl.ca/elearning with a valid Toronto Public Library

card).

The Arduino Inventor's Guide (2017) by Derek Runberg and Brian Huang

Ready to move on from the Arduino kits and start

working on some more advanced projects? Why

not build a tiny electric piano, a desktop

greenhouse, or a colour-mixing night light? You’ll

find ten fun Arduino projects in this new eBook,

available from Safari (via tpl.ca/elearning with a valid

Toronto Public Library card)

Arduino Playground : Geeky Projects for the Experienced Maker (2017) by Warren Andrews

This is the perfect resource for more advanced

Arduino projects. One of our favourites is the

Garage Sentry Parking Assistant, a project that can

help you pull into your garage by setting off an

alarm when you’ve gone far enough and need to

hit the brakes. Available in regular print and as an

eBook from Safari (via tpl.ca/elearning with a valid

Toronto Public Library card)