Interfacing a push button with the LPC1768 is another simple application like the LED blinking that makes use of the GPIO functionality of the LPC1768. An introduction to the basic functionalities and the GPIO register configuration in the LPC1768 were provided in the tutorial for LED Blinking along with useful tables. Click To Learn More. In this tutorial, we will read a pulse generated by a push button and we will shift and switch ON an LED. For each pulse generated a different LED will be turned ON. This chapter deals with the firmware details of interfacing a push button with LPC1768 microcontroller device.


Our aim here is to read a pulse from a push button interfaced with the GPIO pin – PIN1.24, of the LPC1768 ARM controller. The output generated will be reflected across 7 different LEDs. In the OpenLab™ platform, PORTD (PIN1.24 to PIN1.31), will be used for this tutorial as the GPIO port. A pulse from the input pin – PIN1.24, which is also the LSB of PORTD will be used to determine the states of the remaining LEDs of PORTD. Each pulse turns on a single LED. On the next pulse, the next LED will be turned ON and the previous LED is turned OFF. This operation continues creating a continuous round shifting sequence. The input pin is pulled up by the software when it is configured as an input pin. When the button is pushed, this PIN is driven LOW and when it is released, the PIN goes back to its default state, thereby making the response of the PIN as ACTIVE-LOW.

Push Button LED Interfacing LPC1768

Image for Illustrative Purpose Only


  • The following definitions will be used in the code for this tutorial.
#define DATA_DIR       LPC_GPIO1->FIODIR
#define DATA_SET       LPC_GPIO1->FIOSET
#define DATA_CLR       LPC_GPIO1->FIOCLR
#define DATA_PIN         LPC_GPIO1->FIOPIN

#define PIN_D0 24
#define PIN_D1 25
#define PIN_D2 26
#define PIN_D3 27
#define PIN_D4 28
#define PIN_D5 29
#define PIN_D6 30
#define PIN_D7 31
  • The first line of code should be to set every pin to its appropriate functionality, using the PINSEL register. In our code, the only operation that we perform with our interfaced pins are GPIO operations, By default, these pins are selected for GPIO function and therefore, it is ok to skip this step.
  • Next, we must set the direction of PIN1.24 as input and PIN1.n as outputs, where n = 25 to 31.
DATA_DIR = (0xFE << PIN_D0);
  • Now, we must continuously check for a pulse from the switch connected to the pin – PIN1.24. Once a pulse is detected, the system encounters an infinite while loop that is used to prevent a common hardware error. This error is caused when the software or the hardware detects multiple pulses upon a single opening or closing event in the circuit. The workaround for this problem is commonly referred to as debouncing. Upon the initial change from the default state (HIGH to LOW in our case), the software detects this change and then waits continuously in a while loop till the hardware goes back to its default state.
    Once the necessary input PIN returns to its default state, we will clear all the previous states from the output PINS and then turn ON the first LED. Initially, the random variable i is given the value of the first pin – PIN_D2. For every successive pulse, the same process continues and the next LED will glow.
        if(!((DATA_PIN >> PIN_D0)&0x01))
            while(!((DATA_PIN >> PIN_D0)&0x01));                // Prevent Debouncing
            DATA_CLR = (0xFE << PIN_D0);
            DATA_SET = (1 << i);
  • Once the final LED is turned on, we must ensure that the next pulse would cause the first LED to glow again. The following line performs this operation.
    if(i > PIN_D7)
        i = PIN_D1;


We have learned a simple program to interface a push button with the LPC1768 to turn ON/OFF, multiple LEDs
The sample code for this tutorial will be available in the Code Library under the section ARM.

Spread the love, share this