Download (10) Upload the code to the Arduino board

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Fault tolerance wikipedia , lookup

Resistive opto-isolator wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Surface-mount technology wikipedia , lookup

Opto-isolator wikipedia , lookup

Transcript
Lesson Plan
Arduino:
Night Light
A student with limited knowledge of using Arduino hardware learns how to create a night light.
Learning Objectives
At the end of the class, the student will:


Understand what a circuit is.
Know how to connect components to an Arduino board.
Materials
 49 jumper cables
 14 200 ohm resistors (or have a variety if you can)
 21 LEDs of any color
 7 Arduino Unos
 7 Breadboards
 7 Chromebooks
 1 buzzer
Estimated Budget: $0 when borrowed from the CTC
Room Setup



Push several tables together with chairs around them.
Charge and log into 7 Chromebooks.
Setup each station with:
o 5 jumper cables
o 2 LEDs
o 1 200 ohm resistor
o 1 Arduino Uno
o 1 Breadboard
o 1 Chromebook
Program Outline
The program is completed in one (120) minute session.
(5) Explain the project for today


Making a night light.
o We’re going to use the sensor in one LED to determine how bright the room is.
o If the room is dark enough, we want the other LED to turn on and brighten it up!
Our night light will be made up of two circuits.
o First, one LED to sense how bright it is.
COMMUNITY TECHNOLOGY CENTER
10 W
14th
Ave Parkway | Denver, CO 80204 | 720.865.1706 | https://denverlibrary.org/ctc
5/17/2016 | sl | Page 1


When you run electricity through an LED, it will use some of it to produce light. But this
also works in reverse. Shine a bright light on an LED and it will discharge some of that
energy back into the circuit.
 We can use an LED as a brightness detector… NEAT!
o Second circuit will take the data (how much energy is being added back into our circuit), and will
push electricity to another LED to make the room brighter if needed.
Once we have the hardware setup, we’ll code it so it knows what to do!
(5) Explain a circuit



+ electricity
- electricity
Explain to the students that a circuit is when we take electricity in a loop to make something happen –
like turn on a light. Use your awesome skills of an artist to draw something like this on a board:

The electricity flows from the positive end of the circuit to the negative.
(5) Explain components






Arduino board - The motherboard of the operation. It’s where the code you write will live and what
generally controls the whole set of circuits you create. Basically, it’s an open source microcontroller that
is relatively easy to program.
Breadboard - Allows you to expand your circuitry from the Arduino board. Huzzah possibility!
Resistors - control the flow of electricity so you don’t blow out your components. Yay!
LEDs - light emitting diodes. They emit light.
o What is the difference in your circuit between a resistor and a diode? Resistor doesn’t care
about the direction of the flow of electricity. Diodes do. That’s why LEDs have a long leg and a
short leg. Power goes in the long leg (or anode) and out the short leg (or cathode). If your LED
is reversed, the circuit won’t work!
Jumper Wires - allow you to connect components to the Arduino board.
C/C++ - the language Arduino programming is based on.
(20) Create LED circuits

Create the Ground on the breadboard.
COMMUNITY TECHNOLOGY CENTER
10 W
14th
Ave Parkway | Denver, CO 80204 | 720.865.1706 | https://denverlibrary.org/ctc
5/17/2016 | sl | Page 2
Run a jumper from GND (ground) on Arduino to the “ – ” channel on the breadboard. (Blue
wire).
Make the Sensor circuit (Red wires and LED).
o Jumper from Analog A5 on Arduino to A20 on breadboard.
o LED on Breadboard from E20 (long leg) -> E21 (short leg).
o Ground it: jumper from A211 -> “ – ” channel on breadboard.
Make the Light circuit.
o Jumper from Digital Pin 3 on Arduino to A15 on the breadboard.
o Resistor on breadboard from E15 (long leg) -> F15 (short leg).
o LED01 on breadboard from J15 (long leg) -> J14 (short leg).
o Ground it: jumper from F14 -> - channel on breadboard.
It’ll end up looking something like this:
o



(10) Software! What is the coding language?



What is codebender?
o We use codebender instead of the Arduino API because we can’t install software.
o Have students navigate to codebender.cc.
o Login for them or ask them to login and approve the addition of the app to their accounts.
o Start a new file.
Arduino is coded in a language called C++.
o What is a variable? How do you create one?
o What is a function? How do you create one?
 Every Arduino sketch must have at least a setup function and a loop function.
