Basic Read/Write Tutorial
This section provides examples of how to write code in Python to read and write data to DYNAMIXEL motors.
NOTE: This tutorial is based on XL-430-W250 DYNAMIXEL motors and uses Protocol 2.0.
Make python file
- Create a python file and open it in a text editor. In this case, we use visual studio code, but you can use any text editor you prefer.
$ mkdir -p my_dxl_project/python $ cd my_dxl_project/python $ code my_read_write.py
Make python file
-
Open the Visual Studio Code and create a python file in your workspace.

Source Code Description
Add Header Files
- Add
#!/usr/bin/env python3and importdynamixel_sdkto the top of your py file. This includes all necessary functions and classes from the DYNAMIXEL SDK.#!/usr/bin/env python3 from dynamixel_sdk import *
Initialize Handler Objects
- Initialize the
PortHandlerandPacketHandler. Set theport nameandprotocol versionaccording to your DYNAMIXEL setup. The example below uses/dev/ttyUSB0as the port name and2.0as the protocol version.portHandler = PortHandler("/dev/ttyUSB0") # your dxl port name packetHandler = PacketHandler(2.0) # protocol version
- Initialize the
PortHandlerandPacketHandler. Set theport nameandprotocol versionaccording to your DYNAMIXEL setup. The example below usesCOM3as the port name and2.0as the protocol version.portHandler = PortHandler("COM3") # your dxl port name packetHandler = PacketHandler(2.0) # protocol version
Open Port and Set Baud Rate
- Open the port and set the baud rate. The example below uses
57600as the baud rate.portHandler.openPort() portHandler.setBaudRate(57600)
ERROR LOGGING
openPort() and setBaudRate() functions return a boolean value indicating success or failure. If you want to check for failure, you can write like below to print an error message and exit the program.
if portHandler.openPort():
print("Succeeded to open the port!")
else:
print("Failed to open the port!")
exit()
if portHandler.setBaudRate(57600):
print("Succeeded to change the baudrate!")
else:
print("Failed to change the baudrate!")
exit()
Write data to enable torque
- Turn on the torque of the DYNAMIXEL.
dxl_id = 1 torque_on_address = 64 data = 1 packetHandler.write1ByteTxRx(portHandler, dxl_id, torque_on_address, data)id: Dynamixel ID you setaddress: The address of the data you want to write. Refer to the Control Table of your DYNAMIXEL model, which can be found in its specification manual or Dynamixel Wizard 2.0. In this example, we use X series.data: The data you want to write to the specified address.
ERROR LOGGING
write1ByteTxRx() returns a communication result. If you want to check the communication result and error, you can write the code as shown below.
dxl_comm_result, dxl_error = packetHandler.write1ByteTxRx(portHandler, dxl_id, torque_on_address, data)
if dxl_comm_result != COMM_SUCCESS:
print("%s" % packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % packetHandler.getRxPacketError(dxl_error))
else:
print("Dynamixel has been successfully connected")
Get User Input
- Get user input for the target position.
while True: try: target_position = int(input("Enter target position (0 ~ 4095, -1 to exit): ")) except ValueError: print("Please enter an integer.") continue if target_position == -1: break elif target_position < 0 or target_position > 4095: print("Position must be between 0 and 4095.") continue
Write data to set target position
- Write the target position to the DYNAMIXEL.
goal_position_address = 116 packetHandler.write4ByteTxRx(portHandler, dxl_id, goal_position_address, target_position)
ERROR LOGGING
write4ByteTxRx() returns a communication result. If you want to check the communication result and error, you can write the code as shown below.
dxl_comm_result, dxl_error = packetHandler.write4ByteTxRx(portHandler, dxl_id, goal_position_address, target_position)
if dxl_comm_result != COMM_SUCCESS:
print("%s" % packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % packetHandler.getRxPacketError(dxl_error))
Read data from DYNAMIXEL
- Read the current position from the DYNAMIXEL.
while True: present_position_address = 132 present_position, dxl_comm_result, dxl_error = packetHandler.read4ByteTxRx(portHandler, dxl_id, present_position_address) print(f"Current Position: {present_position}") if abs(target_position - present_position) <= 10: break
ERROR LOGGING
read4ByteTxRx() returns a communication result. If you want to check the communication result and error, you can write the code as shown below.
present_position, dxl_comm_result, dxl_error = packetHandler.read4ByteTxRx(portHandler, dxl_id, present_position_address)
if dxl_comm_result != COMM_SUCCESS:
print("%s" % packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % packetHandler.getRxPacketError(dxl_error))
- Turn off the torque when while loop is exited. And close the port.
data = 0 packetHandler.write1ByteTxRx(portHandler, dxl_id, torque_on_address, data) portHandler.closePort()
Run the Code
- Run the code using python3.
$ python3 my_read_write.py
-
Run the code through Visual Studio Code.

