Arduino with SR firmware
The Arduino provides a total of 18 pins for either digital input or output (labelled 2 to 13 and A0 to A5), pins A0 to A5 also support analog input.
The kit can control multiple Arduinos at once, however we only provide one in the kit.
If there is exactly one Arduino connected to your robot, it can be accessed using the
arduino property of the
from sr.robot3 import * robot = Robot() my_arduino = robot.arduino
The serial number of each detected Arduino is printed to the log when your robot starts. It will look something like this:
sr.robot3.robot INFO - Found Arduino: 752303138333517171B1
If you have more than one Arduino attached, you need to specify which one you want to control.
This is done using the serial number of the board and the
For example, if you had a board whose serial number was “752303138333517171B1”, you need to do this instead:
my_arduino = robot.arduinos["752303138333517171B1"]
When you have more than one Arduino connected to your kit, you can’t use
This is because the kit needs to know which Arduino you want to control.
Setting pin modes
To use one of the pins on the Arduino, you must first set whether you want it to behave as an input or as an output. The possible modes for a pin are:
- set the pin to input mode
- set the pin to output mode
- set the pin to input mode with a pull-up resistor
An example of how to use this is below:
# set Arduino pin 2 to output robot.arduino.pins.mode = OUTPUT # set Arduino pin 3 to input robot.arduino.pins.mode = INPUT # set Arduino pin 4 to input and enable pull-up resistor robot.arduino.pins.mode = INPUT_PULLUP
You can read a digital input pin with the following code:
# to read Arduino's digital pin 3... value = robot.arduino.pins.digital_read() # to read Arduino's digital pin 7... value = robot.arduino.pins.digital_read()
value will now contain
False depending on whether the pin was high (5V) or low (0V), respectively.
You can read an analog input pin with the following code:
# to read Arduino's analog pin A0... value = robot.arduino.pins[A0].analog_read() # to read Arduino's analog pin A4... value = robot.arduino.pins[A4].analog_read()
The analog pin numbers are available as
You can only set digital outputs (there’s no analog_write, although feel free to modify the Arduino’s firmware to add the ability to output PWM (Pulse-width modulation) if you desire). To set a digital output pin, you would use the following:
# to set Arduino's pin 2 high: robot.arduino.pins.digital_write(True) # to set Arduino's pin 2 low: robot.arduino.pins.digital_write(False)
The Arduino possesses the ability to enable a built-in pull-up resistor on any input pin. This takes a small amount of explanation.
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 5.0V (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 5.0V (a pull-up resistor) to keep the input in a known state until the switch overrides it by connecting directly to 0V.
However, the built-in pull-up resistor alleviates this need. It essentially wires in a resistor connected to 5.0V, 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.