As an Amazon Associate we earn from qualifying purchases.
Reading a push button and switching an LED is the first real input and output exercise on any microcontroller board. This tutorial does it on an ARM Cortex-M4 board, the Tiva C series TM4C123G LaunchPad, using the Energia IDE. It needs no extra parts, because the board already has two push buttons and an RGB LED built in.
When you press a button, its matching LED turns on. When you release it, the LED turns off. The same pattern is behind many simple controls, such as indicators, mode selectors and basic user interfaces.
What you need
- A Tiva C series TM4C123G LaunchPad (ARM Cortex-M4).
- A USB cable to power and program the board.
- The Energia IDE, installed with the board support for the LaunchPad.
If you have not used the board before, start with our notes on LED basics on the Tiva C. They cover installing Energia and uploading your first sketch.
How the on-board buttons and LEDs are connected
Everything in this example sits on port F of the microcontroller. In Energia, pins are written as PF_0, PF_1 and so on.
- PF_4: push button SW1
- PF_0: push button SW2
- PF_1: red LED
- PF_2: blue LED
- PF_3: green LED
The three LEDs are one RGB LED, so you can also mix colors by turning on more than one at a time. The buttons connect the pin to ground when pressed. That matters for the code, as explained below.
Why INPUT_PULLUP is used
A button pin that is not pressed must still have a defined voltage. If it is left floating, it can pick up noise and read randomly. The INPUT_PULLUP mode turns on an internal resistor that pulls the pin up to the supply voltage.
With the pull-up enabled, the pin reads HIGH when the button is released and LOW when it is pressed, because pressing connects the pin to ground. This is why the program checks for == 0 to detect a press. Beginners often expect the opposite.
The code
This sketch uses the two on-board buttons and two of the on-board LEDs. The button on PF_0 controls the red LED, and the button on PF_4 controls the blue LED.
void setup()
{
pinMode(PF_0, INPUT_PULLUP); //push button SW2
pinMode(PF_1, OUTPUT); //red led
pinMode(PF_4, INPUT_PULLUP); //push button SW1
pinMode(PF_2, OUTPUT); //blue led
}
void loop()
{
if(digitalRead(PF_0)==0) //checks whether SW2 is pressed
digitalWrite(PF_1,HIGH); //turn on red led if SW2 is pressed
else {digitalWrite(PF_1,LOW);}
if(digitalRead(PF_4)==0) //checks whether SW1 is pressed
digitalWrite(PF_2,HIGH); //turn on blue led if SW1 is pressed
else {digitalWrite(PF_2,LOW);}
}
How the program works
setup()
This function runs once when the board starts. It sets the two button pins as inputs with the internal pull-up, and the two LED pins as outputs.
loop()
This function repeats forever. Each pass does two checks:
- Read PF_0. If it is LOW, the button is pressed, so PF_1 is set HIGH and the red LED lights. Otherwise PF_1 is set LOW and the LED goes off.
- Read PF_4. If it is LOW, PF_2 is set HIGH and the blue LED lights. Otherwise it is set LOW.
The two buttons are independent, so you can hold both and both LEDs will be on. Because the loop runs very fast, the LED reacts as soon as you press or release a button.
Upload and test the sketch
- Connect the LaunchPad to your computer with the USB cable.
- Open Energia and select the Tiva C LaunchPad board that matches your model, under the Tools menu.
- Select the correct serial port.
- Paste the code, then click Upload.
- Press the two buttons and watch the red and blue LEDs.
If it does not work
- No LED reacts: check that the right board and port are selected, and that the upload finished without errors.
- The LED is on without a press: the logic may be reversed. With
INPUT_PULLUP, a pressed button reads 0, so the check must be== 0. - Wrong color lights up: the LED pins are PF_1 for red, PF_2 for blue and PF_3 for green. Compare your pins with the list above.
- The PF_0 button is unreliable: PF_0 is a special-function pin on this chip. It works as a button on the LaunchPad in Energia, but if it does not respond on your setup, test with the PF_4 button first.
Ways to extend the project
- Use the green LED: add a third output on PF_3 and light it when both buttons are pressed.
- Toggle instead of hold: keep a variable that stores the LED state and flip it on each press, so the LED stays on until the next press.
- Add debouncing: mechanical buttons bounce for a few milliseconds. A short delay or a time check prevents false extra presses, which matters most when toggling.
- Use interrupts: instead of checking the button constantly, you can react to a change on the pin. Our article on using a button interrupt shows the idea on Arduino, and the concept carries over.
- Use an external button and LED: move the same logic to a breadboard with a resistor in series with the LED.
Related reading
The logic is the same on most boards, only the pin names and tools change. If you also work with Arduino, our guide on how to read a button with Arduino and the article on button-controlled LED projects show the same idea with different hardware. For a PIC-based version, see push button interfacing with dsPIC30F2010 and Mikro C.
FAQ
Do I need resistors for the on-board buttons and LEDs?
No. The LaunchPad already includes the current-limiting parts for its LEDs, and the buttons use the chip’s internal pull-up resistors. You only need resistors when you add your own external LEDs.
Why does a pressed button read 0 instead of 1?
The button connects the pin to ground when pressed. With the pull-up enabled, the idle state is HIGH, so a press pulls the pin LOW.
Can I use this code on Arduino?
The structure is the same, but the pin names are different. Replace PF_0, PF_1 and the others with Arduino pin numbers, and wire an external button and LED with a resistor.
Is Energia the only way to program this board?
No. The TM4C123G can also be programmed with vendor tools such as Code Composer Studio or Keil. Energia is simply the easiest start, because it uses the Arduino-style functions seen in this sketch.