Arduino Intermediate Projects · If you lose an Arduino Kit, you will be charged the purchase price...

37
1 Arduino Intermediate Projects Created as a companion manual to the Toronto Public Library Arduino Kits. Arduino Intermediate Projects Copyright © 2018 Toronto Public Library. All rights reserved. Published by the Toronto Public Library.

Transcript of Arduino Intermediate Projects · If you lose an Arduino Kit, you will be charged the purchase price...

Page 1: Arduino Intermediate Projects · 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

1

Arduino Intermediate

Projects

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

Arduino Intermediate Projects

Copyright © 2018 Toronto Public Library. All rights reserved.

Published by the Toronto Public Library.

Page 2: Arduino Intermediate Projects · 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

2

Table of Contents

TABLE OF CONTENTS ....................................................................................................................... 2

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

INTRODUCTION ................................................................................................................................ 4

SAFETY TIPS .................................................................................................................................... 12

ARDUINO UNO TOUR ..................................................................................................................... 14

GETTING TO KNOW YOUR BREADBOARD ....................................................................................... 15

USING SERIAL DEBUG AND A POTENTIOMETER ............................................................................. 17

USING AN RGB LED ........................................................................................................................ 21

CONTROL COLOURS WITH A POTENTIOMETER .............................................................................. 24

HOTTER OR COLDER THAN AVERAGE ............................................................................................. 29

RECOMMENDED RESOURCES ........................................................................................................ 34

Page 3: Arduino Intermediate Projects · 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

3

Preface

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: Arduino Intermediate Projects · 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

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: Arduino Intermediate Projects · 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

5

Introduction

An Arduino is a microcontroller; a small, simple computer. 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

You assembled kit includes all the parts you’ll need for the Arduino projects outlined in

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

proper slots, as indicated in this diagram, 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: Arduino Intermediate Projects · 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

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: Arduino Intermediate Projects · 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

7

Jumper wires are used to create electric circuits and can be

inserted into the breadboard.

RGB LED (red, green and blue light-emitting diodes) are

electronic devices that emit light when an electrical current

passes through them. This includes three LED’s in one to

create colours. Using the 4 pins from the RGB LED you can

make it output red, green and blue light and also mix them

together.

Resistors help control the flow of electricity in your circuit by

limiting the current. They come in different values measured

Page 8: Arduino Intermediate Projects · 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

8

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.

Potentiometers are a manually adjustable variable resistor

with 3 terminals. Two terminals are connected to both ends of

a resistive element, and the third middle terminal connects to

a sliding contact, called a wiper, moving over the resistive

element. The position of the wiper determines the output

voltage of the potentiometer.

Page 9: Arduino Intermediate Projects · 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

9

Temperature Sensors work very similar to a potentiometer.

The middle pin can be connected to our analog input pin on

the Arduino to read the resistance value. That value can be

converted to a temperature. The outer pins are used to

connect 5 volts and GND.

Page 10: Arduino Intermediate Projects · 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

10

Part Inventory for the Intermediate 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.

3x 220ohm

Resistors (red, red,

brown, gold)

Resistors are used to

reduce the flow of

electric current in a

circuit. This prevents

damage to LEDs that

require a lower

voltage.

1x Temperature

Sensor

Used to detect the

ambient temperature

of the room when

connected correctly to

the Arduino analog

input pins.

3x Potentiometers

Can be used to detect

the position of a knob

when connected to

the Arduino analog

input pins.

1x RGB LED

LED stands for Light

Emitting Diode & is

used to create light.

These LEDs typically

require around 1 volt.

Page 11: Arduino Intermediate Projects · 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

11

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 12: Arduino Intermediate Projects · 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

12

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 13: Arduino Intermediate Projects · 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

13

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 14: Arduino Intermediate Projects · 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

14

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.

Page 15: Arduino Intermediate Projects · 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

15

Getting to Know your Breadboard Time Required: 20 minutes

Link to 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.

Page 16: Arduino Intermediate Projects · 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

16

On the previous page left there 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 17: Arduino Intermediate Projects · 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

17

Using Serial Debug and a Potentiometer Time Required: 35 minutes

For this project, we will go over the benefits of using our serial monitor and how to use

it in our Arduino software. You are able to use serial monitor to output messages and

values of variables from the Arduino that we are using in our sketch. This allows you to

see how the information is being processed. Serial communication could also be used to

send messages to an Arduino to trigger commands, however we will not be going over

this functionality in this kit.

We also will be using a potentiometer and reading it as a user input for our Arduino. A

potentiometer is a knob that provides a variable resistance, which we can read using the

Arduino board as an analog value. We connect the potentiometer with three wires to the

Arduino board. The first wire goes to ground from one of the outer pins of the

potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer.

The third goes from analog input 2 to the middle pin of the potentiometer.

What are the pins on a typical Potentiometer?

Ground

Output: This pin

can be connected to

an Arduino. The

Arduino will read

the resistance value

from the

potentiometer.

Positive Power

Page 18: Arduino Intermediate Projects · 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

18

By turning the shaft of the potentiometer, we change the amount of resistance on either

side of the wiper which is connected to the center pin of the potentiometer. This changes

the relative "closeness" of that pin to 5 volts and ground, giving us a different analog

input. When the shaft is turned all the way in one direction, there are 0 volts going to the

pin, and we read 0. When the shaft is turned all the way in the other direction, there are

5 volts going to the pin and we read 1023 (this is not exactly what happens, but a

simplified explanation). In between, analogRead() returns a number between 0 and 1023

that is proportional to the amount of voltage being applied to the pin.

Required Components

1x Arduino 1x Breadboard 3x Jumper Wires 1x Potentiometer

Page 19: Arduino Intermediate Projects · 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

19

Wiring Guide

Page 20: Arduino Intermediate Projects · 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

20

Code

Line 1. const int inputPin = A0; Line 2. Line 3. int userInputValue = 0; Line 4. Line 5. void setup() { Line 6. Line 7. pinMode(inputPin, INPUT); Line 8. Line 9. Serial.begin(9600); Line 10. Line 11. } Line 12. Line 13. void loop() { Line 14. Line 15. userInputValue = analogRead(inputPin); Line 16. Line 17. Serial.print("User input: "); Line 18. Line 19. Serial.println(userInputValue); Line 20. Line 21. delay(1000); Line 22. Line 23. }

Create a variable where it’s value will never change (const) called

inputPin that will store an integer (A0).

Create a variable where it’s value will change called

userInputValue that will store the value of the potentiometer.

Initially set the value to 0.

Set our inputPin (A0) as an input when the Arduino first turns

on or reboots.

Start Serial communication. Set the baud speed to 9600 (how fast

the communication is between the computer and the Arduino).

Analog read the inputPin (A0), and store the

value in our variable called userInputValue.

In the serial monitor, output the text “User input: “.

In the serial monitor, output the value in our

userInputValue variable. Since we are using

Serial.println, the ln means to create a line break

after the text is outputted.

Pause for a second. This pause is only so Arduino won’t send our messages to the serial monitor too

quickly so we can read them easier.

Page 21: Arduino Intermediate Projects · 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

21

Using an RGB LED Time Required: 45 minutes

For this project, we will be using an RGB led to create many different colours. An RGB

led is nothing more than three led’s in one package. For this example we will create a

project that will turn on each LED colour on for one second. The project will make the

RGB LED glow green for one second, then to blue for one second, and then glow red for

one second and repeat forever.

1x Arduino 1x Breadboard 4x Jumper

Wires

3x 220ohm

Resistors

(red, red,

brown, gold)

1x RGB LED

Page 22: Arduino Intermediate Projects · 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

22

How to use an RGB LED?

Our RGB LED is just three LED’s in one

device. The one included in the kit is a

common cathode type. This means one of the

LED legs (which is the longest wire coming

from the RGB led) is the negative (cathode)

and is shared for the red, green and blue led’s

inside.

The other 3 legs are positive (anode) that we

will send individual power from three of our analog output pins, letting us full

control of the colours that should be displayed.

Wiring Guide

Connect the RGB LED with the longest

leg (longest leg is the negative cathode)

into the breadboard pin #G2.

Page 23: Arduino Intermediate Projects · 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

23

Code

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

Create variables where its value will never change (const) to

store the value of what pins we will be using and call them

ledGreen (pin 9), ledBlue (pin 10) and ledRed (pin 11).

Set ledGreen (pin 9), ledBlue (pin 10) and ledRed (pin 11) as

an OUTPUT.

Set ledGreen (pin 9) as HIGH (turn on), set ledBlue (pin 10) as

LOW (turn off) and set ledRed (pin 11) as LOW (turn off).

Pause for one second.

Set ledGreen (pin 9) as LOW (turn off), set ledBlue (pin 10) as

