Robot Expansion UnitClass Library Reference

Published 2019/04/23
Revised 2019/09/19

Revision History

Date

Revised Contents

Revised2019/4/23

First release2019/9/19

Color Sensors and Ultrasonic Sensors added

Index1.

Getting Started ....................................................................................................... 1

2.

ArtecRobo 2.0 ........................................................................................................ 1
2.1.

Robot Expansion Unit Layout .................................................................................. 1

2.2.

MicroPython ............................................................................................................. 3

3.

Development Environment ..................................................................................... 3

4.

ArtecRobo 2.0 Class Library .................................................................................. 4
4.1.

The DCMotor Class ................................................................................................. 4

4.1.1.

Constructors .................................................................................................... 4

4.1.2.

Controlling a DC Motor .................................................................................... 5

4.2.

The Servomotor Class ............................................................................................. 6

4.2.1.

Constructors .................................................................................................... 6

4.2.2.

Controlling Servomotors .................................................................................. 6

4.2.3.

Releasing PWMs ............................................................................................. 6

4.3.

The Buzzer Class..................................................................................................... 7

4.3.1.

Constructors .................................................................................................... 7

4.3.2.

Controlling Buzzers ......................................................................................... 7

4.3.3.

Releasing PWMs ............................................................................................. 7

4.4.

The LED Class ......................................................................................................... 8

4.4.1.

Constructors .................................................................................................... 8

4.4.2.

Controlling LEDs .............................................................................................. 8

4.5.

The IRPhotoReflector Class .................................................................................... 9

4.5.1.

Constructors .................................................................................................... 9

4.5.2.

Finding IR Photoreflector Values..................................................................... 9

4.6.

The LightSensor Class........................................................................................... 10

4.6.1.

Constructors .................................................................................................. 10

4.6.2.

Finding Light Sensor Values ......................................................................... 10

4.7.

The SoundSensor Class ........................................................................................ 11

4.7.1.

Constructors .................................................................................................. 11

4.7.2.

Finding Sound Sensor Values ....................................................................... 11

4.8.

The TouchSensor Class ........................................................................................ 12

4.8.1.

Constructors .................................................................................................. 12

4.8.2.

Finding Touch Sensor Values ....................................................................... 12

4.9.

The Temperature Class ......................................................................................... 13

4.9.1.

Constructors .................................................................................................. 13

4.9.2.

Finding Temperature Sensor Values............................................................. 13

4.10. The Accelerometer Class ...................................................................................... 14
4.10.1.

Constructors .................................................................................................. 14

4.10.2.

Finding Accelerometer Values ...................................................................... 14

4.11. The UltrasonicSensor Class .................................................................................. 15
4.11.1.

Constructors .................................................................................................. 15

4.11.2.

Finding Ultrasonic Sensor Values ................................................................. 15

4.12. The ColorSensor Class .......................................................................................... 16

5.

4.12.1.

Constructors .................................................................................................. 16

4.12.2.

Finding Color Sensor Values ......................................................................... 16

Appendices .......................................................................................................... 17
5.1.

Sound Output ......................................................................................................... 17

5.2.

Color Codes ........................................................................................................... 17

5.3.

Class Table ............................................................................................................ 18

1. Getting Started
This manual is a reference guide for the ArtecRobo 2.0 class library. Using this manual
requires some prior understanding of the basics of Python.

2. ArtecRobo 2.0
ArtecRobo 2.0 consists of the Core Unit (Studuino:bit) and the Robot Expansion Unit.
MicroPython is one of the programming environments compatible with ArtecRobo 2.0.

2.1. Robot Expansion Unit Layout
The layout of the Robot Expansion Unit is shown below.

1The Robot Expansion Unit provides additional ports you can use to attach sensors andmotors to your robot. It contains digital output ports, DC Motor ports, analog input ports, andan I2C communications port. Consult the table below to see which parts are usable withwhich ports.
Port

Parts

Digital Output Ports

LEDs, Buzzers, Servomotors

DC Motor Ports

DC Motors

Analog Input Ports

Light Sensors, Sound Sensors, Touch Sensors, IR Photoreflectors

I2C Communication

Accelerometers

Port

2

2.2. MicroPython
MicroPython is version of Python developed specifically for use with microcontrollers. The
programming syntax is identical to Python 3.0, but some Python libraries are not usable in
MicroPython because they’re not designed to work with the limited memory and CPU of a
microcontrolller. However, some MicroPython-specific libraries (such as the library for GPIO
microcontrollers) are included as standard.
MicroPython has been widely ported to different microcontrollers, and the ArtecRobo 2.0
library utilizes a customized version.

3. Development Environment
uPyCraft is one development environment used for MicroPython. See section 6.2 in theCore Unit Class Library Reference manual for instructions on how to use uPyCraft.

3

4. ArtecRobo 2.0 Class Library
The ArtecRobo 2.0 class library is structured as follows.
Packagepyatcrobo2

Module
parts

Class

Parts

DCMotor

DC Motors

Servomotor

Servomotors

BuzzerBuzzers

LEDs

LEDs

LightSensor

Light Sensors

SoundSensor

Sound Sensors

TouchSensor

Touch Sensors

Temperature

Temperature Sensors

UltrasonicSensor

Ultrasonic Sensors

ColorSensor

Color Sensors

Accelerometer

Accelerometers4.1. The DCMotor ClassThis class is used to control DC Motors.

4.1.1. Constructors
Use this to make objects that control DC Motors plugged into a specified DC Motor port (M1or M2) on the Robot Expansion Unit.
from pyatcrobo2.parts import DCMotor
m1 = DCMotor(‘M1’)

4

4.1.2. Controlling a DC Motor
Use the cw() method to make a DC Motor spin clockwise and the ccw() method to make itspin counter-clockwise. The stop() method will stop making the DC Motor spin, while thebrake() method will bring the DC Motor’s movement to a full stop. The power(power) method
can be used to adjust a DC Motor’s speed. Set the power parameter to a number between 0
and 255 to pick a speed.
from pyatcrobo2.parts import DCMotor
import time
m1 = DCMotor(‘M1’)
m2 = DCMotor(‘M2’)
m1.power(100)m2.power(100)m1.cw()
m2.cw()
time.sleep_ms(1000)
m1.stop()
m2.stop()This will make the DC Motors connected to ports M1 and M2 spin clockwise at speed 100 for
1 second before stopping.

5

4.2. The Servomotor ClassThis class is used to control Servomotors.

4.2.1. Constructors
Use this to make objects that control Servomotors plugged into a specified port (P13, P14,
P15 or P16) on the Robot Expansion Unit.
from pyatcrobo2.parts import Servomotor
sv13 = Servomotor(‘P13’)

4.2.2. Controlling Servomotors
Use the set_angle(degree) method to set a Servomotor’s angle.
from pyatcrobo2.parts import Servomotor
import time
sv13 = Servomotor(‘P13’)
sv13.set_angle(0)time.sleep_ms(1000)
sv13.set_angle(180)This will make the Servomotor connected to port P13 turn to 0°, then turn to 180° 1 second
later.

4.2.3. Releasing PWMs
ArtecRobo 2.0 uses PWM to set Servomotor angles. It can use up to four PWMs
simultaneously. If you’re using 5 or more parts that utilize PWMs, you will be unable to use
any Servomotor objects you make. You can resolve this by using the release() method torelease a PWM assigned to a different Servomotor object.

6

4.3. The Buzzer ClassThis class is used to control the Buzzers.

4.3.1. Constructors
Use this to make objects that control Buzzers plugged into a specified port (P13, P14, P15
or P16) on the Robot Expansion Unit.
from pyatcrobo2.parts import Buzzer
bz13 = Buzzer(‘P13’)

