Activity 1
Activity 1
Learning Objectives:
1. Introduction to the Arduino IDE
2. Knowledge of associated hardware and peripherals
3. Understand of hardware and software integration
Hardware Required
Arduino Board
Circuit
Only your Arduino Board is needed for this example.
Code
The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start
using libraries, etc. The setup function will only run once, after each powerup or reset of the
Arduino board.
After creating a setup() function, the loop() function does precisely what its name suggests, and
loops consecutively, allowing your program to change and respond as it runs. Code in the loop()
section of your sketch is used to actively control the Arduino board.
The code below won't actually do anything, but it's structure is useful for copying and pasting to
get you started on any sketch of your own. It also shows you how to make comments in your
code.
Any line that starts with two slashes (//) will not be read by the compiler, so you can write
anything you want after it. Commenting your code like this can be particularly helpful in
explaining, both to yourself and others, how your program functions step by step.
void setup() {
// put your setup code here, to run once:
void loop() {
// put your main code here, to run repeatedly:
Blink
This example shows the simplest thing you can do with an Arduino to see physical output: it
blinks an LED.
Hardware Required
Arduino Board
LED
Resistor, anything between 220 ohm to 1K ohm
Circuit
To build the circuit, connect one end of the resistor to Arduino pin 13. Connect the long leg of
the LED (the positive leg, called the anode) to the other end of the resistor. Connect the short
leg of the LED (the negative leg, called the cathode) to the Arduino GND, as shown in the
diagram and the schematic below.
Most Arduino boards already have an LED attached to pin 13 on the board itself. If you run this
example with no hardware attached, you should see that LED blink.
Schematic
After you build the circuit plug your Arduino board into your computer, start the Arduino IDE, and
enter the code below.
Code
In the program below, the first thing you do is to initialize pin 13 as an output pin with the line
pinMode(13, OUTPUT);
In the main loop, you turn the LED on with the line:
digitalWrite(13, HIGH);
This supplies 5 volts to pin 13. That creates a voltage difference across the pins of the LED, and
lights it up. Then you turn it off with the line:
digitalWrite(13, LOW);
That takes pin 13 back to 0 volts, and turns the LED off. In between the on and the off, you want
enough time for a person to see the change, so the delay() commands tell the Arduino to do
nothing for 1000 milliseconds, or one second. When you use the delay() command, nothing else
happens for that amount of time. Once you've understood the basic examples, check out the
BlinkWithoutDelay example to learn how to create a delay while doing other things.
Once you've understood this example, check out the DigitalReadSerial example to learn how
read a switch connected to the Arduino.
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This sketch demonstrates how to blink an LED without using delay(). It turns on the LED on and
then makes note of the time. Then, each time through loop(), it checks to see if the desired blink
time has passed. If it has, it toggles the LED on or off and makes note of the new time. In this
way the LED blinks continuously.
An analogy would be warming up a pizza in your microwave, and also waiting some important
email. You put the pizza in the microwave and set it for 10 minutes. The analogy to using
delay() would be to sit in front of the microwave watching the timer count down from 10 minutes
until the timer reaches zero. If the important email arrives during this time you will miss it.
What you would do in real life would be to turn on the pizza, and then check your email, and
then maybe do something else (that doesn't take too long!) and every so often you will come
back to the microwave to see if the timer has reached zero, indicating that your pizza is done.
In this tutorial you will learn how to set up a similar timer.
Hardware Required
Arduino Board
LED
Resistor, anything between 220 ohm to 1K ohm
Circuit
To build the circuit, connect one end of the resistor to Arduino pin 13. Connect the long leg of
the LED (the positive leg, called the anode) to the other end of the resistor. Connect the short
leg of the LED (the negative leg, called the cathode) to the Arduino GND, as shown in the
diagram above and the schematic below.
Most Arduino boards already have an LED attached to pin 13 on the board itself. If you run this
example with no hardware attached, you should see that LED blink.
Schematic:
After you build the circuit plug your Arduino board into your computer, start the Arduino IDE, and
enter the code below.
Code
The code below uses the millis() function, a command that returns the number of milliseconds
since the Arduino board started running its current program, to blink an LED.
The circuit:
* LED attached from pin 13 to ground.
* Note: on most Arduinos, there is already an LED on the board
that's attached to pin 13, so no hardware is needed for this example.
*/
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
Hardware Required
Arduino Board
A momentary switch, button, or toggle switch
10k ohm resistor
breadboard
hook-up wire
Circuit
Connect three wires to the Arduino board. The first two, red and black, connect to the two long
vertical rows on the side of the breadboard to provide access to the 5 volt supply and ground.
The third wire goes from digital pin 2 to one leg of the pushbutton. That same leg of the button
connects through a pull-down resistor (here 10 KOhms) to ground. The other leg of the button
connects to the 5 volt supply.
Pushbuttons or switches connect two points in a circuit when you press them. When the
pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton,
so the pin is connected to ground (through the pull-down resistor) and reads as LOW, or 0.
When the button is closed (pressed), it makes a connection between its two legs, connecting
the pin to 5 volts, so that the pin reads as HIGH, or 1.
If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is
because the input is "floating" - that is, it doesn't have a solid connection to voltage or ground,
and it will randomly return either HIGH or LOW. That's why you need a pull-down resistor in the
circuit.
Schematic
Code
In the program below, the very first thing that you do will in the setup function is to begin serial
communications, at 9600 bits of data per second, between your Arduino and your computer with
the line:
Serial.begin(9600);
Next, initialize digital pin 2, the pin that will read the output from your button, as an input:
pinMode(2,INPUT);
Now that your setup has been completed, move into the main loop of your code. When your
button is pressed, 5 volts will freely flow through your circuit, and when it is not pressed, the
input pin will be connected to ground through the 10-kilohm resistor. This is a digital input,
meaning that the switch can only be in either an on state (seen by your Arduino as a "1", or
HIGH) or an off state (seen by your Arduino as a "0", or LOW), with nothing in between.
The first thing you need to do in the main loop of your program is to establish a variable to hold
the information coming in from your switch. Since the information coming in from the switch will
be either a "1" or a "0", you can use an int datatype. Call this variable sensorValue, and set it to
equal whatever is being read on digital pin 2. You can accomplish all this with just one line of
code:
Once the Arduino has read the input, make it print this information back to the computer as a
decimal value. You can do this with the command Serial.println() in our last line of code:
Serial.println(sensorValue);
Now, when you open your Serial Monitor in the Arduino environment, you will see a stream of
"0"s if your switch is open, or "1"s if your switch is closed.
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor