All About Circuits

Industry White Paper

Control LED Brightness and Pulse Widths: Generating PWM Output on the FPB-RA8E1

Learn how to generate PWM output on the Renesas FPB-RA8E1, configure the General PWM Timer in e² studio, and sweep LED brightness by changing the duty cycle.


November 21, 2026 by Renesas Electronics

Toggling a microcontroller GPIO pin high and low gives you binary on-or-off control, which is enough for status indicators or relay coils. As soon as you need to dim an LED smoothly, regulate the speed of a DC motor, or drive a servo actuator, simple digital writes fall short. Generating a variable average voltage from a digital pin requires Pulse Width Modulation (PWM), and doing it efficiently means offloading the waveform generation to a dedicated hardware timer rather than bit-banging delays in software.

This walkthrough demonstrates how to configure and generate a variable-duty-cycle PWM signal on the Renesas FPB-RA8E1 Fast Prototyping Board. Using the e² studio IDE and the Flexible Software Package (FSP), you will configure the General PWM Timer (r_gpt) stack, route its dual output signals to both the onboard user LED and the Arduino Uno R3 interface header, write C code to sweep the duty cycle from 5% to 100% in 5% increments, and verify the resulting waveform on a logic analyzer.

 

Project Specs

Target Board FPB-RA8E1
MCU Family Renesas RA8 Series (R7FA8E1AFDCFB)
Peripheral Covered General PWM Timer (r_gpt, Channel 3 / GPT3)
IDE / Toolchain e² studio + LLVM Embedded Toolchain for Arm (21.1.1)
FSP Version 6.3.0
Interface Used Onboard LED1 (P404 / GTIOC3B) and Arduino Header D2 (P300 / GTIOC3A)
Baud Rate N/A (PWM output at 10000 raw counts / about 12 kHz)
Skill Level Beginner
Build Time 20 minutes
Prerequisites e² studio installed, FPB-RA8E1 initialized, logic analyzer (optional)

 

Why Does Hardware PWM Matter on the FPB-RA8E1?

Driving an LED or motor coil with a software delay loop forces the main processor to babysit every rising and falling edge. On a 360 MHz Arm Cortex-M85 microcontroller like the Renesas RA8E1, any interrupt latency from communication stacks or sensor-processing tasks would jitter a software-timed pulse train, causing visible LED flicker or audible motor whine.

The Renesas RA family solves this with the General PWM Timer (GPT) peripheral. Once you configure the timer period and start the counter, the GPT hardware toggles up to two output pins per channel (GTIOCA and GTIOCB) autonomously with single-clock-cycle precision. Your application only intervenes when you want to change the duty cycle via R_GPT_DutyCycleSet, leaving the Cortex-M85 core completely free between updates.

 

What You’ll Need

Hardware

  • Renesas FPB-RA8E1 Fast Prototyping Board
  • USB cable for power and onboard J-Link debugging
  • Logic analyzer (such as Saleae Logic 2.4.36) connected to Arduino header pin D2 (P300) for waveform inspection

 

Software

 

The Concept in 60 Seconds

Pulse Width Modulation controls the average power delivered to a load by switching a digital pin between high and low states at a fixed frequency and varying the duty cycle, which is the percentage of each period that the signal remains high. An 80% duty cycle delivers high average voltage and high LED brightness, a 40% duty cycle produces medium brightness, and a 10% duty cycle yields low brightness (Figure 1).

 

Duty cycle and LED brightness: an 80% duty cycle delivers high brightness, 40% medium, and 10% low.

Figure 1. Duty cycle and LED brightness: an 80% duty cycle delivers high brightness, 40% medium, and 10% low. Image used courtesy of Renesas

 

In Saw-wave PWM mode, the GPT hardware counter counts up from zero to a configured Period value (10000 Raw Counts in this project) and then resets to zero. Within each cycle, the pin stays active until the counter matches the compare-match duty-cycle count.

To update the pulse width dynamically at runtime, your firmware follows a five-stage flow:

  1. Open the GPT module (R_GPT_Open).
  2. Start the hardware counter (R_GPT_Start).
  3. Query the active timer period in raw counts (R_GPT_InfoGet).
  4. Calculate the raw compare-match count for your target duty-cycle percentage.
  5. Apply the new count to the output pins (R_GPT_DutyCycleSet).

 

Step-by-Step: Building the PWM Project

Step 1: Verify the Onboard User LED Pin Assignment

  1. Open the FPB-RA8E1 v1 User’s Manual [PDF] (R20UT5474EG0100) to page 9 and inspect the solder-bridge configuration table. Verify that solder bridge E27 (User LED 1) is closed by default and connects LED1 to P404 (while E26 connects LED2 to P408), as shown in Figure 2.

     

    The solder-bridge table on page 9 of the FPB-RA8E1 v1 User’s Manual, with E27 connecting LED1 to P404 and E26 connecting LED2 to P408.

    Figure 2. The solder-bridge table on page 9 of the FPB-RA8E1 v1 User’s Manual, with E27 connecting LED1 to P404 and E26 connecting LED2 to P408. Image used courtesy of Renesas

     

Confirming the onboard LED pin assignment in the hardware documentation ensures you select the matching GPT channel (GPT3) in the FSP configurator.

 

Step 2: Create a Minimal Bare-Metal Project in e² studio

  1. Open e² studio and click File > New > Renesas C/C++ Project > Renesas RA.
  2. In the New C/C++ Project (Templates for Renesas RA Project) dialog, select Renesas RA C/C++ Project and click Next >.
  3. In the Project Name and Location dialog, enter FPB_RA8E1_PWM into the Project name field and click Next >.
  4. In the Device and Tools Selection dialog (Figure 3), confirm FSP Version is 6.3.0 and select FPB-RA8E1 from the Board drop-down menu, which populates R7FA8E1AFDCFB as the device and CM85 as the core. Verify C is selected as the language, LLVM Embedded Toolchain for Arm (21.1.1) is selected, and J-Link ARM is chosen as the debugger, then click Next >.

     

    The Device and Tools Selection dialog with FSP 6.3.0 and the FPB-RA8E1 board selected, which populates the R7FA8E1AFDCFB device and CM85 core.

    Figure 3. The Device and Tools Selection dialog with FSP 6.3.0 and the FPB-RA8E1 board selected, which populates the R7FA8E1AFDCFB device and CM85 core. Image used courtesy of Renesas

     

  5. Select Flat (Non-TrustZone) Project and click Next >. Choose None for SmartBundle and click Next >. Select Executable and No RTOS, click Next >, select Bare Metal - Minimal, and click Finish. Click Open Perspective when prompted to open the FSP Configuration perspective.

Starting with the Bare Metal - Minimal template sets up the system clocks and I/O port driver without extra RTOS overhead.

 

Step 3: Add and Configure the General PWM Timer Stack

  1. In the FSP Configuration editor (configuration.xml), open the Stacks tab and click New Stack > Timers > Timer, General PWM (r_gpt).
  2. Click the newly added g_timer0 Timer, General PWM (r_gpt) block and open the Properties window.
  3. Under Common, change Pin Output Support from Disabled to Enabled.
  4. Expand Module g_timer0 Timer, General PWM (r_gpt) > General. Change Mode from Periodic to Saw-wave PWM, set Period to 10000, and keep Period Unit set to Raw Counts (Figure 4).

     

    The g_timer0 properties with Pin Output Support enabled, Mode set to Saw-wave PWM, and Period set to 10000 raw counts.

    Figure 4. The g_timer0 properties with Pin Output Support enabled, Mode set to Saw-wave PWM, and Period set to 10000 raw counts. Image used courtesy of Renesas

     

  5. Expand the Output section. Keep Duty Cycle Percent (only applicable in PWM mode) at 50, change GTIOCA Output Enabled from False to True, and change GTIOCB Output Enabled from False to True, leaving both stop levels at Pin Level Low (Figure 5).

     

    The Output section with both GTIOCA and GTIOCB Output Enabled set to True and their stop levels left at Pin Level Low.

    Figure 5. The Output section with both GTIOCA and GTIOCB Output Enabled set to True and their stop levels left at Pin Level Low. Image used courtesy of Renesas

     

Enabling Pin Output Support along with both GTIOCA Output Enabled and GTIOCB Output Enabled allows a single GPT channel to output synchronized PWM waveforms on two physical pins at once.

 

Step 4: Map GPT3 to P404 and P300 and Resolve Pin Conflicts

  1. Switch to the Pins tab in the FSP Configuration editor. Under Pin Selection > Ports > P4, select P404 (Symbolic Name: LED1). Expanding Port Capabilities at the bottom of the Pin Configuration pane shows that P404 supports GPT3_GTIOC3B.
  2. Change Mode for P404 from Output mode (Initial Low) to Disabled.
  3. Scroll down Pin Selection under Peripherals to Timers:GPT, expand it, and select GPT3.
  4. In the Pin Configuration pane for GPT3, change Operation Mode from Disabled to GTIOCA and GTIOCB, which automatically assigns GTIOC3A to P403 and GTIOC3B to P404. Per the FPB-RA8E1 Quick Start Guide, P300 connects to pin D2 of the Arduino interface header; change GTIOC3A from P403 to P300 (Figure 6).

     

    The GPT3 pin configuration with Operation Mode set to GTIOCA and GTIOCB, GTIOC3A reassigned to P300, and GTIOC3B on P404.

    Figure 6. The GPT3 pin configuration with Operation Mode set to GTIOCA and GTIOCB, GTIOC3A reassigned to P300, and GTIOC3B on P404. Image used courtesy of Renesas

     

  5. To clear the red pin-conflict error that appears on P300, scroll up Pin Selection to Ports > P3 and select P300 (Symbolic Name: Arduino_D2). Change its Mode from Input mode to Disabled, which transitions P300 into Peripheral mode (GPT3_GTIOC3A).
  6. Return to the Stacks tab, select g_timer0 Timer, General PWM (r_gpt), and under Properties > General, change Channel from 0 to 3. Confirm that GTIOCA (P300) and GTIOCB (P404) replace under Pins (Figure 7), then click Generate Project Content and click Proceed.

     

    The g_timer0 stack after Channel is changed to 3, with GTIOC3A and GTIOC3B resolved to P300 and P404.

    Figure 7. The g_timer0 stack after Channel is changed to 3, with GTIOC3A and GTIOC3B resolved to P300 and P404. Image used courtesy of Renesas

     

  7. In Project Explorer, expand the src folder and open hal_entry.c.

Disabling the default GPIO modes on P404 (LED1) and P300 (Arduino_D2) hands control of both physical pins over to the GPT3 peripheral output stage.

 

