# ROS 2 cmd\_vel bridge to an Arduino differential drive robot: design worksheet and code

> A ₹6,990 design worksheet for driving a two-wheel Arduino robot from ROS 2 by publishing velocity commands on /cmd\_vel, with the wheel-speed maths, a Python bridge node, an Arduino parser and a stop-on-silence watchdog.

Canonical: https://roboticsengineering.in/projects/ros2-cmd-vel-bridge-arduino-differential-drive/ | Published: 2026-11-04 | Updated: 2026-11-04

**A ₹6,990 link between the robot software world and a cheap Arduino robot. A Raspberry Pi running ROS 2 receives standard velocity commands on the /cmd_vel topic, a small Python node turns them into left and right wheel speeds, and an Arduino drives the motors. Once this works, any ROS tool that publishes /cmd_vel, from keyboard teleop to a navigation stack, can drive the robot.**

**Scope:** advanced. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The wheel-speed table was computed by script. ROS 2 releases differ in details, so check the install steps and package names for your distribution against its documentation. The 0.35 m/s top speed and 0.13 m wheel base are example numbers that you must measure on your own robot. This page covers the command path only: there is no odometry, mapping or navigation here.

## How it works

A ROS 2 Twist message on /cmd_vel carries a forward speed v in m/s and a turn rate ω in rad/s. For a differential-drive robot with wheel base B, the wheel speeds are left = v − ω × B / 2 and right = v + ω × B / 2. With B = 0.13 m, a command of v = 0.2 and ω = 0.5 gives 0.1675 m/s on the left and 0.2325 m/s on the right. Turning on the spot, v = 0 and ω = 1.0, gives −0.065 and +0.065 m/s.

The Python node converts each wheel speed to a PWM value by dividing by the robot’s measured top speed (0.35 m/s here) and multiplying by 255, then sends a short line such as 120,-120 over USB serial. The Arduino parses the line and drives the motors. If no line arrives for 500 ms, the Arduino stops by itself, and the Python node also sends a zero command when the topic goes quiet, so a crashed program or lost link cannot leave the robot running.

## Bill of materials

| Part | Qty | Unit | Total |
| --- | --- | --- | --- |
| Raspberry Pi 4 or 5 with case | 1 | ₹4,500 | ₹4,500 |
| microSD card, 32 GB | 1 | ₹400 | ₹400 |
| Raspberry Pi power supply | 1 | ₹600 | ₹600 |
| Arduino Uno R3 (or compatible) with USB cable | 1 | ₹650 | ₹650 |
| L298N motor driver board | 1 | ₹180 | ₹180 |
| 2WD chassis kit with two gear motors and wheels | 1 | ₹450 | ₹450 |
| Battery holder with two 18650 cells | 1 | ₹210 | ₹210 |
| Parts total | ₹6,990 |  |  |

Scroll the table sideways to see every column.

Prices checked Nov 2026. Shipping and tools are not included.

Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings.

## The numbers

| v | ω | Left wheel | Right wheel | Left PWM | Right PWM |
| --- | --- | --- | --- | --- | --- |
| 0.2 m/s | 0 rad/s | 0.200 m/s | 0.200 m/s | 146 | 146 |
| 0.2 m/s | 0.5 rad/s | 0.168 m/s | 0.233 m/s | 122 | 169 |
| 0 m/s | 1 rad/s | -0.065 m/s | 0.065 m/s | -47 | 47 |
| 0.2 m/s | -1 rad/s | 0.265 m/s | 0.135 m/s | 193 | 98 |
| 0.3 m/s | 2 rad/s | 0.170 m/s | 0.430 m/s | 124 | 313 |

*Left = v − ω × 0.13 / 2, right = v + ω × 0.13 / 2; PWM = speed / 0.35 × 255 (example top speed). Speeds above the top speed are clipped to 255 in the code.*

Note the last row: v = 0.3 and ω = 2.0 asks for a right wheel speed of 0.43 m/s, above the example top speed of 0.35 m/s. The bridge clips it, so the robot turns less sharply than commanded. Real navigation software expects the robot to follow commands, so set the speed and turn limits in your ROS configuration below what the robot can do.

## Power and wiring

| Part | Connects to | Note |
| --- | --- | --- |
| Pi to Arduino | USB cable | Appears as /dev/ttyACM0 on many systems; check with ls /dev/tty\* |
| L298N ENA / IN1 / IN2 | Arduino D5 / D7 / D8 | Left motor |
| L298N ENB / IN3 / IN4 | Arduino D6 / D9 / D4 | Right motor |
| Motor battery | L298N 12V input | Join the Arduino GND to the L298N GND |
| Pi power | Its own supply or a regulated 5 V bank | Never power the Pi from the motor driver 5 V |

*Power the Pi from a clean, separate source so motor noise cannot reset it.*

## The sketch

