Skip to content

uBit.io

Overview

uBit.io provides a means of accessing the pins exposed on the edge connector, enabling analog and digital input/output operations. Some pins can also perform basic touch sensing either through capacitative or resistive methods.

There are six analog-capable pins which can be used to generate analog wave forms and approximate analog inputs. There is only one Analog to Digital converter on the nRF52833, which prevents concurrent analog to digital conversions.

Analog waveforms are simulated via Pulse Width Modulation generated by software. As such, there is a limit of 3 concurrent PWM outputs.

There are two ways of accessing pins on the edge connector:

  • uBit.io.P0.setDigitalValue(1) - Would configure P0 as a digital output, and set that pin HI.
  • uBit.io.pin[1].setDigitalValue(1) - Would configure P1 as a digital output, and set that pin HI.

By design, the operation of the pins on the edge connector are dynamic, and can swap between modes based on the method that as been called.

For example:

while(1)
{
    uBit.io.P0.setDigitalValue(1);
    uBit.sleep(1000);
    uBit.io.P0.setServoValue(90);
    uBit.sleep(1000);
}
Will transition between Digital and Analog output every second.

Shared functionality

As well as being General Purpose Input Output (GPIO) pins, some pins on the micro:bit have other functionality used internally by the micro:bit.

Therefore, some functionality on the micro:bit may have to be disabled in order to use some GPIO pins.

Pin out

This is the pin out for the micro:bit V2.

microbit schematic

Message Bus Info

Message Bus ID

Constant Value Capability
MICROBIT_ID_IO_P0 7 Analog, digital and touch
MICROBIT_ID_IO_P1 8 Analog, digital and touch
MICROBIT_ID_IO_P2 9 Analog, digital and touch
MICROBIT_ID_IO_P3 10 Analog and digital
MICROBIT_ID_IO_P4 11 Analog and digital
MICROBIT_ID_IO_P5 12 Digital only
MICROBIT_ID_IO_P6 13 Digital only
MICROBIT_ID_IO_P7 14 Digital only
MICROBIT_ID_IO_P8 15 Digital only
MICROBIT_ID_IO_P9 16 Digital only
MICROBIT_ID_IO_P10 17 Analog and digital
MICROBIT_ID_IO_P11 18 Digital only
MICROBIT_ID_IO_P12 19 Digital only
MICROBIT_ID_IO_P13 20 Digital only
MICROBIT_ID_IO_P14 21 Digital only
MICROBIT_ID_IO_P15 22 Digital only
MICROBIT_ID_IO_P16 23 Digital only
MICROBIT_ID_IO_P19 24 Digital only
MICROBIT_ID_IO_P20 25 Digital only

Message Bus Events

When isTouched() is called, provided that the selected pin has touch capabilities (outlined above), an instance of button will be created. Listeners can therefore be placed using the desired pins' ID, with the exact same events produced by a standard button.

uBit.messageBus.listen(MICROBIT_ID_IO_P0, MICROBIT_EVT_ANY, someFunction);
uBit.io.P0.isTouched();
Constant Value
MICROBIT_BUTTON_EVT_DOWN 1
MICROBIT_BUTTON_EVT_UP 2
MICROBIT_BUTTON_EVT_CLICK 3
MICROBIT_BUTTON_EVT_LONG_CLICK 4
MICROBIT_BUTTON_EVT_HOLD 5
MICROBIT_BUTTON_EVT_DOUBLE_CLICK 6

API

setDigitalValue

int
setDigitalValue
(
int
value)

Description

Configures this IO pin as a digital output (if necessary) and sets the pin to 'value'.

Parameters

int
value - 0 (LO) or 1 (HI)

Returns

DEVICE_OK on success, DEVICE_INVALID_PARAMETER if value is out of range, or DEVICE_NOT_SUPPORTED if the given pin does not have digital capability.

Example
 Pin P0(DEVICE_ID_IO_P0, DEVICE_PIN_P0, PIN_CAPABILITY_BOTH); 
 P0.setDigitalValue(1); // P0 is now HI 

getDigitalValue

int
getDigitalValue
()

Description

Configures this IO pin as a digital input (if necessary) and tests its current value.

Returns

