arduino‎ > ‎

lesson1flashingled

Flashing Lights


Overview


In this tutorial you'll create a simple circuit that can turn a light on and off. Below you'll find a diagram and the code.



Set up

  1. Open your Arduino software on your laptop

  2. Use a USB wire to connect your Arduino board to your computer

  3. Select the Arduino under Tools > Port


Schematic


When you setup the Arduino it's very important that all the wires go into the same spots as shown below. Also LEDs can only work on one direction. The long side should be connected to pin 7.


Things connect on the breadboard by sharing the same column. In the below schematic one side of the LED is connected to pin 7 and the other is connected to GND (ground by way of a resistor).


schematic.png


Code


In this project we're going to learn two new commands.

  1. pinMode

The pinMode command tells the Arduino if a in is input or output. To use it we tell it first a pin on the Arduino and second INPUT or OUTPUT. Here is an example...

// here we set pin 7 to OUTPUT
pinMode(7, OUTPUT);

// Here we set pin 4 to INPUT
pinMode(4, INPUT);

  1. digitalWrite

The digitalWrite command either sends or stops sending electricity to a pin. To use it we tell it first a pin on the Arduino and second HIGH or LOW where HIGH means ON and LOW means OFF. Always remember that we cannot write to a pin that isn't set to output.

// Here we turn pin 7 on.
digitalWrite(7, HIGH);

// Here we turn pin 4 off.
digitalWrite(4, LOW);

With these two commands use the code below to program your arduino! Simply copy this file into your Arduino editor and upload it!




//                 FLASHING LED TUTORIAL                //

// **************************************************** //

//                                                      //

// Before you run this program make sure you have a led //

// connected to pin 7 of your Arduino.                  //

//                                                      //

// To connect the led connect the long end to pin 7 and //

// the short end to a resistor going to ground (GND)    //

//                                                      //

// **************************************************** //

// RESISTOR = Resistor (100 ohm)                        //

// LED = led (light emitting diode)                     //

// **************************************************** //

//                                                      //

//                                       +-----+ //

//          +----[PWR]-------------------| USB |--+     //

//          |                 +-----+ | //

//          | GND/RST2  [ ][ ] |    //

//          | MOSI2/SCK2  [ ][ ] A5/SCL[ ] | .   //

//          | 5V/MISO2 [ ][ ]  A4/SDA[ ] | . //

//          |                  AREF[ ] | //

//          |                   GND[ ] | //

//        . | [ ]N/C                    SCK/13[ ] | . //

//        . | [ ]v.ref                 MISO/12[ ] | . //

//        . | [ ]RST                   MOSI/11[ ]~| . //

//        . | [ ]3V3    +---+      10[ ]~| . //

//        . | [ ]5v    | A |       9[ ]~| . //

// RESISTOR | [@]GND   -| R |- 8[ ] | .   //

//        . | [ ]GND   -| D |-              | //

//        . | [ ]Vin   -| U |-         7[@] | LED //

//          | -| I |-               6[ ]~| . //

//        . | [ ]A0   -| N |-         5[ ]~| . //

//        . | [ ]A1   -| O |-         4[ ] | . //

//        . | [ ]A2    +---+  INT1/3[ ]~| . //

//        . | [ ]A3                    INT0/2[ ] | . //

//        . | [ ]A4/SDA  RST SCK MISO  TX>1[ ] | . //

//        . | [ ]A5/SCL  [ ] [ ] [ ]  RX<0[ ] | . //

//          | [ ] [ ] [ ]              | //

//          | UNO_R3 GND MOSI 5V  ____________/ //

//           \_______________________/                  //

//                                                      //

// **************************************************** //







void setup() {

// **************************************************** //

//                    SETUP FUNCTION            //

// **************************************************** //

// Put your setup code here, this code will only be run //

// once and is used to tell the Arduino where all the   //

// wires are connected.                                 //

// **************************************************** //



// In this line we tell the Arduino the pin 7 (our led)

// is an output. We'll use this to toggle the led.

pinMode(7, OUTPUT);

}



void loop() {

// **************************************************** //

//                     LOOP FUNCTION          //

// **************************************************** //

// Any code here will be run over and over again        //

// instantly unless we tell the program to delay itself //

// here we turn on the led delay turn it off and then   //

// delay again. Try it and see what happens!            //

// **************************************************** //



// Turn on the led

digitalWrite(7, HIGH);


// Delay one second

delay(1000);


// Turn off the led

digitalWrite(7, LOW);


// Delay one second

delay(1000);

}


upload.png






Stretch Goals

  1. Can you get the LED to flash twice as fast?

  2. Can you get the LED to flash for one second every ten seconds?