0

RGB LED on Arduino Nano RP2040 Connect
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

RGB LED on Arduino Nano RP2040 Connect

by blakebr on Wed Aug 04, 2021 3:26 pm

Does Circuit Python 6.3.0 for the Arduino Nano RP2040 Connect support the RGB LED on the Arduino Nano RP2040 Connect? Is there any example code available? Will it be supported in CP version 7.0.0?

TIA
Bruce

blakebr
 
Posts: 86
Joined: Tue Apr 17, 2012 6:23 pm

Re: RGB LED on Arduino Nano RP2040 Connect

by mikeysklar on Thu Aug 05, 2021 1:38 pm

Yes, both releases (6.3.0 and 7.x) support the built-in LED.

https://learn.adafruit.com/circuitpytho ... nect/blink

Code: Select all | TOGGLE FULL SIZE
import time
import board
from digitalio import DigitalInOut, Direction

# LED setup for onboard LED
led = DigitalInOut(board.LED)
led.direction = Direction.OUTPUT

while True:

    led.value = True
    print("led on")
    time.sleep(1)

    led.value = False
    print("led off")
    time.sleep(1)



I did not see in the documentation that this is an RGB LED.

mikeysklar
 
Posts: 5456
Joined: Mon Aug 01, 2016 8:10 pm

Re: RGB LED on Arduino Nano RP2040 Connect

by blakebr on Thu Aug 05, 2021 6:12 pm

Mike,

There are two LED devices on the 'Arduino Nano RP2040 Connect' board. A single amber LED, which your code works with, and a triple RGB LED. It is not a NeoPixel device. It is three LEDs in a package driven through the WiFi NINA chip. Commands must be given to the NINA device to cause the triple LED to light via NINA pins 25, 26, and 27. There is a driver that works through a micropython version. It is beyond my skills to reverse engineer it and make a library for CircuitPython. Here is code from https://github.com/adafruit/circuitpython/pull/4802. I have not been able to make this code work on my device using CircuitPython 6.3.0. Do I need to move up to the alpha version of CircuitPython 7.0.0? The line with "from micropython import const" should be noted.

The 'Arduino Nano RP2040 Connect' board with CP 6.3.0 easily drives NeoPixel devices.

Thanks for your help.
Bruce

Code: Select all | TOGGLE FULL SIZE
# WIFININA spi driver https://github.com/arduino-libraries/WiFiNINA/blob/master/src/utility/spi_drv.cpp
# Commands https://github.com/arduino-libraries/WiFiNINA/blob/master/src/utility/wifi_spi.h
# Adafruit esp32spi https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI

# NINA 27, 26, 25 are R, G, B
from machine import Pin
from machine import SPI
import time
from micropython import const

_START_CMD             = const(0xe0)
_END_CMD               = const(0xee)
_ERR_CMD               = const(0xef)
_REPLY_FLAG            = const(1<<7)
_CMD_FLAG              = const(0)

_SET_PIN_MODE      = 0x50
_SET_DIGITAL_WRITE   = 0x51
_SET_ANALOG_WRITE   = 0x52
_GET_DIGITAL_READ    = 0x53
_GET_ANALOG_READ     = 0x54

ESP_BUSY  = Pin(10, Pin.IN)
ESP_RESET = Pin(3, Pin.OUT, False)
ESP_CS    = Pin(9, Pin.OUT, True)
ESP_GPIO0 = Pin(2)
ESP_MISO  = Pin(8)
ESP_MOSI  = Pin(11)
ESP_SCK   = Pin(14)

LEDR = 27
LEDG = 25
LEDB = 26

spiwifi = SPI(1, baudrate=100000, sck=ESP_SCK, mosi=ESP_MOSI, miso=ESP_MISO)

def reset():
    ESP_GPIO0.init(Pin.OUT)
    ESP_GPIO0.value(True)

    ESP_CS.high()
    ESP_RESET.low()

    time.sleep(0.01)
    ESP_RESET.high()
    time.sleep(0.75)

    ESP_GPIO0.init(Pin.IN)

    print("ESP32 reset")