1 if this input is high, 0 if input is LO, or DEVICE_NOT_SUPPORTED if the given pin does not have digital capability.

Example
 Pin P0(DEVICE_ID_IO_P0, DEVICE_PIN_P0, PIN_CAPABILITY_BOTH); 
 P0.getDigitalValue(); // P0 is either 0 or 1; 

setAnalogValue

int
setAnalogValue
(
int
value)

Description

Configures this IO pin as an analog/pwm output, and change the output value to the given level.

Parameters

int
value - the level to set on the output pin, in the range 0 - 1024

Returns

DEVICE_OK on success, DEVICE_INVALID_PARAMETER if value is out of range, or DEVICE_NOT_SUPPORTED if the given pin does not have analog capability.


setServoValue

int
setServoValue
(
int
value)

Description

Configures this IO pin as an analog/pwm output (if necessary) and configures the period to be 20ms, with a duty cycle between 500 us and 2500 us.

A value of 180 sets the duty cycle to be 2500us, and a value of 0 sets the duty cycle to be 500us by default.

This range can be modified to fine tune, and also tolerate different servos.

Parameters

int
value - the level to set on the output pin, in the range 0 - 180.

Returns

DEVICE_OK on success, DEVICE_INVALID_PARAMETER if value is out of range, or DEVICE_NOT_SUPPORTED if the given pin does not have analog capability.

int
setServoValue
(
int
value,
int
range)

Description

Configures this IO pin as an analog/pwm output (if necessary) and configures the period to be 20ms, with a duty cycle between 500 us and 2500 us.

A value of 180 sets the duty cycle to be 2500us, and a value of 0 sets the duty cycle to be 500us by default.

This range can be modified to fine tune, and also tolerate different servos.

Parameters

int
value - the level to set on the output pin, in the range 0 - 180.

int
range - which gives the span of possible values the i.e. the lower and upper bounds (center +/- range/2). Defaults to DEVICE_PIN_DEFAULT_SERVO_RANGE.

Returns

DEVICE_OK on success, DEVICE_INVALID_PARAMETER if value is out of range, or DEVICE_NOT_SUPPORTED if the given pin does not have analog capability.

int
setServoValue
(
int
value,
int
range,
int
center)

Description

Configures this IO pin as an analog/pwm output (if necessary) and configures the period to be 20ms, with a duty cycle between 500 us and 2500 us.

A value of 180 sets the duty cycle to be 2500us, and a value of 0 sets the duty cycle to be 500us by default.

This range can be modified to fine tune, and also tolerate different servos.

Parameters

int
value - the level to set on the output pin, in the range 0 - 180.

int
range - which gives the span of possible values the i.e. the lower and upper bounds (center +/- range/2). Defaults to DEVICE_PIN_DEFAULT_SERVO_RANGE.

int
center - the center point from which to calculate the lower and upper bounds. Defaults to DEVICE_PIN_DEFAULT_SERVO_CENTER

Returns

DEVICE_OK on success, DEVICE_INVALID_PARAMETER if value is out of range, or DEVICE_NOT_SUPPORTED if the given pin does not have analog capability.


getAnalogValue

int
getAnalogValue
()

Description

Configures this IO pin as an analogue input (if necessary), and samples the Pin for its analog value.

Returns

the current analogue level on the pin, in the range 0 - 1024, or DEVICE_NOT_SUPPORTED if the given pin does not have analog capability.

Example
 Pin P0(DEVICE_ID_IO_P0, DEVICE_PIN_P0, PIN_CAPABILITY_BOTH); 
 P0.getAnalogValue(); // P0 is a value in the range of 0 - 1024 

isInput

int
isInput
()

Description

Determines if this IO pin is currently configured as an input.

Returns

1 if pin is an analog or digital input, 0 otherwise.


isOutput

int
isOutput
()

Description

Determines if this IO pin is currently configured as an output.

Returns

1 if pin is an analog or digital output, 0 otherwise.


isDigital

int
isDigital
()

Description

Determines if this IO pin is currently configured for digital use.

Returns

1 if pin is digital, 0 otherwise.


isAnalog

int
isAnalog
()

Description

Determines if this IO pin is currently configured for analog use.

