Neopixel LED ring to work with PCA9685 pwm driver

Hello all,

I have a 12 LED neopixel ring, which works fine with the Adafruit_NeoPixel.h library using an ESP32.

Since the ESP32 has limited gpio pins for my project I purchased a PCA9685 Module Controller 16 Channel 12 Bit PWM Servo Driver, I know it is called a servo driver but effectively it is a pwm expander and there's no reason why it shouldn't work for other pwm devices. I've seen instances of people controlling single LEDs with it but not neopixel ring. However, in most cases people use Adafruit_PWMServoDriver.h and wire.h library and which does not provide enough parameters to pass to the Adafruit_NeoPixel.h library.

Here is an example of my devices:

Here is my code which works without using a pwm expander:

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
#define PIN        5 // On Trinket or Gemma, suggest changing this to 1

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 12 //16 Popular NeoPixel ring size

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels
byte loopcoloursvariable = 0;
byte runonce = 0;
String command;

void setup() {
 
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.
 Serial.begin(9600);
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  pixels.clear(); // Set all pixel colors to 'off'
}

void loop() {

  if (runonce == 0){
    runonce = 1;
  }
  
  if (Serial.available()) {
   command = Serial.readStringUntil('\n');
  command.trim();

  if (command.equals("1"))
  {

        setpixelon();
  }

   if (command.equals("2"))
  {

       setpixeloff();
  }

  }
 
}

void    setpixelon(){

//pixels.setPixelColor(0, is zero based, so for a 12 ring neopixel go from 0 - 11
  
   pixels.setPixelColor(0, pixels.Color(0, 0, 50));
   pixels.setPixelColor(1, pixels.Color(0, 0, 50));
 pixels.setPixelColor(2, pixels.Color(0, 0, 50));
  pixels.setPixelColor(3, pixels.Color(0, 0, 50));
    pixels.setPixelColor(4, pixels.Color(0, 0, 50));
     pixels.setPixelColor(5, pixels.Color(0, 0, 50));
    pixels.setPixelColor(6, pixels.Color(0, 0, 50));
     pixels.setPixelColor(7, pixels.Color(0, 0, 50));
    pixels.setPixelColor(8, pixels.Color(0, 0, 50));
     pixels.setPixelColor(9, pixels.Color(0, 0, 50));
    pixels.setPixelColor(10, pixels.Color(0, 0, 50));
     pixels.setPixelColor(11, pixels.Color(0, 0, 50));
  // 
   pixels.show();  
  }

void    setpixeloff(){
  loopcoloursvariable = 0;
  pixels.clear(); // Set all pixel colors to 'off'
  
  pixels.setPixelColor(1, pixels.Color(0, 0, 0));
 pixels.setPixelColor(2, pixels.Color(0, 0, 0));
  pixels.setPixelColor(3, pixels.Color(0, 0, 0)); 
   pixels.show();  

  }

I would much appreciate any advice on getting these components working together.

Neopixels only require a single pin to drive any number of them and do not use PWM so I am confused as to what you are trying to do and why

Hi UKHeliBob

I assumed that Neopixels required a pwm pin like any other led. Regardless, my project will eventually use 14 x Neopixels, 4 x stepper motors, 2 x dc motors and as well as 20 - 40 different sensors. The stepper motors and dc motors definitely require pwm, so I do need to know how to send commands to each component using a pwm expander.

Stepper motors are not controlled by pwm. Use a current controling stepper driver and using Step and Dir.

Hi Railroader

My stepper motors are currently working fine, they are connected to a stepper driver which is connected to pwm pins for step and direction. This is besides the point of my thread.

They don't, hence the use of libraries such as FastLED to control them.

Ok UKHeliBob, I will accept your knowledge that Neopixels don't require a pwm pin. So can you provide a solution that will give me additional io pins to add to my ESP32 in consideration that I do have a number of different components that do require pwm pins (listed above)?

First determine how many digital and PWM pins that you require and come back with a list

I require 12 pwm pins included in the pins i require in #post 3

The stepper motors may currently be using 2 PWM pins each but I am not convinced that they need to use such pins. That is not "besides the point" of this topic

My stepper motors work when I use the step & dir on pwm pins, my dc motors work similarly (using an Arduino mega). If I use a non pwm pin (normal digital pin) they don't work . As per the manufacture documentation.
I don't what else to say, my eyes are not deceiving me!!!!!

My point of topic was Neopixel LED ring to work with PCA9685 pwm driver but we are talking about my stepper motors that already work perfectly fine?

NeoPixel LED ring does not work with PCA9685 pwm, use any single digital pin for it.

Hi b707

I need additional io pins for my microcontroller, if I can't use a PCA9685 the can you suggest what I can use?

The point of your tormenters is, you seemingly don't know what you don't know yet; case in point, Neopixels do not need PWM. Nor, normally, do steppers, UNLESS a particular software driver you are using requires it. We haven't discussed that, and you've provided no links to evidence.

When asked to make a list of IO, you really need to do so, and then ask what is the appropriate way to drive each type of I/O. That will drive the discussion of what hardware you need, per your question to @b707.
That approach will probably serve you better than getting defensive about it.

Believe it or not, people here actually are trying to help you determine what it is you truly need.

1 ordinary digital pin required (PWM pin not required). However, the 3.3V signal from the ESP32 may not reliably drive neopixels, which require a 5V signal. A signal level shifter may be needed such as 74hct125 (not level shifter designed for i²c devices).

Sounds like you are using stepper drivers that each have a direction and step input. The direction inputs can be ordinary digital pins and don't need to be PWM pins. The step pins may need to be connected to PWM pins by the library you are using. This is because the Arduino's internal hardware timers are being used to generate a square wave of variable frequency. Technically that's not a PWM signal, but on some Arduino, only the PWM pins are capable of generating those square waves using timer hardware. But with ESP32, things are different, I think. The internal hardware timers are different from most Arduino. I guess your library must be compatible with ESP32 or your steppers would not be working.

The number of PWM signals needed could be one or two for each motor, depending on the h-bridge driver you are using. Some h-bridge drivers need 2 PWM signals, others need only one, plus an ordinary pin for direction. Please let us know the details of your h-bridge drivers.

That would need 20-40 pins, or more, or fewer.... There's no way for us to advise you without details.

Sorry, missed some of that. Here's a start:

Neopixels - simple digital output each string(is that 14 pixels on one string, or 2x7, or?).
Steppers - simple digital outputs suffice for most drivers, 2 per motor; if you can, provide a link to your stepper drivers that need a PWM pin, as I'm curious about the functionality.
DC motors - (bidirectional, or unidirectional?) - again, 1 or 2 digital outputs per, one each needs to be PWM if you want speed control, the other is for direction(if bidirectional).
Sensors - are we talking digital signals, or analog? What signal range, if analog?

They may be PWM capable pins but are you actually outputting a PWM signal or is it just a digital signal I wonder ?

The thing is: Neopixels do not need a simple digital output; they need an output that can be manipulated to send a special and rather high-speed encoded digital signal. Most of the processors used in Arduino-like boards can bit-bang this signal on one of their ordinary GPIO pins, but it's not possible to do though something like an I2C port expander or PWM controller. I2C is far too slow (unless the I2C device is specifically a "neopixel driver", like maybe the Adafruit NeoDriver)

First of all I would like to say to camsysca that I do greatly appreciate any advice on this forum, admittedly I was quite defensive as I was drawn into a discussion about stepper/dc motors when that was not my original topic. I spent many nail biting months getting my motors working perfectly & it was just a case of "if it aint broke don't try to fix it". I have not intended to offend anyone with my responses and I apologize if anyone feels that way.
I have taken all comments in consideration and tried to provide as much detail as possible. I do understand that some people may want to know why I used pwm pins in order to establish whether I actually need to use pwm pins but I have tested certain components with/without pwm pins so I know what does/doesnt work with my hardward/software.

In total I plan to use the following components , from what I have tested & from the advice provided on this forum I have detailed the total pwm pins I need to be 12, I may need 1 or 2 more based on a couple components that I have not tested yet. However, in total I need 63 pins;

  • 14 x Neopixels - using Adafruit_NeoPixel library. total 14 pins

  • 14 x Motion sensors, total 14 pins

  • 4 x stepper motors on TMC2209 drivers mounted to a cnc shield using Mobatools library, step/dir pins need pwm, enable pins non pwm. total 10 pins (8 pwm)

  • 2 x dc motors using 2 x BTS7960 drivers, total 6 pins (2 pwm)

  • 6 x hall sensors, total 6 pins

  • 4 x limit switches, total 4 pins

  • 2 x servos, total 2 pins (2 pwm) using Mobatools library

  • 2 x fans, total 2 pins (2 pwm) Noctua fans speed controllable (can change to non-pwm fans if necessary but these ones are silent with less vibration)

  • 2 x relays, total 2 pins

  • 1 x SD card reader, total 1 pin (don't think pwm required, have not tested yet)

  • 1 x thermometer, total 1 pin (don't think pwm required, have not tested yet)

  • 1 x Audio component with speaker, total 1-2 pins (may require pwm, have not tested yet)

As detailed above, in total I need 63 pins. In total my ESP32 has around 26 gpio pins (not all can be used), I don't need pins for a Bluetooth module because it is built in so I need an additional 37 pins. I can use the pwm pins on the microcontroller but I need to be able to use libraries with pins not connected to the microcontroller (for e.g. a MCP23017 port expander provides additional pins but I found with the Adafruit_MCP23X17 library it only accepts high/low commands as inputs, which would not work with the Neopixel library).

Wrong. You need only a single digital pin to control 14 Neopixels

Which interface will you be using that requires only a single pin ?

Are you sure that the pins need to be PWM ?

There may be problems that you have not accounted for such as the fact that multiple libraries may want to use the same timers on the chip