Programming The ESP32 In C - The S3's RGB LED
Written by Harry Fairhead   
Tuesday, 24 September 2024
Article Index
Programming The ESP32 In C - The S3's RGB LED
The Program

The ESP32 S3 has an onboard RGB NeoPixel LED and you can use it via a simple GPIO line - really easy. This is an extract from Harry Fairhead's latest book on programming the ESP32 using C and the IDF.

Programming The ESP32 In C
Using The Espressif IDF

By Harry Fairhead

espC360

To be published in October 2024. Sign up to our newsletter or RSS feed to be notified.

Contents

       Preface

  1. The ESP32 – Before We Begin
  2. Getting Started
  3. Getting Started With The GPIO 
  4. Simple Output
         
    Extract: The S3's RGB LED ***NEW!
  5. Some Electronics
  6. Simple Input
  7. Advanced Input – Interrupts
  8. Pulse Width Modulation
  9. Controlling Motors And Servos
  10. Getting Started With The SPI Bus
  11. Using Analog Sensors
  12. Using The I2C Bus
  13. One-Wire Protocols
  14. The Serial Port
  15. Using WiFi
  16. Direct To The Hardware
  17. Free RTOS 

<ASIN:187196282X>

This is an advanced topic that pushes direct GPIO signal generation to the limit. In simple terms the requirements are such that the ESP32 is only just fast enough to cope. The task is to make use of the RGB LED that is standard on an ESP 32 S3. This is an addressable LED in the WS2812 family and you can run this program on an ESP32 by connecting a WS2312 “NeoPixel” device to a GPIO line.
In the most general case you can daisy chain NeoPixels to produce linear strips of LEDs:

ledneo1

To set an LED to a color you have to send a 24 bit value on the Data line using a coding:

ledneo2

The data line rests low and a zero is sent with a high time of 350ns and a one is sent with a high time of 700ns and a total pulse width of 1.25μsAll timings are ±150ns. The RET code is just a minimum low time between groups of 24bit signals. If you have multiple LEDs daisy chained you can control each one individually by sending groups of 24bits at a time. Each LED consumes 24bits of the signal and passes the remaining bits on to the next LED in the chain. In this way each LED in the chain gets its own 24bits specifying the color. The LEDs process the stream of bits until the line is held low for at least 50μsi.e. the Ret code. At this point all of the LEDs transfer the data to a latch and display the specified color. The format used to set the color is:

ledneo3
As already mentioned you can connect a NeoPixel device to any GPIO line to act as the data line but the ESP32 S3 has a single NeoPixel connected to either GPIO38 or GPIO48 depending on the development board. Some boards also have a solder pad next to the LED that has to be connected before the LED can be used. You can connect them with a small blob of solder:

ledneo4



Last Updated ( Tuesday, 24 September 2024 )