Returns

1 if pin is analog, 0 otherwise.


isTouched

int
isTouched
()

Description

Configures this IO pin as a "makey makey" style touch sensor (if necessary) and tests its current debounced state.

Users can also subscribe to DeviceButton events generated from this pin.

Returns

1 if pin is touched, 0 if not, or DEVICE_NOT_SUPPORTED if this pin does not support touch capability.

Example
 DeviceMessageBus bus; 

 Pin P0(DEVICE_ID_IO_P0, DEVICE_PIN_P0, PIN_CAPABILITY_ALL); 
 if(P0.isTouched()) 
 { 
 //do something! 
 } 

 // subscribe to events generated by this pin! 
 bus.listen(DEVICE_ID_IO_P0, DEVICE_BUTTON_EVT_CLICK, someFunction); 

setServoPulseUs

int
setServoPulseUs
(
uint32_t
pulseWidth)

Description

Configures this IO pin as an analog/pwm output if it isn't already, configures the period to be 20ms, and sets the pulse width, based on the value it is given.

Parameters

uint32_t
pulseWidth - the desired pulse width in microseconds.

Returns

DEVICE_OK on success, DEVICE_INVALID_PARAMETER if value is out of range, or DEVICE_NOT_SUPPORTED if the given pin does not have analog capability.


setAnalogPeriod

int
setAnalogPeriod
(
int
period)

Description

Configures the PWM period of the analog output to the given value.

Parameters

int
period - The new period for the analog output in milliseconds.

Returns

DEVICE_OK on success, or DEVICE_NOT_SUPPORTED if the given pin is not configured as an analog output.


setAnalogPeriodUs

int
setAnalogPeriodUs
(
uint32_t
period)

Description

Configures the PWM period of the analog output to the given value.

Parameters

uint32_t
period - The new period for the analog output in microseconds.

Returns

DEVICE_OK on success, or DEVICE_NOT_SUPPORTED if the given pin is not configured as an analog output.


getAnalogPeriodUs

uint32_t
getAnalogPeriodUs
()

Description

Obtains the PWM period of the analog output in microseconds.

Returns

the period on success, or DEVICE_NOT_SUPPORTED if the given pin is not configured as an analog output.


getAnalogPeriod

int
getAnalogPeriod
()

Description

Obtains the PWM period of the analog output in milliseconds.

Returns

the period on success, or DEVICE_NOT_SUPPORTED if the given pin is not configured as an analog output.


setPull

int
setPull
(
PullMode
pull)

Description

Configures the pull of this pin.

Parameters

PullMode
pull - one of the mbed pull configurations: PullUp, PullDown, PullNone

Returns

DEVICE_NOT_SUPPORTED if the current pin configuration is anything other than a digital input, otherwise DEVICE_OK.


drainPin

int
drainPin
()

Description

Utility function to drain any residual capacitative charge held on a pin. This is useful for applicaitons that measure rise/fall time of digital inputs, such as resititve touch sensors like makeymakey.

Returns

DEVICE_NOT_SUPPORTED if the current pin configuration is anything other than a digital input, otherwise DEVICE_OK.


setIRQ

int
setIRQ
(
void(*)(int)
gpio_interrupt)

Parameters

void(*)(int)
gpio_interrupt


getPulseUs

int
getPulseUs
(
int
timeout)

Description

Measures the period of the next digital pulse on this pin. The polarity of the detected pulse is defined by setPolarity() . The calling fiber is blocked until a pulse is received or the specified timeout passes.

Parameters

int
timeout - The maximum period of time in microseconds to wait for a pulse.

Returns

the period of the pulse in microseconds, or DEVICE_CANCELLED on timeout.


eventOn

int
eventOn
(
int
eventType)

Description

Configures the events generated by this Pin instance.

