Arduino Nano 33 IoT - Button LED

This guide shows you how to use the Arduino Nano 33 IoT and a button to control the LED. We will look at two different examples.

Example 1: Keeping the LED and button states in sync, explained below:

Use-Case 2 - Changing the LED light on or off each time you press the button, explained below:

In Use-Case 2, it is very important to remove false signals from the button so that everything works correctly. We will show why this is needed by comparing how the LED behaves when we use the Arduino Nano 33 IoT code with and without this button signal cleaning.

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Breadboard-mount Button with Cap
1×Breadboard-mount Button Kit
1×Panel-mount Push Button
1×Push Button Module
1×LED Kit with resistor
1×LED (red)
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×Optionally, 5V Power Adapter for Arduino Nano 33 IoT
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of LED and Button

If you're new to using the LED, Button, and Arduino Nano 33 IoT, please check out these tutorials:

These tutorials explain how LED and Button work, their pinouts, how to connect them to the Arduino Nano 33 IoT, and how to program Arduino Nano 33 IoT to work with the LED and Button.

Wiring Diagram

The wiring diagram between Arduino Nano and 33 IoT Button LED

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. Although these pins can be used as digital input pins, it is recommended to avoid using them for digital input. If you must use them, do not use internal or external pull-down resistors for these pins

Application 1 - The LED state is in sync with the button state

Arduino Nano 33 IoT Code

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-button-led */ #define BUTTON_PIN 2 // The Arduino Nano 33 IoT pin connected to the button #define LED_PIN 5 // The Arduino Nano 33 IoT pin connected to the LED int button_state = 0; // variable for reading the pushbutton status void setup() { // Configure the LED pin as a digital output pinMode(LED_PIN, OUTPUT); // initialize the button pin as an pull-up input (HIGH when the switch is open and LOW when the switch is closed) pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the pushbutton value: button_state = digitalRead(BUTTON_PIN); // control LED according to the state of button if(button_state == LOW) // If button is pressing digitalWrite(LED_PIN, HIGH); // turn on LED else // otherwise, button is not pressing digitalWrite(LED_PIN, LOW); // turn off LED }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and send the code to the board.
Arduino IDE Upload Code
  • Press the button and hold it for a few seconds.
  • Look at the LED to see if it changes.

You'll notice that the LED light always matches the button's condition.

Code Explanation

Look at the step-by-step explanation in the comments of the code!

Application 2 - Button toggles LED

Arduino Nano 33 IoT Code - Button Toggles LED Without Debouncing

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-button-led */ #define BUTTON_PIN 2 // The Arduino Nano 33 IoT pin connected to the button #define LED_PIN 5 // The Arduino Nano 33 IoT pin connected to the LED int led_state = LOW; // The current state of LED int prev_button_state; // The previous state of button int button_state; // The current state of button void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode button_state = digitalRead(BUTTON_PIN); } void loop() { prev_button_state = button_state; // save the last state button_state = digitalRead(BUTTON_PIN); // read new state if(prev_button_state == HIGH && button_state == LOW) { Serial.println("The button is pressed"); // toggle state of LED led_state = !led_state; // control LED according to the toggled state digitalWrite(LED_PIN, led_state); } }

Code Explanation

You can find the explanation in the comments of the Arduino Nano 33 IoT code above.

In the program, the line "led_state = !led_state" works exactly the same as the code shown below:

if(led_state == LOW) led_state = HIGH; else led_state = LOW;

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code and open it in the Arduino IDE.
  • Upload the code to your Arduino Nano 33 IoT.
  • Press the release and button a few times.
  • Watch how the LED changes.

You might see that the LED light changes each time you press the button. However, this may not happen every time. Sometimes the LED light can switch on and off very quickly with one press, or it might not change at all (it might change twice so fast that you can't really notice it).

To fix this problem, we need to adjust the button so it only sends one signal when pressed. Check out this guide for more details.

Arduino Nano 33 IoT Code - Button Toggles LED With Debouncing

Handling button bounce can be hard for beginners. Luckily, the ezButton library makes it simple.

Why is debouncing needed? Check out the Arduino Nano 33 IoT - Button Debounce guide for more details.

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-button-led */ #include <ezButton.h> #define BUTTON_PIN 2 // The Arduino Nano 33 IoT pin connected to the button #define LED_PIN 5 // The Arduino Nano 33 IoT pin connected to the LED ezButton button(BUTTON_PIN); // create ezButton object int led_state = LOW; // The current state of LED void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) { Serial.println("The button is pressed"); // toggle state of LED led_state = !led_state; // control LED according to the toggleed sate digitalWrite(LED_PIN, led_state); } }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Download and install the ezButton library. Follow the instructions at the How To link for help.
  • Copy the code and open it in the Arduino program.
  • Click the Upload button in the Arduino program to send the code to the Arduino Nano 33 IoT.
  • Press and release the button several times.
  • Watch the LED change its state.

Every time you press the button, the LED light changes its state only once.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!