Arduino Starter’s Guide (1/7): Blinking LED

LEDs (Light Emitting Diodesare used in all clever things,so we have included them in this kit. We start with something very simple by repeatedly switching on and off and creating pleasant flashing effect. To get started, grab the parts listed below, pin the layout sheet to your breadboard and then plug everything in.

Hardware Used:

  • 1 – 100 ohms Resistor

  • 1 – LED
  • 
    
  • 1 – Arduino Uno
  • 
    
    

    You can buy all this Hardware at Createlabz.

    Software Used:

    Arduino IDE

    Set-up the Hardware:

     

    Code:

     

    /*
      Blink
    
      Turns an LED on for one second, then off for one second, repeatedly.
    
      Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
      it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
      the correct LED pin independent of which board is used.
      If you want to know what pin the on-board LED is connected to on your Arduino
      model, check the Technical Specs of your board at:
      https://www.arduino.cc/en/Main/Products
    
      modified 8 May 2014
      by Scott Fitzgerald
      modified 2 Sep 2016
      by Arturo Guadalupi
      modified 8 Sep 2016
      by Colby Newman
    
      This example code is in the public domain.
    
      http://www.arduino.cc/en/Tutorial/Blink
    */
    
    // the setup function runs once when you press reset or power the board
    
    int ledPin = 13;
    void setup() {
      // initialize digital pin LED_BUILTIN as an output.
      pinMode(ledPin, OUTPUT);
    }
    
    // the loop function runs over and over again forever
    void loop() {
      digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);                       // wait for a second
      digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);                       // wait for a second
    }

     

    Code Breakdown:

     

    (/*) (*/)

    If you want to put Comments, Notes you can use (/*) and it should end with (*/)

    //

    The difference of (//) and (/*,*/) is that the (//) is used for small comments just like for one line only, and the (/*,*/) is for long comments.

    int ledPin = 13;

    is a declaration to say “that we would like to create a box named ledPin and put the number 13 in that box.” Notice that int indicates that the type of box is an integer. After the declaration, you use ledPin where you had 13 before.

    You can choose on whatever Pin you want to assign the led. Digital Pins has (0-13), you can also use Analog Pins (0-5) analog A0 is 14, analog A1 is 15……. Give it a try so that you will see!

    void setup()

    any statements in this procedure will be executed once at the beginning of your program, when it is uploaded or when the Arduino wakes up after being reset.

    pinMode(ledPin,OUTPUT);

    ledPin was assigned as an output, which means that the voltage will be sent to the assign pin in order to control the LED.

    void loop()

    any statements in this procedure will be executed over and over again forever, or at least until you turn your Arduino off or upload something new. The statements in this procedure are the core of what you want your Arduino program to do.

    digitalWrite(ledPin, HIGH)

     

    Write a HIGH or a LOW value to a digital pin.

    If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.

    If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(), the LED may appear dim. Without explicitly setting pinMode(), digitalWrite() will have enabled the internal pull-up resistor, which acts like a large current-limiting resistor.

    delay(1000)

    the number of milliseconds to wait before continuing on to the next line.

    1 sec = delay(1000)

    Try MORE!!!

    Changing the blink time:

    In the code change the lines:

    digitalWrite(ledPin, HIGH);
    
    delay(time on);  //remember 1 sec = delay(1000)
    
    digitalWrite(ledPin, LOW);
    
    delay(time off); //remember 1 sec = delay(1000)

     

    Control the brightness:

    Change the LED to pin 9:

    int ledPin = 13; -------> int ledPin = 9;

    Replace the code inside the { }’s of loop() with this:

    analogWrite(ledPin, new number);
    
    (new number) = any number between 0 and 255.

     

    Fading:

    Change the LED to pin 9:

    int ledPin = (#) ------> int ledPin = 9;

    Replace all the code in the void loop with this code:

    for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(ledPin, fadeValue);
    delay(30);
    }
    
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    analogWrite(ledPin, fadeValue);
    delay(30);

     

    Fade

     

     

     

    The post Arduino Starter’s Guide (1/7): Blinking LED appeared first on CreateLabz.

    ArduinoArduino kitArduino unoBlinkGuideKnowledgebaseLed

    Leave a comment

    All comments are moderated before being published