Input button in LPC2148


In the last tutorial we saw how to interface LCD with the microcontroller. If you guys have noticed, till now we only programmed the controller in a way so that our pins are always output. So today we are going to make one of our pin as input and attach a button to it.

If the button is pressed, LED will turn ON and will turn OFF on releasing the button.

While assigning a pin as an input, always remember that we have to use Pull-up resistor. Pull-up resistor is just like a normal resistor, connected between the line (the input) and Vcc. So by default, it pulls the line high or you can say input is high (1).

Coming back to button, as you can see in the figure below, a 100k resistor is connected between Vcc and the line.

Here by default line or input voltage will be high but as we press the button, the current will flow to the ground via sw1 and our input will be low. We can assign some function for this and wait for pin to go low, as soon that happens, another pin will go high and a LED will glow.

Some insight into the Code:-

Check if button is pressed..
    if (!(IOPIN0&(1<<0)))    // if the button is pressed
IOSET0 |= (1<<1);    // turn on green LED
The '!' sign in the if statement is also called 'not equal to'. So the if statement goes like this- if Port0.0 is not equal to 1, do the following.


#include <lpc214x.h>

int main ()
 PINSEL0 = 0;    // setting PINSEL as normal GPIO
 IODIR0 |= (0<<0)|(1<<1)|(1<<2);  //setting pin 0 as input and pin 1 and 2 a o/p
 while (1)
  if (!(IOPIN0&(1<<0)))    // if the button is pressed
   IOSET0 |= (1<<1);    // turn on green LED
   IOCLR0 |= (1<<2);    // turn off red LED
   IOSET0 |= (1<<2);    // turn on red LED
   IOCLR0 |= (1<<1);    // turn off green LED


Initially, when button is not pressed,  RED led is on and green is off.

When button is not pressed

After button is pressed, RED led is off and GREEN led is on

When button is pressed

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x