Mistake on this page?
Report an issue in GitHub or email us


Use the PortIn interface to define which pins of a hardware GPIO port are set as an input and to read the value of those pins. The port name is device specific and defined in the device's PortNames.h file in the mbed-os/targets folder.

A bit mask defines which pins of the GPIO port are set as an input (1b = include, 0b = ignore). The default mask value is 0xFFFFFFFF, which sets all pins as an input.

The device-specific PinNames.h and the respective datasheet or reference manual define the pins associated with a GPIO port.

Note: You can combine pins from different GPIO ports using the BusIn interface. Use PortOut to define which GPIO pins are to be used as digital output.

PortIn class reference

Public Member Functions
 PortIn (PortName port, int mask=0xFFFFFFFF)
 Create a PortIn, connected to the specified port. More...
int read ()
 Read the value input to the port. More...
void mode (PinMode mode)
 Set the input pin mode. More...
 operator int ()
 A shorthand for read() More...

PortIn hello, world

/* mbed Example Program
 * Copyright (c) 2006-2014 ARM Limited
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

// Switch on an LED if any of mbed pins 21-26 is high
#include "mbed.h"
PortIn     p(Port2, 0x0000003F);   // p21-p26
DigitalOut ind(LED4);
int main() {
    while(1) {
        int pins = p.read();
        if(pins) {
            ind = 1;
        } else {
            ind = 0;
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.