5% OFF Over $19, Code: Elephant5; 10% OFF Over $59, Code: Elephant10



Arduino UNO R3/MEGA 2560 R3 Project 6: Button-controlled LED

Posted by Fiona Su on

Introduction

I/O port means interface for INPUT and OUTPUT. Up until now, we have only used its OUTPUT function.
In this experiment, we will try to use the input function, which is to read the output value of device connecting to it. We use 1 button and 1 LED using both input and output to give you a better understanding of the I/O function.
Button switches, familiar to most of us, are a switch value (digital value) component. When it's pressed,the circuit is in closed (conducting) state.

Hardware Required

  • Arduino board *1
  • Button switch*1
  • Red M5 LED*1
  • 220Ω resistor*1
  • 10KΩ resistor*1
  • Breadboard*1
  • Breadboard jumper wire *6

Video Tutorial

Connection

Connection for arduino uno REV4:

thumb

Connection for arduino mege 2560 R3:

thumb

Sample Code

Now, let's begin the compiling. When the button is pressed, the LED will be on. Based on the previous study, the coding may be easy for you. In this program, we add a statement of judgment. Here, we use an if() statement.
Arduino IDE is based on C language, so statements of C language such as while, switch, etc. can certainly be used for Arduino program.
When we press the button, pin 7 will output high level. We can program pin 11 to output high level and turn on the LED. When pin 7 outputs low level, pin 11 also outputs low level and the LED remains off.

int ledpin=11;// initialize pin 11
int inpin=7;// initialize pin 7
int val;// define val
void setup()
{
pinMode(ledpin,OUTPUT);// set LED pin as “output”
pinMode(inpin,INPUT);// set button pin as “input”

}
void loop()
{
val=digitalRead(inpin);// read the level value of pin 7 and assign if to val
if(val==LOW)// check if the button is pressed, if yes, turn on the LED
{ digitalWrite(ledpin,LOW);}
else
{ digitalWrite(ledpin,HIGH);}
}

Result

When the button is pressed, LED is on, otherwise, LED remains off. So the button controlled LED experiment is completed.

thumb 

The simple principle of this experiment is widely used in a variety of circuit and electric appliances. You can easily come across it in your everyday life. One typical example is when you press a certain key of your phone, the backlight will be on.

All Tutorial

Buy 

0 comments

Leave a comment

Please note, comments must be approved before they are published