def wait_ready():
    wait_status = ESP_BUSY.value()

    while wait_status:
        time.sleep(0.05)
        wait_status = ESP_BUSY.value()


def read_response():
    response = []
    response_bytes = []
    bytes_read = 0

    max_read = 32

    wait_ready()
    ESP_CS.low()

    while True:
        read_byte = spiwifi.read(1)
        response_bytes.append(read_byte)
        if read_byte == bytes([_END_CMD]):
            break
        if read_byte == bytes([_ERR_CMD]):
            raise RuntimeError('Error waiting on WiFi Command')

        bytes_read += 1
        if bytes_read > max_read:
            raise RuntimeError('Error waiting on WiFi command')

    ESP_CS.high()

    num_params = response_bytes[2]
    index = 3

    for _ in num_params:
        length = int.from_bytes(response_bytes[index], "little")
        newparam = response_bytes[index+1:index+1+length]
        index += 1 + length
        response.append(b''.join(newparam))

    return response

def send_command(cmd, params=[], num_params=-1):
    if num_params < 0:
        num_params = len(params)
   
    total_bytes = 3 + num_params + 1

    for p in params:
        pbytes = bytearray(p)
        total_bytes = total_bytes + len(pbytes)
   
    sendbuffer = bytearray(total_bytes)

    sendbuffer[0] = _START_CMD
    sendbuffer[1] = cmd & ~_REPLY_FLAG
    sendbuffer[2] = num_params

    index = 3

    for p in params:
        pbytes = bytearray(p)
        lenpbytes = len(pbytes)

        sendbuffer[index] = lenpbytes
        sendbuffer[index+1:index+1+lenpbytes] = pbytes
        index += 1+lenpbytes
    sendbuffer[index] = _END_CMD

    wait_ready()
    ESP_CS.low()
    spiwifi.write(sendbuffer)
    ESP_CS.high()

def set_ninapin_mode(pin, mode):
    send_command(_SET_PIN_MODE, [[pin], [mode]])
    return read_response()

def ninapin_digital_write(pin, value):
    if value == 0:
        value = 1
    else:
        value = 0

    send_command(_SET_DIGITAL_WRITE, [[pin], [value]])
    return read_response()

reset()

for pin in [LEDR, LEDG, LEDB]:
    set_ninapin_mode(pin, Pin.OUT)
    ninapin_digital_write(pin, 0)

while True:
    time.sleep(0.25)
    ninapin_digital_write(LEDR, 1)
    ninapin_digital_write(LEDG, 0)
    ninapin_digital_write(LEDB, 0)
    time.sleep(0.25)
    ninapin_digital_write(LEDR, 0)
    ninapin_digital_write(LEDG, 1)
    ninapin_digital_write(LEDB, 0)
    time.sleep(0.25)
    ninapin_digital_write(LEDR, 0)
    ninapin_digital_write(LEDG, 0)
    ninapin_digital_write(LEDB, 1)

blakebr
 
Posts: 86
Joined: Tue Apr 17, 2012 6:23 pm

Re: RGB LED on Arduino Nano RP2040 Connect

by mikeysklar on Fri Aug 06, 2021 12:19 pm

Bruce,

I think the esp32spi module will be the CircuitPython path to control the Nano RP2040 RGB LED. What I don't see that the Arduino world and Micropython world seem to have is a ninapin definnition that hides all the layers that need to be gone through to toggle the LED pins. It might make sense to open an issue here requesting ninapin support to control the RGB LED.

https://github.com/adafruit/Adafruit_Ci ... SPI/issues

arduino:
https://forum.arduino.cc/t/example-mult ... d/865075/7

micropython:
https://github.com/adafruit/circuitpyth ... -852197442

mikeysklar
 
Posts: 5456
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.