4.3.2. Controlling Buzzers
You can use the method on(sound, *, volume=None, duration=None) to play sound from
Buzzers. Use the sound parameter to set the frequency of the sound using a MIDI notenumber or note name. For a list of usable note numbers and names, see section 5.1. The
volume parameters lets you set the volume from 0 to 100. The duration parameter is used to
set the length of the sound output in milliseconds. If the duration parameter is left
unspecified, the off() method can be used to stop the buzzer.
from pyatcrobo2.parts import Buzzer
bz13 = Buzzer(‘P13’)
bz13.on(‘C4’, duration=1000)
This will make the Buzzer plugged into port P13 on the Robot Expansion Unit play the note
C4 for 1 second.

4.3.3. Releasing PWMs
ArtecRobo 2.0 uses PWM for its sound output. It can use up to four PWMs simultaneously. If
you’re using 5 or more parts that utilize PWMs, you will be unable to use any Buzzer objects
you make. You can resolve this by using the release() method to release a PWM assigned
to a different Buzzer object.

7

4.4. The LED ClassThis class is used to control LEDs.

4.4.1. Constructors
Use this to make objects that control LEDs plugged into a specified port (P13, P14, P15 orP16) on the Robot Expansion Unit.
from pyatcrobo2.parts import LED
led13 = LED(‘P13’)

4.4.2. Controlling LEDs
Use the on() method to switch an LED on and and the off() method to switch it off.
from pyatcrobo2.parts import LED
import time
led13 = LED(‘P13’)
while True:
led13.on()
time.sleep_ms(500)
led13.off()time.sleep_ms(500)This will make the LED connected to port P13 on the Robot Expansion Unit turn on and off
in 500 millisecond intervals.

8

4.5. The IRPhotoReflector ClassThis class is used to control IR Photoreflectors.

4.5.1. Constructors
Use this to make objects that control IR Photoreflectors plugged into a specified port (P0, P1,
or P2) on the Robot Expansion Unit.
from pyatcrobo2.parts import IRPhotoReflector
sensor0 = IRPhotoReflector(‘P0’)

4.5.2. Finding IR Photoreflector Values
Use the method get_value() to find the analog value of an IR Photoreflector. It will beformatted as an integer from 0 to 4095.
from pyatcrobo2.parts import IRPhotoReflector
import time
sensor0 = IRPhotoReflector(‘P0’)
while True:
print(sensor0.get_value())
time.sleep_ms(500)This will display the value of the IR Photoreflector plugged into port P0 on the Robot
Expansion Unit through the terminal every 500 milliseconds.

94.6. The LightSensor ClassThis class is used to control Light Sensors.

4.6.1. Constructors
Use this to make objects that control Light Sensors plugged into a specified port (P0, P1, or
P2) on the Robot Expansion Unit.
from pyatcrobo2.parts import LightSensor
sensor0 = LightSensor(‘P0’)

4.6.2. Finding Light Sensor Values
Use the method get_value() to find the analog value of a Light Sensor. It will be formatted as
an integer from 0 to 4095.
from pyatcrobo2.parts import LightSensor
import time
sensor0 = LightSensor(‘P0’)
while True:
print(sensor0.get_value())
time.sleep_ms(500)This will display the value of the Light Sensor plugged into port P0 on the Robot Expansion
Unit through the terminal every 500 milliseconds.

10

4.7. The SoundSensor ClassThis class is used to control Sound Sensors.

4.7.1. Constructors
Use this to make objects that control Sound Sensors plugged into a specified port (P0, P1,
or P2) on the Robot Expansion Unit.
from pyatcrobo2.parts import SoundSensor
sensor0 = SoundSensor(‘P0’)

