As an Amazon Associate we earn from qualifying purchases.
Reading a push button is usually the first step after blinking an LED. It is how a microcontroller starts reacting to the outside world instead of just running a fixed pattern. This article shows how to read digital inputs on an ARM Cortex-M4 board, the Texas Instruments Tiva C Series TM4C123G LaunchPad, using the Energia IDE and its Arduino-style functions.
If you are new to the board, start with the overview of the Tiva C Series TM4C123G. If you have not driven an output pin yet, our digital output example and the first LED blink program make good warm-ups.
What a digital input does
A digital input pin reads one of two states: a high voltage (logic 1) or a low voltage (logic 0). In Energia you read it with digitalRead(), which returns HIGH or LOW. The pin must be configured first with pinMode().
The three modes you can use for an input are:
- INPUT: a plain high-impedance input. If nothing drives the pin, it floats and can read random values.
- INPUT_PULLUP: enables an internal resistor that pulls the pin up to the supply voltage when nothing else drives it.
- INPUT_PULLDOWN: enables an internal resistor that pulls the pin down to ground.
The LaunchPad switches and LEDs
The LaunchPad has two user switches and one RGB LED, all connected to port F. The pins used in the examples below are:
- SW1 (S1): PF_4
- SW2 (S2): PF_0
- Blue LED: PF_2
- Green LED: PF_3
The switches connect the pin to ground when pressed. When released, nothing drives the pin, so you need a pull-up. That is why the code uses INPUT_PULLUP. The pin reads HIGH when the button is idle and LOW while it is pressed. This is also why the code checks for logic zero with !digitalRead().
One detail to keep in mind: PF_0 doubles as a special-function pin on the TM4C123G chip and is locked by default. Energia normally handles this, but if S2 never seems to respond, check the pin setup first. The same pull-up idea applies on other boards. For comparison, see digital input on the MSP430.
Example 1: Blink the green LED when S1 is pressed
This sketch configures PF_3 as an output and PF_4 as an input with pull-up. It blinks the green LED once at start-up as a quick check that the LED works. After that, the loop waits for S1 to be pressed and blinks the LED once for each press.
void setup()
{
pinMode(PF_3, OUTPUT); // setting PF.3 Pin as output
digitalWrite(PF_3, HIGH); // turn the GREEN LED on
delay(1000); // wait for a second
digitalWrite(PF_3, LOW); // turn the GREEN LED off by making the voltage LOW
delay(1000);
pinMode(PF_4, INPUT_PULLUP);
}
void loop() {
if(!digitalRead(PF_4)) //checking for logic zero
{
digitalWrite(PF_3, HIGH); // turn the GREEN LED on
delay(1000); // wait for a second
digitalWrite(PF_3, LOW); // turn the GREEN LED off by making the voltage LOW
delay(1000); // wait for a second
}
}
Example 2: Blink the blue LED when S2 is pressed
The second sketch uses the other switch, S2 on PF_0, and the blue LED on PF_2. The structure is the same: set the pin modes once in setup(), then test the switch repeatedly in loop().
void setup()
{
pinMode(PF_2, OUTPUT); // setting PF.2 Pin as output
pinMode(PF_0, INPUT_PULLUP); // setting PF.0 Pin as input with pull up
}
void loop() {
if(!digitalRead(PF_0)) //checking for logic zero
{
digitalWrite(PF_2, HIGH); // turn the BLUE LED on
delay(1000); // wait for a second
digitalWrite(PF_2, LOW); // turn the BLUE LED off by making the voltage LOW
delay(1000); // wait for a second
}
}
Example 3: Two switches, two LEDs
The third sketch combines both inputs. S1 (PF_4) blinks the blue LED once, and S2 (PF_0) blinks the green LED once. Each switch has its own if block, so the program checks both on every pass through the loop.
void setup()
{
pinMode(PF_2, OUTPUT); // setting PF.2 Pin as output
pinMode(PF_3, OUTPUT); // setting PF.3 Pin as output
pinMode(PF_0, INPUT_PULLUP); // setting PF.0 Pin as input with pull up
pinMode(PF_4, INPUT_PULLUP); // setting PF.4 Pin as input with pull up
}
void loop() {
if(!digitalRead(PF_0)) //checking for logic zero
{
digitalWrite(PF_3, HIGH); // turn the GREEN LED on
delay(1000); // wait for a second
digitalWrite(PF_3, LOW); // turn the GREEN LED off by making the voltage LOW
}
if(!digitalRead(PF_4)) //checking for logic zero
{
digitalWrite(PF_2, HIGH); // turn the BLUE LED on
delay(1000); // wait for a second
digitalWrite(PF_2, LOW); // turn the BLUE LED off by making the voltage LOW
}
}
For another take on this idea, see the push button and LED project.
How the code works
- setup() runs once. It sets each LED pin as an output and each switch pin as an input with the internal pull-up.
- loop() runs forever. Each pass reads the switch pin.
- !digitalRead() is true only when the pin reads LOW, which means the button is pressed.
- The LED is switched on, held with delay(), then switched off.
Limitations of these examples
These sketches are meant for learning, so they are simple. Keep the following in mind before you reuse the pattern:
- delay() blocks the program. While the LED is on, the board cannot read any switch. A press during the delay can be missed. Examples 1 and 2 also wait one more second after the LED turns off.
- Holding the button repeats the blink. The code checks the current level, not a press event. If you hold the switch, the LED blinks again after each cycle. To react only once per press, remember the previous state and act when it changes from HIGH to LOW.
- Switch bounce. Mechanical contacts can flicker for a few milliseconds when pressed. With a one-second delay in the loop this rarely shows, but in faster code you may need a short debounce delay or a timer-based check.
Troubleshooting
- The LED stays off: check that you selected the correct board in Energia and the right serial port, then re-upload.
- The LED blinks without a press: make sure the switch pin is set to INPUT_PULLUP and not plain INPUT, so it does not float.
- Wrong LED lights: compare the pin names in the code with the LED you expect. PF_2 is blue and PF_3 is green.
- Only one switch works: confirm that both switch pins are configured in setup().
FAQ
Why does the code check for logic zero?
The LaunchPad switches connect the pin to ground when pressed. With the pull-up enabled, the pin reads HIGH at rest and LOW when pressed, so a press shows up as logic zero.
Do I need an external resistor for the buttons?
Not for the onboard switches. The internal pull-up set by INPUT_PULLUP is enough. If you add your own button on a different pin, you can use the same approach, wiring the button between the pin and ground.
Can I use other port F pins as inputs?
Yes. Other GPIO pins can be configured with pinMode() in the same way. Check the pin map for your board first, because some pins are shared with on-board features.
Why does the LED sometimes miss a button press?
Because delay() stops the program from reading the switch. To avoid that, use millis() to time the LED instead of blocking the loop.