Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Adafruit Logo
0
Adafruit Trinket M0 CircuitPython Internal RGB LED

Adafruit Trinket M0

CircuitPython or Arduino IDE on this tiny little microcontroller board

Image for user adafruit2
published August 23, 2017, last edited June 24, 2025
Save Link Note Download
161
Beginner
Product guide

CircuitPython Internal RGB LED

Every board has a built in RGB LED. You can use CircuitPython to control the color and brightness of this LED. There are two different types of internal RGB LEDs:DotStar andNeoPixel. This section covers both and explains which boards have which LED.

circuitpython_NeoPixelDotStar.jpg
Un NeoPixel en la izquierda, un DotStar a la derecha. No es a escala... ¡los DotStars integrados son pequeños!

The first example will show you how to change the color and brightness of the internal RGB LED.

To use with CircuitPython, you need to first install a few libraries, into the lib folder on yourCIRCUITPY drive. Then you need to updatecode.py with the example script.

Thankfully, we can do this in one go. In the example below, click theDownload Project Bundle button below to download the necessary libraries and thecode.py file in a zip file. Extract the contents of the zip file, open the directoryCircuitPython_Essentials/CircuitPython_Internal_RGB_LED_colors/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to yourCIRCUITPY drive.

YourCIRCUITPY drive should now look similar to the following image:

CIRCUITPY
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries## SPDX-License-Identifier: MIT"""CircuitPython Essentials Internal RGB LED red, green, blue example"""import timeimport boardif hasattr(board, "APA102_SCK"):    import adafruit_dotstar    led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)else:    import neopixel    led = neopixel.NeoPixel(board.NEOPIXEL, 1)led.brightness = 0.3while True:    led[0] = (255, 0, 0)    time.sleep(0.5)    led[0] = (0, 255, 0)    time.sleep(0.5)    led[0] = (0, 0, 255)    time.sleep(0.5)
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries## SPDX-License-Identifier: MIT"""CircuitPython Essentials Internal RGB LED red, green, blue example"""import timeimport boardif hasattr(board, "APA102_SCK"):    import adafruit_dotstar    led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)else:    import neopixel    led = neopixel.NeoPixel(board.NEOPIXEL, 1)led.brightness = 0.3while True:    led[0] = (255, 0, 0)    time.sleep(0.5)    led[0] = (0, 255, 0)    time.sleep(0.5)    led[0] = (0, 0, 255)    time.sleep(0.5)

Create the LED

First, we create the LED object and attach it to the correct pin or pins. In the case of a NeoPixel, there is only one pin necessary, and we have called itNEOPIXEL for easier use. In the case of a DotStar, however, there are two pins necessary, and so we use the pin names APA102_MOSI and APA102_SCK to get it set up. Since we're using the single onboard LED, the last thing we do is tell it that there's only1 LED!

Trinket M0,Gemma M0,ItsyBitsy M0 Express, andItsyBitsy M4 Expresseach have an onboardDotstar LED, sono changes are neededto the initial version of the example.

Remember: To "comment out" a line, put a # and a space before it. To "uncomment" a line, remove the # + space from the beginning of the line.

QT Py M0, Feather M0 Express, Feather M4 Express, Metro M0 Express, Metro M4 Express, and Circuit Playground Express each have an onboardNeoPixel LED, so you mustcomment out import adafruit_dotstarandled = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1), anduncomment import neopixelandled = neopixel.NeoPixel(board.NEOPIXEL, 1).

Brightness

To set the brightness you simply use thebrightness attribute. Brightness is set with a number between0 and1, representative of a percent from 0% to 100%. So,led.brightness = (0.3) sets the LED brightness to 30%. The default brightness is1 or 100%, and at it's maximum, the LED is blindingly bright! You can set it lower if you choose.

Main Loop

LED colors are set using a combination of red,green, andblue, in the form of an (RG,B) tuple. Each member of the tuple is set to a number between 0 and 255 that determines the amount of each color present. Red, green and blue in different combinations can create all the colors in the rainbow! So, for example, to set the LED to red, the tuple would be (255, 0, 0), which has the maximum level of red, and no green or blue. Green would be (0, 255, 0), etc. For the colors between, you set a combination, such as cyan which is (0, 255, 255), with equal amounts of green and blue.

