To answer the TLC5940 side of the question:
First of all, bear in mind that when using TLC5940 your intensity need not be 12-bit values (4096 values): you can use the TLC5940 using with intensities of any value 12 bits or less. For instance, 8-bit intensities (256 values) do provide a very satisfying result. More on this latter.
Assuming 12-bit intensities, here's how GSCLK
and BLANK
work: TLC5940 doesn't have its own clock. So GSCLK
is used to figure out when to turn on and off each LED. At the beginning of a cycle, all LEDs are on. Each time positive-going edge on GSCLK
is received an internal counter is incremented on TLC5940. Each LED whose intensity value is lower than the counter is turned off. So LEDs with intensity 1
are turned off after the first cycle, LEDs with intensity 2
are turned off after the second cycle, and LEDs with intensity 4096 are not turned off at all. At the end of the cycle the chip does not reset itself, rather it expects a positive-going edge on BLANK
to reset it, and after this the cycle begins again.
Here's what this means for driving the TLC5940: you need two PWM outputs; one for GSCLK
and one for BLANK
, and the one for BLANK
needs to happen every 4096 cycles of GSCLK
. Now notice that we are talking about the frequncy here, and not the duty cycle, whereas it is the duty cycle that analogWrite()
controls. To drive the TLC5940, you could use a library written for driving TLC5490, or you can do the lower-level driving of TLC5940 yourself, which can use one of the following approaches (assuming you are using an ATmega-based Arduino, and in scale of increasing difficulty):
- Program the two timers yourself such that they use different prescalers such that the
BLANK
line is driven at 1/4096th the frequency of the GSCLK
- Program the
CKOUT
fuse on the ATmega, causing it to output the clock signal on one of its output pins. Use this for GSCLK
. Then use a timer to generate a BLANK
pulse at 1/4096th of clock frequency.
- Clock the ATmega externally, and use the same clock for
GSCLK
. Have an ATmega timer generate the BLANK
pulse at 1/4096th of clock frequency.
Now to the question of frequency relationship between the TLC5940 clocking and the PWM. The BLANK
line will have a duty cycle of 1/4096 (or whatever the maximum intensity value you are using), so that probably will not work for your servos. The GSCLK
is usually 50/50 duty cycle but need not be. Lets assume that you want your LEDs to appear to be steady, and lets take the flicker theshhold to be 50Hz. This would mean that you need your intensity 1
LED to be flickering at 50Hz or above, meaning that a 4096-clock long cycle should complete in 20 milliseconds, meaning that your GSCLK
clock should be at least 204kHz. At 204kHz the clock pulses are about 5uS long. So while in theory you could use the same clock for your servos and the TLC5940 (I think that's what you are asking): if you maintain the clock frequency (at 204kHz) and change the duty cycle you could control your servos and clock the TLC5940. However, if you use 12-bit intensities, then the greyscale clock needed by TLC5940 is going to be too fast for the servos.
But, if 4096 intensity values is too much to handle, consider using 8-bit intensity values. You will still have to send them as 12-bit values (that's what the TLC5940 interface expects), however, the is no law that says that your BLANK
pulse must occur every 4096 GSCLK
clocks. If it occurs every 256 clocks, you have yourself 8-bit intensity. So your 8-bit intensities should be sent as valid 12-bit values (with the high four bits being zero), and you'll restart the clocking cycle every 256 clocks. You can use any other number of intensity bits, as long as it is 12 or less, in the same manner. If you are using 256 intensity (=greyscale) values, then your minimum clock is 12.8kHz, and the clock duration is 78uS. Closer the 2400uS +90 pulse, but still quite far away. If we assume that +90 pulse is 90/10 duty cycle, then we calculate the clock cycle length to be 2.6mS, which translates into 375Hz clock. At this clocking, the maximum intensity value that will yield no flickering is 8 values (3 bits) at 50Hz persistence theshhold, and 16 values (4 bits) at 25Hz. You can decide whether that is good enough for your purposes.
While a datasheet for this servo does not seem to be readily available after a quick Google search, I would say you should be fine. All hobby/RC servos such as the SG90 that I have come across use that same protocol. Unless they specify they are a digital servo, which use a greater pulse frequency, the 1.5ms pulse width = center position standard is followed.
The best thing to do would just be to test it and see what happens. The pulses are just control signals. If you give the motor a pulse that is too short or too long, it will just turn to its minimum and maximum angles.
Best Answer
The hobby servo needs a signal like this:
In a 20ms window, (50 times a second) a pulse with a width between approximately 1ms and 2ms (may vary, depending on servo) with a middle point of 1.5ms. So in the 20ms window, there is almost 90% of the time being "silence" with no signal level. The pulse is only high for a very short (relatively) period.
The Arduino pins are default output to either 500Hz and 3.9Khz. You cannot directly use the pin in PWM mode (with analogWrite), because it will be receiving pulses too fast, and will bug out/malfunction/won't do anything useful.
The Servo library available for inclusion to an Arduino sketch uses the built-in timers of the ATMEGA328P (or other chip, depends on which arduino you are using) and some fancy software and interrupts to get the proper 50Hz timing, and uses an ordinary digital output pin to send the required pulse.
You can do this yourself in a simple loop, if you only control 1 servo and have a simple program, by simply setting a digital pin HIGH and then delaying (using DelayMicroeconds) for between 1000 and 2000 microseconds depending on what position you want to move to, then setting the pin LOW and delaying for (20,000 - the time you delayed for the servo).
You can send the same signal constantly and the servo will stay at that point, or you can send it until it reaches there and stop sending a signal. The servo should stay at that point, but I do not think it will remember the point (you can move it with your hand and it will stay at the new point, until a new signal is sent again).
It's better not to try and use the PWM hardware outputs for the Arduino to control a servo, even if you do manage to pre-scale the timers down to the required 50Hz (I think this would be hard to get, but I believe you can do it!) as the PWM output registers will need to be set with 8 bit resolution in a tiny range of 10-20%, which only gives you 25 total positions available to the servo (that is pretty bad, 25 positions for a whole +-90 degree movement range!). So go with the Servo Library, basically :)