Arduino Turn LED On And Off with Button or Switch Control Digital Input with digitalRead Function

Arduino Turn LED On And Off With Button

Controlling a LED with button is one of important learning because it cover “How to take digital input”. Second learning is if decision logic application.

Arduino Turn LED On And Off With Button code uses three main functions digitalWrite, digitalRead and pinMode. Digital input pins are used to interface the switch and LED for control on and off.

To implement this experiment you have to interface switch or button with Arduino on pin number 12 and we use build-in LED on pin number 13.

Example 1 – Arduino LED Control by Button Project Code, LED on pin number 13 and Switch on pin number 12.

/*
	Arduino LED Control With Switch 
	https://elextutorial.com
	Turns on LED by pressing a switch or button. 
*/

void setup() 
{  
	pinMode(13, OUTPUT);	// set pin 13 as an output, the pin having onboard LED.
	pinMode(12, INPUT);	// set pin 12 as an input.
}

void loop() 
{
	int x = digitalRead(12); // read the pin number 12
	if( x == 1)
	{
		digitalWrite(13, HIGH); // turn the pin 13 HIGH means on (set)
	}else
	{
		digitalWrite(13, LOW); // turn the pin 13 LOW means off (reset)
	}
	delay(100); // delay for 100 milli second
}
				 

In the setup() function we are assuming that the switch is on pin 12 and we have to set pin 12 as input. And to use LED we have to set pin 13 as output.

In the loop() function we first read the pin 12 in the integer variable and then test the variable in the if logic. If the read value is 1 then on the LED and value is 0 off the LED.

And finally we use delay() function to insert a delay of 100 milli second to read the switch again.

Arduino pinMode Function

All the digital pins can be used as input or output in Arduino. And we have to set the pin 13 into output mode and pin 12 into input mode with the help of pinMode function.

Set pin 13 into output mode with pinMode Function

pinMode(13,OUTPUT);

Set pin 12 into input mode with pinMode Function

pinMode(12,INPUT);

Arduino Digital Input with digitalRead Function

Arduino library have a function called digitalWrite and this function is used to read the pin. This function is used to take the digital input.

digitalWrite() function return a boolean value based on the state of the pin. If the pin is set to +5 volt it will read HIGH otherwise read LOW.

Read the pin 12 into int variable x with digitalWrite() Function

int x = digitalRead(12);

Arduino Digital Switch Interfacing

Interfacing a switch or button with Arduino is little bit tricky and involve some circuit making.

The figure 1 below shows the circuit diagram of switch interfacing. When the switch is not pressed the pin will read as 1, because the current flows from vcc to resistance. In case when switch is pressed the pin will read as 0, because switch is short circuit to ground.

Arduino LED Control with Switch or Button Interfacing with Digital Input Pins
Fig.1 – Arduino Switch or Button Interfacing with Digital Input Pins

Caution: It is necessary to connect with resistance, you can not connect directly with VCC. The value of resistance may be 10k ohm. If value of the resistance lower than there is a risk of damage of the pin.

The figure 2 below shows the circuit diagram of switch interfacing with Arduino on breadboard.

Arduino LED Control with Button or Switch Interfacing as Digital Input with digitalRead Function
Fig.2 – Arduino LED Control with Button or Switch Interfacing on Pin Number 12

Switch Interfacing and Concept of the Pullup Resistance

The resistance connected with switch is known as pullup resistance, because it provide the current to the device to read high or 1. The “LOW” to “HIGH” logic transition of digital signal is known as “PULL UP” action and “HIGH” to “LOW” logic transition of digital signal is known as “PULL DOWN” action.

Example 2 – Arduino LED Control Code.

/*
	Arduino LED Control With Switch 
	https://elextutorial.com
	
*/

void setup() 
{  
	pinMode(13, OUTPUT);	// set pin 13 as an output, the pin having onboard LED.
	pinMode(12, INPUT);	// set pin 12 as an input.
}

void loop() 
{
	int x = digitalRead(12); // read the pin number 12
	digitalWrite(13, x); // turn the pin HIGH / LOW based on x
	delay(100); // delay for 100 milli second
}
				 

digitalRead function return boolean value (0 or 1) and can be used directly used in the digitalWrite.

You can leave a response, or trackback from your own site.
Leave a Reply to the article



Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape