Understanding the digital read pin on a microcontroller

Understanding the digital read pin on a microcontroller
Published: by Renier Delport

Working with microelectronics means dealing with digital signals. Digital readings are one of the more common inputs we have to deal with.

Understanding the digital read pin on a microcontroller explores the similarities of digital readings in order to give the DIY hobbyist a better grasp on their usage in projects.

This post will be updated from time to time when I stumble upon more relevant information. If you stumbled upon more relevant information, see mistakes or doesn’t understand anything, please feel free to comment at the end.

Note about the pins

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 reading pin reads values from a connected device 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 for simple yes/no logic (e.g. IF/ELSE statements).

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.

Compared to analog pins, which can also read the value of the voltage connected, digital pins are much faster. Analog readings can however tell 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.setmode(GPIO.BCM)
GPIO.setup(PinNumber,GPIO.IN) # initialize the digital read pin as an input

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

Naming

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.

More microelectronics related posts

Online micro-electronic components, consumables & accessories
Buy great value for money micro-electronic components online from BangGood.com.
22 November 2017 | 1 029 532 reader(s) found this helpful so far.
Reviewing a 10 Watt LED integrated tube light
Reviewing a 10 Watt LED integrated tube light
See my #review on a 10Watt #LED integrated tube light
11 October 2017 | | 1 reader(s) found this helpful so far.
Using the Autodesk EAGLE free version for electronic hobbyists
Using the Autodesk EAGLE free version for electronic hobbyists
Read more about the #Autodesk EAGLE #free version for #electronichobbyists...
2 October 2017 | | 1 reader(s) found this helpful so far.
Connecting the ESP8266 to a breadboard and FTDI programmer
Connecting the ESP8266 to a breadboard and FTDI programmer
See #howto connect the #ESP8266 to a breadboard and FTDI programmer...
12 September 2017 | Updated 14 September 2017 | | 1 reader(s) found this helpful so far.
Installing the Arduino IDE on a Raspberry Pi 3
Installing the Arduino IDE on a Raspberry Pi 3
See #howto install the #Arduino IDE on a #RaspberryPi 3...
1 July 2017 | | 3 reader(s) found this helpful so far.
ESP32 development boards for Wi-Fi communication
ESP-32 development boards for Wi-Fi communication
#ESP32 #microcontroller development boards with build-in Wi-Fi & Bluetooth... #tinkering #DIY #homeautomation #IoT
25 June 2017 | Updated 26 June 2017 | | 2 reader(s) found this helpful so far.

Save, share, comment & 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 shit. Keep it constructive and polite.
Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPin on PinterestShare on StumbleUponShare on Tumblr

Comment via Facebook

Disqus

About the author

Renier finds himself busy with creative web design and his websites, motorcycling, photoshopping, micro electronics, non-commercialised music, superhero movies, badass series and many other things that are not interesting to most people.