Created on: 2 August 2012
Updated on: 16 January 2023
How to read a digital value from an Arduino Uno digital pin. In this project for absolute beginners, the Arduino continuously reads the state of digital pin 2 to see if the attached switch is open or closed. The state of the switch is continuously sent out of the serial (USB) port for display on the Arduino IDE serial monitor window.
Besides an Arduino Uno, USB cable, breadboard and wire links, you will need:
The DigitalReadSerial project at the Arduino website shows the circuit and circuit diagram or schematic for this project.
The following video shows the DigitalReadSerial project used to read a digital value from an Arduino Uno digital pin. A wire is used instead of a switch in the video. It is a bit difficult to see, but when the wire or switch is open, a 0 is shown in the Arduino IDE serial monitor window. When the wire or switch is closed, a 1 appears.
Find the sketch for this project in the Arduino IDE by selecting File → Examples → 1.Basics → DigitalReadSerial from the top Arduino IDE menu.
The DigitalReadSerial page on the Arduino website shows the circuit diagram and breadboard connections for this project.
The circuit diagram at the above link shows how to wire a switch to the Arduino using a breadboard and resistor.
If using just a wire link instead of a switch, start by connecting a wire link from Arduino pin 2 to one end of a 10k resistor on the breadboard. Connect the other end of the resistor to one of the Arduino GND pins using a wire link.
To simulate a switch, connect a wire to the Arduino 5V pin. To close the "switch" touch the other end of the wire to Arduino pin 2. This can be done on the breadboard where Arduino pin2 connects to the 10k resistor. Don't connect the wire to the wrong end of the resistor or you will be shorting out the 5V power. To open the "switch", disconnect the wire link that is connected to the 5V pin from the pin 2 / resistor junction simply by lifting it, instead of touching it on this junction.
In the Arduino IDE select File → Examples → 1.Basics → DigitalReadSerial to open the sketch for this project. Verify and then upload the sketch.
Open the serial monitor window in the Arduino IDE. With the switch open, zero (0) will be continuously sent to the serial monitor window. Closing the switch will continuously send one (1) to the serial monitor window.