o How do you make something repeat?
o In C++, what goes at the end of almost every line?
Explain the language you’ll be using:
o int = integer. Where data is stored. This allows you to define variables.
o void = a function declaration.
COMMUNITY TECHNOLOGY CENTER
10 W
14th
Ave Parkway | Denver, CO 80204 | 720.865.1706 | https://denverlibrary.org/ctc
5/17/2016 | sl | Page 3
o
o

pinMode = configures a specified pin as INPUT or OUTPUT.
OUTPUT = allows the pin to provide a substantial current to the circuit. Do NOT connect to
ground or positive power.
o analogRead = allows the circuit to read the voltages running through it.
o digitalWrite = works with pinMode to make the LED brighter or dimmer.
o Ask students what these features look like in other coding languages they know. How do you
declare a function in Python? How do you declare a variable in JavaScript?
EMPHASIZE TYPING PRECISION!
(25) Start programming!





Delete the code that is already on the page.
Define your variables:
int sense = A5;
int LED01 = 3;
int value = 0;
int light = 150;
 What does defining these variables do for you?
 Now you can refer to the circuit for your sensing LED as “sense” instead of having to
look it up to see what pin you had it in every time. It’s easier.
Every sketch for Arduino will have at least these 2 functions:
o setup ( )
 defines what’s happening on your board.
 in this case, we are saying that we are going to be pushing data/energy to whatever is
connected to LED01 (which is pin 13).
o loop ( )
 you won’t keep your Arduino connected to a computer, so it needs instructions on what
to do and how to interact without you there.
Create the setup function:
void setup (){
Serial.begin (9600);
pinMode (LED01,OUTPUT);
pinMode (sense,INPUT);
}
 If they mistype anything (e.g. lowercase ‘output’) the code doesn’t change color. This is
a visual indicator that you may have missed something.
Create the loop function:
void loop (){
value = analogRead(sense);
Serial.println(value);
if (value>=light){
digitalWrite(LED01,LOW);
}
else{
digitalWrite(LED01,HIGH);
}
}
COMMUNITY TECHNOLOGY CENTER
10 W
14th
Ave Parkway | Denver, CO 80204 | 720.865.1706 | https://denverlibrary.org/ctc
5/17/2016 | sl | Page 4
o
What does LOW and HIGH stand for? What do they do to our LED?
(10) Verify the code & troubleshoot



Click the verify code button.
Read through the errors. Help students debug problems in their script.
Most likely problems:
o Missed one or more semicolons.
o Didn’t close a function.
o Didn’t type something correctly (e.g. needed capitals instead of lowercase).
(10) Upload the code to the Arduino board





Plug in the Arduino to the computer.
Click the upload button.
Cover the sense LED with your hand and watch your night light turn on!
Troubleshoot any problems.
Most likely problems:
o Bad hardware. Try changing out LEDs, resistors & jumper wires to see if that fixes it.
(30) Independent practice





Demonstrate how to make a comment. Students can then comment in their code telling you what each
section of code does. If they are really ambitious, have them define each line.
Hand out 2 more jumpers, an LED, and a resistor to each student. Ask them to add another light to the
circuit. Remember! They’ll have to change their code!
o Team Safety says “always unplug your Arduino before futzing with the hardware!” We don’t
want to shock our students, and we certainly don’t want to accidentally fry our equipment. We
prefer to fry our equipment intentionally.
How could we make the night light turn on in a brighter room?
How could we make the night light never turn on?
Hand out the buzzer. Ask the students to look at the logic. How could they rewrite the script so that we
turn the night light into an alarm clock (i.e. the buzzer goes off when the room gets lighter)?
o Remember to play with the resistor here. A weaker resistor = a louder noise!
COMMUNITY TECHNOLOGY CENTER
10 W
14th
Ave Parkway | Denver, CO 80204 | 720.865.1706 | https://denverlibrary.org/ctc
5/17/2016 | sl | Page 5
Instructor’s Notes
1. We use Chromebooks. If you use laptops instead, download and install the Arduino software on each of
your computers. Link below.
Links
Learning Arduino coding vocabulary: https://www.arduino.cc/en/Reference/HomePage
Arduino software: https://www.arduino.cc/en/Main/Software
COMMUNITY TECHNOLOGY CENTER
10 W
14th
Ave Parkway | Denver, CO 80204 | 720.865.1706 | https://denverlibrary.org/ctc
5/17/2016 | sl | Page 6