Getting Started with Arduino Uno: Controlling LED with Push Button

Published  June 1, 2015   0
Aswinth Raj
Author
Arduino LED Project for Beginners

The name Arduino is typically buzz word among most of the electronics students or hobbyists alike. Its ability to build things faster and cheaper with a massive online community support has made it an ideal choice for many people who are just getting started with electronics or programming. Because of its simplified hardware design and programming environment, it is possible even for people without any electronics or computer science background to learn it with ease. So what actually is this Arduino? How can you get started with it? What can you make with it to improve your lifestyle?

All these questions will be tried to be answered in this tutorial, as we step through. We will set up the Arduino IDE on our PC/Laptop and upload a sample blink program to the Arduino. Then we will also build a small hardware using a bread board with a simple circuit consisting of a switch and LED and program our Arduino according to it. Sounds interesting enough?!!

 

What is Arduino?

Before we start learning Arduino, we should first know what it is. This is important because there is a common misconception among people that Arduino is a microcontroller. This is not true, let’s make it clear here that Arduino is not a microcontroller. So what is it?

Arduino is an open-source development platform which consists of an easy to use hardware and a programming environment. Here the easy to use hardware refers to the Arduino UNO and the programming environment refers to the Arduino IDE. There are many hardware board other than Arduino UNO like the Arduino Mega, nano, mini, etc.. But for the purpose of this tutorial, we will stick with only Arduino UNO. The Arduino IDE is the software using which we will program the Arduino UNO board.

 

Download, Install, and Setup Arduino IDE

The first step in our process will be to set up the Arduino IDE on your Laptop/PC. The below instructions are direct for windows users only, for other platforms the procedure is almost the same. If you get some problem Mac users and Linux users can use the respective links. Also make sure you have the admin rights of the computer for easy installation.

Step 1: Download Arduino IDE from the official Arduino website, by simply clicking on the link below-

 https://www.arduino.cc/download_handler.php

Step 2: This will download an exe file, which will be the latest Arduino IDE of your time. By the time I am documenting this, the latest version is Arduino-1.8.5 and the size of the file is 90.4MB. There is a very good chance that it got updated by the time you are trying this out.

Once the download is complete, launch the exe file. You might be asked for admin rights, if prompted click on yes.

Step 3: Click on “I Agree” to agree to the License Agreement of Arduino.  

Step 4: Next Under Installation options, make sure all the checkboxes are ticked as shown below and then click on next.

Installation Arduino Software

 

Step 5: Now, we have to select the location at which the IDE has to be installed. By default, it will be installed under Program files directory of C drive. It is high recommended to leave it as such and click on Install

Install Arduino

 

Step 5: You should see the IDE getting installed on your computer. Wait till the progress bar is completed. The screen will be something like this shown below. Once completed it will show “completed” then click on the close button.

Arduino Install

 

Step 6: After closing the installer. Go to your desktop and find the Arduino exe file and launch it. It will show a boot up label and then open the IDE with a bare minimum code on it as shown below

Setting up Arduino IDE

 

Connecting your Arduino board with Computer:

Once the Arduino IDE is installed on our computer, the next step will be to connect the Arduino UNO board with computer. To do this, simply use the programming cable (blue colour) to connect your Arduino board with the USB port of your computer. 

This blue colour programming cable has three function in total which are listed below-

1. It provides the required power for the Arduino UNO to operate, so you can run your projects just by directly powering them though the USB cable

2. It programs the ATmega328 microcontroller on the Arduino UNO board. The program you write on the IDE is sent into the microcontroller through this cable

3. It acts as a serial communication cable; it can talk to the computer through serial communication acting as a good debugging tool. You will understand more about this as we dig deep.

 

Once the board is powered, you will notice a small LED staying high. This is to indicate that the board is supplied with power. You should also notice another LED blinking due to the default blink program which was already uploaded by the vendor on your Arduino UNO

Since this is the first time you are connecting your board to the computer, it might take some time for the drivers to begin installation automatically. Let’s check if the board was discovered successfully by our computer. To do this search for “Device manager” on your computer.

Search for device manager

 

After opening device manager there will an option called “Ports (COM & LPT)” click on it and check if the board is listed under that option as shown below

Check for Arduino UNO in PORTS

 

Note: The Port name for my Arduino board has appeared as Arduino Uno, the name of your Arduino might differ based on the board vendor. It can be CCH450 or something like that so do not worry about the name of the port.

If you cannot find the option called “Ports (COM & LPT)” it means your board is not discovered. In that case it is a driver problem, so you have to manually install the correct drivers for your board.

In some cases you will find more than one COM port listed under the ports section and you will not know which one is for the Arduino board since the naming will also be different. In that case just disconnect the board and connect again. Check which COM port is disappearing and appearing again, this COM port is your Arduino UNO.

Once you have found the correct COM port note down the COM port number of your board. Here for my board the COM port number is 13. This number will change each time you change the USB port to which the board is connected to.

 

Uploading the Arduino LED Blink Code

Now, let’s upload our first program to the Arduino board through the Arduino IDE that we just downloaded. The Arduino IDE when installed comes with some example programs which comes very handy for beginners. So let’s open one of the example programs by choosing File -> Examples -> Basics -> Blink as shown below

Upload the blink program

 

This will open the Blink program; the objective of the program is to blink the Led on the Arduino board. Once the program is opened, we have to select the correct board. To select the board that we are using choose Tool -> Boards -> Arduino UNO/Genuino as shown below