The main loop is quite simple. It sets the first LED tored using(255, 0, 0), thengreen using(0, 255, 0), and finallyblue using(0, 0, 255). Next, we give it atime.sleep() so it stays each color for a period of time. We chosetime.sleep(0.5), or half a second. Without thetime.sleep() it'll flash really quickly and the colors will be difficult to see!

Note that we setled[0]. This means the first, and in the case of most of the boards, the only LED. In CircuitPython, counting starts at 0. So the first of any object, list, etc will be0!

Try changing the numbers in the tuples to change your LED to any color of the rainbow. Or, you can add more lines with different color tuples to add more colors to the sequence. Always add thetime.sleep(), but try changing the amount of time to create different cycle animations!

Making Rainbows (Because Who Doesn't Love 'Em!)

Coding a rainbow effect involves a little math and a helper function calledcolorwheel. For details about how wheel works, seethis explanation here!

The last example shows how to do a rainbow animation on the internal RGB LED.

To use with CircuitPython, you need to first install a few libraries, into the lib folder on yourCIRCUITPY drive. Then you need to updatecode.py with the example script.

Thankfully, we can do this in one go. In the example below, click theDownload Project Bundle button below to download the necessary libraries and thecode.py file in a zip file. Extract the contents of the zip file, open the directoryCircuitPython_Essentials/CircuitPython_Internal_RGB_LED_rainbow/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to yourCIRCUITPY drive.

YourCIRCUITPY drive should now look similar to the following image:

CIRCUITPY
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries## SPDX-License-Identifier: MIT"""CircuitPython Essentials Internal RGB LED rainbow example"""import timeimport boardfrom rainbowio import colorwheelif hasattr(board, "APA102_SCK"):    import adafruit_dotstar    led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)else:    import neopixel    led = neopixel.NeoPixel(board.NEOPIXEL, 1)led.brightness = 0.3i = 0while True:    i = (i + 1) % 256  # run from 0 to 255    led.fill(colorwheel(i))    time.sleep(0.01)
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries## SPDX-License-Identifier: MIT"""CircuitPython Essentials Internal RGB LED rainbow example"""import timeimport boardfrom rainbowio import colorwheelif hasattr(board, "APA102_SCK"):    import adafruit_dotstar    led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)else:    import neopixel    led = neopixel.NeoPixel(board.NEOPIXEL, 1)led.brightness = 0.3i = 0while True:    i = (i + 1) % 256  # run from 0 to 255    led.fill(colorwheel(i))    time.sleep(0.01)

We add thecolorwheel function in after setup but before our main loop.

And right before our main loop, we assign the variablei = 0, so it's ready for use inside the loop.

The main loop contains some math that cyclesi from0 to255 and around again repeatedly. We use this value to cyclecolorwheel() through the rainbow!

Thetime.sleep() determines the speed at which the rainbow changes. Try a higher number for a slower rainbow or a lower number for a faster one!

Circuit Playground Express Rainbow

Note that here we useled.fill instead ofled[0]. This means it turns on all the LEDs, which in the current code is only one. So why bother withfill? Well, you may have a Circuit Playground Express, which as you can see has TEN NeoPixel LEDs built in. The examples so far have only turned on the first one. If you'd like to do a rainbow on all ten LEDs, change the1 in:

led = neopixel.NeoPixel(board.NEOPIXEL, 1)

to10 so it reads:

led = neopixel.NeoPixel(board.NEOPIXEL, 10).

This tells the code to look for 10 LEDs instead of only 1. Now save the code and watch the rainbow go! You can make the same1 to10 change to the previous examples as well, and useled.fill to light up all the LEDs in the colors you chose! For more details, check out theNeoPixel section of the CPX guide!

Page last edited January 22, 2025

Text editor powered bytinymce.

Related Guides
Search

Search

Categories

[8]ページ先頭

©2009-2025 Movatter.jp