HIGH (turn on) and set ledRed (pin 11) as LOW (turn off).

Pause for one second.

Set ledGreen (pin 9) as LOW (turn off), set ledBlue (pin 10) as

LOW (turn off) and set ledRed (pin 11) as HIGH (turn on).

Pause for one second.

Page 24: Arduino Intermediate Projects · 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

24

Control Colours with a Potentiometer Time Required: 45 minutes

For this project, we will be using an RGB led to create many different colours. We will be

using three potentiometers’ so we can control how intense we want to show red, green

and blue from the single RGB LED. By changing how bright and intense each colour is

from the LED, we can create thousands of colours.

1x Arduino 1x

Breadboard

16x Jumper

Wires

3x 220ohm

Resistors

(red, red,

brown, gold)

1x RGB LED 3x

Potentiometers

Page 25: Arduino Intermediate Projects · 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

25

Wiring Guide Connect the RGB LED with the longest

leg (longest leg is the negative cathode)

into the breadboard pin #J2.

Page 26: Arduino Intermediate Projects · 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

26

Code

Line 1. const int ledRedPin = 11; Line 2. const int ledGreenPin = 10; Line 3. const int ledBluePin = 9; Line 4. Line 5. const int sensorRedPin = A2; Line 6. const int sensorGreenPin = A1; Line 7. const int sensorBluePin = A0; Line 8. Line 9. int sensorRedValue = 0; Line 10. int sensorGreenValue = 0; Line 11. int sensorBlueValue = 0; Line 12. Line 13. int outputRed = 0; Line 14. int outputGreen = 0; Line 15. int outputBlue = 0; Line 16. Line 17. void setup() { Line 18. pinMode(ledRedPin, OUTPUT); Line 19. pinMode(ledGreenPin, OUTPUT); Line 20. pinMode(ledBluePin, OUTPUT); Line 21. Line 22. pinMode(sensorRedPin, INPUT); Line 23. pinMode(sensorGreenPin, INPUT); Line 24. pinMode(sensorBluePin, INPUT); Line 25. Line 26. Serial.begin(9600); Line 27. }

code continues

next page

Create 3 variables where its value will never change (const) that is an

integer to store the value of what pins we will be using and call them

ledRedPin (pin 11), ledGreenPin (pin 10) and ledBluePin (pin 9).

Create 3 variables where its value will never change (const) to store

the value of what pins we will be using and call them sensorRedPin

(pin A2), sensorGreenPin (pin A1) and sensorBluePin (pin A0).

Create 3 integer variables where its values will change that will be

called sensorRedValue, sensorGreenValue, sensorBlueValue. Initially

it will only store 0. These variables will be used to store the raw

values from our potentiometer.

Create 3 integer variables where its values will change that will be

called outputRed, outputGreen, outputBlue. Initially it will only store

0. This variable will be used to store how bright each colour will be

for our RGB LED.

