Arduino
Congratulations! We're done with studying for the AP Exam!
To me, there are three things that computers are good for: managing databases, playing games, and controlling Killer Robots.
It's time to learn about building Killer Robots.
0. General Orientation
The study of robotics typically includes two components: the mechanical and the electronic. The mechanics aspect is concerned with the physical means of movement—an actuator—often an arm or a locomotion system. The electronics aspect consists of providing electrical input, either via an input device like a joystick or an autonomous system that probably relies on one or more sensors.
For our study, we'll begin by studying a simple stepper motor, which is the foundation for almost all robotic motion, and a microprocessor, which we'll use to issue commands to the stepper motor. For our microprocessor, we'll be using an Arduino.
1. Setting up the Arduino
We'll need to set up our development environment so that we can program the Arduino. Here we go!
1.a. Get Arduino Software
Follow the instructions at www.arduino.cc/en/Guide/HomePage to get the software installed on your computer.
1.b. Connect the Arduino
At the start here, we'll be using the USB cable between your computer and the Arduino for power. Attach that cable between the two devices. You should see the green power light on the Arduino come on.
1.c. Launch the Arduino app
At the start here, we'll be using the USB cable between your computer and the Arduino for power. Attach that cable between the two devices. You should see the green power light on the Arduino come on.
1.d. Work through the examples
Try out each of these programs and see if you can figure out how things work.
In each case you will:
- open up the program in the Arduino app
- read the program to see how it works (note how helpful the comments are!)
- click the upload button to load the program onto the Arduino
- observe the program's behavior, as carried out by the Arduino
- edit the program as desired and re-upload it to the Arduino to experiment
Programs to try, learn from, and understand:
- File > Examples > Basics > BareMinimum
- File > Examples > Basics > Blink
- File > Examples > Digital > BlinkWithoutDelay
- File > Examples > Digital > Button
- File > Examples > Digital > DeBounce
2. Arduino Programs
Arduino programs are effectively C/C++ functions that are compiled and then run on the ATmega168 microprocessor that is the heart of the Arduino board.
2.a. Introductory commands summary
Here is a summary of the commands you can use in an Arduino program.
- // This is a comment
Double forward-slashes indicate a comment
- # define LED 13
# define SENSOR 0
# define BUTTON 12
Used to identify a label like LED, SENSOR, or BUTTON with a specific port number. Note: no semi-colon after a DEFINE statement.
- void setup()
{
pinMode(LED, OUTPUT)
pinMode(BUTTON, INPUT)
Serial.begin(9600)
}
The setup() function establishes what different pins will be used for when the program is running.
- void loop() { ... }
The loop function contains the behaviors of your device. This function will repeat infinitely until the board is turned off.
- digitalWrite(LED, HIGH); // turns the indicated pin on
The constant HIGH means that the specified pin is set to a value of +5 Volts.
- digitalWrite(LED, LOW); // turns the indicated pin off
The constant LOW means that the specified pin is set to a value of 0 Volts.
- delay(1000); // pauses the program for the indicated number of milliseconds
This is not the most efficient way to work, especially if your program needs to be doing other things while part of it is paused. We'll look at tricks that you can use to leave one part of your program running while the other one stops for a bit.
- int val = 0; // Establishes an integer variable
Declaring a variable, just like one does in Java.
- val = digitalRead(BUTTON); // save an input value
We can store a value in a variable for use later on.
- if (val == HIGH) { ... } else { ... }
This is a decision making statement, the structure of which will be familiar to Java users...
2.b. Two introductory programs
A simple Arduino program
Take a look at this simple program. Enter and run it using the Arduino.
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the Uno and
Leonardo, it is attached to digital pin 13. If you're unsure what
pin the on-board LED is connected to on your Arduino model, check
the documentation at http://arduino.cc
This example code is in the public domain.
modified 8 May 2014
by Scott Fitzgerald
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
A more advanced Arduino program
Take a look at this program. Can you determine what it does, and how it works? Enter and run it using the Arduino.
/*
Debounce
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
a minimum delay between toggles to debounce the circuit (i.e. to ignore
noise).
The circuit:
* LED attached from pin 13 to ground
* LED2 attached from pin 14 to ground
* pushbutton attached from pin 2 to +5V
* 10K resistor attached from pin 2 to ground
* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don't need any extra components for this example.
created 21 November 2006
by David A. Mellis
modified 30 Aug 2011
by Limor Fried
modified 28 Dec 2012
by Mike Walters
modified 2015-05-13
by Richard White
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Debounce
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int ledPin2 = 14; // the number of the second LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
digitalWrite(ledPin2, !ledState); // What is the NOT of HIGH?
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
digitalWrite(ledPin2, !ledState);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
3. Pulse Width Modulation
Pulse Width Modulation, or PWM, turns out to be critical for what we're going to be doing. Let's see what it is and how it works.
3.a. Blinking... really fast.
In the Blink program that we've already seen, there was a delay of 1 second while the LED was on, and then 1 second while it was off. That blinking effect was easily visible to our eyes.
Dimming an LED
Take a moment to go back to that program and alter the amount of time in the "off" delay until you don't see the LED blinking anymore. It's still not "on" the entire time, so the effect is one of reducing the LED's visible output...even though we're setting the LED only to "on" or "off."
Alter the values for the Blink program so that it is ON for one-fourth of the time that it is off. Does it appear as if the LED is one-fourth as bright? Alter the values so that the LED is on for three-fourths of the time that it is off. The LED appears three-fourths as bright as its full brightness.
By varying the "width of the pulse"—the amount of time the LED is on—we can modulate how much light is produced. This is the essence of Pulse Width Modulation (PWM).
It works with lights... and it works with certain types of motors.
4. Working with motors
There are four different types of motors that you might want to run with the Arduino: Brushed DC motors, Brushless DC motors, Servo motors, and Stepper motors.
Learn more here:
Let's get started working with motors.
4.a. Get a Motor Shield
Because the voltage/current levels required by our motors are near the limits of what an Arduino board can supply—and will soon exceed them—we're going to protect the Arduino board by stacking a separate board on top of it, in this case, a "motor shield" from Adafruit.
We'll use stacking headers with the shield to mount it above the Arduino (see learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/install-headers for info, if needed.)
4.b. Get the software
We'll need some additional software to run with this motorshield. Download the Adafruit Motor Shield V2 Library and copy it to the libraries folder inside your Arduino sketchbook folder, then re-name it to Adafruit_Motorshield.
On my OS X machine, the file path is: Computer > Users > rwhite > Documents > Arduino > libraries > Adafruit_Motorshield
4.c. Run a DC motor
Following the instructions at learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/install-software, attach and run a DC motor.
Careful!
Pay close attention to the description of how to attach power to the motor shield. Attaching the wrong wire to the wrong location on your Arduino/Motor shield can cause permanent damage to the boards.
4.d Run a Stepper Motor
Continue following the tutorial to set up a stepper motor, and run the stepper motor test to see how it works.
4.e. Powering motors
Read the tutorial at https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/powering-motors to learn more about how to power any given motor using the motor shield.
4.f. Run a Servo
Read the tutorial at learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/using-rc-servos to figure out how to use a servo with the motor shield.