Generating 50 channels of PWM signals usin Arduino Mega 2650

Hello,
I am currently working on a project where I am required to make a device that can generate PWM signal on 50 channels with independent control of frequency and duty cycle on each channel. The frequency range 1Hz to 10,000 Hz.
I am doing it by using a separate signal generator for each channels and Arduino Mega will only control the frequency and and duty cycle by sending a relevant signal to the independent circuitry of each channel. But I still believe Arduino mega can produce 50 PWM signals on the its digital pins through the use of timers. Being a mechanical engineer I haven’t got into such thing before with Arduino.
I thought of 2 variables for each channel to store frequency and duty cycle for each channel and then through some clever programming the timer interrupts will be updated accrodingly.
Sorry if I sound too dumb.
Any suggestion will highly be appreciated.
Thanks.

The ATmega2560 has 2 8-bit timers with 2 output compare channels each, and 4 16-bit timers with 3 output compare channels each. Unfortunately, two of the outputs of these channels go to the same pin, so you can only have 15 separate channels.
You cannot have 50 PWM channels in hardware using an Arduino Mega.

Pieter

Exactly, however, depending on the application, there are several approaches to use SOFTWARE PWM.
This example was written on a 4MHz PIC a few years ago, and doesn’t even use the PIC I/O pins, but rather - a string of TPIC595 shift registers.

Okay As I expected, 50 channels are quite a lot, so given the 15 number of channels, even if I use 3 Arduino mega, it will give 45 channels. But at least I know I can do the same thing 15 channels.

and the shift register TPIC95 is also quite amazing. I am new to signal generation and find it very interesting.

I am thinking about using ATTINY 10 with mosfet, which will constantly turn on/off the mosfet generating a square wave signal. Arduino mega will only update the values of frequency and duty cycle in each ATTINY 10. I find this solution very robust.

Hello Jebranakh,
Have a look for PCA9685, which is a 16 channel PWM controller, it may well be what you need (well, several of them anyway). It provides 16 independent PWM channels on 1 chip.

PerryBebbington:
...PCA9685... provides 16 independent PWM channels on 1 chip.

Unfortunately, no. Independent duty cycle but same frequency for all channels. And only one or a small choice of fixed frequencies.

Using Mega's timers results in the same problem. You can have 15 PWM channels with independent duty cycles, but not with 15 independent frequencies.

Jebranakh:
generate PWM signal on 50 channels with independent control of frequency and duty cycle on each channel. The frequency range 1Hz to 10,000 Hz.

But with what precision? At the maximum 10KHz, each cycle is only 100us. With what precision do you need to control the duty cycle? If you need 100 different duty cycles, you need a precision of 1us, and an update frequency of 1MHz.

If using 7 shift registers, the Mega would need to output bits at a rate of 56MHz to achieve a 1MHz update rate. The fastest Mega can do this, in theory, is 8MHz, and in practice far less, using its SPI hardware.

If using 7 output ports, using direct port manipulation, 7 ports would have to be updated every 1us, which is once every 16 instruction cycles.

Thanks for the correction Paul.

PerryBebbington:
Hello Jebranakh,
Have a look for PCA9685, which is a 16 channel PWM controller, it may well be what you need (well, several of them anyway). It provides 16 independent PWM channels on 1 chip.

Thank you for the suggestion, I even had one lying around and you just showed me a very practical alternative. But as PaulRB indicated it would be enough to meet my requirements but I believe it will come in handy in my future research.

@Jebranakh +1 karma for maintaining a positive attitude. Right now I can't think of a way to do this that isn't really complicated, with a separate circuit for each of the 50 channels. And then you have the challenge of communicating instructions to those 50 channels!

So can we ask, why do you need 50 channels of independent duty cycle and independent frequency? If it's a requirement set in a brief, challenge it! Maybe it is a genuine requirement with a valid reason, but no-one will think less of you for wanting to understand or question the reasons behind it.

PaulRB:
@Jebranakh +1 karma for maintaining a positive attitude. Right now I can't think of a way to do this that isn't really complicated, with a separate circuit for each of the 50 channels. And then you have the challenge of communicating instructions to those 50 channels!

