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
- DFRobot Beetle RP2040, 1, Purchase Link
- USB Type-C Cable, 1
Software Preparation
- Arduino IDE: Download Link
- Raspberry Pi Pico/RP2040 SDK: Installed via Arduino Boards Manager (see Getting Started)
Wiring Diagram
No external wiring required (uses the onboard LED).
Other Preparation Work
- Open Arduino IDE.
- Select "DFRobot Beetle RP2040" as the development board.
- 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.

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?
