How to read switch input in Arduino

In this arduino tutorial we show how to read switch input in Arduino. The tutorial illustrates with code and physical hardware setup to read switch input connected to Arduino and a LED to show the state of the switch.

In this arduino tutorial we show how to read switch input in Arduino. To show that Arduino microcontroller is reading the switch input, we will use a LED to indicates whether we have pressed the switch or not. That is, if the switch is pressed the LED will turn ON otherwise it will remain off.

What you need in complete this tutorial are:

  1. Arduino UNO
  2. Breadboard
  3. Switch
  4. LED
  5. 220Ohm resistor
  6. 10KOhm resistor

Circuit Schematic to read switch input in Arduino

Connect the circuit onto your breadboard according the following circuit diagram.

arduino switch connection

Physical Setup using Breadboard

The physical hardware connection should something like the following.

reading switch input arduino

Program Code

The next thing we need is the program code. The following shows how to read switch input in Arduino using Arduino Native C++ programming language.

Program Explanation

First we have labelled the pin 13 and 10 with LED and BUTTON for easier program writing. Then in the setup function we have initialized the pin mode of the pin 13 and pin 10 as output and input respectively using the pinMode() function. Finally in the loop function, we continuously check the Button state whether it is low or high using the if statement and digitalRead function. This digitalRead() function is the responsible function to read switch input in Arduino. If it is LOW then we keep the LED LOW using digitalWrite function which means the LED remains off. If it changes to HIGH, which means the button is pressed then we change turn on the LED.

Compile/ Upload the Program

The final step is to compile and upload the program. Once you have uploaded the sketch the LED should blink only after the switch is pressed.

In this tutorial you learned how to read switch input in Arduino. See our previous tutorial Testing Proteus simulated Arduino Design in Hardware.

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

Your email address will not be published. Required fields are marked *

Testing arduino with LED

Testing Proteus simulated Arduino Design in Hardware

using analogWrite for PWM in arduino

How to use arduino analogWrite function for PWM