So can we ask, why do you need 50 channels of independent duty cycle and independent frequency? If it's a requirement set in a brief, challenge it! Maybe it is a genuine requirement with a valid reason, but no-one will think less of you for wanting to understand or question the reasons behind it.

Thanks a lot for the detailed analysis, Now I really know that it cant be done with an Arduino Mega at all. The reason for these requirement is a research project where we the researcher is developing a medical device for some therapeutic purposes. He needs to control 50 far infrared LEDs by varying both its frequency and duty cycle.

Since it will be used for medical purpose and even later on as a commercial product I want to come up with a very robust design.

So far I came up with having an ATTINY 10 (or similar micro controller) turning on and off a mosfet. An Arduino will communicate with all the 50 channels through I2C communication. I think this is much more simpler and robust.

Jebranakh:
So far I came up with having an ATTINY 10 (or similar micro controller) turning on and off a mosfet. An Arduino will communicate with all the 50 channels through I2C communication.

I don't know if there is an Arduino core for attiny10.

I can suggest attiny45/85. Having 2 timers per chip, maybe it could drive 2 channels and "only" 25 chips would be required. And there are several Arduino cores for this chip including an excellent core developed by one of the forum members.

I don’t know the fundamentals of your project, but it’s curious that you want to run the LEDs at different frequencies AND duty cycles in what appears to be a fairly straightforward application.

I think we’re out of our depth here without more understanding of up your goals.
The suggestions above address your original topic, but there seems to be more that hasn’t come to the surface.
i.e.
What determines the frequency / duty choices for operation etc.
Are all 50 IR LEDs independent, or running in some form of sync etc ?

PaulRB:
I don't know if there is an Arduino core for attiny10.

I can suggest attiny45/85. Having 2 timers per chip, maybe it could drive 2 channels and "only" 25 chips would be required. And there are several Arduino cores for this chip including an excellent core developed by one of the forum members.

PaulRB:
I don't know if there is an Arduino core for attiny10.

I can suggest attiny45/85. Having 2 timers per chip, maybe it could drive 2 channels and "only" 25 chips would be required. And there are several Arduino cores for this chip including an excellent core developed by one of the forum members.

I selected Attiny 10 because it is really tiny and economical, so if the device is approved for commercialization, the cost will not add up. I personally prefer attiny 85 for the reasons you mentioned.
On the other hand, STM8 is far cheaper and have more pins and features. But I dont know if there is Arduino core available that will make it able to receive instruction from a master Arduino, and it also occupies more space.
50 channels occupy a good amount of space in a 2 layered PCB. A little more study will make me things much clear.

lastchancename:
I don’t know the fundamentals of your project, but it’s curious that you want to run the LEDs at different frequencies AND duty cycles in what appears to be a fairly straightforward application.

I think we’re out of our depth here without more understanding of up your goals.
The suggestions above address your original topic, but there seems to be more that hasn’t come to the surface.
i.e.
What determines the frequency / duty choices for operation etc.
Are all 50 IR LEDs independent, or running in some form of sync etc ?

All the 50 IR LEDs will have independent frequencies (1Hz to 10KHz) and duty cycles. The user will be setting the frequency and duty cycle for each channel via an app. The app will communicate with the device via a bluetooth.

The user can set any value for both the frequency and duty cycles for any of the 50 IR LEDs, he wants.

Jebranakh:
All the 50 IR LEDs will have independent frequencies (1Hz to 10KHz) and duty cycles. The user will be setting the frequency and duty cycle for each channel via an app. The app will communicate with the device via a bluetooth.

The user can set any value for both the frequency and duty cycles for any of the 50 IR LEDs, he wants.

The obvious question is why? Why would a user care about the PWM frequency of IR LEDs? Makes no sense to me.

PerryBebbington:
The obvious question is why? Why would a user care about the PWM frequency of IR LEDs? Makes no sense to me.

