You can use the GPIO (General Purpose Input/Output) pins for anything, from microswitches to LEDs. GPIO is only available on pins 2 to 12, because pins 0 and 1 are reserved for communication with the rest of our kit.
GPIO pins have four different modes. A pin can only have one mode at a time, and some pins aren’t compatible with certain modes. These pin modes are represented by an enum which needs to be imported before they can be used.
from robot import PinMode
The input modes closely resemble those of an Arduino. More information on them can be found in their docs.
In this mode, the digital state of the pin (whether it is high or low) can be read.
PinMode.INPUT, but with an internal pull-up resistor enabled.
Sets the pin to a high output (+5V).
Sets the pin to a low output (0V).
from robot import PinMode r.servo_board.gpios.mode = PinMode.INPUT_PULLUP
To read the state of a pin, simply call
read(), which will return either
PinValue enum must be imported to be used.
from robot import PinMode, PinValue r.servo_board.gpios.read() == PinValue.HIGH >>> True
read() will raise an exception if the pin’s mode hasn’t been set to an input type beforehand.
Certain sensors output analogue signals rather than digital ones, and so have to be read differently. The servo assembly has six analogue inputs, of which four (analogue pins 4 and 5) are reserved for communication between the Arduino and the servo shield.
Analogue signals can have any voltage, while digital signals can only take on one of two voltages. You can read more about digital vs analogue signals here.
To read the value of an analogue pin, call
read_analogue() on the servo board. This will give you the value of all analogue pins.
r.servo_board.read_analogue()["a0"] >>> 1.569
The values are the voltages read on the pins, between 0 and 5.