Getting the ESP32-S3 to control the onboard Neopixel LED


Introduction

The ESP32-S3 dev board includes a single multicolor LED called a neopixel. Neopixels communicate over a special protocol that permits one control wire to set the value of the red, green, and blue components. This is accomplished via a serial pulse chain according to specific timing. Fortunately, micropython includes a neopixel library that provides the proper addressing and timing for controlling Neopixel LEDs.

Call To Action

This tutorial is part of a bigger course called "Intelligent Product Design", which I am building out at https://backpack.danaukes.com/course/intelligent_product_design/. Please pardon the dust as I build it out.

If you just want the tutorial, you're in the right spot. But, if you need a little more, such as live lectures, a discussion board, or a community of other students, consider enrolling once the course opens. For now, please feel free to explore https://backpack.danaukes.com.

Explanation of Code

We import the three elements we will need, the Pin class from the machine library, the NeoPixel class from the neopixel library, and the time library.

from machine import Pin
from neopixel import NeoPixel
import time

We create an instance of the neopixel class, connected to pin 48. Pin 48 is pre-wired

np = NeoPixel(Pin(48), 1)

Code

from machine import Pin
from neopixel import NeoPixel
import time

np = NeoPixel(Pin(48), 1)

for n in range(12):
    np[0] = (255, 0, 0)
    np.write()    
    time.sleep(1)
    np[0] = (0, 255, 0)
    np.write()    
    time.sleep(1)
    np[0] = (0, 0, 255)
    np.write()    
    time.sleep(1)

External Resources

About

I am an engineer and educator, having spent ten years as a professor. My goal is to help you build your knowledge of design and technology, get your hardware working, and propel your startup or small business. Get in touch!

Read More