Example Code for Arduino-PWM Output

Last revision 2026/01/18

This article guides users on creating a breathing LED effect using Arduino PWM on the DFRobot Beetle RP2040, detailing hardware and software setup, sample code, and PWM functionality.

Hardware Preparation

Software Preparation

Wiring Diagram

No external wiring required (uses the onboard LED).

Other Preparation Work

  1. Open Arduino IDE.
  2. Select "DFRobot Beetle RP2040" as the development board.
  3. Connect the Beetle RP2040 to your computer.

Sample Code

Function: use PWM to drive the onboard LED to present a breathing lighting effect

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 downloading the code, the onboard green LED gradually gets bright and then fades.

Result

Additional Information

  • PWM works by varying the duty cycle (percentage of time the signal is high) of a digital signal.
  • The analogWrite() function takes a value between 0 (0% duty cycle, LED off) and 255 (100% duty cycle, LED full brightness).

Was this article helpful?

TOP