Back to docs

GPIO (General Purpose Input Output) is a type of pin that the brain box has. They are very useful as they allow switches and other sensors to be added very easily.

First you must select the mode that the pin should operate in by using:

from sr.robot import *

R = Robot()                         #Standard initialization process


The pin is indexed from 1 to 4. The mode can be set to one of 3 modes.

  • OUTPUT – Sets the pin up as an output
  • INPUT_ANALOG – Sets the pin up to take analog readings
  • INPUT_PULLUP – Creates a weak pull up on the pin to prevent it floating and sets it as an input
We are using the American spelling which is ‘analog’. PLEASE do not use ‘ analogue’ .

GPIO as Outputs

If you selected the OUTPUT mode you will be able to select the state of the pin by using:


Where the state is a Boolean (true or false) value that the pin should be set to.

GPIO as digital inputs

Normally, input pins are not connected to anything – known as “floating”. In this state, they might read high or low, or different values depending on their environment. This is obviously not good for consistent control.

Many pieces of off-the-shelf electronics that have some form of standard I/O output will connect this pin to 3.3V (high) and 0V (low) when required, so this is not a problem. However, for simple electronics, a microswitch for example, you would normally be required to connect a resistor between the input pin and 3.3V (a pull-up resistor), or between the input pin and 0V (a pull-down resistor) to keep the input in a known state until the switch overrides it by connecting directly to the opposite state.

However, the built-in pull-up resistor alleviates this need. It essentially wires in a resistor connected to 3.3V, meaning that when this option is enabled, an input pin will “default” to being high. This means you can simply connect a switch between the input pin and a ground pin without any need of resistors – when the switch is open, the pin will read high; when closed, it will read low. This is why the digital input mode is called INPUT_PULLUP.

When the pin is set to be a digital input there is a piece of code that can be called a will return a value:


This piece of code can then be used to assign a value to a variable like so

Result = R.gpio.digital_read(<PIN>)

You can then use that value for what-ever you like

For example, to do something if GPIO 1 is high:

From sr.robot import *

R = Robot()

R.gpio.pin_mode(1, INPUT_PULLUP)

Result = _read(1)

If Result == TRUE:

Print ‘So it is TRUE’

GPIO as analog inputs

In this mode the pins will be used in much the same way as before.


Only this time it will return a value between 1 and 16384. GPIO 3 is also incapable of being an analogue input and will return an error.

We are using the American spelling which is ‘analog’. PLEASE do not use ‘ analogue’ .
Last Updated On December 17, 2017