Set ledRedPin (pin #11), ledGreenPin (pin #10), ledBluePin (pin #9) as

an OUTPUT.

Set sensorRedPin (analog input pin A2), sensorGreenPin (analog input

pin A1), sensorBluePin (analog input pin A0) as an INPUT.

Start serial communication from the Arduino to the computer. Set the

communication speed to 9600 baud.

Page 27: Arduino Intermediate Projects · 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

27

Line 28. void loop () {

Line 29. sensorRedValue = analogRead(sensorRedPin);

Line 30. sensorGreenValue = analogRead(sensorGreenPin);

Line 31. sensorBlueValue = analogRead(sensorBluePin);

Line 32.

Line 33. outputRed = map(sensorRedValue, 0, 1023, 0, 255);

Line 34.

Line 35. outputGreen = map(sensorGreenValue, 0, 1023, 0, 255);

Line 36.

Line 37. outputBlue = map(sensorBlueValue, 0, 1023, 0, 255);

Line 38.

Line 39. analogWrite(ledRedPin, outputRed);

Line 40. analogWrite(ledGreenPin, outputGreen);

Line 41. analogWrite(ledBluePin, outputBlue);

Line 42.

Line 43. Serial.print("Output - R:"); Serial.print(outputRed);

Line 44. Serial.print(" G:"); Serial.print(outputGreen);

Line 45. Serial.print(" B:"); Serial.println(outputBlue);

Line 46. }

analogRead each of our sensor pins (A0, A1, A2)

and store each potentiometer’s raw data into our

variables sensorRedValue, sesnsorGreenValue,

sensorBlueValue.

Map the raw sensor data (our sensor gives us a

value between 0-1024) to a value between (0-255)

and store the converted value to outputRed.

Map the raw sensor data (our sensor gives us a

value between 0-1024) to a value between (0-255)

and store the converted value to outputGreen.

Map the raw sensor data (our sensor gives us a

value between 0-1024) to a value between (0-255)

and store the converted value to outputBlue.

analogWrite the values for outputRed,

outputGreen, outputBlue to our corresponding

pins to the RGB LED (ledRedPin [pin 11],

ledRedGreen [pin 10], ledBluePin [pin 9]).

This code will output the values of the variable

outputRed, outputGreen, and outputBlue to the

serial monitor.

This will allow us to see exactly the values the

Arduino is outputting for the brightness of each

colour (red, green, blue) for our RGB LED while

your computer is plugged into the Arduino.

Page 28: Arduino Intermediate Projects · 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

28

What is the map() function?

Re-maps a number from one range to another.

This is perfect for when we have a input values from analogRead() ranges from

0-1024, but our analogWrite() outputs a value from 0-255. We can map it so

when our input is 1024, it will output 255 for us. When the input value is 512, it

could automatically output 128 for us.

Page 29: Arduino Intermediate Projects · 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

29

Hotter or Colder than Average Time Required: 60 minutes

For this project, we will be using an RGB led to show to the user blue when the

temperature is lower than the median, and show the user red when the temperature is

higher than the median. In this project, we will not be using the green portion of our

RGB LED. This will be able to show if it is colder or hotter in the room than usual.

1x Arduino 1x Breadboard 8x Jumper Wires

2x 220ohm

Resistors

(red, red, brown,

gold)

1x RGB LED 1x Temperature

Sensor

Page 30: Arduino Intermediate Projects · 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

30

What are the pins on Temperature Sensor?

Be careful! When using the temperature sensor, make sure you don’t

reverse the positive and negative pins when connecting it to the Arduino.

In this diagram, the front of the temperature sensor is the flat part facing

towards you. Take a note below of the the pins to watch our for.

If the temperature sensor gets hot and does not work, unplug the power to the

Arduino and double check your connections.

Positive

Power

(5 Volts)

GND

(Negative)

Voltage Output (This is the

pin the Arduino will use to

read the temperature)

Flat part of

sensor facing

towards you

Page 31: Arduino Intermediate Projects · 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

31

Wiring Guide

Connect the RGB LED with the longest leg

(longest leg is the negative cathode) into the

breadboard pin #G2

Page 32: Arduino Intermediate Projects · 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

32

Code

Line 1. const int ledBluePin = 10;

Line 2. const int ledRedPin = 11;

Line 3.

Line 4. const int sensorPin = A0;

Line 5. int rawSensorValue = 0;

Line 6. float sensorVoltage = 0.0;

Line 7.

Line 8. float sensorTemperature = 0.0;

Line 9.

Line 10. int outputRed = 0;

Line 11. int outputBlue = 0;

Line 12.

Line 13. float sensorMinValue = 1024.0;

Line 14. float sensorMaxValue = -1024.0;

Line 15. float halfTemp = 0;

Line 16.

Line 17. void setup() {

Line 18. pinMode(ledRedPin, OUTPUT);

Line 19. pinMode(ledBluePin, OUTPUT);

Line 20.

Line 21. pinMode(sensorPin, INPUT);

code continues

next page

Create 2 variables where its value will never change (const) that is an

integer to store the value of what pins we will be using and call them

ledBlue (pin 10) and ledRed (pin 11).

Create a const integer variable to store the value for the analog input

pin (A0) for the temperature sensor.

Create an integer variable to store the raw temperature sensor values.

zzzzzzzzzzzzzzzz Create a float variable to store the converted raw temperature sensor

value to a voltage value.

Create a float variable to store the converted voltage value to a

temperature in Celsius.

Create an integer variable called outputRed and outputBlue to store how

bright the red and blue will be to the RGB led.

Create a float variable called sensorMinValue and sensorMaxValue to

remember the lowest and highest temperatures it knows.

Create a float variable called halfTemp, that will be used to store the

median value of our temperatures that the Arduino remembers.

Set our ledRedPin (Pin #11) and ledBluePin (Pin #10) as an OUTPUT.

Set our sensorPin (Pin A0) as an INPUT.

What is a float variable?

A float variable is used to store numbers with

decimal point unlike an integer, which stores

only whole numbers.

Page 33: Arduino Intermediate Projects · 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

33

Line 22. Serial.begin(9600);

Line 23. }

Line 24. void loop () {

Line 25. rawSensorValue = analogRead(sensorPin);

Line 26.

Line 27. sensorVoltage = (rawSensorValue/1024.0) * 5.0;

Line 28.

Line 29. sensorTemperature = (sensorVoltage - 0.5) * 100;

Line 30.

Line 31. Serial.print("Temperature: ");

Line 32. Serial.println(sensorTemperature);

Line 33.

Line 34. if(sensorTemperature < sensorMinValue) {

Line 35. sensorMinValue = sensorTemperature; }

Line 36.

Line 37. if(sensorTemperature > sensorMaxValue) {

Line 38. sensorMaxValue = sensorTemperature; }

Line 39.

Line 40. halfTemp = (sensorMaxValue - sensorMinValue) / 2;

Line 41. halfTemp = sensorMinValue + halfTemp;

Start serial communication at the baud speed of 9600.

Read the temperature sensor and save its values into the variable

.rawSensorValue. Convert the raw sensor values to how many volts is being read

from the analogRead() (the value was stored previously in

rawSensorValue).

Convert the voltage from the temperature sensor into a

temperature in Celsius.

code continues

next page

Output debug information to the serial monitor. This will output the user

“Temperature: “ and then the temperature of the sensor in Celsius.

Check if the current temperature is lower than the lowest recorded

temperature. If it is, then store the current temperature into the variable

sensorMinValue which holds the lowest temperature the Arduino

remembers.

Check if the current temperature is higher than the highest recorded

temperature. If it is, then store the current temperature into the variable

sensorMaxValue which holds the highest temperature the Arduino

remembers.

Calculate the median value of our temperature by getting the

value of sensorMaxValue minus the value of sensorMinValue

and then dividing the result by two.

After, use the previous value that was calculated and add it to

the value of sensorMinValue, which gives us our median

value between the sensorMaxValue and sensorMinValue.

Page 34: Arduino Intermediate Projects · 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

34

Line 42. outputBlue = map(sensorTemperature, sensorMinValue, halfTemp + 1, 255, 0);

Line 43.

Line 44. outputRed = map(sensorTemperature, halfTemp - 1, sensorMaxValue, 1, 255);

Line 45.

Line 46. outputRed = constrain(outputRed, 0, 255);

Line 47. outputBlue = constrain(outputBlue, 0, 255);

Line 48.

Line 49. analogWrite(ledRedPin, outputRed);

Line 50. analogWrite(ledBluePin, outputBlue);

Line 51.

Line 52. Serial.print("Half Temp: "); Serial.println(halfTemp);

Line 53.

Line 54. Serial.print("Min Temp: "); Serial.println(sensorMinValue);

Line 55. Serial.print("Max Temp: "); Serial.println(sensorMaxValue);

Line 56.

Line 57. Serial.print("Blue Output: "); Serial.println(outputBlue);

Line 58. Serial.print("Red Output: "); Serial.println(outputRed);

Line 59.

Line 60. delay(600);

Map how bright the blue colour should be in the LED. The

blue is brightest when the current temperature is closest to

the lowest temperature the Arduino remembers. The

“halfTemp + 1” has the “+ 1” so the blue light will still be on

dimly when the current temperature is the same as the

median temperature the Arduino remembers.

Map how bright the red colour should be in the LED. The

blue is brightest when the current temperature is closest to

the lowest temperature the Arduino remembers. The

“halfTemp + 1” has the “+ 1” so the blue light will still be on

dimly when the current temperature is the same as the

median temperature the Arduino remembers.

Make sure the integer in outputRed and

outputBlue only have a value between 0-255.

Use analogWrite() to output to the red and blue part of

our RGB LED using the values from outputRed and

outputBlue that we already calculated.

Print to the serial monitor the median

temperature the Arduino remembers

currently for debugging.

Print to the serial monitor the

lowest and highest temperature

the Arduino remembers.

Print to the serial monitor the

current brightness of our red

and blue values to the RGB

LED. Create a delay so that we can see the serial monitor text.

Without the delay the Arduino will talk to the computer

too fast for us to read.

Page 35: Arduino Intermediate Projects · 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

35

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 36: Arduino Intermediate Projects · 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

36

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 37: Arduino Intermediate Projects · 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

37

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)