4.7.2. Finding Sound Sensor Values
Use the method get_value() to find the analog value of a Sound Sensor. It will be formatted
as an integer from 0 to 4095.
from pyatcrobo2.parts import SoundSensor
import time
sensor0 = SoundSensor(‘P0’)
while True:
print(sensor0.get_value())
time.sleep_ms(500)This will display the value of the Sound Sensor plugged into port P0 on the Robot Expansion
Unit through the terminal every 500 milliseconds.

11

4.8. The TouchSensor ClassThis class is used to control Touch Sensors.

4.8.1. Constructors
Use this to make objects that control Touch Sensors plugged into a specified port (P0, P1,
or P2) on the Robot Expansion Unit.
from pyatcrobo2.parts import TouchSensor
sensor0 = TouchSensor(‘P0’)

4.8.2. Finding Touch Sensor Values
Use the method get_value() to find the digital value of a Touch Sensor. The value will beeither 0 or 1. Using the is_pressed() method will return a True/False to tell you whether the
Touch Sensor is being pressed.
from pyatcrobo2.parts import TouchSensor
import time
sensor0 = TouchSensor(‘P0’)
while True:
print(sensor0.get_value())
print(sensor0.is_pressed())
time.sleep_ms(500)This will state whether the Touch Sensor plugged into port P0 on the Robot Expansion Unit
is being pressed or not on through the terminal every 500 milliseconds. If the Touch Sensor
is being pressed, it will display 0 and True. If not, it will display 1 and False.

124.9. The Temperature ClassThis class is used to control Temperature Sensors.

4.9.1. Constructors
Use this to make objects that control Temperature Sensors plugged into a specified port (P0,P1, or P2) on the Robot Expansion Unit.
from pyatcrobo2.parts import Temperature
sensor0 = Temperature(‘P0’)

4.9.2. Finding Temperature Sensor Values
Use the method get_value() to find the analog value of a Temperature Sensor. It will beformatted as an integer from 0 to 4095. The get_celsius() method can be used to find a
Temperature Sensor’s value in degrees Celsius.
from pyatcrobo2.parts import Temperatureimport time
sensor0 = Temperature(‘P0’)
while True:
print(sensor0.get_value())
print(sensor0.get_celsius())
time.sleep_ms(500)This will display the analog value and the temperature in Celsius from the Temperature
Sensor plugged into port P0 through the terminal every 500 milliseconds.

13

4.10. The Accelerometer ClassThis class is used to operate Accelerometers.

4.10.1. Constructors
Use this to make objects that control Accelerometers plugged into the I2C port on the Robot
Expansion Unit.
from pyatcrobo2.parts import Accelerometer
sensor0 = Accelerometer(‘I2C’)
The Accelerometer has a full scale (maximum measurable acceleration) of ± 2G, shown inunits of G.

4.10.2. Finding Accelerometer Values
Use the get_values() method to find your Accelerometer’s values. The values will be
returned in a tuple (x, y, z) format. The get_x(), get_y() and get_z() methods can each beused find the acceleration along the x-axis, y-axis and z-axis separately.
from pyatcrobo2.parts import Accelerometer
import time
sensor0 = Accelerometer(‘I2C’)
while True:
print(sensor0.get_values())
time.sleep_ms(500)This will display the values of the Accelerometer plugged into the I2C port on the Robot
Expansion Unit through the terminal every 500 milliseconds.

14

4.11. The UltrasonicSensor ClassThis class is used to control Ultrasonic Sensors.

4.11.1. Constructors
Use this to make objects that control Ultrasonic Sensors plugged into a specified port (P0 or
P1) on the Robot Expansion Unit.
from pyatcrobo2.parts import UltrasonicSensor
sensor0 = UltrasonicSensor (‘P0’)

