The circuit

A very good project to start with if you’re new to Arduino is to get a LED to blink. The first you need to do is connect it physically to the Arduino board. Do this according to the schematics below. Please note that the LED only works in one direction. The long leg connects to pls (the resistor) and the short leg connects to minus (GND). A LED may never be connected directly yo a battery or other power source without a suitable resistor or other protection. In this instance we’re using a resistor of 220 ohm (red-red-black-black) which is a good compromise for most LEDs connected to 5 volts. White and blue LEDs may need a smaller resistor, say 100 ohm, in order to shine at full power, but will work with 220 ohm.

Arduino exempel 001 - blinkande lysdiod

Arduino example 001 – blinking LED

Description

Copy the code below to the Arduino development environment, compile and upload to the Arduino board. When the board is powered the processor will first run setup(). The only thing setup() does is telling the processor that we want connection 2 to be an output. All digital connections on the Arduino board are set to be inputs at startup.

After this the loop()function takes over. This function will be called over and over again infinitely until the power to the board is removed or you push the reset-button. In loop() we first turn on the LED by setting connection 2 to high (5 Volts). Then we wait for half a second (500 ms). Then we turn the LED off by writing a zero on connection 2 and then we wait half a second again. After this the program ends, but as previously noted the loop() function will automatically be called again meaning that the program starts all over again, and so the LED will flash once per second.

The code

void setup() {
  // Set pin 2 to be an output.
  pinMode(2, OUTPUT);
}

void loop() {
  // Turn the LED on.
  digitalWrite(2, HIGH);
  // Wait half a second (500ms).
  delay(500);
  // Turn the LED off.
  digitalWrite(2, LOW);
  // Wait another half a second before the program restarts.
  delay(500);
}

 

Pin It on Pinterest

Share This