The person who will be using it is a medical researcher. He has been researching on the effects of far infrared light on body tissues and how it can be used to treat or help treat different diseases. He have got some impressive results in lab experimentation and will soon be going to conduct clinical trials but he wants this device in a compact product like form rather than a bunch of wires and microcontrollers laying around in a box. His goal is to market this device as a product once it passes the clinical trials.
I dont know much detail about how this is gonna work but I think his main research revolves around the pulses of far infrared radiation at different intensities.

While the objective sounds honourable and certainly doable, I believe there are a number of design, manufacturing & assembly issues that are going to derail your tidy prototype.

My first question is the importance of the need for the 50 LEDs being phase coherentl or not?
Second will be power source and cabling.

One thought would be to put a TINY at each LED node, a common ‘therapy controller’ that configures each TINY/LED with the required operating parameters, and a timing sync pulse distributed to the nodes. This could be 3/4 wires to each node f no coherence is needed, you coul possibly lose one of those conductors.
You could use a single. ode to drive several LEDs, but that will mean a spiderweb of local wiring at the nodes.

The project also needs to be designed with consideration for the total power requirement and cabling requirements.

PerryBebbington:
The obvious question is why? Why would a user care about the PWM frequency of IR LEDs? Makes no sense to me.

Me neither. If it were an audio application it might… but its’ not.

With IR the point of the PWM is to control the delivered power. Frequency has no effect on that, and I can’t see what physiological effect it would either.

Unless there’s some “woo” placebo effect from the “look, we are varying the frequency and power of 50 leds!” And surely any clinical trial worth it’s salt should pick up on that.

If I really needed 50 entirely independent PWM channels I think I’d be either looking at…
a) An dedicated PWM controller with multiple outputs
…if one isn’t available…
b) a FPGA based solution controlled from a single micro

Also you might want to look at regulatory approval issues up front. Medical equipment can have some fairly stringent requirements. I suppose if it’s not a safety of life application you may be okay.

lastchancename:
While the objective sounds honourable and certainly doable, I believe there are a number of design, manufacturing & assembly issues that are going to derail your tidy prototype.

My first question is the importance of the need for the 50 LEDs being phase coherentl or not?
Second will be power source and cabling.

One thought would be to put a TINY at each LED node, a common ‘therapy controller’ that configures each TINY/LED with the required operating parameters, and a timing sync pulse distributed to the nodes. This could be 3/4 wires to each node f no coherence is needed, you coul possibly lose one of those conductors.
You could use a single. ode to drive several LEDs, but that will mean a spiderweb of local wiring at the nodes.

The project also needs to be designed with consideration for the total power requirement and cabling requirements.

The total power consumption is around 60W( or better make it 65W), each channel draws 0.1 ampere at 12 volts.
And they are not phase coherent. An ATTINY 85 at each channel that recieves input from a main controller, and just updates the time of on/off the mosfet.
I am even avoiding sharp corners in PCB signal tacks to avoid interference at higher frequencies.
And If you have component's names, It will be easier for me to search but I will search it any way.

pcbbc:
Me neither. If it were an audio application it might... but its' not.

With IR the point of the PWM is to control the delivered power. Frequency has no effect on that, and I can't see what physiological effect it would either.

Unless there's some "woo" placebo effect from the "look, we are varying the frequency and power of 50 leds!" And surely any clinical trial worth it's salt should pick up on that.

If I really needed 50 entirely independent PWM channels I think I'd be either looking at...
a) An dedicated PWM controller with multiple outputs
...if one isn't available...
b) a FPGA based solution controlled from a single micro

Also you might want to look at regulatory approval issues up front. Medical equipment can have some fairly stringent requirements. I suppose if it's not a safety of life application you may be okay.

Honestly I dont know what effect it could have on the body as at higher frequencies, the LED will just literally be lit as it is when one simply turn it on.

There are dedicated PWM controllers but I couldn't find any that controls both the frequency and duty of all its channels.

and I have no experience in FPGAs, in fact the circuit I have just explained is a kind of pseudo FPGA ( in theory) :stuck_out_tongue: