The Arduino provides a total of 18 pins for either digital input or output (labelled 2 to 13 and A0 to A5), including 6 for analogue input (labelled A0 to A5).
The Arduino can be accessed using the
arduino property of the
my_arduino = r.arduino
You can use the GPIO (General Purpose Input/Output) pins for anything, from microswitches to LEDs. GPIO is only available on pins 2 to 13 and A0 to A5 because pins 0 and 1 are reserved for communication with the rest of our kit.
In the simulator, the Arduino’s pins are pre-populated and pre-configured. The first few digital pins are occupied by digital inputs, the next few by digital outputs, and the analogue pins are attached to ultrasound sensors.
To find out how many inputs and outputs each type of robot has, check the robot docs.
You won’t be able to change pin mode like in a physical robot (see below), but pins 0 and 1 are still unavailable.
Each robot has a number of digital inputs, starting from pin 2. If your robot has 5 inputs, those would occupy pins 2-6.
These all have a digital state which you can read as a boolean.
bumper_pressed = r.arduino.pins.digital_state
The digital outputs start the pin after the last input. If your robot has 5 inputs and 3 outputs, the outputs would occupy pins 7-9.
You can set their state similarly to reading the inputs, and you can also read the last value that was set.
led_state = r.arduino.pins.digital_state r.arduino.pins.digital_state = not led_state # Toggle output
Any analogue input devices (e.g. distance sensors) are connected to the
Arduino’s analogue input pins starting from pin
A0. You can read their
values like this:
distance = r.arduino.pins[AnaloguePin.A0].analogue_value
The value read is returned as a float.
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 sbot import GPIOPinMode
The input modes closely resemble those of an Arduino. More information on them can be found in their docs.
You will need to ensure that the pin is in the correct pin mode before performing an action with that pin. You can read about the possible pin modes below.
r.arduino.pins.mode = GPIOPinMode.DIGITAL_INPUT_PULLUP
In this mode, the digital state of the pin (whether it is high or low) can be read.
r.arduino.pins.mode = GPIOPinMode.DIGITAL_INPUT pin_value = r.arduino.pins.digital_state
GPIOPinMode.DIGITAL_INPUT, but with an internal pull-up
r.arduino.pins.mode = GPIOPinMode.DIGITAL_INPUT_PULLUP pin_value = r.arduino.pins.digital_state
In this mode, we can set binary values of
5V to the pin.
r.arduino.pins.mode = GPIOPinMode.DIGITAL_OUTPUT r.arduino.pins.mode = GPIOPinMode.DIGITAL_OUTPUT r.arduino.pins.digital_state = True r.arduino.pins.digital_state = False
Certain sensors output analogue signals rather than digital ones, and so
have to be read differently. The Arduino has six analogue inputs, which
A5; however pins
A5 are reserved and
cannot be used.
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.
from sbot import AnaloguePin r.arduino.pins[AnaloguePin.A0].mode = GPIOPinMode.ANALOGUE_INPUT pin_value = r.arduino.pins[AnaloguePin.A0].analogue_value
The values are the voltages read on the pins, between 0 and 5.
A5 are reserved and cannot be used.