Step 5: Implement the Duty-Cycle Sweep Function in hal_entry.c

  1. Inside hal_entry.c, above void hal_entry(void), define a helper function void update_pwm() and declare a timer_info_t info; structure and an error variable fsp_err_t err = FSP_SUCCESS;.
  2. To reference the official duty-cycle calculation formula, switch to the FSP Configuration view, click the blue information icon (i) on the g_timer0 Timer, General PWM (r_gpt) stack block to open the RA Flexible Software Package Documentation, and scroll down to GPT Duty Cycle Update Example (gpt_duty_cycle_calculation_example).
  3. Return to hal_entry.c, expand Developer Assistance > HAL/Common > g_timer0 Timer, General PWM (r_gpt) in Project Explorer, and drag Call R_GPT_InfoGet() into update_pwm(). Pass &info as the second argument (err = R_GPT_InfoGet(&g_timer0_ctrl, &info);) and follow it with assert(FSP_SUCCESS == err);.
  4. At the top of hal_entry.c below #include "hal_data.h", declare two global 32-bit variables to store the timer period and calculated pulse width (uint32_t pwm_period; and uint32_t width_cycles;). Inside update_pwm() below assert(FSP_SUCCESS == err);, read the period from the timer_info_t structure by assigning pwm_period = info.period_counts;.
  5. Below pwm_period = info.period_counts;, create a for loop that steps i from 5 to 100 in increments of 5, and calculate the raw compare-match count using 64-bit intermediate multiplication:
    for(uint8_t i = 5; i <= 100; i += 5)
    {
    width_cycles = (uint32_t) (((uint64_t) pwm_period * i) / 100);
  6. Drag Call R_GPT_DutyCycleSet() from Developer Assistance into the for loop and add assert(FSP_SUCCESS == err); directly below it. Pass width_cycles as the second argument and GPT_IO_PIN_GTIOCA_AND_GTIOCB as the third argument (err = R_GPT_DutyCycleSet(&g_timer0_ctrl, width_cycles, GPT_IO_PIN_GTIOCA_AND_GTIOCB);), then follow the assertion with a 100-millisecond delay: R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS); (Figure 8).

     

    The completed update_pwm() function, with the period query, the duty-cycle sweep, and the 64-bit cast that guards the multiplication.

    Figure 8. The completed update_pwm() function, with the period query, the duty-cycle sweep, and the 64-bit cast that guards the multiplication. Image used courtesy of Renesas

     

Querying info.period_counts at runtime and casting pwm_period to uint64_t before multiplying by the percentage i prevents 32-bit arithmetic overflow and keeps your duty-cycle math independent of hardcoded period constants.

 

Step 6: Open and Start the Timer in hal_entry()

  1. Inside void hal_entry(void), declare fsp_err_t err = FSP_SUCCESS;.
  2. Drag Call R_GPT_Open() from Developer Assistance into hal_entry() (err = R_GPT_Open(&g_timer0_ctrl, &g_timer0_cfg);) and add assert(FSP_SUCCESS == err);.
  3. Drag Call R_GPT_Start() from Developer Assistance below the open check (err = R_GPT_Start(&g_timer0_ctrl);) and add assert(FSP_SUCCESS == err);.
  4. Add an infinite while(1) super-loop that calls update_pwm(); continuously (Figure 9).

     

    hal_entry() with the timer opened and started, followed by the while(1) super-loop that calls update_pwm().

    Figure 9. hal_entry() with the timer opened and started, followed by the while(1) super-loop that calls update_pwm(). Image used courtesy of Renesas

     

 

Step 7: Build, Debug, and Verify the PWM Waveform

  1. Select FPB_RA8E1_PWM in Project Explorer and click the hammer icon to build the project.
  2. Right-click FPB_RA8E1_PWM, select Debug As > Renesas GDB Hardware Debugging, and click Switch to open the Debug perspective (where execution initially pauses at SystemInit() inside Reset_Handler() in startup.c).
  3. Click Resume once to advance to hal_entry() in main.c, and click Resume a second time to begin running the application (Running appears in the bottom-left status bar if there are no errors. If there are errors, the indicator displays an error state and halts program execution).
  4. Connect a logic analyzer to Arduino header pin D2 (P300) and capture the waveform on Channel 0 while observing LED1 (P404) on the FPB-RA8E1 smoothly ramping in brightness.

 

Code Snippet

void update_pwm() {
    timer_info_t info;
    fsp_err_t err = FSP_SUCCESS;
    err = R_GPT_InfoGet(&g_timer0_ctrl, &info);
    assert(FSP_SUCCESS == err);
    pwm_period = info.period_counts;

    for(uint8_t i = 5; i <= 100; i += 5) {
        width_cycles = (uint32_t) (((uint64_t) pwm_period * i) / 100);
        err = R_GPT_DutyCycleSet(&g_timer0_ctrl, width_cycles, GPT_IO_PIN_GTIOCA_AND_GTIOCB);
        assert(FSP_SUCCESS == err);
        R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
    }
}

 

