Notes for Arduino Platform Summer Camp activities at LTU, 2012

You may be familiar with the idea of
open-source software. The Arduino is open-source
software on open-source hardware. Most previous
development boards or prototyping platforms were
tailored to sell one particular micro-controller and
encouraged the use of peripheral chips sold by the same
company. The Arduino approach allows you to use working
solutions from a large number of sources, modify them to
suit your purposes, and re-assemble them to solve any
problem that you want to work on.

What's a micro-controller, what's an Arduino? A
micro-controller is just a small computer that has been
dedicated to controlling some physical process. As an
example, we will try to turn on a fan if the room is too hot
and turn off the fan if we are no longer working or moving
about in the room. Of course we can already do this with
our hands. Can we have the Arduino do this for us? The
Arduino project has made it quite a bit easier for an
average student to become a hardware hacker and actually
build their own solution to a problem like this.
Getting started:

Electrical safety and measuring volts, amps and watts. The
voltages on the Arduino board, 5 volts, 9 volts and 3.3
volts are pretty safe for a person to touch with their bare
hand. But our little project is about controlling something
plugged into a wall outlet. Trying to move electricity
efficiently over distances, power companies have settled on
120 volts alternating 60 times per second. Dry skin resists
low voltages pretty well, but 120 volts not so well. Safety
for tissues with electricity running through them follows
a U shaped curve with the bottom of the valley
(unfortunately for us) at just about 60 cycles per second.
So we have to be careful and learn a little about electricity
and electronics:

Ohm's law. E = I × R or voltage = current ×
resistance. One Volt will move one Ampere of current through
one Ohm of resistance. For our purposes here the analogy with
water flow is helpful: The same water pressure will push more
water through a big pipe (lower resistance) than through a
smaller pipe (higher resistance.)

Power. P = E × I or power = voltage ×
current. 1 Watt = 1 Volt moving 1 Ampere of current. 60
Watts = 120 volts moving 0.5 Ampere of current. Power is
what the power company charges us for and what we are
trying to conserve by turning the fan off when it is not
needed. How much does it cost to run our desktop fan?

Using the multimeter. The Fluke multimeter has many
imitators of its functionality and many more imitators of
its yellow case.

Ohm's law in action with a multimeter, the
Arduino and the resistors from our kit.

When ground is not ground: For a three pronged plug
the round prong (ground or green wire) should be
connected, near the service box for the house, to the
wider flat prong (neutral or white wire) and so there
should be zero volts between the green and white wires.
There should be 120 volts between the narrower flat prong
(hot or black wire) and either the white wire or the green
wire. There are many reasons the voltage between the
green and white wires could be much higher than 0 -- and
enough to hurt you or destroy the Arduino board, (Things
like broken wires, corroded connections, wiring errors and
adaptors.) A bit ago, when I was about your age and
working in auto repair, I remember a lecture about never
trusting a customer who said, "The brakes are fine" and
always testing those brakes yourself before moving the
car. I would ask when someone hands you the end of an
extension cord that you assume nothing before you plug
your device into it. Another digression to follow when
you have had a little more experience: The Tweet A Watt
design on LadyAda.net, mentioned above, is an interesting
one where the hacked Kill A Watt can send data to an
Arduino without having to worry if both the Kill A Watt
and the Arduino are properly grounded.

This sensor is both easy to use and inexpensive.
The tutorial page is helpful. If using the 5 volt
power is not accurate enough, try the 3.3 volt sketch --
remembering to change either the analog pin connection or the
tempPin variable.

This sensor is also easy to use. This is a digital sensor
rather than an analog sensor like the TMP36.

Turning 120 volt electricity on and off. There are two very
important considerations:

The person could be injured or the micro-controller could
be damaged if either comes in contact with the 120 volts.
The usual manual switch has a plastic toggle or knob that
insulates your hand from high voltage. The limit of that
protection (in volts) is stamped on the switch. The
Powertail is opto-isolated. That is a small LED is turned
on with the low voltage and that light is sensed by a
separate circuit that turns on the higher voltage. The
separation protection is maintained up to 6000 volts.

The on-off switch needs to be robust enough to handle the
amount of current flowing when the device is on. Our fan
runs at a maximum of 0.8 amps a manual switch usually comes
labeled with a capacity of 15-20 amps. The little
relay switch in the Powertail II has a 20 amp capacity.

Soldering is one electronic construction technique we
will practice along with tinning the ends of stranded wires
and applying heat shrink tubing. (Maybe later we can also
play with liquid electrical tapes
and Sugru.) Building the Powertail II kit, we will
generally follow
the assembly instructions. Yet another digression on
grounding: The extension cord we are splitting with our
Powertail II kit follows the custom for 3-wire lamp cord --
the hot wire has a smooth side and the neutral wire has a
ribbed side. Always check this with the multimeter, and
don't assume the customary standard was followed. Before we
screw the cover on, we will make a connector cable (out of 22 gauge
speaker wire, some bits of hookup wire and some heat shrink tubing)

for the low voltage circuit and attach it to the terminal
strip. Also we will add a little more strain relief to the
power cords in the form of zip ties just inside the grommets
and to the connector cable with a little nylon bracket.

Testing the Powertail II with the Arduino board connected to your
laptop by the USB cable.:

