LED Blinking
In most programming languages, the first program you write prints
"hello world" to the screen. Since an Arduino board doesn't have a
screen, we blink an LED instead.
The boards are designed to make it easy to blink an LED using
digital pin 13. Some (like the Diecimila and LilyPad) have the LED
built-in to the board.
On most others (like the Mini and BT), there is a 1 KB resistor on
the pin, allowing you to connect an LED directly. (To connect an LED
to another digital pin, you should use an external resistor.)
LEDs have polarity, which means they will only light up if you orient
the legs properly.
The long leg is typically positive, and should connect to pin 13. The
short leg connects to GND; the bulb of the LED will also typically
have a flat edge on this side. If the LED doesn't light up, trying
reversing the legs (you won't hurt the LED if you plug it in
backwards for a short period of time).
Circuit
Code
The example code is very simple, credits are to be found in the
comments.
/* Blinking LED
* ------------
*
* turns on and off a light emitting diode(LED) connected
to a digital
* pin, in intervals of 2 seconds. Ideally we use pin 13
on the Arduino
* board because it has a resistor attached to it, needing
only an LED
int ledPin = 13; // LED connected to digital pin 13
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as
output
}
void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}
Push Button and LED
The pushbutton is a component that connects two points in a circuit when
you press it. The example turns on an LED when you press the button.
We connect three wires to the Arduino board.
The first goes from one leg of the pushbutton through a pull-up resistor
(here 2.2 KOhms) to the 5 volt supply.
The second goes from the corresponding leg of the pushbutton to ground.
The third connects to a digital i/o pin (here pin 7) which reads the
button's state.
When the pushbutton is open (unpressed) there is no connection between
the two legs of the pushbutton, so the pin is connected to 5 volts
(through the pull-up resistor) and we read a HIGH.
When the button is closed (pressed), it makes a connection between its
two legs, connecting the pin to ground, so that we read a LOW. (The pin
is still connected to 5 volts, but the resistor in-between them means that
the pin is "closer" to ground.)
You can also wire this circuit the opposite way, with a pull-down resistor
keeping the input LOW, and going HIGH when the button is pressed. If
so, the behavior of the sketch will be reversed, with the LED normally on
and turning off when you press the button.
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 will more-or-
less randomly return either HIGH or LOW. That's why you need a pull-up
or pull-down resister in the circuit.
Circuit
Code
int ledPin = 13; // choose the pin for the LED
int inPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button
released)
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}