• Skip to main content
  • Skip to primary sidebar

Homemade Circuit Projects

Get free circuit help 24/7

New Projects | Privacy Policy | About us | Contact | Disclaimer | Copyright | Videos 

You are here: Home / Arduino Engineering Projects / Monitoring State of a Switch (Digital Read Serial) – Arduino Basics

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

Last Updated on June 20, 2019 by Swagatam

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.

Digital Read Serial

Here we learn 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

caution electricity can be dangerous

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:

  • 1.  Digital Weighing Scale Using Load Cell and Arduino
  • 2.  Make this Advanced Digital Ammeter using Arduino
  • 3.  How to Control Servo Motor Using Joystick
  • 4.  Automatic Street Light Dimmer Circuit
  • 5.  How to Send and Receive SMS Using GSM Modem
  • 6.  Arduino Pure Sine Wave Inverter Circuit with Full Program Code

About Swagatam

I am an electronic engineer (dipIETE ), hobbyist, inventor, schematic/PCB designer, manufacturer. I am also the founder of the website: https://www.homemade-circuits.com/, where I love sharing my innovative circuit ideas and tutorials.
If you have any circuit related query, you may interact through comments, I'll be most happy to help!

Primary Sidebar

Categories

  • 3-Phase Power (15)
  • 324 IC Circuits (19)
  • 4017 IC Circuits (52)
  • 4060 IC Circuits (26)
  • 555 IC Circuits (99)
  • 741 IC Circuits (20)
  • Arduino Engineering Projects (83)
  • Audio and Amplifier Projects (115)
  • Battery Chargers (83)
  • Car and Motorcycle (95)
  • Datasheets (74)
  • Decorative Lighting (Diwali, Christmas) (33)
  • Electronic Components (101)
  • Electronic Devices and Circuit Theory (36)
  • Electronics Tutorial (120)
  • Fish Aquarium (5)
  • Free Energy (34)
  • Fun Projects (13)
  • GSM Projects (9)
  • Health Related (20)
  • Heater Controllers (29)
  • Home Electrical Circuits (104)
  • How to Articles (20)
  • Incubator Related (6)
  • Industrial Electronics (28)
  • Infrared (IR) (40)
  • Inverter Circuits (98)
  • Laser Projects (12)
  • LED and Light Effect (93)
  • LM317/LM338 (21)
  • LM3915 IC (25)
  • Meters and Testers (66)
  • Mini Projects (150)
  • Motor Controller (67)
  • MPPT (7)
  • Oscillator Circuits (26)
  • PIR (Passive Infrared) (8)
  • Power Electronics (34)
  • Power Supply Circuits (79)
  • Radio Circuits (10)
  • Remote Control (48)
  • Security and Alarm (62)
  • Sensors and Detectors (121)
  • SG3525 IC (5)
  • Simple Circuits (75)
  • SMPS (29)
  • Solar Controllers (61)
  • Timer and Delay Relay (53)
  • TL494 IC (5)
  • Transformerless Power Supply (8)
  • Transmitter Circuits (41)
  • Ultrasonic Projects (16)
  • Water Level Controller (45)

Calculators

  • AWG to Millimeter Converter
  • Battery Back up Time Calculator
  • Capacitance Reactance Calculator
  • IC 555 Astable Calculator
  • IC 555 Monostable Calculator
  • Inductance Calculator
  • LC Resonance Calculator
  • LM317, LM338, LM396 Calculator
  • Ohm’s Law Calculator
  • Phase Angle Phase Shift Calculator
  • Power Factor (PF) Calculator
  • Reactance Calculator
  • Small Signal Transistor(BJT) and Diode Quick Datasheet
  • Transistor Astable Calculator
  • Transistor base Resistor Calculator
  • Voltage Divider Calculator
  • Wire Current Calculator
  • Zener Diode Calculator

© 2023 · Swagatam Innovations