Skip to main content

pin.read()

Reads the state of the specified pin

Availability

Device

Returns

Integer — the state of the specified pin

Description

This method reads the state of the specified pin, according to its configured mode.

Digital Inputs

Digital inputs return a 0 or 1.

All non-analog uses of pin.read() will return the actual state of the pin even if it is configured for output — see the Digital Output example below, I²C, SPI and UART.

Analog Inputs

Analog inputs return values in the range 0-65,535 (16 bits), where 0 corresponds to 0V, and 65,535 corresponds to the imp power supply voltage, usually 3.3V.

The current ADC in the imp is only accurate to 12-bits. The value is returned as a 16-bit number to accommodate the possibility of more accurate ADCs being incorporated into future releases of the imp.

Pulse Counters

Pulse counters return the number of pulses counted over the requested time interval.

Pulse counting is synchronous (blocking). When counting pulses for 0.5 seconds, the pin.read() call will take 0.5 seconds to execute.

Example Code

Reading a digital input

Polling an analog input

Reading a pulse counter

Reading a digital output