🌈 ESP32-S3 Rainbow: ZX Spectrum Emulator Board! Get it on Crowd Supply →
View All Posts
read
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi

Lots of LEDs? It’s not Christmas yet!

I had a big bundle of addressable WS2811 LED strings and an ESP-CAM board (an ESP32 dev board with a camera). There’s only one possible project that you can do with these components. Turn the disorganised chaos of lights into something a bit more organised.

As an added bonus I’ve ended up duplicating the image processing code in JavaScript so you don’t even need a camera on your ESP32 board - you can just use a plain dev board to drive the LEDs.

You can see the results of my efforts in the video below and I’ll run through a bit more detail of the code in the following text.

The full sample code can be found here: https://github.com/atomic14/self-organising-leds

If you want to do this yourself then you will need an ESP32 dev board of some kind and of course you’ll need some kind of addressable LEDs. I’m using the FastLED library for driving the LEDs so with some small code changes you can probably support pretty much any addressable LEDs.

ESP32 and ESP-CAM Boards

Our challenge comes down to a very basic problem, given access to a stream of images from a camera identity the approximate locations of each LED in 2D space. Once you’ve done that it’s a simple problem to map from each LED’s x and y location onto a frame buffer containing the pattern you want to show.

There’s a bunch of boiler plate code to initialise the ESP-CAM - I took inspiration from the sample code here and copied the bits I needed to get the camera up and running.

An important change I’ve made is only capture greyscale images at the lowest framesize:

config.pixel_format = PIXFORMAT_GRAYSCALE;
config.frame_size = FRAMESIZE_QQVGA;

And then to grab a frame from the camera we simply do:

    camera_fb_t *fb = esp_camera_fb_get();
    Frame *frame = new Frame(fb);
    esp_camera_fb_return(fb);

With our Frame class grabbing a copy of the pixels along with the width and the height of the image.

pixels = (uint8_t *)malloc(fb->height * fb->width);
memcpy(pixels, fb->buf, fb->height * fb->width);
width = fb->width;
height = fb->height;
length = fb->width * fb->height;

The image below shows a frame grabbed from the ESP-CAM sensor.

Grabbed Frame

For the JavaScript version of this code it’s a bit more complicated. One of the biggest problems is that we need to be running over HTTPS to have access to the camera - more on this later….

const stream = await navigator.mediaDevices.getUserMedia({
  video: { facingMode: "environment" },
  audio: false,
});
const canPlayListener = () => {
  // the video is loaded and we can grab frames
  onVideoReady(video);
  video.removeEventListener("canplay", canPlayListener);
};
video.addEventListener("canplay", canPlayListener);
video.srcObject = stream;
video.play();

Once we have a video stream coming from the camera we can grab a frame by drawing the video to a canvas context and then getting the imageData from it.

function getVideoFrame(video: HTMLVideoElement, canvas: HTMLCanvasElement) {
  const width = video.videoWidth;
  const height = video.videoHeight;
  const context = canvas.getContext("2d");
  // draw the video to the canvas
  context!.drawImage(video, 0, 0, width, height);
  // get the raw image bytes
  const imageData = context!.getImageData(0, 0, width, height);
  // convert to greyscale
  const bytes = new Uint8Array(width * height);
  for (let y = 0; y < height; y++) {
    for (let x = 0; x < width; x++) {
      const r = imageData.data[(y * width + x) * 4];
      const g = imageData.data[(y * width + x) * 4 + 1];
      const b = imageData.data[(y * width + x) * 4 + 2];
      // https://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
      const grey = Math.min(255, 0.299 * r + 0.587 * g + 0.114 * b);
      bytes[y * width + x] = grey;
    }
  }
  return bytes;
}

Phone Interface

Now we can grab frames we just need to grab a frame with no LEDs lit, light one LED, grab another frame and then compare the two. The difference should tell us where the LED is. To avoid noise or small movements of the camera having a bit impact we apply a guassian blur to the captured frames before taking the difference.

This is a of course a very naive and simple algorithm and could easily be improved on.

In the C++ code of the ESP32 we do all this directly in code. In the JavaScript version we call API functions on the web interface of the ESP32 to turn LEDs on and off and once we’ve finished the processing send up the calculated positions of the LEDs to the board.

In our ESP32 code we create a framebuffer and draw patterns into it. We then use the locations of each LED to work out what color it should be.

Organised

To solve the issue of needing HTTPS to access the camera and also needing the API calls to be HTTPS as well (we can’t mix content nowadays!) we need a way of serving both the UI and the API from the ESP32 web server over HTTPS. There are web servers that support HTTPS and self signed certificates available for the ESP32 but this leads to other problems and would require a rewrite of the device code. An easy workaround to this problem is to use a service such as ngrok to provide a secure URL from the cloud through our computer to the ESP32 device. Slightly convoluted, but it works!

This in only needed if you are not using an ESP-CAM and have to use your phone’s camera for calibrating the LEDs. Sign up for a free acount with ngrok and then find the IP address of your ESP32 board:

ping espcam.local
PING espcam.local (10.0.1.17): 56 data bytes
64 bytes from 10.0.1.17: icmp_seq=0 ttl=255 time=14.343 ms
64 bytes from 10.0.1.17: icmp_seq=1 ttl=255 time=6.493 ms

Take the IP-Address and ask ngrok to start proxying requests for us:

ngrok http 10.0.1.17 -inspect=false

You’ll need steady hands - there’s quite a lot of latency going on so the space between turning an LED on and off and grabbing a frame can be quite large. I slightly moved as I was taking the frame to show the locations so the positions are slightly off.

LED locations

Checkout the video to see how well it works - surprising for such a simple algorithm.

Related Posts

Interesting LED Curtain - Dove into hacking an LED curtain and discovered that despite lacking visible daisy chaining, it's hackable! With LEDs pre-programmed for their position, it can receive collective data and recognize individual signals. Flashed WLED firmware onto an ESP32 and, voila, it lit up beautifully, even supporting 2D grid patterns for cool visuals. The key components: a BLE SoC, buffer IC for level shifting, and a curious way of LED data handling that's cleverly efficient.
Dave come see - it's beautiful man - Hey folks, I revisited my flame lamp project from 2020 and gave it a serious upgrade - we're talking a leap from 300 to a whopping 1024 LEDs thanks to these nifty flexible WS2812 panels. I've kitted it out with a versatile PCB from my Crystal Balls project, tackled power injection challenges and kept things cool with a current-limiting power supply. I had to wrestle with 3D printing and getting the panels to behave inside the lamp's tube, but the end result? A mesmerizing blaze of colors with gorgeous fire effect, all orchestrated by the WLED software. It's like my very own Phoenix rising from the 2020 ashes. Curious to see this electronic inferno come to life? Check out my videos for the full, fiery scoop!
More fun and games with high voltage - Hey everyone! I know it's been a while since the last video, but things have been crazy busy with shipping off the ESP32 Rainbows. Lately, I've been diving into the world of high voltage again. My last endeavor with a high voltage board led to some gnarly waveforms and flickering LEDs, so I tested with beefy resistors but wasn't thrilled with the results. Fortunately, I've found some nixie tube driver boards that work wonders with LEDs, providing smooth output with little ripple. Still, playing with the old boards has its thrill—hello, 787 volts and some wild sparks! Stay tuned for more high voltage adventures!
ESP32-S3 Dev Board Assembly - I finally assembled our ESP32-S3 dev boards—used a stencil for easy SMD work, fixed a few tiny USB solder bridges with flux, and even hand-built one for fun. The EPAD isn’t required (per the datasheet), power LEDs look good, and on macOS you can spot it under /dev before flashing. A quick boot-button dance and the blink sketch runs great—full build and walkthrough in the video.
Look at my shiny crystal balls - Just upgraded my basic AliExpress crystal balls with some tech wizardry - I've thrown in an ESP32-S3-MINI, a mic, and made them battery powered. Thanks to WLED software, they're now smart and responsive! Shared the KiCAD project for fellow tinkerers. Check out my video to see these balls in action!

Related Videos

Magic LEDs: Self-Organizing with ESP32 CAM & Simple Image Processing! - Unleash your creativity and transform chaos into order using an ESP32 camera board, image processing, and a string of WS2811 RGB LEDs. Find out how wiring, level shifting, and a simple web interface bring this mesmerizing project to life.
Make Your Christmas Tree Dance to the Beat! - Transform your Christmas tree into an interactive light show using a string of 200 WS2811 addressable LEDs, self-organizing LEDs project, and an ESP32 to drive them. Learn how to map images onto LED locations and create amazing visual effects.
787 Volts From a $2 Board! - Let's have some fun with high voltage experiments and LED filaments! I've been dabbling with 300-400 volts, testing out capacitor charging, and saving some flickery Philips bulbs. With a new ESP32 Rainbow project in the works and a nifty minimalist ESP32 S3 board arriving soon, there's lots going on. Despite some high ripple issues and quirky sounds from the modules, swapping to an old-school surface mount board provides smooth performance with almost no ripple. Join me as I explore the limits and potential projects with these LEDs. And for a shocking tidbit at the end, explore some high voltage calculations—it's electrifying!
I Asked AI to Design 12 ESP32 Dev Boards. It Said No. - I got bored of making one ESP32 dev board at a time, so I scripted the whole lot. After my favourite AI monkey told me to automate the repetitive bits, I built a pipeline that generates fully laid-out USB-native ESP32 dev boards for a dozen modules—schematics, placement, routing, silkscreen, and Gerbers—automatically. It uses a module database to choose safe pins, LED and boot pins, lays everything out in a standard USB-C two-layer format with differential USB pairs and ground pours, and publishes releases via GitHub Actions. You can fab them right away (thanks PCBWay), but treat them like open hardware: trust, verify, and tell me which ones to build.
ESP32 HTTP Web Server With Content Served from SPIFFS Filesystem - Learn how to create a simple ESP32 web server and user interface using basic HTML and JavaScript, and control an LED via an HTTP endpoint. This tutorial demonstrates the ease of setting up a web server on ESP32 devices and serving compressed content from SPIFFS.
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening


Published

> Image

atomic14

A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts