Quantcast
Channel: Raspberry Pi Forums
Viewing full articles (7807 articles)
Browse article index
↧

SDK • SOLVED: Can the Status LED and the RGB LED be used together with pico_status_led?

$
0
0
I have a Vcc-gnd yd-rp2040 board which has a status LED and an RGB status LED on board. I have written a couple of projects to blink one or the other with no problems.

I wondered if it was possible to use both LEDs in the same project, so I wrote the code below. It works, but as you might note from the comments, there is intermittent behaviour going on. Here's the code:

Code:

//============================================================// BlinkyLED_RGB.//// Can we use the RGB and normal status LED at the same time?// It appears so, but there may be problems after flashing or// powering on the board as sometimes the RGB LED doesn't come// back on.//// Tested on Vcc-gnd yd-rp2040 boards with 16Mb on board. //// Requires SDK 2.2.0 or higher.//// Norman Dunbar.// February 4 2026.//============================================================// NOTE.//// You will see some "return 0" statements in the code, used// when the Status LED isn't supported or won't initialise. In// embedded systems, returning from main() is considered a // no-no! However, with the Pico, returning from main() will// call exit() which then calls _exit(), which spins. So, we// can safely return from main().//============================================================#include <stdio.h>#include "pico/stdlib.h"#include "pico/status_led.h"int main() {    const uint32_t colours[] = {0x00000030,     // Blue                                0x00300000};    // Red        stdio_init_all();    //--------------------------------------------------------    // Do we have status LED access?    //--------------------------------------------------------    if (!status_led_supported()) {        printf("Status LED is not supported.\n");        return 0;    }        //--------------------------------------------------------    // Initialise the LED, wherever it is. Sets LED to output    // automatically.    //--------------------------------------------------------    if (!status_led_init()) {        printf("Cannot initialise Status LED.\n");        return 0;    }    //--------------------------------------------------------    // Do we have RGB status LED access?    //--------------------------------------------------------    if (!colored_status_led_supported()) {         printf("RGB Status LED is not supported.\n");        return 0;    }        //--------------------------------------------------------    // Initialise the RGB LED, wherever it is. Sets RGB LED to    // output automatically.    //--------------------------------------------------------    if (!status_led_init()) {        printf("Cannot initialise RGB Status LED.\n");        return 0;    }        //--------------------------------------------------------    // WARNING WARNING WARNING WARNING WARNING WARNING WARNING    //--------------------------------------------------------    // On my vcc-gnd-yd-rp2040 boards, any changes made here    // need the board to be reset by the RST button. Power    // cycling leaves the RGB led unlit constantly. I have     // seen the RST button not having any effect too. Weird.    // I wonder if it's a bug? SDK? Board itself?    //--------------------------------------------------------    // INTERMITTENT BEHAVIOUR    //    // 1. Power on, flash project - no RGB LED. Blue LED ok.    // 2. Long, 2 second, RST - RGB LED blinks, blue LED ok.    // 3. Flash unchanged code - both LEDs ok.    // 4. Power off. Wait 5--10 seconds.    // 5. Power on - sometimes both LEDs ok, usually not RGB.    // 6. Short RST press, usually bring RGB LED on again.    // 7. Bonus! Id RGB is on and ok, short RST press hangs it    //    showing just one colour. After which, short or long    //    RST presses simply turn off the RGB LED, never to    //    return until power reset and long button press.    //--------------------------------------------------------    while (true) {        // Illuminate the status LED, set RGB to blue.        status_led_set_state(true);        colored_status_led_set_on_with_color(colours[0]);        sleep_ms(500);                // Extinguish the status LED, set RGB to red.        status_led_set_state(false);        colored_status_led_set_on_with_color(colours[1]);        sleep_ms(500);        // Extinguish the RGB LED now too.        colored_status_led_set_state(false);        sleep_ms(500);    }        //--------------------------------------------------------    // Never gets here, but if it did, we should deinitialise    // the status LED when we are done using it.    //--------------------------------------------------------    status_led_deinit();}
And here's the CMakeLists.txt file to build it.

Code:

# Generated Cmake Pico project filecmake_minimum_required(VERSION 3.13)set(CMAKE_C_STANDARD 11)set(CMAKE_CXX_STANDARD 17)set(CMAKE_EXPORT_COMPILE_COMMANDS ON)# Initialise pico_sdk from installed location# (note this can come from environment, CMake cache etc)# == DO NOT EDIT THE FOLLOWING LINES for the Raspberry Pi Pico VS Code Extension to work ==if(WIN32)    set(USERHOME $ENV{USERPROFILE})else()    set(USERHOME $ENV{HOME})endif()set(sdkVersion 2.3.0)set(toolchainVersion 15_2_Rel1)set(picotoolVersion 2.3.0)set(picoVscode ${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)if (EXISTS ${picoVscode})    include(${picoVscode})endif()# ============================================================set(PICO_BOARD vcc-gnd_yd-rp2040_16m CACHE STRING "Board type")# Pull in Raspberry Pi Pico SDK (must be before project)include(pico_sdk_import.cmake)project(BlinkyLED_RGB C CXX ASM)# Initialise the Raspberry Pi Pico SDKpico_sdk_init()# Add executable. Default name is the project name, version 0.1add_executable(BlinkyLED_RGB BlinkyLED_RGB.c )pico_set_program_name(BlinkyLED_RGB "BlinkyLED_RGB")pico_set_program_version(BlinkyLED_RGB "0.1")# Modify the below lines to enable/disable output over UART/USB.pico_enable_stdio_uart(BlinkyLED_RGB 1)pico_enable_stdio_usb(BlinkyLED_RGB 0)# Add the standard library to the buildtarget_link_libraries(BlinkyLED_RGB        pico_stdlib        pico_status_led)# Add the standard include files to the buildtarget_include_directories(BlinkyLED_RGB PRIVATE        ${CMAKE_CURRENT_LIST_DIR})# Create the EF2 and other files from the ELF.pico_add_extra_outputs(BlinkyLED_RGB)
And here are the problems I'm seeing:

1. Power on, flash project - no RGB LED. Blue status LED ok.
2. Long, 2 second, RST - RGB LED blinks, blue LED ok.
3. Flash unchanged code - both LEDs now ok.
4. Power off. Wait 5--10 seconds.
5. Power on - sometimes both LEDs ok, usually RGB remains unlit.
6. Short RST press, usually brings RGB LED on again, but not always.
7. Bonus! If RGB is on and ok, short RST press hangs it showing just one colour. After which, short or long RST presses simply turn off the RGB LED, never to return until power reset and long button press.

I'm thinking it is probably the board that is the problem, but it has occurred on two boards of this type. I'd be grateful if anyone knows whether the pico_status_led library should not be used in this manner?


Cheers,
Norm.

Statistics: Posted by NormanDunbar — Tue Sep 15, 2026 1:21 pm — Replies 3 — Views 42


↧

Viewing full articles (7807 articles)
Browse article index

Latest Images

Trending Articles



Latest Images