• Skip to main content
  • Skip to primary sidebar

Homemade Circuit Projects

Need circuit help? Post them in the comments! I've answered over 50,000!

Blog | Categories | About | Hire Me | Contact | Calculators-online
You are here: Home / Arduino Projects / Monitoring State of a Switch (Digital Read Serial) – Arduino Basics

Monitoring State of a Switch (Digital Read Serial) – Arduino Basics

Last Updated on May 14, 2026 by Swagatam Leave a Comment

This Arduino basics discusses the method of implementing a code through which the ON or OFF state of an external push-button could be read or monitored within the Arduino.

Table of Contents
  • Digital Read Serial
    • Hardware
    • Circuit Operation
    • Schematic
      • Understanding the Code

Digital Read Serial

Here I have explained through an example how to monitor the state of a switch by performing serial communication across your Arduino and your PC via USB.

In excess to your Arduino Board, you would require the following fundamental items:

Hardware

A momentary switch, button, or a push-to-ON switch

10k, 1/4 watt ohm resistor

breadboard
hook-up or jumper wire links.

Circuit Operation

The operation may be done with the following steps:

Take 3 pieces of jumper wires and hook them up with your Arduino board.Two of the wires, red and black, goes to the two long vertical rows on the side of the breadboard which become the supply cords of the board in order to carry the required 5V DC to the board.

The third wire is used for connecting the digital pin 2 to one of the leads of the push-to-ON switch.

This particular lead of the button also links up with a pull-down 10k resistor to the negative supply rail or the ground. The other free lead of the switch is linked with the positive of 5 volt supply.

With the above connections made, the switch toggles or performs a dual action in the circuit when given a push.

Normally when the switch is in a disconnected position, its two leads stay isolated, such that the pin which is linked with the ground via the pull-down resistor renders a LOW, or a logic 0 level.

In the pressed situation the switch executes a momentary a bridging of its two leads, such that its leads are subjected to + 5 volts, rendering a HIGH, or logic 1 level across them.

Isolating the digital i/o pinouts from rest of the things, could force the LED to go haywire and cause erratic blinking. This is due to the fact the input being not rendered to anything, or kept in a "hanging" position - meaning it's not designated to any definite logic, neither high nor low ( +5V or 0V), this is reason why we employ the pull-down resistor with the switch.

Schematic

Understanding the Code

In the following program below, we commence with serial communication within the setup function at the rate of 9600 bits of data per second, this is initiated between the Arduino board and the attached computer:Serial.begin(9600);

In the next step we trigger digital pin 2, the pin that would be responsible for  the output with the push switch as an input:pinMode(2,INPUT);This completes our "setup", now we sail into the main loop of our code.

Here on pressing the pushbutton, 5 volts is allowed to get through our circuit, while the input pin gets linked with the ground through the 10-kilohm resistor when it's in an unpressed condition.

The above is wha we call a digital input, which refers to a condition where the switch can only be in a particular state either an on state (accepted by the Arduino as a "1", or LOGIC HIGH) or an off state (visualized by the Arduino as a "0", or LOGIC LOW), with no other undefined sates in between whatsoever.

The fundamental action that we need to execute in the main loop of the program is to apply a variable for keeping the information in place that's been sent via the push button.

As discussed above with the signals being  in the form of either a "1" or a "0", we here employ an int datatype. We can name this variable as sensorValue, and fix it to correspond everything that's being read on digital pin 2. All these become achievable via a one line of code:

int sensorValue = digitalRead(2);Once the Arduino has read the input, print it back to the computer in the form of a decimal value.

This can be implemented with the help of the command Serial.println() in the concluding line of the code:Serial.println(sensorValue);

After this, whenever the Serial Monitor is initiated in the Arduino domain, we would witness a chain of "0"s during the push button is open position, and chains "1"s in cases the button is held in closed condition.

/*
  DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor 
 This example code is in the public domain.
 */
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1);        // delay in between reads for stability
}

You'll also like:

  • Receiver12312 2Wireless Servo Motor Control Using 2.4 GHz communication link
  • H-Bridge Sine Wave Inverter Circuit using Arduino
  • hello2BworlddddHow to Interface Cellphone Display with Arduino
  • breadboard arduinoHow to Make Arduino on Breadboard – Step by Step Instructions

Filed Under: Arduino Projects Tagged With: Digital, Monitoring, Read, Serial, State, Switch

About Swagatam

I am an electronics engineer and doing practical hands-on work from more than 15 years now. Building real circuits, testing them and also making PCB layouts by myself. I really love doing all these things like inventing something new, designing electronics and also helping other people like hobby guys who want to make their own cool circuits at home.

And that is the main reason why I started this website homemade-circuits.com, to share different types of circuit ideas..

If you are having any kind of doubt or question related to circuits then just write down your question in the comment box below, I am like always checking, so I guarantee I will reply you for sure!

Previous Post: « Customized Water Flow Controller with Timer Circuit
Next Post: Converting Analogue to Digital (Analogue Read Serial) – Arduino Basics »

