015 - MicroPython TechNotes: Neopixel 16

Introduction

In this article, we will learn about the addressable NeoPixel. Addressable Neopixel contains a WS2812B chip and an RGB LED in one package. With WS2812B, it is possible to make the a chain of RGB LED to be addressable. Meaning to say, it can be controlled individually.

Bill Of Materials

  1. ESP32 development board.
  2. ESP32 shield from Gorillacell ESP32 development kit (optional, you can still make it without this shield. It is just use to simplify the circuit connection).
  3. 3-pin female-female dupont jumper wires.
  4. Neopixel Ring (16 RGB)

Pinout

  1. GND – for the ground pin.
  2. VCC – for the supply voltage.
  3. DI – for the Data Input (DI) control signal pin.

Hardware Instruction

  1. First, attach the ESP32 dev board on top of the ESP32 shield making sure that pins are aligned and the USB ports are on the same side.
  2. Next, attach the dupont wires to the Neopixel ring according to the color coding which is black for the ground, red for the VCC, and yellow for the control signal pin.
  3. Next, attach the other side of the dupont to the ESP32 shield by matching the colors of the wires to the colors of the pin headers in the ESP32 shield, that is black to black, red to red, and yellow to yellow.
  4. Next, power the ESP32 shield by attaching an external power supply with a type-C USB connector. Make sure that the power switch is set to the ON state.
  5. Next, connect the ESP32 to the computer through the micro USB cable. The demo circuit should now be ready.

Software Instruction

MicroPython has a builtin module or driver library for the Neopixel. Just import it in the source code.

Video Demonstration

Call To Action

For any concern, write your message in the comment section.

You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.

Thank you and have a good days ahead.

See you,

– George Bantique | tech.to.tinker@gmail.com

Source Code

1. Example # 1, basic exploration of the neopixel:

 1# More details can be found in TechToTinker.blogspot.com 
 2# George Bantique | tech.to.tinker@gmail.com
 3
 4from machine import Pin
 5from neopixel import NeoPixel
 6
 7NUM_OF_LED = 16
 8np = NeoPixel(Pin(23), NUM_OF_LED)
 9
10
11# # The following lines of codes should be tested using the REPL
12# # Syntax:
13# #    np[] = (, , )
14# #    np.write()
15# # ------------------------------------------------------------
16# # 1. To set the 1st neopixel to red color:
17# np[0] = (255, 0, 0)
18# np.write()
19# 
20# # 2. To set the 4th neopixel to green color:
21# np[3] = (0, 255, 0)
22# np.write()
23# 
24# # 3. To set the 5th and 7th neopixel to blue color:
25# np[4] = (0, 0, 255)
26# np[6] = (0, 0, 255)
27# np.write()
28# 
29# # 4. To set the 1st to red color,
30# #               3rd to green color, and
31# #               5th to blue color
32# np[0] = (255, 0, 0)
33# np[2] = (0, 255, 0)
34# np[4] = (0, 0, 255)
35# np.write()
36# 
37# # 5. The value given to each neopixel LED
38# #    represents its brightness
39# np[0] = (1, 0, 0)
40# np.write()
41# 
42# # 6. To turn OFF all neopixel:
43# for npixel in range(16):
44#     np[npixel] = (0, 0, 0)
45#     np.write()

2. Example # 2, rotating LED light:

 1# More details can be found in TechToTinker.blogspot.com 
 2# George Bantique | tech.to.tinker@gmail.com
 3
 4from machine import Pin
 5from neopixel import NeoPixel
 6from time import sleep_ms
 7
 8NUM_OF_LED = 16
 9np = NeoPixel(Pin(23), NUM_OF_LED)
10
11while True:
12    for npixel in range(NUM_OF_LED):
13        np[npixel] = (0, 0, 255)
14        np.write()
15        sleep_ms(100)
16        np[npixel] = (0, 0, 0)
17        np.write()

3. Example # 3, same as Example # 2 but changes LED color each rotation:

 1# More details can be found in TechToTinker.blogspot.com 
 2# George Bantique | tech.to.tinker@gmail.com
 3
 4from machine import Pin
 5from neopixel import NeoPixel
 6from time import sleep_ms
 7
 8NUM_OF_LED = 16          # Number of Neopixel LED
 9LED_ON_VALUE = 255       # 255 is maximum value for the LED brightness
10LED_OFF_VALUE = 0        # while 0 to turned it OFF
11red_value = LED_ON_VALUE # Start with red
12grn_value = 0            # and all others
13blu_value = 0            # are turned OFF
14
15# Create the neopixel instantiation named np
16np = NeoPixel(Pin(23), NUM_OF_LED)
17
18while True:
19    for npixel in range(NUM_OF_LED):
20        np[npixel] = (red_value, grn_value, blu_value)
21        np.write()
22        sleep_ms(100)
23        np[npixel] = (0, 0, 0)
24        np.write()
25        
26        # If its the last LED, change color
27        if npixel==NUM_OF_LED-1:
28            # If its now red, make it green
29            if red_value==LED_ON_VALUE:
30                red_value = LED_OFF_VALUE
31                grn_value = LED_ON_VALUE
32            # If its green, make it blue
33            elif grn_value==LED_ON_VALUE:
34                grn_value = LED_OFF_VALUE
35                blu_value = LED_ON_VALUE
36            # If its blue, make it red
37            elif blu_value==LED_ON_VALUE:
38                blu_value = LED_OFF_VALUE
39                red_value = LED_ON_VALUE 

References And Credits

  1. Purchase the Gorillacell ESP32 development kit at: gorillacell.kr


Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.