I am using the ESP32 for a simple project. I know how to animate an led strip using atmega328p with arduino but when I animate it on the ESP32 it does not work that great. It will be nice if I can use the same ESP32 to run my entire project without having to include another MCU. Has someone managed to be able to animate an led strip meanwhile using wifi on the ESP32 ?
This is the code I use to animate an LED stip meanwhile using wifi. I wrote this code myself so maybe I am doing something wrong. It works better than any of the libraries I have found online. The library that works best is esp32WS2811-master. But still this code works better than that library in my case.
#include <Arduino.h>
#include "WiFi.h"
#include <Adafruit_NeoPixel.h>
// we will runt the task that animates led strip on a separate core of arduino
TaskHandle_t Task1;
// where led pin is connected and number of leds to animate
#define PIN 32
#define NUM_LEDS 80
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
// mutex that helps enter critical sections meanwhile we animate led strip
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
// it will run on core 0 of esp32. Responsible for animating led strip.
void loop2(void *parameter)
{
uint32_t _color = strip.Color(0, 0, 0);
for (;;) // infinite loop
{
_color = strip.Color(0, 0, 255);
// animation 1
for (uint16_t i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, _color);
//portDISABLE_INTERRUPTS(); // arternative to portENTER_CRITICAL
portENTER_CRITICAL(&mux); /* start critical section */
strip.show();
//portENABLE_INTERRUPTS(); // alternative to portEXIT_CRITICAL
portEXIT_CRITICAL(&mux); /* end critical section */
vTaskDelay(15); // one tick delay (15ms) in between reads for stability
}
// animation 2
_color = strip.Color(0, 255, 0);
for (uint16_t i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, _color);
//portDISABLE_INTERRUPTS(); // arternative to portENTER_CRITICAL
portENTER_CRITICAL(&mux); /* start critical section */
strip.show();
//portENABLE_INTERRUPTS(); // alternative to portEXIT_CRITICAL
portEXIT_CRITICAL(&mux); /* end critical section */
vTaskDelay(15); // one tick delay (15ms) in between reads for stability
}
}
}
void setup()
{
Serial.begin(115200);
// disabling wifi helps but we need it
// WiFi.mode(WIFI_OFF);
// init led strip
strip.begin();
strip.setBrightness(4);
strip.show(); // Initialize all pixels to 'off'
// create a task that runs on core 0 of esp32
xTaskCreatePinnedToCore(
loop2,
"Task_1",
1000,
NULL,
2, // priority of 0 is the lowest.
&Task1,
0 // use core 0. Core 1 is the default core
);
// continue running code on default core 1 of esp32
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
Serial.println("Setup done");
}
// on main loop scan network just to show how led strip does not work great when wifi is in use
// This code I copied and pasted from the WiFi examples of the esp32
void loop()
{
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0)
{
Serial.println("no networks found");
}
else
{
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
delay(10);
}
}
Serial.println("");
// wait 5 seconds for next network scan
auto end = millis() + (unsigned long)5000;
while(millis() < end) {
vTaskDelay(1);
}
}
When I run this code the first led flickers. The other LEDs animate great 99.99% of the time. When I disable WiFi this code works great. I still need portENTER_CRITICAL(&mux);
or portDISABLE_INTERRUPTS();
for it to work even if I disable WiFi. Has anyone managed to be able to animate an led-strip with the ESP-32 successfully meanwhile using WiFi?
If I am required to use a separate micro controller which one do you guys recommend? The ATtiny85?