Full Source Code
#!/usr/bin/env python3
from dynamixel_sdk import *
portHandler = PortHandler("/dev/ttyUSB0")
packetHandler = PacketHandler(2.0)
if portHandler.openPort():
print("Succeeded to open the port!")
else:
print("Failed to open the port!")
exit()
if portHandler.setBaudRate(57600):
print("Succeeded to change the baudrate!")
else:
print("Failed to change the baudrate!")
exit()
dxl_id = 1
torque_on_address = 64
data = 1
dxl_comm_result, dxl_error = packetHandler.write1ByteTxRx(portHandler, dxl_id, torque_on_address, data)
if dxl_comm_result != COMM_SUCCESS:
print("%s" % packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % packetHandler.getRxPacketError(dxl_error))
else:
print("Dynamixel has been successfully connected")
while True:
try:
target_position = int(input("Enter target position (0 ~ 4095, -1 to exit): "))
except ValueError:
print("Please enter an integer.")
continue
if target_position == -1:
break
elif target_position < 0 or target_position > 4095:
print("Position must be between 0 and 4095.")
continue
goal_position_address = 116
dxl_comm_result, dxl_error = packetHandler.write4ByteTxRx(portHandler, dxl_id, goal_position_address, target_position)
if dxl_comm_result != COMM_SUCCESS:
print("%s" % packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % packetHandler.getRxPacketError(dxl_error))
while True:
present_position_address = 132
present_position, dxl_comm_result, dxl_error = packetHandler.read4ByteTxRx(portHandler, dxl_id, present_position_address)
if dxl_comm_result != COMM_SUCCESS:
print("%s" % packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % packetHandler.getRxPacketError(dxl_error))
print(f"Current Position: {present_position}")
if abs(target_position - present_position) <= 10:
break
data = 0
packetHandler.write1ByteTxRx(portHandler, dxl_id, torque_on_address, data)
portHandler.closePort()
#!/usr/bin/env python3
from dynamixel_sdk import *
portHandler = PortHandler("COM3")
packetHandler = PacketHandler(2.0)
if portHandler.openPort():
print("Succeeded to open the port!")
else:
print("Failed to open the port!")
exit()
if portHandler.setBaudRate(57600):
print("Succeeded to change the baudrate!")
else:
print("Failed to change the baudrate!")
exit()
dxl_id = 1
torque_on_address = 64
data = 1
dxl_comm_result, dxl_error = packetHandler.write1ByteTxRx(portHandler, dxl_id, torque_on_address, data)
if dxl_comm_result != COMM_SUCCESS:
print("%s" % packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % packetHandler.getRxPacketError(dxl_error))
else:
print("Dynamixel has been successfully connected")
while True:
try:
target_position = int(input("Enter target position (0 ~ 4095, -1 to exit): "))
except ValueError:
print("Please enter an integer.")
continue
if target_position == -1:
break
elif target_position < 0 or target_position > 4095:
print("Position must be between 0 and 4095.")
continue
goal_position_address = 116
dxl_comm_result, dxl_error = packetHandler.write4ByteTxRx(portHandler, dxl_id, goal_position_address, target_position)
if dxl_comm_result != COMM_SUCCESS:
print("%s" % packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % packetHandler.getRxPacketError(dxl_error))
while True:
present_position_address = 132
present_position, dxl_comm_result, dxl_error = packetHandler.read4ByteTxRx(portHandler, dxl_id, present_position_address)
if dxl_comm_result != COMM_SUCCESS:
print("%s" % packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % packetHandler.getRxPacketError(dxl_error))
print(f"Current Position: {present_position}")
if abs(target_position - present_position) <= 10:
break
data = 0
packetHandler.write1ByteTxRx(portHandler, dxl_id, torque_on_address, data)
portHandler.closePort()

