Python Tutorial
Last revision 2026/01/29
This Python tutorial offers a detailed guide to setting up a programming environment for robotics, focusing on installing Python, configuring PyCharm, and using the pymycobot package to control the MyCobot robotic arm, with step-by-step instructions and practical use cases for various robotic functions.
Hardware Preparation
Environment Building
pymycobot is a Python package used for serial communication with myCobot. It supports Python2, Python3.5 and later versions.
Before using pymycobot, make sure that you have built a Python environment, have a robot arm, and have made preparations.
- Install Python
Enter Python's official downloading webpage

There are many versions, and we choose the latest version here.

To confirm whether your PC is a 64-bit or 32-bit operating system, you can right-click on the My Computer icon on the left and select Properties. The following figure shows a 64-bit operating system, so we select the 64-bit Python installation package.


After downloading is complete, click Run, and the installation interface will appear. Remember to check Add Python 3.9 to PATH.

When the figure below appears, it means that the 64-bit Python is successfully installed.

Running Python. After successful installation, open the command prompt window (win+R, input cmd and press Enter), and type Python, there will be two situations:
Situation 1:

When this appears, it means that Python is successfully installed. When you see the prompt >>>, it means that we have been in the Python interactive environment. Input any Python code, press Enter and we will obtain the execution result immediately.
Situation 2:
Because I installed it successfully, I still want to provide a demo in which I used the wrong pythonn instead of python, so that an error message was prompted.

Configuring environment variables: The reason for doing so is that Windows will search python.exe according to the path set by a Path environment variable. If it is not found, an error will be reported. If you fail to check Add Python 3.9 to PATH during installation, you have to manually add the path where python.exe is located into Path. If you find that you forgot to check Add Python 3.9 or you cannot set the PATH, you have to reinstall Python, and do remember to check Add Python 3.9 to PATH. (Step 2: When an error message appear, it is generally due to failure to configure environment variables.)
Steps: Right-click My Computer -> select Properties -> select Advanced System Settings -> Select Environment Variables in the lower right corner.

The environment variables mainly include user variables and system variables. The environment variables that need to be set are in these two variables. The user variables means that you can use your own downloaded program in the cmd command, and that you can use it after writing the absolute path of the program into the user variables.


Here go back to and open the command prompt window (win+R; input cmd and press Enter), and type Python, displaying this means that there are no problems.

Detailed installation steps of pycharm
Pycharm is a powerful python editor with the nature of cross-platform. The way to install pycharm into Windows is introduced below.
After entering the website, we will see the following interface.

Download the file according to the introduction on the interface. Professional means the professional version, and community is the community version. It is recommended to install the latter version because it is free.
After downloading, click the file to install it. Here I install it in the E disk. Of course, you can install it in another disk after modifying the installation path.

Click Next to enter the next step:

Click Next to enter the next step:

Click Install to install it. After installation is completed, the following interface appears, and click Finish to end the installation:

After completing the above steps, it means that Pycharm is successfully installed. At this time, let's enter the software and create our first program:
(1) Click the pycharm icon on the desktop, enter pycharm, and click OK directly, as shown in the figure below:

(2) Select the second one, and then click OK:

(3) Click the Accept in the figure above to enter the next step:

(4) Click the OK in the figure above to enter the next step

(5) Click New Project to enter the interface shown in the figure below. The interpreter therein is used for you to select the python to be installed; Location is used to customize the project storage directory; after selecting the directory, click Create.

(6) In the interface you have entered as shown in the figure below, right-click the place pointed by the arrow in the figure, select python file, and fill in the file name in the pop-up box (It is allowed to fill in any file name).



(7) After the file is successfully created, you will enter the following interface where you can write your own program.


Preparations
Be sure to burn AtomMain for Atom at the top and minirobot for Basic at the bottom. And then choose transponder function. For device firmware burning, refer to MyStudio) chapter.
The downloading address for firmwares AtomMain and minirobot
You can also use myStudio to burn the firmware. MyStudio's downloading address
Pip installation command
Open a console terminal (shortcut key: Win+R; input cmd and enter the terminal), and input the following command:
pip install pymycobot --upgrade --user

Notes:
Currently only Atom2.4 and later versions are available. If you are using previous versions, install pymycobot 1.0.7.
Open a console terminal (shortcut key: Win+R; input cmd and enter the terminal), and input the following command:
pip install pymycobot==1.0.7 --user

Installing source code
Open a console terminal (shortcut key: Win+R; input cmd and enter th terminal), and input the following command to install it:
git clone https://github.com/elephantrobotics/pymycobot.git <your-path>
#Fill in your installation address in <your-path>, if you fill in nothing, the current path is used by default
cd <your-path>/pymycobot
#Go to the pymycobot folder of the downloaded package.
#Run one of the following commands according to your python version.
# Install
python2 setup.py install
# or
python3 setup.py install
Simple use of Python
Use method
After completing the steps above, open the installed pycharm, create a new python file, enter the code and import to our library:
from pymycobot import MyCobot, Angle, Coord, utils
After you import the installed project package on pycharm, for example, inputting: from pymycobot.mycobot import MyCobot, if no red wavy line appears below the font, this proves that it has been successfully installed and can be used; and if a red wavy line appears there,you can refer to How to install the API library and How to call the API library.
If you don't want to install the API library through the above command, you can use it the following operations.
First, you need to go to the project's github to download it locally.
Enter the project address: https://github.com/elephantrobotics/pymycobot
Then click the code button on the right side of the webpage, and click Download ZIP to download it locally; put the pymycobot file in the zip pymycobot file into your python dependent library directory, and then you can import and use it directly.
LED Blinking
Create a new Python file, and enter the following code to realize the LED blinking.
Notice: The baud rates corresponding to all types of devices are not the same. Before using them, refer to the information related to get the baud rate. The serial port number can be viewed through calculator device manager or a serial helper.
from pymycobot.mycobot import MyCobot
from pymycobot import PI_PORT, PI_BAUD # When using the Raspberry Pi version of mycobot, you can refer to these two variables to initialize MyCobot, if not, you can omit this line of code
import time
#The above needs to be written at the beginning of the code, which means importing the project package
# MyCobot class initialization requires two parameters: serial port and baud rate
# The first is the serial port string, such as:
# linux: "/dev/ttyUSB0"
# windows: "COM3"
# The second is the baud rate:
# M5 version is: 115200
# Example:
# mycobot-M5:
# linux:
# mc = MyCobot("/dev/ttyUSB0", 115200)
# windows:
# mc = MyCobot("COM3", 115200)
# mycobot-raspi:
# mc = MyCobot(PI_PORT, PI_BAUD)
#
# Initialize a MyCobot object
# Create object code here for windows version
mc = MyCobot("COM3", 115200)
i = 7
#loop 7 times
while i > 0:
mc.set_color(0,0,255) #blue light on
time.sleep(2) #wait for 2 seconds
mc.set_color(255,0,0) #red light on
time.sleep(2) #wait for 2 seconds
mc.set_color(0,255,0) #green light on
time.sleep(2) #wait for 2 seconds
i -= 1
Result: The top light of the robot flashes 7 times continuously in blue, red and green at 2-second intervals.
Use Case
LED Blinking
from pymycobot.mycobot import MyCobot
from pymycobot import PI_PORT, PI_BAUD # When using the Raspberry Pi version of mycobot, you can refer to these two variables to initialize MyCobot, if not, you can omit this line of code
import time
#The above needs to be written at the beginning of the code, which means importing the project package
# MyCobot class initialization requires two parameters: serial port and baud rate
# The first is the serial port string, such as:
# linux: "/dev/ttyUSB0"
# windows: "COM3"
# The second is the baud rate:
# M5 version is: 115200
# Example:
# mycobot-M5:
# linux:
# mc = MyCobot("/dev/ttyUSB0", 115200)
# windows:
# mc = MyCobot("COM3", 115200)
# mycobot-raspi:
# mc = MyCobot(PI_PORT, PI_BAUD)
#
# Initialize a MyCobot object
# Create object code here for windows version
mc = MyCobot("COM3", 115200)
i = 7
#loop 7 times
while i > 0:
mc.set_color(0,0,255) #blue light on
time.sleep(2) #wait for 2 seconds
mc.set_color(255,0,0) #red light on
time.sleep(2) #wait for 2 seconds
mc.set_color(0,255,0) #green light on
time.sleep(2) #wait for 2 seconds
i -= 1
Control the robot arm to make it return to the origin
from pymycobot.mycobot import MyCobot
from pymycobot import PI_PORT, PI_BAUD # When using the Raspberry Pi version of mycobot, you can refer to these two variables to initialize MyCobot
# MyCobot class initialization requires two parameters:
# The first is the serial port string, such as:
# linux: "/dev/ttyUSB0"
# windows: "COM3"
# The second is the baud rate:
# M5 version is: 115200
#
# Example:
# mycobot-M5:
# linux:
# mc = MyCobot("/dev/ttyUSB0", 115200)
# windows:
# mc = MyCobot("COM3", 115200)
# mycobot-raspi:
# mc = MyCobot(PI_PORT, PI_BAUD)
#
# Initialize a MyCobot object
# Create object code here for Raspberry Pi version
mc = MyCobot(PI_PORT, PI_BAUD)
# Check whether the program can be burned into the robot arm
if mc.is_controller_connected() != 1:
print("Please connect the robot arm correctly for program writing")
exit(0)
# Fine-tune the robotic arm to ensure that all the bayonets are aligned in the adjusted position
# Subject to the alignment of the mechanical arm bayonet, this is only a case
mc.send_angles([0, 0, 0, 0, 0, 0], 30)
# To calibrate the position at this time, the calibrated angular position represents [0,0,0,0,0,0], and the potential value represents [2048,2048,2048,2048,2048,2048]
# The for loop is equivalent to the method set_gripper_ini()
#for i in range(1, 7):
#mc.set_servo_calibration(i)
Single-joint motion
from pymycobot import MyCobot
from pymycobot.genre import Angle
import time
# MyCobot class initialization requires two parameters:
# The first is the serial port string, such as:
# linux: "/dev/ttyUSB0"
# windows: "COM3"
# The second is the baud rate:
# M5 version is: 115200
#
# Example:
# mycobot-M5:
# linux:
# mc = MyCobot("/dev/ttyUSB0", 115200)
# windows:
# mc = MyCobot("COM3", 115200)
# mycobot-raspi:
# mc = MyCobot(PI_PORT, PI_BAUD)
#
# Initialize a MyCobot object
# Create object code for Raspberry Pi version
# mc = MyCobot(PI_PORT, PI_BAUD)
# Create object code for M5 version
mc=MyCobot('COM3',115200)
# Robotic arm recovery
mc.send_angles([0, 0, 0, 0, 0, 0], 40)
time.sleep(3)
# Control joint 3 to move 70°
mc.send_angle(Angle.J3.value,70,40)
time.sleep(3)
# Control joint 4 movement -70°
mc.send_angle(Angle.J4.value,-70,40)
time.sleep(3)
# Control joint 1 to move 90°
mc.send_angle(Angle.J1.value,90,40)
time.sleep(3)
# Control joint 5 movement -90°
mc.send_angle(Angle.J5.value,-90,40)
time.sleep(3)
# Control joint 5 to move 90°
mc.send_angle(Angle.J5.value,90,40)
time.sleep(3)
Multi-joint motion
import time
from pymycobot import MyCobot
# MyCobot class initialization requires two parameters:
# The first is the serial port string, such as:
# linux: "/dev/ttyUSB0"
# windows: "COM3"
# The second is the baud rate:
# M5 version is: 115200
#
# Example:
# mycobot-M5:
# linux:
# mc = MyCobot("/dev/ttyUSB0", 115200)
# windows:
# mc = MyCobot("COM3", 115200)
# mycobot-raspi:
# mc = MyCobot(PI_PORT, PI_BAUD)
#
# Initialize a MyCobot object
# Create object code for Raspberry Pi version
# mc = MyCobot(PI_PORT, PI_BAUD)
# Create object code for 280-M5 version
mc=MyCobot('COM3',115200)
# Robotic arm recovery
mc.send_angles([0,0,0,0,0,0],50)
time.sleep(2.5)
# Control different angles of rotation of multiple joints
mc.send_angles([90,45,-90,90,-90,90],50)
time.sleep(2.5)
# Return the robotic arm to zero
mc.send_angles([0,0,0,0,0,0],50)
time.sleep(2.5)
# Control different angles of rotation of multiple joints
mc.send_angles([-90,-45,90,-90,90,-90],50)
time.sleep(2.5)
Control the robot arm to make it sway left and right
from pymycobot.mycobot import MyCobot
from pymycobot.genre import Angle
from pymycobot import PI_PORT, PI_BAUD # When using the Raspberry Pi version of mycobot, these two variables can be referenced to initialize MyCobot
import time
# Initialize a MyCobot object
# Pi version
# mc = MyCobot(PI_PORT, PI_BAUD)
# M5 version
mc = MyCobot("COM3", 115200)
# Get the coordinates of the current location
angle_datas = mc.get_angles()
print(angle_datas)
# By passing the angle parameter, let each joint of the robotic arm move to the specified position
mc.send_angles([0, 0, 0, 0, 0, 0], 50)
print(mc.is_paused())
# Set the waiting time to ensure that the robotic arm has reached the specified position
# while not mc.is_paused():
time.sleep(2.5)
# Move joint 1 to the 90 position
mc.send_angle(Angle.J1.value, 90, 50)
# Set the waiting time to ensure that the robotic arm has reached the specified position
time.sleep(2)
# set loop times
num = 5
# The following code can make the robotic arm swing left and right
while num > 0:
# Move joint 2 to the 50 position
mc.send_angle(Angle.J2.value, 50, 50)
# Set the waiting time to ensure that the robotic arm has reached the specified position
time.sleep(1.5)
# Move joint 2 to the -50 position
mc.send_angle(Angle.J2.value, -50, 50)
# Set the waiting time to ensure that the robotic arm has reached the specified position
time.sleep(1.5)
num -= 1
# Make the robotic arm retract. You can manually swing the robotic arm, and then use the get_angles() function to get the coordinate sequence,
# use this function to let the robotic arm reach the position you want.
mc.send_angles([88.68, -138.51, 155.65, -128.05, -9.93, -15.29], 50)
# Set the waiting time to ensure that the robotic arm has reached the specified position
time.sleep(2.5)
# Let the robotic arm relax, you can manually swing the robotic arm
mc.release_all_servos()
Control the robot arm to make it dance
from pymycobot.mycobot import MyCobot
from pymycobot import PI_PORT, PI_BAUD # When using the Raspberry Pi version of mycobot, these two variables can be referenced to initialize MyCobot
import time
if __name__ == '__main__':
# MyCobot class initialization requires two parameters:
# The first is the serial port string, such as:
# linux: "/dev/ttyUSB0"
# windows: "COM3"
# The second is the baud rate:
# M5 version is: 115200
#
# Example:
# mycobot-M5:
# linux:
# mc = MyCobot("/dev/ttyUSB0", 115200)
# windows:
# mc = MyCobot("COM3", 115200)
# mycobot-raspi:
# mc = MyCobot(PI_PORT, PI_BAUD)
#
# Initialize a MyCobot object
# Create object code for Raspberry Pi version below
mc = MyCobot(PI_PORT, PI_BAUD)
# M5 version
# mc = MyCobot("COM3",115200)
# set start start time
start = time.time()
# Let the robotic arm reach the specified position
mc.send_angles([-1.49, 115, -153.45, 30, -33.42, 137.9], 80)
# Determine if it reaches the specified position
while not mc.is_in_position([-1.49, 115, -153.45, 30, -33.42, 137.9], 0):
# Return the robotic arm to motion
mc.resume()
# Let the robotic arm move for 0.5s
time.sleep(0.5)
# Pause arm movement
mc.pause()
# Determine if the move timed out
if time.time() - start > 3:
break
# set start time
start = time.time()
# Let the exercise last for 30 seconds
while time.time() - start < 30:
# Let the robotic arm quickly reach this position
mc.send_angles([-1.49, 115, -153.45, 30, -33.42, 137.9], 80)
# Set the color of the light to [0,0,50]
mc.set_color(0, 0, 50)
time.sleep(0.7)
# Let the robotic arm quickly reach this position
mc.send_angles([-1.49, 55, -153.45, 80, 33.42, 137.9], 80)
# Set the color of the light to [0,50,0]
mc.set_color(0, 50, 0)
time.sleep(0.7)
Gripper control
from pymycobot import PI_PORT, PI_BAUD # When using the Raspberry Pi version of mycobot, these two variables can be referenced to initialize MyCobot
from pymycobot.mycobot import MyCobot
import time
def gripper_test(mc):
print("Start check IO part of api\n")
# Check if the gripper is moving
flag = mc.is_gripper_moving()
print("Is gripper moving: {}".format(flag))
time.sleep(1)
# Set the current position to (2048).
# Use it when you are sure you need it.
# Gripper has been initialized for a long time. Generally, there
# is no need to change the method.
# mc.set_gripper_ini()
# Set joint point 1 to rotate to the position of 2048
mc.set_encoder(1, 2048)
time.sleep(2)
# Set six joint positions and let the robotic arm rotate to this position at a speed of 20
mc.set_encoders([1024, 1024, 1024, 1024, 1024, 1024], 20)
# mc.set_encoders([2048, 2900, 2048, 2048, 2048, 2048], 20)
# mc.set_encoders([2048, 3000,3000, 3000, 2048, 2048], 50)
time.sleep(3)
# Get the position information of joint point 1
print(mc.get_encoder(1))
# Set the gripper to rotate to the position of 2048
mc.set_encoder(7, 2048)
time.sleep(3)
# Set the gripper to rotate to the position of 1300
mc.set_encoder(7, 1300)
time.sleep(3)
# Let the gripper reach the state of 2048 at a speed of 70, 2048 will report an error, so change it to 255
mc.set_gripper_value(255, 70)
time.sleep(3)
# Let the gripper reach the state of 1500 at a speed of 70, 1500 will report an error, so change it to 255
mc.set_gripper_value(255, 70)
time.sleep(3)
num=5
while num>0:
# Set the state of the gripper to quickly open the gripper at a speed of 70
mc.set_gripper_state(0, 70)
time.sleep(3)
# Set the state of the gripper to quickly close the gripper at a speed of 70
mc.set_gripper_state(1, 70)
time.sleep(3)
num-=1
# Get the value of the gripper
print("")
print(mc.get_gripper_value())
# mc.release_all_servos()
if __name__ == "__main__":
# MyCobot class initialization requires two parameters:
# The first is the serial port string, such as:
# linux: "/dev/ttyUSB0"
# windows: "COM3"
# The second is the baud rate:
# M5 version is: 115200
#
# Example:
# mycobot-M5:
# linux:
# mc = MyCobot("/dev/ttyUSB0", 115200)
# windows:
# mc = MyCobot("COM3", 115200)
# mycobot-raspi:
# mc = MyCobot(PI_PORT, PI_BAUD)
#
# Initialize a MyCobot object
# Create object code for Raspberry Pi version below
# mc = MyCobot(PI_PORT, PI_BAUD)
# M5 version
mc = MyCobot('COM3', 115200)
# make it move to zero position
mc.set_encoders([2048, 2048, 2048, 2048, 2048, 2048], 20)
time.sleep(3)
gripper_test(mc)
Adsorption pump control
from pymycobot.mycobot import MyCobot
from pymycobot import PI_PORT, PI_BAUD # When using the Raspberry Pi version of mycobot, these two variables can be referenced to initialize MyCobot
import time
# MyCobot class initialization requires two parameters:
# The first is the serial port string, such as:
# linux: "/dev/ttyUSB0"
# windows: "COM3"
# The second is the baud rate:
# M5 version is: 115200
#
# Example:
# mycobot-M5:
# linux:
# mc = MyCobot("/dev/ttyUSB0", 115200)
# windows:
# mc = MyCobot("COM3", 115200)
# mycobot-raspi:
# mc = MyCobot(PI_PORT, PI_BAUD)
#
# Initialize a MyCobot object
# Create object code here for M5 version
mc =MyCobot('COM3',115200)
# The position of the robot arm movement
angles = [
[92.9, -10.1, -60, 5.8, -2.02, -37.7],
[92.9, -53.7, -83.05, 50.09, -0.43, -38.75],
[92.9, -10.1, -87.27, 5.8, -2.02, -37.7]
]
# Turn on the suction pump
def pump_on():
# make position 2 work
mc.set_basic_output(2, 0)
# make position 5 work
mc.set_basic_output(5, 0)
# stop the suction pump
def pump_off():
# Stop position 2 from working
mc.set_basic_output(2, 1)
# Stop position 5 from working
mc.set_basic_output(5, 1)
# Robotic arm recovery
mc.send_angles([0, 0, 0, 0, 0, 0], 30)
time.sleep(3)
#Turn on the suction pump
pump_on()
mc.send_angles(angles[2], 30)
time.sleep(2)
#absorb small blocks
mc.send_angles(angles[1], 30)
time.sleep(2)
mc.send_angles(angles[0], 30)
time.sleep(2)
mc.send_angles(angles[1], 30)
time.sleep(2)
#Turn off the suction pump
pump_off()
mc.send_angles(angles[0], 40)
time.sleep(1.5)
280-Pi version:
from pymycobot.mycobot import MyCobot
from pymycobot import PI_PORT, PI_BAUD # When using the Raspberry Pi version of mycobot, these two variables can be referenced to initialize MyCobot
import RPi.GPIO as GPIO
import time
# MyCobot class initialization requires two parameters:
# The first is the serial port string, such as:
# linux: "/dev/ttyUSB0"
# windows: "COM3"
# The second is the baud rate:
# M5 version is: 115200
#
# Example:
# mycobot-M5:
# linux:
# mc = MyCobot("/dev/ttyUSB0", 115200)
# windows:
# mc = MyCobot("COM3", 115200)
# mycobot-raspi:
# mc = MyCobot(PI_PORT, PI_BAUD)
#
# Initialize a MyCobot object
# Create object code here for Raspberry Pi version
mc = MyCobot(PI_PORT, PI_BAUD)
# The position of the robot arm movement
angles = [
[92.9, -10.1, -60, 5.8, -2.02, -37.7],
[92.9, -53.7, -83.05, 50.09, -0.43, -38.75],
[92.9, -10.1, -87.27, 5.8, -2.02, -37.7]
]
# Turn on the suction pump
GPIO.setmode(GPIO.BCM)
GPIO.setup(20, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
# Turn on the suction pump
def pump_on():
# make position 20 work
GPIO.output(20,0)
# make position 21 work
GPIO.output(21,0)
# stop the suction pump
def pump_off():
# make position 20 work
GPIO.output(21,1)
# make position 21 work
GPIO.output(21,1)
# Robotic arm recovery
mc.send_angles([0, 0, 0, 0, 0, 0], 30)
time.sleep(3)
#Turn on the suction pump
pump_on()
mc.send_angles(angles[2], 30)
time.sleep(2)
#absorb small blocks
mc.send_angles(angles[1], 30)
time.sleep(2)
mc.send_angles(angles[0], 30)
time.sleep(2)
mc.send_angles(angles[1], 30)
time.sleep(2)
#Turn off the suction pump
pump_off()
mc.send_angles(angles[0], 40)
time.sleep(1.5)
Was this article helpful?
