Download Introduction to Arduino and the Internet of Things

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

Rectiverter wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Opto-isolator wikipedia , lookup

Transcript
Arduino
The Internet of Things: Using the Arduino to Teach Coding
Introduction
• Define for your students what is meant by “The Internet of Things”
• It is the network of physical objects, devices, vehicles, buildings and
other items which are embedded with electronics, software, sensors,
and network connectivity, which enables these objects to collect and
exchange data.
• The Internet is no longer bound in pictures, text, audio, and video. It
can now impact the real world.
• Examples: We use watches with heart monitors to sync our workouts
to a web site. We use sensors to detect Earthquakes, volcanoes, or
tsunamis and report that data in time to evacuate. We build robots
to vacuum our homes and mow our lawns autonomously.
Connecting to the Internet of Things
• We need to be able to do three things:
• Listen (Receive data from sensors)
• Act (Turn things on and off)
• Report (Communicate on a network)
• The Arduino microprocessor allows us to do all three. We can
connect to various sensors to detect our environment. We can use
digital and analog pins and PWM (pulse with modulation) to activate
and use motors or servos. Finally we can use wired or wireless
connections to access the network to report the information we
receive and get instructions on how to react to it.
Sensors for Robots
What is Arduino
• The Arduino is a microprocessor and a series of connection hardware
that allows us to tie sensors and motors together and make decisions
on how to use them.
• The Arduino uses a subset of the C++ computer programming
language in order to receive instructions from a programmer which it
then carries out autonomously.
• The Arduino can interact with just about any type of electronic sensor
and it can control just about any electronic device. Given the right
configuration, it can also communicate over most data networks.
These are all the requirements for the Internet of Things
The Hardware
• The most common version of
the Arduino microprocessor is
the Arduino Uno.
• As pictured here, the Uno
features an ATmega328P
processor (the brains) as well
as 14 digital and 6 analog
connectors (the brawn).
• The Uno also has a built in USB
connector (the ears) for
programming. It can be fitted
with a wired/wireless adapter
to free it from cables.
Introduction to Programming in Arduino
• Arduino uses an Open Source IDE to program its instructions. It is
available for download from http://www.Arduino.org
• The Arduino program contains a USB to serial bus to allow the
processor to talk back to the computer.
• As with the hardware, the software is open source. It is openly
available and free to distribute or modify under the open source
agreement.
Anatomy of a Sketch
• We will complete a simple program (called sketches in Arduino
parlance) to get used to the board and become familiar with the
program layout.
• All Arduino sketches are composed of two main functions: A setup()
function and a loop() function. As you might suspect from the names,
setup runs first for one time only and then loop continues indefinitely
until the board is reset.
• Our first program is a common “Hello World” sketch getting the
Arduino to blink and LED.
Hello World
• We will build our circuit according to the layout image below. Be sure to connect
the resistor into the circuit to protect the LED or it can be damaged.
• The circuit board (or bread board) is a simple way to make electrical connections
without soldering. The outside rows of holes bound by the red and blue strips
are connected along the entire length of the board. These are commonly used
for negative and positive rails. The smaller groups of holes that make up the
middle of the board are connected in groups of five.
• The breadboard uses a series of holes to make
electrical connections. The red and black line of
holes at the upper and lower boundaries of the
board are all connected along their length.
These are used as power rails as they run the
entire length of the breadboard. The interior
holes are connected in groups of five as seen in
the image. They provide connection for the
different components we will place into the
board.
Breadboard Layout
• If we plug a lead
from a component
into the
breadboard and
then plug another
lead into one of the
other 4 holes in
that group then we
have made a
connection that will
conduct.
Back to our circuit
• We will use a 220 ohm resistor to
protect our LED. It is useful to
begin your introduction to Arduino
and electronic circuits with a lesson
on the resistor color code. We are
going to skip that today in the
interests of time but many sources
are available on the net and you
will need to go over it with your
students if you plan to delve very
deeply at all into electronics and
circuit design.
• For our purposes, the resistor strips
we are looking for are: Red, Red,
Brown
The Light
• The component we are using to light the board is a common light emitting diode
(LED). Like any diode it allows current to flow in only one direction. We must
observe polarity.
• On most LEDs, the cathode, or negative lead, is shorter than the anode or positive
leg. Also, many LEDs have a flatten space on the side of the bulb that corresponds to
the negative side
Make the connection
• We connect a jumper wire from Digital
Pin 13 to a hole in the middle of the
breadboard. We connect the positive
lead or anode of the LED into one of the
other 4 holes in the group. We connect
the resistor to a hole in the group
where the negative lead of the LED is
plugged and we connect the other end
of the resistor into a hole along the
negative or ground rail of the
breadboard. We finally close the circuit
with a jumper wire from the ground rail
to one of the three ground pins on the
Arduino. Our circuit is complete.
The Code
• When we open
the Arduino
IDE we see a
simple text
editor for the
most part.
• There is a
section at the
bottom for
error messages
and a simple
tool bar to
compile and
upload
programs.
Two Functions
• All Arduino sketches
use the same two
functions, setup()
and loop(). We put
initialization code in
setup(). It runs first
and only once. The
main body of our
code goes into
loop(). It continues
to run until the
board is reset.
Coding the Behavior
• I am making some assumptions on the level of code experience for this
group. There are many excellent Arduino tutorial sites on the Internet. You
can find them easily on Google.
• I will analyze this code at a very shallow level and allow you to go further
into detail at your leisure.
• In the setup() function we begin by establishing the pin we wish to use and
what we want to do with it. We do this by using the pinMode() function.
• This function takes two arguments or parameters; a pin number and the
designation INPUT or OUTPUT. This line tells the Arduino which pin we
want to use and what we want to use it for. An INPUT would have us
listening for a sensor to tell us something. An OUTPUT has us turning
something on or off.
Setup()
• Notice the grey text at the top preceded by the two forward slashes. These are comments. They
are notes that a programmer leaves to explain code to other programmers. The compiler will
ignore them. This is one of the things you should be grading students on. It is a vital skill for
programmers to learn. Also if they can explain what the code does, it is more likely that they
understand it themselves and didn’t just copy it off the Internet. Also notice the semi colon at the
end of the line of code.
Loop()
• Once we have completed the setup of the pin we will not need to do
that again. We can move into the repeating part of our sketch. We
want the LED to blink on and off. We do that in the Loop() function.
• This would be a good time to talk about algorithms in your class.
Programming is about problem solving. Algorithms are a practical
and systematic patterns to solve problems.
• Think about what we need to do to turn a light on and off. First we
turn it on, then we wait. We turn it off and wait. Finally we repeat
the process.
I see the Light
• In order to turn ON our LED we use the digitalWrite() function. We are writing
power to our OUTPUT pin. In Arduino parlance we are setting pin 13 to HIGH.
This command sends 3.3v of electricity to pin 13. Our LED should light up.
• If we just turn the LED off at this point it will happen so fast we won’t be able to
see it. We need to tell our LED how long to stay on before it goes off. We use the
delay() function to tell the LED to wait. The delay() function takes one argument
or parameter; a number in milliseconds. We will use the number 1000 to tell our
LED to stay on for 1000 milliseconds or 1 second.
• We can then turn our LED off. We use digitalWrite() again and this time we write
pin 13 to LOW and shut off the 3.3 volts going to pin 13.
• Finally we tell the Arduino to wait for another full second before starting the loop
function over again. We use the delay() function with a 1000 millisecond
parameter to have it wait. Since this is the loop() function, the operation starts
over again immediately.
The Complete Code
• Here is the complete loop() function for this program. Notice the comments and
what they explain. This is how your students should be commenting their code.
Make sure they understand what each function does and how to use them.
Getting the Code into the Arduino
• At this point we must connect our Arduino to the PC via a USB cable. We
much check the COM port under the “Tools” menu to verify that our PC
recognizes and assigns our Arduino a COM Port.
• Once verified and connected, we should see lights on our Arduino. We can
now upload our code. HOWEVER it is a good idea to verify it free of errors
before we upload.
• Using the Check button in the toolbar we can check our code for syntax
errors and verify that it will at least compile without problem. Click the
Check button and fix any errors that are shown.
• The next button over is the UPLOAD button. Click that button to send the
code to the Arduino. IF we’ve done everything right we should see the LED
on the breadboard begin to blink on for one second and off for one second.
Cause and Effects
• We can now use a computer to effect the real world. If we can turn
an LED on and off we can use the same code idea to operate and
relay and turn huge valves or motors on and off. We can run anything
in the world that uses electricity and has an On/Off switch.
• Now lets see if we can listen to a sensor…
Volume Knob
• We are going to use a potentiometer (adjustable resistor) to act as
our sensor. We could use a photo-resistor, an ultrasonic sensor, a
touch sensor, or any other type of sensor and get the same effect.
They all provide a source of changing voltage based on an outside
condition. Photo-resistors allow more current to flow when lower
light is present. Ultrasonic sensors listen for an echo and send allow
more or less current based on how long the echo takes to return.
Touch sensors send current based on the completed circuit when
something bumps into them and presses them against a conductor.
• All we have to do is listen.
Build the Circuit
Build the Code
• In this sketch we are going to declare some variables before we build
our setup() function. I am making another assumption about your
skill level and am not going to detail variables beyond what we need
to finish the sketch. You can, once again, find a great many
programming tutorials online that will fill any gaps you may have
concerning programming.
• We need four variables to hold the values we will be working with.
They will all be of type Integer. They will hold only whole numbers.
Variables
• First we need an analog pin to act as our sensor listener. We will use pin A0. In
order to use it we must declare it. The integer we use will be A0. We will call it
potPin for potentiometer
• We need a digital pin to act as our output. We will use digital pin 9 to send
current to our LED. We will call it ledPin
• We need an integer to hold the value we get from our sensor, in the case, a
volume knob. We will call it readValue.
• We need an integer to hold the value we write to the LED. We will call this one
writeValue.
• We declare variables in the following way:
Int ledPin = 9; //this will set our led pin value to 9
Or
Int writeValue; //this will create a variable but give it no value at this time
Variable Code
The setup() function
• We need to establish our INPUT and OUTPUT pins and do some other
one time tasks in the setup() function.
• We declare analog pin A0 to be in input using the pinMode() function.
• We declare pin 9 to be an output also using pinMode().
The loop() function
• This is a modestly complicated program. We use the analogRead()
function to read whatever voltage our volume knob (potentiometer)
is allowing through. We assign that value to our readValue integer.
• We use a simple math calculation to take that value and make it
something our output pin can understand. When Arduino reads an
analog voltage it will return somewhere between 0 and 1023 on a
scale. When we are writing a voltage in analog we can write a
number between 0 and 255. In order to get an actual value we will
multiple our readValue times 255/1023
• We assign that value to our writeValue variable.
The loop() Code
Run It
• We can now verify our code and upload it to the Arduino.
• If all went well then our LED may or may not be lit.
• Turn the knob on the potentiometer to one side or another and see if
it affects our LED. Congratulations! Your Arduino is listening to an
outside sensor and it is affecting the real world. We could use this
principle to accept output from any type of sensor and, once again,
control very large pieces of equipment using relays and switches.
And Finally…
• Now we will modify this code to report the value that we are reading
to a serial monitor. I would like to have us post it to a web site or xml
file but I did not have the budget to get Ethernet shields or SD Card
shields for all of us. We are going to report our information to a serial
monitor and make it appear in a window on the screen. Rest assured
that you can also report this information to the Internet or to any
other remote network that will accept a TCP/IP connection.
• We need to modify our code slightly to allow Arduino to
communicate back to the PC via a virtual serial port
Serial Connection
• We need to tell Arduino to use the serial port and how fast to send or
our computer won’t understand what is being sent. We will use a
baud rate of 9600 and declare our intentions in the setup() function.
• Add the following line of code…
Serial.begin(9600); // turn on Serial Port
Back in the loop()
• In the loop() function we will add two lines to let us write information
to the serial monitor. Add the following lines to your loop() function…
Serial.print("You are writing a value of ");
Serial.println(writeValue);
Lets See What Arduino Sees
• Verify and upload this new code. We
then need to open the serial
monitor. This is a simple text
window rather like a command
prompt that is connected to the
microprocessor via a virtual serial
com port.
• We can send input and the Arduino
can send output. We can find the
serial monitor under the “Tools”
menu in the toolbar.
Finally Again
Open the Serial Monitor. Run the code. Turn the Knob. Observer what
is written into the serial monitor. You could just as easily write this
data, or any text data, to a web server or a network node.
You have accomplished all the tasks required to join the Internet of
Things. We accept Input, use Output to effect the real world, and
report our results.