Example Code for Arduino-GPIO PWM

Last revision 2026/01/08

This article offers a comprehensive guide to using Arduino GPIO PWM, including sample code for controlling LED brightness through pulse width modulation, with practical applications in various fields.

Hardware Preparation

Sample Code

void setup(){
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop(){

byte value;

  for (value=0; value<255; value++)
  { 
    analogWrite(LED_BUILTIN, value);
    delay(10);
  } 
  
  for (value=255; value>0; value--)
  { 
    analogWrite(LED_BUILTIN, value);
    delay(10);
  }  
}

Result

After the program is downloaded, the intensity of the blue LED on the board shows a gradually brightening and then gradually dimming breathing state.

Additional Information

PWM (Pulse Width Modulation) is a very effective technique that uses the digital interface of the MCU to control the analog circuit by pulse width modulation. It is widely used in many fields such as light control, motor speed control, measurement, communication to power control and conversion.

All 11 GPIOs of Beetle RP2350 support PWM output. In the Arduino development environment, PWM output is defined as analog output, and the analogWrite() function is used for control. The range of PWM values is 0~255 .

Was this article helpful?

TOP