Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob

How to control LEDs with tactile switches

How to control LEDs with tactile switches

25 sign-ins 5 questions asked 10 sign-ins

The evaluation board AURIX TC375 Lite Kit contains one tactile switch (push button) and two LEDs which are accessible by users. In this article, we will create a project to control LEDs with a tactile switch.

 Identify GPIO

The tactile switches and LEDs are connected to the following General Purpose Input/Output (GPIO) pins:

Pin name









You can use the pin name, but using the function name makes the code easier to read. Therefore, we will declare macros as follows.

#include "IfxPort_PinMap.h" #define LED1 IfxPort_P00_5#define LED2 IfxPort_P00_6#define BUTTON1 IfxPort_P00_7

The header file "IfxPort_PinMap.h" has macro declarations starting with "IfxPort_" and must be included.

Controlling LEDs with GPIOs

The pin associated with an LED is connected to VEXT (3.3V) via the LED and a current limiting resistor. Therefore, the LED is turned on when the GPIO outputs low and it turns the LED off when high level is applied to the GPIO output.



The pins connected to the LEDs must be initialized as an output using the IfxPort_setPinModeOutput() function. 

IfxPort_setPinModeOutput(LED1.port, LED1.pinIndex,       

IfxPort_OutputMode_pushPull, IfxPort_OutputIdx_general);   

IfxPort_setPinModeOutput(LED2.port, LED2.pinIndex,        IfxPort_OutputMode_pushPull, IfxPort_OutputIdx_general);

The third argument selects the output mode, which can be either "pushPull" or "openDrain".

The fourth argument specifies which functional block controls the outputs. "general" is used to control the output by software.

Two functions are provided for the Low-level and High-level outputs, respectively.

IFX_INLINE void IfxPort_setPinLow(Ifx_P *port, uint8 pinIndex)

IFX_INLINE void IfxPort_setPinHigh(Ifx_P *port, uint8 pinIndex)

 These functions are used to turn on and off LEDs.


Detecting the state of the tactile switch

The tactile switch is mounted with a pull-up resistor to VEXT (3.3V) and a capacitor between GND.


When the tactile switch is released, the input pin is pulled to high level via the pull-up resistor. When the tactile switch is pressed, the input pin is pulled to low level (GND, ground) via the tactile switch. The capacitor acts as a filter to suppress multiple high-low transitions caused by the physical bouncing of the switch when the switch moves into a new position.

The pin must be initialized as an input using the IfxPort_setPinModeInput() function.

 IfxPort_setPinModeInput(BUTTON1.port, BUTTON1.pinIndex,        IfxPort_InputMode_pullUp);

The third argument "IfxPort_InputMode_pullUp" indicates an input pin with the built-in weak pull-up device enabled. Since the evaluation board has an external pull-up resistor at the pin, "IfxPort_inputMode_noPullDevice" indicating no internal pull device implemented will also work.

Use the IfxPort_getPinState() function to capture the state of the input pins.

IFX_INLINE boolean IfxPort_getPinState(Ifx_P *port, uint8 pinIndex)

This function returns FALSE if the input pin is at a low level and TRUE if it is at a high level.

Controlling process in the main loop

Write the controlling process in the main loop after the initialization. Here, the program turns on or off the LED according to the state of the tactile switch.

    while(1) {        /* Check button state and change the LED state */        if (IfxPort_getPinState(BUTTON1.port, BUTTON1.pinIndex)) {            /* BUTTON1 released */            IfxPort_setPinHigh(LED1.port, LED1.pinIndex); /* Turn OFF*/            IfxPort_setPinLow(LED2.port, LED2.pinIndex); /* Turn ON */        } else {            /* BUTTON1 pushed */            IfxPort_setPinLow(LED1.port, LED1.pinIndex); /* Turn ON */            IfxPort_setPinHigh(LED2.port, LED2.pinIndex); /* Turn OFF*/        }    }

Repeat the process of detecting the state of the tactile switch in the loop and setting the LED to turn on or off.




High (Released)

High (OFF)

Low (ON)

Low (Pushed)

Low (ON)

High (OFF)