Select for Arduino UNO as your Board types

 

Next, we have to select the correct COM port for our board. We already noted that the COM port for my Arduino was COM13. It could have been different for you. To select the Com port choose Tools -> Port -> COM13

Check on PORTS that Arduino is connected or not

 

If everything is correct you should notice Arduino UNO on COM 13 as the bottom of the screen. After verifying that click on the upload icon (highlighted in Blue) to upload the code to the Board as shown below

Upload Blink program to Arduino UNO

 

Once the button is pressed, you will notice “Compiling sketch” and then, if the code is uploaded successfully you will see a message as “Done Uploading” as shown below

Check that uploading is done or not

 

That is it we have uploaded the first program to the Arduino board with success. But, what is it? What have we done? What is the output of the program? To know the answer of all these questions let’s build a small hardware using which we can glow an LED when a button is pressed and write the program all by our self from scratch

 

Materials Required for Arduino LED Project

The materials required for this project are

  • Arduino UNO
  • Programming Cable
  • Push Button
  • LED (any color)
  • 1k resistor
  • Bread Board
  • Connecting wires

 

Hardware Connection:

The complete connection diagram of the set-up is shown below. You just have to follow the figure to make the connections as such.

Arduino LED Project Circuit

 

In our hardware the push button is input which is connected to the second pin of the Arduino. If you take a close look you can notice that one side of the button is connected to the Ground and the other side is connected to pin number 2. This means that whenever we press the button the pin number 2 will be connected with ground

Next the LED is connected to pin number 3 through a resistor of 1k. Again the cathode pin of led (the shorted pin) is connected to ground and the anode pin (longer pin) is connected to pin 3 through a resistor. This resistor is called a current limiting resistor as it limits the amount of current flowing through the LED. If this current is not limited excess current will flow through the LED damaging it permanently.

 

Programming your Arduino:

Now that our hardware is ready we can start programming our Arduino UNO board. The complete Arduino LED Blink Code given at the end of this page, further below we are just going through them line by line.

For every Arduino program there should be two functions by mandatory. These two functions are void setup () and void loop (), they are called the bare minimum. Everything that we write inside the void setup () will be executed once and everything that we write inside void loop will be execute over and over again. Both the functions are shown below, this is what you get when you select File -> New.

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

 

Let’s start writing program into the setup () function. Normally the pin declarations will be done inside the setup() function. In our hardware we have to declare that pin 2 is an input pin and the pin 3 is an output pin. This can be done by the following lines

pinMode(2,INPUT);
pinMode (3,OUTPUT);

 

But we have a small change here, since the pin 2 is used as INPUT it should never be left floating. Meaning an input pin should always be connected either to +5V or to ground. In our case the input will be connected to ground only if we press the button and if we don’t press the button it will be left floating. To avoid this we use something called internal pull up resistor. This resistor is present inside the ATmega 328 microcontroller and you cannot see it. To use this resistor we just have to write a line on out Arduino IDE.

This line will connect the pin number 2 +5V through a resistor thus making it to go high when not connected with ground. So we change the keyword INPUT as INPUT_PULLUP as shown below

pinMode(2,INPUT_PULLUP);

 

Now that we have done with our setup () function, let us move to the loop () function. Here we have to check if the pin 2 is grounded (LOW) and if it is grounded we have to make the LED glow by making it pin3 HIGH. If not grounded (else) we have to keep the LED turned off by making the pin 3 as LOW. Let us put these words in program like

  if (digitalRead(2) == LOW)
  {
    digitalWrite(3,HIGH);
  }

  else
  {
    digitalWrite(3,LOW); 
  }

 

Here the term digitalRead() is used to read the status of an INPUT pin. If the pin is connected to ground it will return LOW and if the pin is connected to +5V it will return HIGH.

Similarly, the term digitalWrite() is used to set the status of an OUTPUT pin. If we set the pin as HIGH it will provide +5V as output and if we set the pin as LOW it will provide 0V as output.

 

So for our program, when we press the button pin 2 will be grounded (LOW), so we make pin 3 to output +5V (HIGH). This +5V will be enough to turn on the LED. If this condition is not met then pin 3 will be made 0V (LOW) which will eventually turn off the LED.

 

That is it our program is complete, let us upload the code to our Arduino board, just like how we uploaded the blink code above.

 

Verifying our output:

Once we have successfully uploaded the code to the Arduino board it is time to verify the output of our program. The hardware connection of my Arduino is shown below, to verify the output all we have to do is press the push button and check if the LED is turning on. Then when we release it the LED should turn off.

Arduino LED Circuit

 

If you have any problem in getting this work, you can brief you problem in the comment section for help. Or you can also use the forums for technical help. Hope you understood the tutorial and made your first baby step with Arduino, once you get comfortable with this basic you can dive deep to explore Arduino much more.

After knowing the basics about the Arduino you can try interface the basic components like 16x2 LCD, DC Motor, Servo Motor, Keypad, etc.

Also check our all the Arduino Tutorials and Projects.

Code

void setup() {
pinMode(2,INPUT_PULLUP);
pinMode (3,OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(2) == LOW)
  {
    digitalWrite(3,HIGH);
  }
  else
  {
    digitalWrite(3,LOW);  
  }

}

Have any question realated to this Article?

Ask Our Community Members