Maker Pro
FPB-RA6T3

TiltGuard: A Self-Balancing Robot With a Renesas RA6T3 Brain

MB
November 19, 2026 by Mohamed Bedair
Share
banner

I pulled the Arduino Nano out of an ELEGOO Tumbller and handed the whole robot to a Renesas RA6T3 motor-control MCU. It now balances on a 1 kHz interrupt-driven loop, stands itself up from its kickstand, takes a shove without falling, drives from the phone app, and keeps an eye on its own safety.

Why I built this

My day job is automotive embedded software, so when the FPB-RA6T3 landed on my desk, the obvious move was to spin a motor with it. That felt too easy. Renesas lists small robots among the RA6T3's target applications, so I set myself a harder question: can one RA6T3 be the entire brain of a robot? Sensors, control loop, motors, remote control and safety, with no other processor helping.

My test subject is an ELEGOO Tumbller, a cheap two-wheeled balancing robot that normally runs on an Arduino Nano. I pulled the Nano out of its socket and wired the socket to the FPB-RA6T3. Everything the robot does now runs on the RA6T3.

What it does

  • Balances on a 1 kHz control loop, five times faster than the stock firmware's 5 ms loop.
  • Stands itself up: press S1 while it's lying on its kickstand and it kicks itself upright in about 0.2 s.
  • Takes a shove: poke it and it catches itself.
  • Drives from the phone with ELEGOO's own app, unmodified.
  • Watches its own safety: the motors only run when armed, faults latch, and a black box keeps the last 10 seconds.

Why the RA6T3 makes a great robot brain

The RA6T3 is sold as a motor-control MCU: a 200 MHz Arm Cortex-M33 with 256 KB of flash, 40 KB of RAM, PWM timers built for motor drives, a 12-bit ADC and an I3C bus. It turned out to be a very comfortable home for a balancing robot. These are the parts of the chip TiltGuard leans on:

  • GPT timers. GPT0 is the heartbeat: a 1 kHz tick with under 1 µs of jitter. GPT1 and GPT2 drive the two motors with 20 kHz PWM, above the range of hearing, so the motors don't whine.
  • I3C controller. In legacy I²C mode at 400 kHz, it reads all 14 bytes from the MPU-6050 in the background (394 µs) and raises an interrupt when it's done. The control law runs right inside that interrupt, so every cycle works on a fresh sample.
  • 200 MHz Cortex-M33. The whole control step (angle filter, three control terms, mixing and PWM update) takes about 8 µs. That's under 1% of the CPU, which leaves lots of room for what comes next.
  • External interrupts and 5 V-tolerant pins. The wheel encoders, the IMU bus and the Bluetooth module all use 5 V signals, and they plug straight into 5 V-tolerant RA6T3 pins (P408, P411, P100, P101, P410). The encoders count both edges through IRQ7 and IRQ4.
  • SCI UART. It receives the phone's commands from the robot's Bluetooth module.
  • FSP and e² studio. Every peripheral above is set up in the FSP configurator and runs bare-metal, with no RTOS. SEGGER RTT over the on-board debugger gives me live logs and a tuning console without using up a UART.

And there's still headroom on the chip: the POEG for a hardware motor cut-off, the ADC for battery monitoring, and the trigonometric function unit for the angle maths. They're all on my list.

System architecture: one RA6T3 reads every sensor, runs the 1 kHz balance loop and drives both motors.

Hardware

