Using a GPIO Input to alter a pattern

I’m a little bit lost on how to get started both on the hardware and software side of things, and searching the forums didn’t help enough. I have an ESP32 that’s doing a bunch of interactive things, but in the end, will be able to send a wired signal (for prototyping, it lights up a single bare LED) when a successful event happens. In production, when that successful event happens, I want a PixelBlaze LED pattern to turn on, and then turn off again 10 seconds later.

I’m both not sure how to use a GPIO input pin on a PixelBlaze v3 and how to do this in code. I think from the documentation here: https://electromage.com/docs/GPIO that I can use IO25 (or IO26) to do this, with I think the digitalRead(26,1) command, but am not sure how to use it correctly and how to incorporate it in existing patterns.

Ideally, I’d wire the ESP32’s digital out on a pin to the digitalread pin of the PixelBlaze, and when a signal was sent, it’d set the brightness of the pattern from 0 to “regular” for 10 seconds and then turn off again – but this is going beyond what I’ve done before and know how to do. Thanks for helping me with my sometimes limited background in the fundementals of electronics wiring and coding…

Two approaches.

You could do this in one pattern, basically use if in code to do something when a gpio is a particular level. Could use delta to control timing to have it run for a minimum period or fade in/out.

Another way is to use playlists and the sequencer api. You can jump to a specific playlist position.

some related posts:

Hardware wise, if it’s 3.3v then you can wire it directly.

Thanks a lot! I’m up and running. Yes, it’s a 3.3v ESP32 controlling everything. I didn’t realize it was as simple as “wire the ESP32 to the GPIO pin on the PixelBlaze and give them common ground and you’re done” for wiring.

I also didn’t realize ChatGPT finally understands the PixelBlaze – it didn’t a year or so ago, really. Since I found the existing documentation on https://electromage.com/docs/GPIO a bit cryptic as a beginner, I’ll write out what I did as documentation for anyone else reading this later:

ESP32 GPIO27 –> directly to PixelBlaze GPIO Pin 25.
ESP32 Ground –> Common Ground with PixelBlaze.

About as simple of wiring as it gets.

Then I used this code to as my test this evening, and it worked fine. I don’t vouch for the code, really (it could be overly complicated in terms of handling the bounce or falling edge, and AI had a very heavy hand in writing it), but it made sense to me when reading it, and does what what I wanted really cleanly as a v1.

// ==== CONFIG ====
// Choose the actual GPIO you’re using on the Pixelblaze (e.g., 0, 25, or 26 on V3)
var BUTTON_PIN = 25

// If your ESP32 pulls the Pixelblaze pin to GND when “pressed”, use INPUT_PULLUP
pinMode(BUTTON_PIN, INPUT_PULLUP)

// ==== STATE ====
var lastBtn = 1         // matches INPUT_PULLUP idle = 1 (not pressed)
var triggered = false
var remainingMs = 0     // countdown in milliseconds

// How long to stay active after a press
var TRIGGER_MS = 2000  // 2 seconds

export function beforeRender(delta) {
// Read button: with INPUT_PULLUP, pressed == 0, released == 1
var btn = digitalRead(BUTTON_PIN)

// Detect falling edge (released->pressed)
if (lastBtn == 1 && btn == 0) {
triggered = true
remainingMs = TRIGGER_MS
}
lastBtn = btn

// Countdown while triggered
if (triggered) {
remainingMs -= delta
if (remainingMs <= 0) {
triggered = false
remainingMs = 0
}
}
}

export function render(index) {
if (triggered) {
rgb(1, 1, 1)   // white for 10 s after a press. Replace with any other "on press" code.
} else {
rgb(1, 0, 0)   // red otherwise. Replace with any other "when not pressed" code. 
}
}
2 Likes