Reader Interactions

Need Help? Please Leave a Comment! We value your input—Kindly keep it relevant to the above topic! Cancel reply

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

Primary Sidebar

My YouTube Channel

Circuit Simulator: Draw and Simulate Schematics

circuit simulator image

Categories

  • Arduino Projects (95)
  • Audio and Amplifier Projects (134)
  • Automation Projects (18)
  • Automobile Electronics (104)
  • Battery Charger Circuits (90)
  • Datasheets and Components (109)
  • Electronics Theory (151)
  • Energy from Magnets and Earth (43)
  • Games and Sports Projects (11)
  • Grid and 3-Phase (20)
  • Health related Projects (27)
  • Home Electrical Circuits (13)
  • Indicator Circuits (16)
  • Inverter Circuits (98)
  • Lamps and Lights (161)
  • Meters and Testers (72)
  • Mini Projects (28)
  • Motor Controller (68)
  • Oscillator Circuits (30)
  • Pets and Pests (15)
  • Power Supply Circuits (91)
  • Remote Control Circuits (50)
  • Security and Alarm (65)
  • Sensors and Detectors (107)
  • SMPS and Converters (46)
  • Solar Controller Circuits (62)
  • Temperature Controllers (44)
  • Timer and Delay Relay (51)
  • Voltage Control and Protection (44)
  • Water Controller (37)
  • Wireless Circuits (31)

Subscribe to get New Circuits in your Email

Other Links

  • Privacy Policy
  • Cookie Policy
  • Disclaimer
  • Copyright
  • Videos
  • Sitemap

People also Search

555 Circuits | 741 Circuits | LM324 Circuits | LM338 Circuits | 4017 Circuits | Ultrasonic Projects | SMPS Projects | Christmas Projects | MOSFETs | Radio Circuits | Laser Circuits | PIR Projects |

Recent Comments

  • Nicodemus Onate on How to Design Your Own Inverter Transformer
  • Swagatam on Making a Self-Powered Generator
  • Swagatam on Electronic Circuit Projects, Tutorials, and Practical Engineering Solutions
  • Ranjith Gurusinghe on Making a Self-Powered Generator
  • Mazlom on Electronic Circuit Projects, Tutorials, and Practical Engineering Solutions

Social Profiles

  • Twitter
  • YouTube
  • Instagram
  • Pinterest
  • My Facebook-Page
  • Stack Exchange
  • Linkedin

Calculators

  • ZVS Induction Heater + Tank Calculator Tool
  • Zener Diode Calculator
  • Wire Current and Thickness Calculator (Ampacity Calculator)
  • Voltage Divider Calculator
  • Transistor Base Resistor Calculator
  • Transistor Astable Multivibrator Calculator
  • TL431 Calculator
  • Solar Panel, Inverter, Battery Calculator
  • Ferrite Core Air Gap Calculator Tool
  • Parallel MOSFET Calculator Tool: How to Connect MOSFETs in Parallel Safely
  • LC Resonance Calculator for EV Battery Charger Circuits
  • LED String Series Resistor Calculator
  • PFC (Power Factor Correction) Calculator Tool: 3kW
  • Passive Power Factor Correction (PFC) Calculator
  • LM567 IC Calculator Tool
  • SMPS Flyback Boost Converter Calculator
  • Shunt Resistor Calculator for Ammeters
  • SCR and Triac Gate Resistor Calculator
  • Battery Back up Time Calculator
  • Boost Converter Calculator (Non-Isolated)
  • Bootstrap Capacitor Calculator
  • Buck Converter Calculator
  • Buck-Boost Converter Calculator
  • Capacitance Reactance Calculator
  • DCM Flyback Transformer & Wire Gauge Wire Size Calculator Tool
  • Filter Capacitor Calculator
  • IC 4047 Calculator (Frequency and PWM)
  • IC 4060 Calculator
  • IC 555 Astable Calculator
  • IC 555 Monostable Calculator
  • IC SG3525, SG3524 Calculator
  • Inductance Calculator
  • Induction Heater Inductor and Resonant Frequency Calculator
  • Induction Heater Work Coil Calculator
  • Inverter LC Filter Calculator
  • LC Resonance Calculator
  • LED Current Calculator
  • LM317, LM338, LM396 Calculator
  • NAND/NOT Gate RC Values Calculator
  • NOT, NAND Gate Frequency Calculator
  • Notch Filter Calculator Tool
  • Ohm’s Law Calculator
  • Phase Angle Phase Shift Calculator
  • Power Factor (PF) Calculator
  • RC Filter Calculator
  • Reactance Calculator
  • Sine Table Calculator for SPWM Arduino Code
  • Small Signal Transistor(BJT) and Diode Quick Datasheet
  • SMPS Calculator for Toroidal Ferrite Transformers
  • SMPS Flyback Transformer Calculator – Design by Target Duty Cycle
  • TL431 Calculator
  • Op-Amp Hysteresis Resistor Calculator

© 2026 · Swagatam Innovations