2.1 Button Counter

The goal of this example is to teach you how to use the input mode of pins on the Soldered NULA MINI board. Inputs allow the board to react to external signals, such as buttons, switches, or sensors. In this case, we will use a push button as a simple input device to toggle the state of an LED. We wil also introduce the concept of debouncing and implement the said concept.
In this documentation you will learn:
- How to connect a button to the NULA board using a pull-down resistor.
- How to read button input using
digitalRead(). - How to increase a counter with each press and print the result to the Serial Monitor.
- Why button presses may trigger multiple counts.
Hardware required:
- 1x Soldered NULA MINI board
- 1x Breadboard
- 1x Push button
- 1x 10k ohm resistor
- Jumper wires
- USB-c cable

Putting the components together
We will start this example by connecting all the components. Just follow the steps provided below for a no hassle experience.
1. insert the NULA MINI board on the breadboard
If your board has male connectors soldered to its pins, we recommend placing it on the breadboard to make it easier and tidyer for yourself to connect other components.
If not, no worry. You can just connect the board with components by wire. It will be a bit messier but will do the job just fine.
2. Connect the Push button to the NULA MINI board
Take a piece of jumper wire and connect one end to an available GPIO pin on the board. For this example, we will be using IO4 pin. Connect the other end to any free (meaning nothing is connected to it!) power rail on the breadboard.
Connect one leg of the push button to the same power rail. On the opposite leg connect one side of the 10k ohm resistor, the other connect to GND.
To the leg next to the one connected to IO4 pin, connect the 3.3V pin. The finished circuit should look something like this:
What is a Pull-Down Resistor?
When you connect a button directly to a pin on the NULA MINI board, the pin doesn’t always know if it should read as HIGH or LOW when the button is not pressed. In that “floating” state, the pin can randomly pick up electrical noise and switch between HIGH and LOW by itself, which leads to unreliable behavior.
A pull-down resistor fixes this by gently “pulling” the pin’s value down to LOW (0V) whenever the button is not pressed. Think of it like a spring that always pulls the pin back to ground level when nothing else is happening.
- When the button is not pressed, the resistor connects the pin to GND, so the NULA board reads LOW.
- When the button is pressed, the button connects the pin directly to 3.3V, and the board reads HIGH.
The resistor value (commonly 10kΩ) is chosen so it’s strong enough to keep the pin LOW when idle, but weak enough not to interfere when the button is pressed.
Without a pull-down resistor, your button input may behave unpredictably, sometimes showing HIGH even when you are not pressing it.
Reading the button state
To read the button state, we call digitalRead() function. Keep in mind that the pin needs to be set as an INPUT in order to use this function.
The function digitalRead(pin) reads the value from a specified digital pin and returns either HIGH or LOW as result. We call this function inside the loop():
void loop() {
//digitalRead() is a function that reads the value from a specified digital pin, either HIGH or LOW.
bool reading = digitalRead(BUTTON_PIN);
}
How it works
This example introduces how to read digital input and react when the button is pressed.
Every time the button goes HIGH, we increase a counter variable by 1 and display it on the Serial Monitor.
Because this example does not use debouncing, you’ll notice that one press may register multiple counts.
This is intended behaviour and will be worked on in the [2.2 Button Debounce]
Code
Below is the full code for this example. It reads the button input continuously and prints a running count of how many times it has been pressed.
/*
This is a variable to which we pass the number of the pin that we connected the BUTTON to.
The NULA board has a pin naming logic as follows: IO4, where 19 is the number that we give to the variable.
If you wish to use a different pin, make sure you are using a IO__ marked pin.
*/
const int BUTTON_PIN = 4;
/*
This variable holds the value of our counter.
Each time the button is pressed, the counter value will increase by one.
*/
int counter = 0;
void setup() {
/*
pinMode() is a function that configures the specified pin to behave either as an input or an output.
As our pin needs to detect if the button has been pressed, we will put the pin in INPUT mode.
*/
pinMode(BUTTON_PIN, INPUT);
/*
Serial.begin() initializes serial communication between the NULA board and the computer.
We use it here so we can print out the current counter value on the Serial Monitor.
*/
Serial.begin(115200);
/*
Print out the initial message so we know that the program started successfully.
*/
Serial.println("Button Counter Example started!");
Serial.println("Press the button to increase the counter...");
}
void loop() {
/*
digitalRead() is a function that reads the value from a specified digital pin, either HIGH or LOW.
When the button is pressed, it connects the pin to 3.3V, and the board reads HIGH.
When it’s released, the pin is connected to GND through a pull-down resistor and reads LOW.
*/
bool reading = digitalRead(BUTTON_PIN);
/*
If the button is pressed, increase the counter by one and print it to the Serial Monitor.
Since this version does not include debouncing, multiple counts may appear for a single press.
*/
if (reading == HIGH) {
counter++;
Serial.print("Counter: ");
Serial.println(counter);
/*
Wait for the button to be released before allowing another count.
This prevents the counter from increasing too quickly while the button is still held down.
*/
while (digitalRead(BUTTON_PIN) == HIGH) {
// Wait until the button is released
}
}
}
Full example
Check out the full example code on the link below:
2.1_Button_Toggle_LED.ino
Example that shows how to toggle LED light with a press of a button