What You Should See

While the debugger status indicator reads Running, LED1 on the FPB-RA8E1 gradually brightens from dim (5% duty cycle) to full brightness (100% duty cycle) in 20 steps spaced 100 milliseconds apart, resetting back to 5% every 2.0 seconds. Connecting a logic analyzer to Arduino header pin D2 (P300 / GTIOC3A) reveals an approximately 12 kHz square wave (83.5 µs total period, with a 54.25 µs high pulse width when inspected at 64.97% duty cycle) whose positive pulse width increases by 5% every 100 ms until it reaches 100% (Figure 10).

 

The logic analyzer capture on Channel 0, reading 64.97% duty at 11.976 kHz, with a 54.25 µs high pulse inside an 83.5 µs period.

Figure 10. The logic analyzer capture on Channel 0, reading 64.97% duty at 11.976 kHz, with a 54.25 µs high pulse inside an 83.5 µs period. Image used courtesy of Renesas

 

Troubleshooting

The timer runs, but neither LED1 nor D2 outputs a signal

Verify three settings in the FSP Configuration editor: Pin Output Support must be set to Enabled under Common, GTIOCA Output Enabled and GTIOCB Output Enabled must both be set to True under Output, and Channel must be set to 3 so g_timer0 maps to GPT3 (P300 and P404).

 

Red pin-configuration errors appear when assigning P404 or P300 to GPT3

In the default board template, P404 (LED1) is configured as Output mode (Initial Low) and P300 (Arduino_D2) is configured as Input mode. In the Pins tab under Ports, set Mode to Disabled on both P404 and P300 before assigning them to GPT3.

 

The duty cycle jumps erratically at high period counts

If you increase Period to a large 32-bit value, multiplying pwm_period * i in 32-bit math can overflow uint32_t. Always cast pwm_period to (uint64_t) before multiplying by i.

 

Where to Take It Next

Now that GPT3 is generating a saw-wave PWM signal, try switching the timer Mode in the FSP Properties view to Triangle-wave PWM (symmetric) for center-aligned motor-drive waveforms, or replace R_BSP_SoftwareDelay with a periodic timer interrupt to update the duty cycle without blocking the main loop. You can also explore dead-time insertion using the Three-Phase PWM (r_gpt_three_phase) stack.

 

Frequently Asked Questions

How do you change the PWM duty cycle at runtime on a Renesas RA8 MCU?

First call R_GPT_InfoGet to retrieve info.period_counts, calculate the desired raw count as (uint32_t) (((uint64_t) pwm_period * duty_percent) / 100), and pass that value to R_GPT_DutyCycleSet along with the target pin mask (GPT_IO_PIN_GTIOCA, GPT_IO_PIN_GTIOCB, or GPT_IO_PIN_GTIOCA_AND_GTIOCB).

 

Can a single General PWM Timer (GPT) channel drive two pins at once?

Yes. Each GPT channel provides two output compare pins: GTIOCA and GTIOCB. Enabling both outputs in the FSP stack properties and passing GPT_IO_PIN_GTIOCA_AND_GTIOCB to R_GPT_DutyCycleSet updates both pins simultaneously.

 

Why do you need to disable the GPIO mode on P404 and P300 before using them for PWM?

In the FPB-RA8E1 default project configuration, P404 (LED1) is pre-configured as a GPIO digital output (Output mode (Initial Low)) and P300 (Arduino_D2) is pre-configured as a GPIO input (Input mode). Setting Mode to Disabled under Ports in the Pins tab clears the pin-assignment conflict and switches both pins into Peripheral mode (GPT3_GTIOC3B and GPT3_GTIOC3A).

 

Related Resources