How Turn On/Off an LED using Arduino

Posted on

How Turn On/Off an LED using Arduino – This is an article made specifically for you beginners in Arduino. By default, the Arduino board has an internal LED. This LED is connected to PIN 13. So we will try to make this pin produce HIGH and LOW logic for one second.

In theory, the voltage with HIGH logic is 5 Volt and the LOW logic is 0 Volt. But in practice, not all HIGH logic is 5 Volts, maybe the voltage is 4, 4.5 or 4.8 Volt. But in the microcontroller, it still reads as HIGH logic.

 

1. Turn on/off the Internal LED Arduino

Now please connect Arduino to your PC, then open the Arduino IDE and follow these steps:

  • Open the File menu> Examples > 0.1 Basics > Blink
    Then you will see a program like this:
/*
  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
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}
  • Please click the Tools menu> Board.
  • Select the Arduino / Genuino Uno list.
  • Select your Arduino USB Port.
  • Select the AVRISP mkll programmer.
  • Then back to the menu, click Upload as shown in the image below.
    How Turn On/Off an LED using Arduino
Read other articles  Arduino and RTC DS3231 Easy Way

 

  • If the upload process has finished, look at to the LEDs on your Arduino, then you will see the LED turn over in approximately 1 second.
  • This 1-second timing is determined by the delay value (1000). If you want the LED to turn alternately in 2 seconds, change the delay to delay (2000).
  • LED_BUILTIN is command for define pin 13 in Arduino.

 

2. Turn on/off the External LED Arduino

In real projects, it is very rare for us to use internal LEDs. We will use the External LED more often, because we can change the position of the LED. For schematics, please see the following picture.

 

correct resistor value for LED

Program Turn On/Off an External LED using Arduino:

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(7, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(7, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Please connect resistor on pin D7 and we use a resistor with a value of 330Ohm. Why do we use a resistor? for more information about this, please read the article How to choose the correct Resistor for LEDs.

To turn on the External LED, please follow the steps from Turning on the Internal LED and upload the program to Arduino, then look at your Arduino. If the external LED is on, you have successfully assembled it. If it doesn’t turn on, check the circuit again.

Leave a Reply

Your email address will not be published. Required fields are marked *