Project 2: Read a Digital Value (DigitalReadSerial)

Created on: 2 August 2012
Updated on: 16 January 2023

Tutorial 10 – Pages

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.

Parts Needed

Besides an Arduino Uno, USB cable, breadboard and wire links, you will need:

  • 1 × 10k resistor (1/4W, 5%)
  • 1 × push button or toggle switch (or just a wire link will do)

Arduino Website Page

The DigitalReadSerial project at the Arduino website shows the circuit and circuit diagram or schematic for this project.

Project 2 Video: Read a Digital Value

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.

In the Arduino IDE

Find the sketch for this project in the Arduino IDE by selecting File → Examples → 1.Basics → DigitalReadSerial from the top Arduino IDE menu.

Build a Circuit to Read an Arduino Digital Pin Value

The DigitalReadSerial page on the Arduino website shows the circuit diagram and breadboard connections for this project.

Wire the Switch to the Arduino

The circuit diagram at the above link shows how to wire a switch to the Arduino using a breadboard and resistor.

Use a Wire Link as a Switch

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.

Programming and Operating the Circuit

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.