digitalRead()

Abstract

The digitalRead() returns if the specified pin is HIGH or LOW. If the value of the corresponding bit of the register is 1 it returns HIGH, if 0 it returns LOW.

There are three registers, PORTB, PORTC and PORTD. The relation of the bit of the registers and the pins of Arduino Uno is shown below. Ax is an analog pin, Dx is a digital pin.

PORTB
Bit76543210
Pin--D13D12D11D10D9D8
PORTC
Bit76543210
Pin--A5A4A3A2A1A0
PORTD
Bit76543210
PinD7D6D5D4D3D2D1D0

The rough diagram to convert Arduino pin number to ATmega328P register is shown below.

The digitalRead() is a function that dose

  • calculate which bit of PORTx for the pin
  • read the value of the PORTx
  • return the result

Source Code

The digitalRead() is defined in hardware/arduino/avr/cores/arduino/wiring_digital.c as below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
int digitalRead(uint8_t pin)
{
        uint8_t timer = digitalPinToTimer(pin);
        uint8_t bit = digitalPinToBitMask(pin);
        uint8_t port = digitalPinToPort(pin);

        if (port == NOT_A_PIN) return LOW;

        // If the pin that support PWM output, we need to turn it off
        // before getting a digital reading.
        if (timer != NOT_ON_TIMER) turnOffPWM(timer);

        if (*portInputRegister(port) & bit) return HIGH;
        return LOW;
}

The input argument is pin. The type of the variable is uint8_t.

First calculate the timer of the pin. Some of the digital pins are related to timer. These pins can output PWM.

3
        uint8_t timer = digitalPinToTimer(pin);

The digitalPinToTimer() is a macro defined in hardware/arduino/avr/cores/arduino/Arduino.h. It returns a timer name if the pin is related to a timer. If not, it returns NOT_ON_TIMER.

Next calculate the bit mask, which bit in the PORTx, of the pin.

4
        uint8_t bit = digitalPinToBitMask(pin);

The digitalPinToBitMask() is a macro defined in hardware/arduino/avr/cores/arduino/Arduino.h. It returns a value with a corresponding bit, shown in the table above, of DDR set to 1. For example, if the pin is 13, the value 0b00100000 is returned. It means the fifth bit of PORTB.

Next, calculate a port corresponds to the pin.

5
        uint8_t port = digitalPinToPort(pin);

The digitalPinToPort() is a macro defined also in hardware/arduino/avr/cores/arduino/Arduino.h. It returns the PORT corresponds to the pin. If the pin is from 0 to 7, it returns PD, from 8 to 13 it returns PB, from 14(A0) to 19(A5) it returns PC.

If the port is NOT_A_PIN, the function returns LOW.

7
        if (port == NOT_A_PIN) return LOW;

In case of Arduino Uno, if the input value is correct/valid, digitalPinToPort() dose not return NOT_A_PIN. If the input value is incorrect/invalid, it refers to the out of range of an array, and its behavior is undefined. This means we must not give an incorrect input parameters.

 9
10
11
        // If the pin that support PWM output, we need to turn it off
        // before getting a digital reading.
        if (timer != NOT_ON_TIMER) turnOffPWM(timer);

If the pin is related to timer, turn it off. The turnOffPWM() is a function defined in hardware/arduino/avr/cores/arduino/wiring_digital.c, and it stops the timer.

After that, the port is converted to a register, then calculate logical AND with bit. If the result is not 0, the function returns HIGH, else it returns LOW.

13
14
         if (*portInputRegister(port) & bit) return HIGH;
        return LOW;

The portInputRegister() is a macro defined in hardware/arduino/avr/cores/arduino/Arduino.h. It converts the port to an address. It returns one of the PORTB, PORTC or PORTD. If the port is equal to PB, it returns PORTB.

Because PORTs are addresses, ‘*’ is required to get the content of the address.

If the result of calculating logical AND with bit is not 0 it returns HIGH, if 0 it returns LOW.

Version

Arduino AVR Boards 1.8.6

Last Update

March 21, 2023

inserted by FC2 system