Arduinoで3つのLEDを点滅させてみた

先日Arduinoというものを知り、↓これを買ってみました。

Arduinoをはじめようキット

Arduinoをはじめようキット

まず、ブレッドボードについて、
電子工作入門:ブレッドボードを使ってみよう
で勉強。
そして↓を見ながら実験。
SecondWave:Arduinoを始めよう
SecondWave:ArduinoでLEDを光らせる

ブログの最後に「次回は、さらにLEDを増やしてみたいと思います。」と
書かれているけど、以降書かれていないようなので自分で試してみました。

スケッチ

まずはスケッチ。LED2個を交互に光らせるコードはあるので、
LEDを3個にするのは簡単。

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
  This example code is in the public domain.
 */

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
  pinMode(12, OUTPUT);     
  pinMode(11, OUTPUT);     
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  digitalWrite(12, LOW);   // set the LED on
  digitalWrite(11, LOW);   // set the LED on
  delay(500);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  digitalWrite(12, HIGH);   // set the LED on
  digitalWrite(11, LOW);   // set the LED on
  delay(500);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  digitalWrite(12, LOW);   // set the LED on
  digitalWrite(11, HIGH);   // set the LED on
  delay(500);              // wait for a second
}


回路

次は回路。今までのサンプルを見て、LEDの-極(短い方)を集約してGNDに繋げてやればよさそう。
ブレッドボード上の4番のラインにLED1(赤)の+極、
5番のラインにLED1(赤)の-極、
5番のラインにLED2(緑)の-極、
5番のラインにLED3(青)の-極、
6番のラインにLED2(緑)の+極、
7番のラインにLED3(青)の+極を差す。
(ラインの番号は何番からはじめてもOKです。)
基板上の11番ピンと7番のラインを、
12番ピンと6番のラインを、
13番ピンと4番のラインを、
GNDと5番のラインをジャンパー線でつなぐ。

これで上手く3つのLEDが交互に点滅するようになりました!
ちなみにLEDの足の長さが足りない場合は、
別のラインに-極を差して、そのラインと5番のラインをジャンパー線で
つないでやれば大丈夫でした。