4.11.2. Finding Ultrasonic Sensor Values
Use the method get_distance() to find how far a target object is from the sensor. The
distance will be measured in cm and returned as a float-type integer. You can also use themethod get_pulse_time() to find the length of time (in microseconds) it takes for an
ultrasonic wave to bounce back to the sensor after it is emitted. If the distance can’t bemeasured, a Runtime Error exception will occur.
from pyatcrobo2.parts import UltrasonicSensor
import time
sensor0 = UltrasonicSensor('P0')
while True:
try:
print(sensor0.get_distance())
except RuntimeError as e:
print(e)time.sleep_ms(100)This will display the value of the Ultrasonic Sensor plugged into port P0 on the Robot
Expansion Unit through the terminal every 100 milliseconds.

15

4.12. The ColorSensor ClassThis class is used to control Color Sensors.

4.12.1. Constructors
Use this to make objects that control Color Sensors plugged into the I2C port on the Robot
Expansion Unit.
from pyatcrobo2.parts import ColorSensor
sensor0 = ColorSensor (‘I2C’)

4.12.2. Finding Color Sensor Values
Use the get_values() method to find the color value of a target object. The color value will be
returned in a tuple format showing the saturation of each component in the color (red, green,
blue, brightness). The get_colorcode() method finds the color of the target object.
from pyatcrobo2.parts import ColorSensor
import timecolor = ColorSensor('I2C')
while True:
try:
print(color.get_values())
print(color.get_colorcode())
except RuntimeError as e:
print(e)time.sleep_ms(100)This will display the values of the Color Sensor plugged into the I2C port on the Robot
Expansion Unit through the terminal every 100 milliseconds.

16

5. Appendices5.1. Sound Output
The MIDI note numbers and note names used in the Buzzer class are as follows.
MIDI
48
49
50
51
52
53
54
55
56
57
58
59

Note
C3
CS3
D3
DS3
E3
F3
FS3
G3
GS3
A3
AS3
B3

MIDI
60
61
62
63
64
65
66
67
68
69
70
71

Note
C4
CS4
D4
DS4
E4
F4
FS4
G4
GS4
A4
AS4
B4

MIDI
72
73
74
75
76
77
78
79
80
81
82
83

Note
C5
CS5
D5
DS5
E5
F5
FS5
G5
GS5
A5
AS5
B5

MIDI
84
85
86
87
88
89
90
91
92
93
94
95

Note
C6
CS6
D6
DS6
E6
F6
FS6
G6
GS6
A6
AS6
B6

MIDI
96
97
98
99
100
101
102
103
104
105
106
107

Note
C7
CS7
D7
DS7
E7
F7
FS7
G7
GS7
A7
AS7
B7

MIDI
108
109
110
111
112
113
114
115
116
117
118
119

Note
C8
CS8
D8
DS8
E8
F8
FS8
G8
GS8
A8
AS8
B8

MIDI
120
121
122
123
124
125
126
127

5.2. Color Codes
Constant

Color

Value

COLOR_RED

Red

1

COLOR_GREEN

Green

2

COLOR_BLUE

Blue

3

COLOR_WHITE

White

4

COLOR_YELLOWYellow

5

COLOR_ORANGE

Orange6

COLOR_PURPLE

Purple7

COLOR_UNDEF

Unknown

0

17

Note
C9
CS9
D9
DS9
E9
F9
FS9
G9

5.3. Class Table
The full ArtecRobo2 class library is as follows.
Function

Class/Module

Method/Attribute__init__(pin)DC Motors

DCMotor

Servomotor

Use the pin parameter to specify pin M1 or M2 and create a DC Motor instance.
dcm = DCMotor('M1')

cw()

Spin a DC Motor clockwise.

ccw()

Spin a DC Motor counter-clockwise.

stop()

Cease spinning a DC Motor.

brake()

Completely stop the rotation of a DC Motor.

power(power)

Set the power parameter to a number 0-255 to change a DC Motor’s speed.

__init__(pin)Servomotors

Instructions

