Please help me with an arduino code.
How can simply I turn on and off the builtin RGB LED (blue color)??
Looking at the schematic and code (I don't actually have one to try out), it should be:
pinMode(PIN_LED_B, OUTPUT); // pin in output mode
digitalWrite(PIN_LED_B, LOW); // turn LED ON
digitalWrite(PIN_LED_B, HIGH); // Turn LED OFF.
Note that LED On and Off are "backward" compared to most Arduinos - you write LOW to turn on.
Thank you for your quick response.
The problem is: how can I give the right color for RGB Led??
WestfW via Arduino Forum <notifications@arduino.discoursemail.com> ezt írta (időpont: 2023. szept. 12., K, 8:26):
By sending signals to the RGB pins PIN_LED_R
, PIN_LED_G
, PIN_LED_B
. Low level turns on the corresponding color, HIGH turns it off.
Do you mean Blue led>Pin25; Green led> Pin16; Red led> Pin17???
If yes, I know this.
I wasn't clear enough, I would like to turn on/off the other builtin LED>Pin:11.
B707 via Arduino Forum <notifications@arduino.discoursemail.com> ezt írta (időpont: 2023. szept. 12., K, 10:22):
The SEEED RP2040 has 3 individual user LEDs, Red, Blue and Green, pins as given above.
The seperate RGB LED is a WS2812 Neopixel, examples on driving it are on the SEEED Wiki.
According to schematic, it is a n addressable led. To control it you can use any WS2812 compatible library - for example Neopixel or FastLEd
Thank you b707 and srnet!
Can I use the separate RGB Led without Neopixel (or FastLEd) library (as I mentioned, only blue led switch on and off)?
What is WS2812?
B707 via Arduino Forum <notifications@arduino.discoursemail.com> ezt írta (időpont: 2023. szept. 12., K, 10:55):
Why do you need a RGB led, if you want to switch only blue color?
Wouldn't it be easier to take a blue monochrome LED?
OK, you are right!
But I thought about using this RGB led, because my idea is:
Blue led switch on: during measurement, off: when the last data is arrived
AND green LED switch on standby mode.
B707 via Arduino Forum <notifications@arduino.discoursemail.com> ezt írta (időpont: 2023. szept. 12., K, 11:16):
Why doesn't the code from post #2 work for you?
Ah. I checked the schematic, but didn't notice that the board has both a discrete RGB LED (three pins (PIN_LED_x)) AND and a "Smart ws2812 RGB LED"...
No, you cant drive the ws2812 without some sort of library like NeoPixel or FastLED.
I mean, it's sort of annoying to have that fancy library when all you want to do is simple manipulation of a single LED, but AFAIK there is no "simpler" library.
I wrote the following bit of code for driving the "single neopixel" present on some boards. It's been tested on an Adafruit QT Py M0 (and should work on most 48MHz SAMD2x boards) and a Cytron Maker Pi rp2040 board (and should work on most rp2040 boards, including the Xiao, if NEOPIN is adjusted appropriately...
/*
* Single NeoPixel on 48MHz SAMD21 (eg Adafruit QT Py) or RP24040.
* Sep 2023 by William Westfield (WestfW) - Released to the public domain.
*
* API Provided:
* singleNeo(pin, red, green, blue);
* Sets the pin to output and lights it up with the specified RGB value.
*
* https://wp.josh.com/2014/05/13/ws2812-neopixels-are-not-so-finicky-once-you-get-to-know-them/
* A neopixel bit is about 1200ns long. It starts with a HIGH
* signal of about 300ns, has a 2nd ~300ns that matches the data
* value, ends with a low signal of about 500ns.
* debug hint: if the neopixel lights "white", that probably means that the
* initial HIGH pulse is too long for a 0 bit.
*/
//
//-----------------------------------------------------------------
//
#if defined(ARDUINO_ARCH_SAMD) && (F_CPU == 48000000L)
extern "C" {void delay300(); void delay800();};
asm("delay800: push {lr}\n"
" bl delay300\n"
" bl delay300\n"
" pop {pc}\n"
"delay300: push {lr}; nop; nop; pop {pc}\n");
#define _singleneo_setupIO(pin) \
PortGroup *neoport = digitalPinToPort(pin); \
uint32_t pinMask = 1ul << g_APinDescription[pin].ulPin;
#define _singleneo_setbit neoport->OUTSET.reg = pinMask
#define _singleneo_clrbit neoport->OUTCLR.reg = pinMask
// Adafruit QT Py M0
#define NEOPIN 11
//
//-----------------------------------------------------------------
//
#elif (ARDUINO_ARCH_RP2040)
// these delay functions assume that SysTick won't wrap, which is ensured
// in the setupIO function by making sure there is enough time before wrap
// to send out all 24 bits.
void delay800() {
uint32_t startt = SysTick->VAL;
while ((startt - SysTick->VAL) < (uint32_t)(800e-9/(1.0/F_CPU)))
;
}
void delay300() {
uint32_t startt = SysTick->VAL;
while ((startt - SysTick->VAL) < (uint32_t)(300e-9/(1.0/F_CPU)))
;
}
#define _singleneo_setupIO(pin) \
while (SysTick->VAL < (uint32_t) (100*1200e-9*F_CPU)) ; \
auto neoport = sio_hw; \
uint32_t pinmask = 1<<pin
#define _singleneo_setbit neoport->gpio_set = pinmask
#define _singleneo_clrbit neoport->gpio_clr = pinmask
// Cytron Maker Pi
#define NEOPIN 18
#endif
//
//-----------------------------------------------------------------
//
// Support functions
static void inline _singleNeoSendBytes (uint32_t pin, uint8_t *ptr) {
uint8_t p, *end = ptr+3;
_singleneo_setupIO(pin);
p = *ptr++;
uint32_t bitMask = 0x80;
noInterrupts();
for (;;) {
_singleneo_setbit;
delay300();
if (p & bitMask) {
delay800();
_singleneo_clrbit;
} else {
_singleneo_clrbit;
delay800();
}
if (bitMask >>= 1) {
delay300();
asm("nop;nop;nop;nop;");
} else {
if (ptr >= end)
break;
p = *ptr++;
bitMask = 0x80;
}
}
interrupts();
}
// The main API - set the (singe or first) neopixel on the given pin
// to the provided R, G, B values.
void singleNeo(int pin, int redv, int greenv, int bluev) {
uint8_t vals[3] = {(uint8_t)greenv, (uint8_t)redv, (uint8_t)bluev};
pinMode(pin, OUTPUT);
_singleNeoSendBytes(pin, vals);
}
//
//-----------------------------------------------------------------
// Skeleton program
void setup() {
delay(500);
}
void loop() {
singleNeo(NEOPIN, 0, 0, 0); // off
delay(1000);
singleNeo(NEOPIN, 30, 0, 0); // red
delay(1000);
singleNeo(NEOPIN, 0, 30, 0); // green
delay(1000);
singleNeo(NEOPIN, 0, 0, 30); // blue
delay(3000);
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.