With the Powertail unplugged, connect the - lead from the
Powertail II to ground on the Arduino and connect the + lead
to + 5 volts on the Arduino. The red LED on the Powertail II
should light up.

With the Powertail II still not plugged into the wall,
remove the + lead from the Arduino +5 volt pin and connect
it instead to the Arduino digital output pin D3.
Use the Arduino IDE to upload this sketch.

The many wonderful examples already written for the Arduino
tempt you to start by copying and pasting fragments
of code until the mash-up of code seems to do what you wanted.
The more innovative and complex your idea is, the more you
will waste lots of time this way. Begin by describing your
idea in outline form. Flow charts and pseudo-code are
popular ways to do this.

Next turn your outline into comments. Don't just add
your comments as an afterthought. To your generation, a
long term job may last a year. As you move to another job
you will hope that the programmer you are replacing left
good enough comments that you can easily understand what they
were doing. Be kind kind enough to leave the code you write with
clear comments that the programmer that comes after you can
easily understand.

Comments should be complete sentences and be spell-checked.
Don't write comments like you write tweets.

The basic parts to each Arduino program:

Comments with the name, purpose and author of the program.
Also include here acknowledgements of other programmers and
important version history.

Global variables and #defines.

Initialization --

void setup() {
// All the stuff to do just once at the beginning of the program.
}

The main loop --

void loop() {
// The things to do over and over as long as the program is running.
}

Using Arduino libraries, always take a look at the Examples
provided with the library. Using the serial monitor.

What are RFID and NFC? Back when a microcomputer and its
memory were miniaturized enough to be encased in a plastic
card or key-chain fob, there developed two ways to provide
power and communicate with the encased computer. One way
is to use external contacts like those on USB thumb drives
and SD cards. The other way is to also encase a small
antenna that is tuned to a specific radio-frequency. The
various types of Near Field Communication protocols are
examples of this approach.

What is a servo? Servos are motors where it is easy to
turn the armature a set number of degrees. Low power
servos can work with an Arduino without added equipment.
http://www.adafruit.com/products/155

Today we will assemble the RFID/NFC shield and add a
connector for a servo. Then we can program a LEGO NXT
robot to drive up to a gate controlled by our Arduino and
its shield. Sensing the correct "license-plate" card
taped to the front of the NXT, the Arduino should use the
servo to open the gate for the NXT robot. The robot
should sense that the gate is opened and drive through in
less than 10 seconds.

So far we have used just the functions built into the the Arduino
IDE. This project will use 3 more libraries

The Wire library which comes with the IDE and handles serial
communication between the Arduino and the RFID/NFC shield.

The Servo library which comes with the IDE and handles
controlling the servo motor which opens and closes the gate.

The RFID/NFC library which is downloaded from Github and
controls talking to the NFC chip on the shield. The
Wire library can use a few different dialects of serial
communication. The RFID/NFC library comes in two
separate dialects -- we will download and use the I2C
version as it matches the default configuration of the
RFID/NFC shield.

Build it

Soldering techniques.

Assembling the RFID/NFC shield. We will use the header strips
that come with the shield kit.

1

Cut the header strip into pieces to match the headers
on the Arduino Uno R3. Then a 3-pin header for the servo.
There will be one left over pin.

2

Put the header strips in the Uno headers to hold them
straight while you solder.

3

Position the shield on the headers.

4

Hold the shield in place with a little masking tape
until you tack the ends of the headers.

5

The ends of the headers have been tacked and the
masking tape is no longer needed.

6

The soldering of the headers is finished.

7

The 3-pin servo header is placed somewhere in the
prototyping area of the shield.

8

The 3-pin servo header is held for soldering with a bit
of masking tape.

9

The servo header is soldered.

10

Servo connections to ground, +5 volts and digital pin 10
are added.

11

Solder bridges connect the wires to the header pins.

12

The servo can then be easily connected to our shield.

Testing:
Using the format example code changed to add the URL:
http://robofest.net

So our lock works pretty well. We could use it on a door at our home
or even on a car door. (Well, maybe the car door is not so easy.
Finding out how to provide clean power is an example of a problem
where you might go to the Auduino user community for a little help.)

On your own, here are some
books that would help you learn electronics with the Arduino while
building some fun projects:

Pulse-Width Modulation and
slow motors. Now that you have completed your Powertail II
and are finished shrinking tubing with the mini heat gun, if
you learn a little about PWM, your Arduino can do a nice job of
roasting a marshmallow. The windings of small electric motors
are pretty thin. Increasing the voltage to a motor increases the
torque but runs the risk of burning up those thin windings. You need
higher torque to start a small motor slowly. Years ago small motors
could only start and run quickly. Fortunately PWM changed all that
by providing full speed voltage for a small fraction of a cycle.
Now we can turn a marshmallow on a spit slowly and evenly
enough to get it a nice golden brown outside and soft inside.
The Motor Shield from Adafruit makes this speed control easy.
Even the tiny stepper motor to turn a marshmallow needs more power than
can be borrowed from your laptop through the USB cable. For this
project we will get the extra power from the 9 volt wall plug adapter.

Maybe we will add an on-off switch as well. (ladyada.net on pushbuttons.) Or maybe since the mini
heat gun already has a nice on-off switch we should make a circuit
to sense when the mini heat gun is turned on and start turning the
marshmallow automatically? (The multimeter and
CircuitLab.com can help.)