• Puriphico

digitalRead(): digital functions in arduino programming (part 2)

This article details the digitalRead() function in Arduino programming, explaining how it works and how to use it in your own sketches.


digitalRead():

  • Introduction:

  • As you may know, a microcontroller, in simplest terms, can be called the brain of the circuit. That is, the microcontroller is the component of the circuit responsible for storing and processing the code that is written. As you can see in this image, a microcontroller has pins - these tiny metal legs - that “attach” it to the breadboard, so to speak, and connect it to the rest of your circuit:

  • Formally speaking, a pin is a metal terminal of any component in an integrated circuit; however, in this case we’re only referring to those of the microcontroller (as pictured above).

  • If you haven’t familiarized yourself with the first article on digital functions, describing the pinMode function, it will be linked in the description below; for a brief overview, though, pinMode allows pins to be assigned as inputs or outputs based on their role in the circuit.

  • digitalRead():

  • For inputs, as the name implies, the microcontroller will be receiving data. When a pin is configured to be an input, the data it receives can be accessed with the digitalRead function, which reads the value from a specified digital pin (as specified in parenthesis in the code setup), as either HIGH or LOW.

  • To use the digitalRead function in your Arduino program, write digitalRead(pin), where 'pin' refers to the pin number labeled on the microcontroller.

  • When we use this function in programming, most often, if not always, it refers to the use of a sensor or other input. That means that, if your circuit uses LED’s, most likely they will not serve as the components being “read.”

  • If you are having trouble picturing how it works in practice, imagine this: your circuit uses a sensor, which has three pins that are connected to a power, ground, and a pin on the microcontroller, respectively. With this setup, in your program, you can use the digitalRead() function to access the information from the pin of the microcontroller that is connected to your sensor ªthe input in your circuit).

Here is an example of the digitalRead() function used in one of my programs. In this fragment, I declare the variable 'state' as the the number that is returned when the sensor's input is read by the program. However, I could have also replaced the 'state' within the If statement with 'digitalRead(sensorpin).'

  • Warning:

  • It is very important to ensure that the pin you instruct the computer to read is truly the pin used in the circuit; if not, the digitalRead() function can return HIGH or LOW values at random. Also, because writing/pin-labels on microcontrollers is often very small, it can be easy to confuse pins and their respective designations. For that reason, when I am working with my circuits, I often search for an image of the microcontroller schematic. Here is an example of the Arduino Nano 33 BLE sense schematic that I found off the internet:

  • Additionally, it is important to ensure that, when using digitalRead(), you are referencing the digital pins on a microcontroller, as opposed to the analog pins.

  • A future article will explain the difference between digital and analog signals, but an easy trick to identify the digital pins is either through the use of a schematic (which usually specifies the pin designation) or by simply using the pins that are not denoted with a capital A.

  • In some cases, however, analog pins can also serve as digital pins; nonetheless, there are some exceptions, so unless you are running short on pins, I suggest following to the tricks identified above.

#digitalRead #Arduino #programming #Code #Digital #Functions #DigitalFunctions #Puriphico


52 views0 comments