set_angle(degree)
release()Use the pin parameter to specify pin P13, P14, P15 or P16 and create a Servomotor instance.
sv = ServoMotor('P13')
Set the degree parameter to an angle 0-180 to change a Servomotor’s angle.
sv.set_angle(90)Release a PWM assigned to a Servomotor object.

18

__init__(pin)Use the pin parameter to specify pin P13, P14, P15 or P16 and create a Buzzer instance.
bzr = Buzzer('P13')
Set the Buzzer’s sound in the sound, duration, and volume parameters. sound can be set using note names(C3-G9), MIDI note numbers (48-127), or frequency (as an integer). volume can be set 1-100, and duration
can be set in ms. If the duration parameter is left unspecified, the Buzzer will keep playing until you call the offon(sound, *,Buzzers

Buzzermethod. If the volume parameter is left unspecified the volume will be adjusted according to the pitch of thevolume=-1,

sound.

duration=-1)

bzr.on('50', duration=1000)# The buzzer will play the note corresponding to MIDI note number 50 for 1

second.
bzr.on('C4', volume=1, duration=1000)# The buzzer will plat note C4 at volume 1 for 1 second.

bzr.on(440)off()
release()__init__(pin)
LEDs

LED

on()
off()

IR
Photoreflectors__init__(pin)IRPhotoReflector
get_value()

Make the Buzzer stop playing sound.
bzr.off()
Release a PWM assigned to a Buzzer object.
Use the pin parameter to specify pin P13, P14, P15 or P16 and create an LED instance.
led = LED('P13')
Switch an LED on.
led.on()Switch an LED off.
led.on()Use the pin parameter to specify pin P0, P1 or P2 and create an IR Photoreflector instance.
irp = IRPhotoReflector('P0')
Retrieve the sensor’s value (0-4095).
irp.get_value()19

__init__(pin)Light Sensors

LightSensor
get_value()__init__(pin)Temperature
Sensors

Temperature

get_value()
get_celsius()__init__(pin)Sound Sensors

SoundSensor
get_value()__init__(pin)Touch Sensors

TouchSensor

get_value()
is_pressed()UltrasonicSensors

UltrasonicSensor

Use the pin parameter to specify pin P0, P1 or P2 and create a Light Sensor instance.
ls = LightSensor('P0')
Retrieve the sensor’s value (0-4095).
ls.get_value()
Use the pin parameter to specify pin P0, P1 or P2 and create a Temperature Sensor instance.
ts = Temperature('P0')
Retrieve the sensor’s value (0-4095).
ts.get_value()
Retrieve the value of the Temperature Sensor in degrees Celsius.
ts.get_celsius()
Use the pin parameter to specify pin P0, P1 or P2 and create a Sound Sensor instance.
ss = SoundSensor('P0')
Retrieve the sensor’s value (0-4095).
ss.get_value()
Use the pin parameter to specify pin P0, P1 or P2 and create a Touch Sensor instance.
ss = TouchSensor('P0')
Retrieve the sensor’s value (0 or 1).
ss.get_value()
Find the current state of the sensor.
Returns a True if the Touch Sensor is being pressed.

get_pulse_time ()Retrieve the time it takes for an ultrasonic wave to bounce back from the target object in microseconds.

get_distance()

Retrieve the distance from the target object as a float-type integer. The units of distance will be cm.

20

Color Sensors

ColorSensor

get_values()
get_colorcode__init__(pin)Retrieve the sensor’s color value in a tuple format (red, green, blue, brightness). Each value in the tuple is thesaturation of that component color.
Retrieve the color of the target object as a color code. (See section 5.2.)
Use the pin parameter to specify pin I2C and create an Accelerometer instance.
acc = Accelerometer('I2C')
Find the acceleration along the x, y, and z axes. The maximum measurement range is ±2G and the units are

Accelerometers

Accelerometer

get_values()

in G.
acc.get_values()

get_x= get_values()[0]get_y= get_values()[1]get_z= get_values()[2]21Robot Expansion Unit Class Library Reference