The robot keeps its own board, motors and sensors. Only the brain changed. The Nano socket carries every signal the robot has, so I plugged a GERUI screw-terminal adapter into it and put an ElectroCookie push-in terminal shield on the FPB. There's no soldering on either board, and every wire can be re-plugged.

  • ELEGOO Tumbller V1.1 (PCB V06.1): TB6612 motor driver, MPU-6050 IMU, wheel encoders, Bluetooth module, 2S battery
  • Renesas FPB-RA6T3
  • ElectroCookie Uno terminal block shield (on the FPB)
  • GERUI Nano terminal adapter (in the robot's Nano socket)
  • Buck converter, set to 5.0 V
  • 2 × BSS138 4-channel level shifters

Two details matter. First, the TB6612 motor driver and its input buffer run on 5 V logic and need at least 3.5 V to see a "high", which a 3.3 V MCU can't guarantee. So the five motor lines (PWMA, PWMB, AIN1, BIN1 and STBY) go through BSS138 level shifters. Second, power comes from the robot's own battery: a buck converter turns its 7.0–8.4 V into 5 V and feeds the FPB through its J10 input, and the FPB's 5 V pins then supply the robot's logic rail.

Pin map

  • PWMA: robot D5 → shifter → P409 (GPT1, 20 kHz PWM)
  • PWMB: robot D6 → shifter → P113 (GPT2, 20 kHz PWM)
  • AIN1 / BIN1: robot D7 / D12 → shifter → P008 / P110 (GPIO)
  • STBY: robot D8 → shifter → P006 (GPIO; low = motors off)
  • Left / right encoder: robot D2 / D4 → P408 / P411 (IRQ7 / IRQ4)
  • IMU SDA / SCL: robot A4 / A5 → P101 / P100 (I3C0 in I²C mode)
  • Bluetooth: robot D0 → P410 (SCI0 RXD, 9600 baud)

One rule I never break: the battery only goes into the buck converter, never into an FPB pin.

Wiring as built: the robot's Nano socket on the left, the FPB-RA6T3 terminal shield on the right, with the level shifters and the buck converter in between.

Firmware: everything happens in interrupts

The firmware is bare-metal C on Renesas FSP 6.6. I wanted the control loop to be immune to anything else the code does, so the whole control path runs from interrupts. Every millisecond goes like this:

  1. The GPT0 tick fires (priority 2) and starts a burst read of the IMU over I3C.
  2. The bus does its work for 394 µs while the CPU is free.
  3. The read-complete interrupt (priority 5) runs the angle filter, the control law and the PWM update in about 8 µs.
  4. The main loop gets the rest of the millisecond for Bluetooth decoding, the safety monitor, telemetry and the black box.

Encoder edges come in at priority 3 and Bluetooth bytes at priority 4, so nothing they do can delay the tick.

The tick handler is deliberately tiny. It only starts work and keeps timing statistics:

/* control_tick.c - GPT0 cycle-end interrupt (priority 2), every 1 ms.
 * It only starts work and keeps statistics; nothing in here waits. */
void control_tick_isr (timer_callback_args_t * p_args)
{
    if (TIMER_EVENT_CYCLE_END != p_args->event)
    {
        return;
    }

    /* Measure the real tick interval with the DWT cycle counter */
    uint32_t const now      = DWT->CYCCNT;
    uint32_t const interval = now - s_previous_cyccnt;
    s_previous_cyccnt = now;
    /* ... min/max interval and overrun count for telemetry ... */

    s_tick_count++;

    mpu6050_on_tick();      /* start the 14-byte IMU burst read on I3C0 */
    encoders_on_tick();
    ultrasonic_on_tick();
    battery_on_tick();
    button_on_tick();
}

The real work happens when the IMU read completes. The I3C callback turns the raw sample into a tilt angle with a complementary filter (τ = 1 s) and calls the controller straight away, timing itself with the DWT cycle counter:

/* mpu6050.c - runs inside the I3C read-complete callback (priority 5), excerpt */
static void mpu6050_tick_transfer_succeeded (uint8_t const * p_data, uint32_t length)
{
    /* ... unpack the 14 bytes into raw.accel_* and raw.gyro_* ... */

    uint32_t const control_step_start = DWT->CYCCNT;

    float const accel_angle_deg = atan2f((float) raw.accel_y, (float) raw.accel_z) * MPU6050_RAD_TO_DEG;
    float const gyro_rate_dps   = ((float) raw.gyro_x - s_gyro_x_bias_raw) / MPU6050_GYRO_SENSITIVITY_LSB_PER_DPS;
    float const gyro_z_dps      = ((float) raw.gyro_z - s_gyro_z_bias_raw) / MPU6050_GYRO_SENSITIVITY_LSB_PER_DPS;

    /* Complementary filter: tau = 1 s at 1 kHz, alpha = tau / (tau + dt) */
    float const filtered_angle_deg =
        (MPU6050_COMPLEMENTARY_FILTER_ALPHA * (s_angle.filtered_angle_deg + (gyro_rate_dps * MPU6050_FILTER_DT_S)))
        + ((1.0f - MPU6050_COMPLEMENTARY_FILTER_ALPHA) * accel_angle_deg);

    /* Balance control law on this fresh sample */
    balance_step(filtered_angle_deg, gyro_rate_dps, gyro_z_dps);

    s_last_control_step_cycles = DWT->CYCCNT - control_step_start;   /* about 8 us */
}

Firmware architecture: the 1 ms interrupt timeline, the control law and the safety state machine.

The control law

I didn't reinvent the controller. ELEGOO's cascaded law is well tuned for this chassis, so I ported it and gave it a faster loop. Each wheel's command adds up three terms:

  • Balance (PD): 55 × (tilt − setpoint) + 0.75 × pitch rate.
  • Speed (PI, every 40 ms): works on the encoder speed and the speed target from the phone, with gains of 10 and 0.26.
  • Turn: 0.5 × yaw rate, plus the turn target from the phone.

I added two things the stock code doesn't have: the speed loop ramps in over 1.5 s after arming, and its integrator freezes while both wheels are saturated, so it can't wind up. That second one came straight out of a bug I hit (more on that below). Here's the heart of balance_step():

/* balance.c - balance_step(), excerpt. Units: ELEGOO's 8-bit PWM (+/-255) */
float const error_deg   = angle_deg - p.setpoint_deg;
float const p_term      = p.kp * error_deg;
float const d_term      = p.kd * gyro_x_dps;
float const balance_out = p_term + d_term;

if (s_speed_step_count >= BALANCE_SPEED_PERIOD_STEPS)   /* every 40 ms */
{
    /* ... car_speed = signed encoder counts in the window, (L + R) / 2 ... */
    s_speed_filter = (s_speed_filter * BALANCE_SPEED_FILTER_OLD) + (car_speed * BALANCE_SPEED_FILTER_NEW);

    /* Anti-windup: while both wheels are already clamped at full duty in the
     * direction this term would push, don't integrate */
    float const integrand     = s_speed_filter - speed_target;
    bool const  saturated_fwd = (s_pwm_left >= BALANCE_PWM_LIMIT_8BIT) && (s_pwm_right >= BALANCE_PWM_LIMIT_8BIT);
    bool const  saturated_rev = (s_pwm_left <= -BALANCE_PWM_LIMIT_8BIT) && (s_pwm_right <= -BALANCE_PWM_LIMIT_8BIT);
    bool const  windup        = (saturated_fwd && (integrand > 0.0f)) || (saturated_rev && (integrand < 0.0f));

    if (!windup)
    {
        s_speed_integral = balance_clamp(s_speed_integral + integrand, BALANCE_SPEED_INTEGRAL_LIMIT);
    }

    s_speed_out = (-p.speed_kp * s_speed_filter) - (p.speed_ki * s_speed_integral);
    s_turn_out  = turn_target + (p.turn_kd * yaw_rate_stock_dps);
}

/* ... soft start scales s_speed_out into speed_applied ... */

s_pwm_left  = balance_clamp(balance_out - speed_applied - s_turn_out, BALANCE_PWM_LIMIT_8BIT);
s_pwm_right = balance_clamp(balance_out - speed_applied + s_turn_out, BALANCE_PWM_LIMIT_8BIT);

motors_plan(balance_to_permille(s_pwm_left), balance_to_permille(s_pwm_right), p_command);

if (!dry_run)
{
    driving = motors_apply(p_command);   /* GPT1/GPT2 duty + AIN1/BIN1 + STBY */
}

Safety, the automotive way

Coming from cars, I wanted the robot to be safe by default. A small state machine owns the motors: INIT, CALIBRATING, READY, ACTIVE and FAULT. The motors only run in ACTIVE, and the only way in is pressing S1 while the robot is within 40° of upright. Once it's running, any of these faults cuts the motors:

  • Tilt: beyond ±45° during the first 2 s after arming (room for the stand-up), then beyond ±25°.
  • Stale IMU: a failed read, or no fresh sample for more than 5 ms.
  • Tick overrun: the 1 kHz loop missed its deadline.
  • Stalled wheel: driven at 30% or more for about 0.5 s with no encoder edges.
  • Motor driver error reported by FSP.

Faults stay latched until someone presses S1. If arming is refused, LED1 blinks fast, so I can see why without a laptop. And a black box records the last 10 s of tilt, rates, motor commands and encoder counts at 100 Hz, in RAM that survives a reset; a "dump" console command exports it as CSV for plotting.

/* safety.c - safety_update(), checked every 1 ms (excerpt) */
if (SAFETY_STATE_ACTIVE == s_state)
{
    /* Wider limit for the first 2 s after arming, so it can stand up */
    float const tilt_limit_deg = (s_active_ticks < SAFETY_STANDUP_TICKS) ? SAFETY_STANDUP_TILT_LIMIT_DEG
                                                                         : SAFETY_TILT_LIMIT_DEG;
    /* ... */
    if (fabsf(tilt_angle_deg) > tilt_limit_deg)
    {
        new_faults |= SAFETY_FAULT_TILT;
    }
}

if (imu_failed_this_tick || (s_ticks_since_imu_update > SAFETY_IMU_STALE_TICKS))
{
    new_faults |= SAFETY_FAULT_IMU_STALE;
}

if (tick_overrun_this_tick) { new_faults |= SAFETY_FAULT_TICK_OVERRUN; }
if (motor_error_this_tick)  { new_faults |= SAFETY_FAULT_ACTUATOR; }
if (stall_this_tick)        { new_faults |= SAFETY_FAULT_ENCODER; }

if (0U != new_faults)
{
    s_fault_reasons |= new_faults;   /* latched until S1 resets it */

    if (SAFETY_STATE_FAULT != s_state)
    {
        s_state = SAFETY_STATE_FAULT;
        motors_disable();
    }

    return;
}

Driving it from the phone

The Tumbller has a Bluetooth (BLE) module on its board, and ELEGOO's app talks to it. I wanted that app to work unchanged with the RA6T3. One extra wire takes the module's serial output from the Nano socket to P410 (SCI0). The module only ever pulls that line low (the Nano used to provide the high level), so P410's internal pull-up does that job now.

Before writing the decoder, I logged every byte the app sends: zero errors, zero dropped bytes, and a few surprises. The app sends one key when the stick moves and nothing while it's held. A diagonal arrives as two keys about 1 ms apart, and "s" comes when the stick returns to the centre. So keys that arrive within 5 ms count as one command, each command holds until the next one, and a 5 s backstop stops the robot if the link goes quiet.

The UART interrupt only timestamps and queues each byte; the main loop groups the keys and turns them into speed and turn targets:

/* bt_remote.c - SCI0 receive interrupt: timestamp and queue, nothing else */
void bt_uart_callback (uart_callback_args_t * p_args)
{
    if (UART_EVENT_RX_CHAR == p_args->event)
    {
        uint32_t const head = s_head;

        if ((head - s_tail) >= BT_REMOTE_QUEUE_SIZE)
        {
            s_dropped++;
            return;
        }

        s_queue[head & (BT_REMOTE_QUEUE_SIZE - 1U)].tick = control_tick_count();
        s_queue[head & (BT_REMOTE_QUEUE_SIZE - 1U)].byte = (uint8_t) p_args->data;
        s_head = head + 1U;
    }
    /* ... framing / parity / overrun errors are counted ... */
}

/* Main loop: keys that arrive within BT_REMOTE_FRAME_GAP_MS of each other form
 * one joystick update. A key that is absent means 0. */
static void bt_remote_apply_frame (void)
{
    uint32_t const keys = s_frame_keys;
    s_frame_keys = 0U;

    if (0U != (keys & BT_KEY_STOP))
    {
        drive_stop();
        return;
    }

    int32_t const speed = bt_remote_axis(keys, BT_KEY_FORWARD, BT_KEY_BACKWARD,
                                         BT_REMOTE_FORWARD_SIGN * BT_REMOTE_SPEED);
    int32_t const turn  = bt_remote_axis(keys, BT_KEY_LEFT, BT_KEY_RIGHT, BT_REMOTE_TURN);

    (void) drive_command(speed, turn, BT_REMOTE_HOLD_MS, "bt");
}

What went wrong along the way

  • The wheels spun every time I pressed reset. While the MCU is in reset its pins float, and the level shifters' pull-ups drive the motor lines high. The fix is a 1 kΩ pull-down on the 3.3 V side of STBY and the PWM lines.
  • Held in my hand, both wheels ran flat out in one direction. I blamed the balance code first. It was the speed loop: on the floor, wheel motion tilts the body and the balance term corrects it, but in my hand that link is gone and the speed term winds up. Testing the balance loop alone, with the speed loop off, proved it in minutes.
  • It wouldn't stand up from its kickstand like the stock firmware did. My arming limit was 22°, but the robot rests at −18.6° to −34°. The stock code drives at any angle for its first 2 s, so I borrowed that idea with a safer twist: arming works up to 40°, and the wide tilt limit only applies for the first 2 s.
  • The I3C configuration wouldn't generate. The RA6T3 needs I3CCLK/2 ≤ PCLKA ≤ I3CCLK. Setting I3CCLK to PLL/2 (100 MHz) fixed it.
  • One ground wire wasn't enough. I measured 0.15 V between the two boards' grounds, so now there are three ground wires.

Results

  • Control loop: 1 kHz, with tick jitter under 1 µs
  • IMU read: 394 µs in the background; control step: about 8 µs
  • Stand-up from −34°: about 0.2 s
  • Bluetooth: 0 receive errors, 0 dropped bytes
  • Balances untethered on its own battery, takes pushes and drives from the app

What's next

The RA6T3 still has plenty of room. Next on my list: battery monitoring on the ADC, a hardware motor cut-off through the POEG that works even if the software hangs, faster detection of a lost Bluetooth link, and automatic emergency braking with the ultrasonic sensor. That last one is fun on an inverted pendulum, because to stop, it first has to lean back.

Code

Firmware, wiring docs and tools: github.com/Bedair/tiltguard-ra6t3 (MIT licence). The control structure and gains come from ELEGOO's Tumbller example code.

Related Content

Comments


You May Also Like