DEVICE_PIN_INTERRUPT_ON_EDGE - Configures this pin to a digital input, and invokes gpio_irq with the new state, whenever a rise/fall is detected on this pin. DEVICE_PIN_EVENT_ON_EDGE - Configures this pin to a digital input, and generates events whenever a rise/fall is detected on this pin. (DEVICE_PIN_EVT_RISE, DEVICE_PIN_EVT_FALL) DEVICE_PIN_EVENT_ON_PULSE - Configures this pin to a digital input, and generates events where the timestamp is the duration that this pin was either HI or LO. (DEVICE_PIN_EVT_PULSE_HI, DEVICE_PIN_EVT_PULSE_LO) DEVICE_PIN_EVENT_ON_TOUCH - Configures this pin as a makey makey style touch sensor, in the form of a DeviceButton. Normal button events will be generated using the ID of this pin. DEVICE_PIN_EVENT_ON_TOUCH - Configures this pin as a makey makey style touch sensor, in the form of a DeviceButton. Normal button events will be generated using the ID of this pin. DEVICE_PIN_EVENT_NONE - Disables events for this pin.

Parameters

int
eventType - One of: DEVICE_PIN_EVENT_ON_EDGE, DEVICE_PIN_EVENT_ON_PULSE, DEVICE_PIN_EVENT_ON_TOUCH, DEVICE_PIN_EVENT_NONE

Returns

DEVICE_OK on success, or DEVICE_INVALID_PARAMETER if the given eventype does not match

Example
 DeviceMessageBus bus; 

 Pin P0(DEVICE_ID_IO_P0, DEVICE_PIN_P0, PIN_CAPABILITY_BOTH); 
 P0.eventOn(DEVICE_PIN_EVENT_ON_PULSE); 

 void onPulse(Event evt) 
 { 
 int duration = evt.timestamp; 
 } 

 bus.listen(DEVICE_ID_IO_P0, DEVICE_PIN_EVT_PULSE_HI, onPulse, MESSAGE_BUS_LISTENER_IMMEDIATE) 

Note

In the DEVICE_PIN_EVENT_ON_PULSE mode, the smallest pulse that was reliably detected was 85us, around 5khz. If more precision is required, please use the InterruptIn class supplied by ARM mbed.


getAndSetDigitalValue

int
getAndSetDigitalValue
(
int
value)

Description

Set pin value iff its current value as input is the opposite.

If pin is configured as input and reads as !value, set it to value and return DEVICE_OK. Otherwise, do nothing and return DEVICE_BUSY. Note, that this is overwritten in hardware-specific classes to check the condition immedietly before changing the pin value.

Parameters

int
value


isActive

int
isActive
()

Description

Determines if pin is active, taking into account polarity information defined by setActive().

By default, pins are ACTIVE_HI (a high voltage implies a TRUE logic state).

Returns

1 if the digital value read from this pin matches its active polarity state, and 0 otherwise.


setPolarity

void
setPolarity
(
int
polarity)

Description

Sets the polarity of the pin to be ACTIVE_HI or ATIVE_LO, depending on the given parameter.

polarity

The polarity of the pin - either 1 for ACTIVE_HI or 0 for ACTIVE_LO

Parameters

int
polarity - The polarity of the pin - either 1 for ACTIVE_HI or 0 for ACTIVE_LO


getPolarity

int
getPolarity
()

Description

Determines the polarity of the pin - either ACTIVE_HI or ATIVE_LO.

Returns

1 for ACTIVE_HI or 0 for ACTIVE_LO


setActiveHi

void
setActiveHi
()

Description

Sets the polarity of the pin to be ACTIVE_HI.


setActiveLo

void
setActiveLo
()

Description

Sets the polarity of the pin to be ACTIVE_LO.


disconnect

void
disconnect
()

Description

Disconnect any attached peripherals from this pin.


Component Constructor

Advanced users only

Do not use this unless you really know what you're doing. It's usually best to use uBit.

Pin(
int
id,
PinNumber
name,
PinCapability
capability)

Description

Constructor. Create a Pin instance, generally used to represent a pin on the edge connector.

Parameters

int
id - the unique EventModel id of this component.

PinNumber
name - the PinNumber for this Pin instance.

PinCapability
capability - the capabilities this Pin instance should have. (PIN_CAPABILITY_DIGITAL, PIN_CAPABILITY_ANALOG, PIN_CAPABILITY_AD, PIN_CAPABILITY_ALL)

Example
 Pin P0(DEVICE_ID_IO_P0, DEVICE_PIN_P0, PIN_CAPABILITY_ALL);