Lesson goal: Read a digital input from the Arduino (a 1 or 0 only)

Previous: Make an etch-a-sketch | Home | Next: Read a push-button switch

This lesson will show you how to read a digital input from one of the digital inputs (2..13) on the Arduino.
digitalread(pin)
Move the mouse over a dotted box for more information.

Now you try. Read digital inputs from the outside world. Note the values returns are either a 1 or a 0. This is the raw nature of digital information.

Type your code here:


See your results here:

Notes:
  • Although pins 0 and 1 are available, you shouldn't use them, as this will interfere with the Arduino's communication with your computer.

  • Note we have to use pinmode(2,INPUT) to set pin 2 to be an input, which here means to take in data from the outside world.

This experiment only requires the Arduino and a single wire. Insert one end of the wire into the pin labeled "2," as shown here. Insert the free end of the wire into the 5V pin and run the code above. What do you see displayed on the screen? Next, remove the end from the 5V pin and insert it into the 0V pin and run the code again. Do you see how a digital value is either a 1 or a 0? What input voltage gives a 1? A 0?

Dismiss.
Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.