Understanding the digital read pin on a microcontroller

Understanding the digital read pin on a microcontroller

When working with electronics, one will very often come across working with digital readings. Digital read (and write) pins are available on many microcontrollers and is one of the simpler pins to work with.

This is an ongoing post. Please suggest corrections, explanations, etc. in the comment section at the bottom of this page.


Digital read pins are available on almost any microcontroller. These pins are used to determine whether a signal is high or low. Depending on these two variables, further logic can be applied.

On many microcontrollers, many of the pins, including digital read pins doubles, or triples, as other pins too. It is the programming software (e.g. Arduino IDE or Python) that defines the final function of the pin – in this case, digital read.

Analog pins can also be declared as digital pins, but are much slower.

Digital readings

A digital read pin is used to read values from a connected component that outputs voltages. Being digital, this input signal will either be interpreted as a (relative) HIGH or (relative) LOW (i.e. 1 or 0). This interpreted signal can then be used further for simple yes/no, true/false or on/off logic.

A programmed microcontroller will run its code over and over again to detect input from (and/or send output to) the declared pins. Declared digital reading pins will read the state of the pin at the specific instant in time the code is triggered to.

In order for a digital reading signal to be transmitted, the connected device also needs to be connected to the same (or a similar) power supply source than the microcontroller. Digital pins on a microcontroller use TTL (transistor-to-transistor logic) and will read voltages ranging from 0V to as high as the Vcc level of the microcontroller (usually 3.3 – 5 V DC).

Compared with analog pins, which can also read the value of voltages, digital pins are much faster. Analog readings can, however, tell more precisely what the (relative) voltage is on a pin.

Examples of digital output devices include buttons, sensor modules (e.g. HC-SR501 PIR Motion Sensors, Magnetic door sensors, etc.) and receiver modules (e.g. Laser Transmitter Receivers, etc.).

Arduino programming language

In the Arduino programming language, digital reading is programmed as follows:

// Constants to set the pin number:
const int PinNumber = 2; // the number of the digital read pin

void setup() {
  pinMode(PinNumber, INPUT); // initialize the digital read pin as an input

void loop() {
  State = digitalRead(PinNumber); // read the state of the digital read value

  if (State == HIGH) { // if the value is HIGH
    // do something
  } else { // if the value is LOW
    // do something else
  delay(200); // wait for 200 ms

Raspberry Pi & Python

import RPi.GPIO as GPIO
import time

PinNumber = 17 # the number of the digital read pin
GPIO.setup(PinNumber,GPIO.IN) # initialize the digital read pin as an input

while True:
  if (GPIO.input(PinNumber)):
    # do something
    # do something else
  time.sleep(0.2) # wait for 200 ms


Digital pins are usually named D1, D2, D3, etc.

Receiver modules

With certain connected receiver modules (e.g. 433MHz RF Transmitter Receivers), the model itself can convert their received data (e.g. strings) to a series of 1’s and 0’s, which can then be transmitted to the digital read pin as a set of HIGH’s and LOW’s. With the correct libraries, these combination/sets of HIGH/LOW readings can then be converted back to the original string value.

The fact that the digital read pin can only read the state of the pin at a certain given moment means that an extra parameter, time, needs to be involved. Although this couldn’t be confirmed, I suspect that this is where an interrupt pin comes in. The interrupt pin is able to detect a combination of input before declaring the signal complete. Different length pulses of HIGH/LOW are used to indicate a 1 or a 0. A long HIGH and short LOW could be a 1, and a short HIGH and long LOW could be a 0.

Related product links

Please support Behind The Scenes by using these links to purchase these related products online from our partner websites. Links will open in a new window...

About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.
Behind the Scenes is a free, informative website. If you find value in any of our content, please consider making a donation to our cause.
Donate via PayPal

Save, share & Disqus

Use the buttons below, on the left or the bottom of this page to share this post. Your comment is important, but don't be a knob. Keep it constructive and polite.

Comment via Disqus

Disqus is a worldwide comment hosting service for web sites and online communities. This secure platform ensures a pleasant commenting environment which is manageable from one account. Use the Login button to sign up.

More microelectronics related posts

Discover the easy way to send bulk SMS from your PC, Mac or mobile device
Discover the easy way to send bulk SMS from your PC, Mac or mobile device
19 August 2019
Ad: Using the latest in technology, WinSMS offers cost effective messaging solutions that makers and business owners can use. With their online portal and API functionality, buying SMS bundles and sending them to recipients is more cost effective and easy to do. More…
Using the Arduino IDE to program the ESP8266
Using the Arduino IDE to program the ESP8266
12 October 2018
After several steps of preparation, the ESP8266 can finally be programmed. Fortunately this step has been made fairly simple by using, what is a familiar platform for many, the Adruino IDE. This post will show how to upload a few variations of the basic Blink sketch to an ESP-01 together with the wiring of LEDs. More…