```
# bridge.py: ROS 2 /cmd_vel to Arduino over serial (rclpy + pyserial)
# Design worksheet: not run on a robot by this site. Source your ROS 2 setup first.
import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Twist
import serial

PORT = '/dev/ttyACM0'
BAUD = 115200
WHEEL_BASE = 0.13      # metres, measure yours
MAX_SPEED = 0.35       # m/s at PWM 255, measure yours
TIMEOUT_S = 0.5

def clip(x):
    return max(-255, min(255, int(round(x))))

class Bridge(Node):
    def __init__(self):
        super().__init__('cmd_vel_bridge')
        self.ser = serial.Serial(PORT, BAUD, timeout=0.1)
        self.create_subscription(Twist, 'cmd_vel', self.on_cmd, 10)
        self.last = self.get_clock().now()
        self.create_timer(0.1, self.watchdog)

    def send(self, left, right):
        self.ser.write(f'{left},{right}\n'.encode())

    def on_cmd(self, msg):
        v, w = msg.linear.x, msg.angular.z
        vl = v - w * WHEEL_BASE / 2.0
        vr = v + w * WHEEL_BASE / 2.0
        self.send(clip(vl / MAX_SPEED * 255), clip(vr / MAX_SPEED * 255))
        self.last = self.get_clock().now()

    def watchdog(self):
        age = (self.get_clock().now() - self.last).nanoseconds / 1e9
        if age > TIMEOUT_S:
            self.send(0, 0)

def main():
    rclpy.init()
    node = Bridge()
    try:
        rclpy.spin(node)
    finally:
        node.send(0, 0)
        node.destroy_node()
        rclpy.shutdown()

if __name__ == '__main__':
    main()

// =====================================================================
// Arduino sketch (a separate file): reads "left,right" lines, e.g. 120,-120
// =====================================================================
const int L_EN = 5, L_IN1 = 7, L_IN2 = 8;
const int R_EN = 6, R_IN3 = 9, R_IN4 = 4;
const unsigned long TIMEOUT_MS = 500;

char buf[24];
unsigned long lastCmd = 0;

void drive(int left, int right) {
  digitalWrite(L_IN1, left >= 0);  digitalWrite(L_IN2, left < 0);
  digitalWrite(R_IN3, right >= 0); digitalWrite(R_IN4, right < 0);
  analogWrite(L_EN, constrain(abs(left), 0, 255));
  analogWrite(R_EN, constrain(abs(right), 0, 255));
}

void setup() {
  pinMode(L_EN, OUTPUT); pinMode(L_IN1, OUTPUT); pinMode(L_IN2, OUTPUT);
  pinMode(R_EN, OUTPUT); pinMode(R_IN3, OUTPUT); pinMode(R_IN4, OUTPUT);
  Serial.begin(115200);
  Serial.setTimeout(50);
}

void loop() {
  if (Serial.available()) {
    int n = Serial.readBytesUntil('\n', buf, sizeof(buf) - 1);
    buf[n] = 0;
    int l, r;
    if (sscanf(buf, "%d,%d", &l, &r) == 2) { drive(l, r); lastCmd = millis(); }
  }
  if (millis() - lastCmd > TIMEOUT_MS) drive(0, 0);   // stop on silence
}
```

The Python node needs the rclpy and pyserial packages and a sourced ROS 2 environment; on a fresh Pi that means installing a ROS 2 release that supports your operating system first. Opening the serial port often resets an Arduino Uno, so the first command can be lost for about two seconds. The sketch has been proofread but not compiled on this site.

## Test plan

1. **Arduino alone:** open a serial terminal at 115200, type 120,120 and press Enter; the wheels should turn, and stop 500 ms later without more input.
2. **Wheels up:** test 120,120, -120,-120, 120,-120 with the wheels off the floor and swap motor wires if a direction is wrong.
3. **Bridge only:** run the node and publish one command from the terminal with ros2 topic pub, and watch the wheels.
4. **Keyboard teleop:** install the teleop_twist_keyboard package for your ROS 2 release and drive with the keys.
5. **Watchdog:** kill the teleop program mid-move and confirm the robot stops within about half a second.
6. **Top speed:** time a metre run at full PWM, update MAX_SPEED, and check that a 0.2 m/s command really gives about 0.2 m/s.

## What usually goes wrong

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| No /dev/ttyACM0 | Cable or port name different | Run ls /dev/tty\* with the Arduino unplugged and plugged in |
| Permission denied on the port | User not in the serial group | Add the user to the dialout group on many Linux systems and log in again |
| Robot ignores commands | Baud mismatch | Use 115200 in both the node and the sketch |
| First command lost | Arduino resets when the port opens | Wait two seconds after starting the node |
| Robot turns the wrong way | Sign or motor wiring reversed | Swap the motor wires or the sign of ω |
| Commands not received | Topic name or namespace differs | Check ros2 topic list and echo /cmd_vel |

## Upgrades

- **Encoders and odometry:** publish wheel odometry from the Arduino.
- **Closed-loop wheels:** run PID on each wheel, as in the motor-speed worksheet.
- **Launch file:** start the bridge automatically at boot.
- **Navigation:** use the robot with a navigation stack once odometry and a lidar are added.

## Cost and parts notes

The ₹6,990 total is an illustration and does not include shipping. Use the [cost calculator](https://roboticsengineering.in/robotics-project-cost-calculator/) to compare sellers.

## Common questions

**What is /cmd_vel?**

The standard ROS topic name for velocity commands, carrying a forward speed and a turn rate.

**Why convert to wheel speeds?**

A differential-drive robot has no steering wheel; it turns by running its two sides at different speeds.

**Why a watchdog on both sides?**

If either the program or the cable fails, the robot should stop on its own. Two independent stop rules are safer than one.

**Which ROS 2 release should I use?**

One that supports your Pi’s operating system. Check the ROS 2 documentation for the current supported releases; steps differ between them.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.
