Back to Course

Arduino Programming using Arduino IDE

0% Complete
0/0 Steps
  1. Chapter-1->How to download Arduino IDE?
    3 Topics
    |
    1 Quiz
  2. Chapter-2-> Introduction to Arduino IDE
    3 Topics
    |
    1 Quiz
  3. Chapter-3–> How to connect any board with Arduino IDE?
    4 Topics
    |
    1 Quiz
  4. Chapter-4–>Steps to write a code in Arduino Programming
    5 Topics
    |
    1 Quiz
  5. Chapter-5–>Introduction to Arduino Uno
    5 Topics
    |
    1 Quiz
  6. Chapter-6–>Explanation of Variables and operators in programming
    5 Topics
    |
    1 Quiz
  7. Chapter-7–>How to use conditional statement and loops?
    6 Topics
    |
    1 Quiz
  8. Chapter-8–>How to use analog input in Arduino IDE ?
    6 Topics
    |
    1 Quiz
  9. Chapter-9–>How to add libraries in Arduino IDE ?
    6 Topics
    |
    1 Quiz
  10. Chapter-10–>How the Arduino Serial library performs serial communication?
    4 Topics
    |
    1 Quiz
  11. Chapter-11–>Interfacing of Dht11 sensor with Arduino
    4 Topics
    |
    1 Quiz
  12. Chapter-12–>Troubleshooting and Errors in Programming
    3 Topics
    |
    1 Quiz
  13. Assessment
    6 Topics
Lesson 6, Topic 4
In Progress

6.4 Activity 2 : Led on/ off using switch with arduino uno

08/11/2021
Lesson Progress
0% Complete

To make this Led ON/OFF project using switch, first we will make the circuit connection of LED and Push button with Arduino Uno and then we will write a code in Arduino IDE.

Components Required :

LED

Resistor

Push button

Arduino UNO

Longer leg(+ve)

Any of the one terminal of 220 ohm resistor

 

——-

——-

 

——-

Second terminal of 220 ohm resistor

 

——-

Digital Pin 2

Shorter leg(-ve)

   

GND of Arduino via breadboard

 

——-

 

——-

Any One terminal

Digital Pin 4

 

——-

Any of the one terminal of 10k ohm resistor

Opposite terminal

 

——-

 

——-

Second terminal of 10k ohm resistor

 

 

——-

GND of Arduino via breadboard

 

Note : Push button have four terminals and to make connection we just need only two terminals of Push button. To learn more about the basic electronics components refer our basic electronics course.

Coding :

Steps to do coding in Arduino IDE

1. Open Arduino IDE from the desktop.You will be see the following page when you will open Arduino IDE:
2. Now, clear the page and then for writing a code first we will define a variable named “LED” and “button” with integer data type to assign which pin we are using of Arduino UNO.

Expression : int LED = 2;   

                         int button= 4;

Note : Integers(int) are the data type that can store numbers.A data type, that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error. 
3. In void setup ( ) function, we define the pin as input or output.So, a pin (Pin 2) of the Arduino as output for LED and another pin (Pin 4) as input for button. As mentioned in previous topics,we need to use the function pinMode to initialize a pin as input or output. Hence, write the pinMode functions for both LED and button pin in the setup function.

Expression :

void setup() {
pinMode(LED, OUTPUT);

pinMode(button, INPUT);

}

 

4. Now,after initialization of pin,we need to check the status of the switch.To check the status we need to use digitalRead() function.The digitalRead() function is used to to read the value of the digital pin.

Syntax: digitalRead(pin no. or variable name)

In the syntax the pin number or variable name is the number of the digital pin from where you want to read the data and connected the component.

As, we need to read the status of the button pin i.e. 4th pin . Create a variable before setup and name it as “buttonstate”, so that the value returned by the digitalRead function can be captured. Now in the void loop() function, assign the return value of the digitalRead of  4th pin to the buttonstate variable.

Expression:

void loop() {

buttonstate = digitalRead(4);

In the void loop() function, we write the main code and it run over and over again.

5. Next,we have to control the LED as per the button state.To control the Led we have to use if- else statement.

If-Else statements are the conditional statements which is used to check the statement and execute a block of code.If the condition in the “If statement” is true then the code in the if runs and if it is false then the else block of code runs.

Buttonstate variable gives to values HIGH or LOW.So, in the if statement we will compare buttonstate with LOW because Arduino will detect the logic LOW when the button is pressed. If the button is pressed or or the if stetement is true then the LED will glow using digitalWrite() function as done earlier and else the logic is false the LED will not glow.

Expression:

if (buttonstate==LOW)

{

digitalWrite(LED, HIGH);

}

else 

{

digitalWrite(LED, LOW);

}

 

Note : Lines mentioned in the “// ” are the comments.These comments are written for the explanation of program to programmer.