Sampling rate for PID control to balance an inverted pendulum using 8051 - AT89c51rc2
This depends on the dynamics of your system, which you haven't told us anything about.  It really comes down to the height of your inverted pendulum.  The shorter (from pivot to center of mass), the faster it will respond and the faster the control loop needs to be.

Assuming this is a human-scale pendulum, maybe 1 foot to 1/2 meter or so, my gut feel says run the control loop every 10 ms for starters.  Significant things should be happening on the time scale of a few 100 ms, so 10 ms loop time seems about right.  You generally want to run the loop 20-50 times per first order time constant.

The tradeoffs are that faster loop time gives you more granularity, but too fast means the D term will see mostly noise, and the I contributions will very small relative to the output value.  Let's say for speed and simplicity, you use floating point with 16 bit mantissa.  If the I contributions are so small that they are 1000 times smaller than the accumulated value, then you're down to 6 bits of I each contribution.  That might still be OK, but I wouldn't want to go below that.  You can throw more bits (and therefore more cycles) at the I resolution problem, but you can't fix the D term seeing mostly noise or individual sensor steps (quantization noise).

You seem to be assuming that sampling rate and loop rate are the same.  They shouldn't be.  I would try to sample the sensors much faster than the loop period, like maybe every 100 to 500 &micro;s, depending on what you A/Ds and sensors can do.  Then apply two poles of low pass filtering to the sensor values, but keep the step response time to at least 80% or so in one loop time.

The advantage of filtering is that you attenuate the high frequencies that won't help to control the plant, but will cause jitter and other problems.  It also reduces the quantization noise, and even gets you a extra bit or two maybe due to any one reading being the result of a number of input samples.

For example, let's say you can sample all the inputs once every 200 &micro;s.  With two poles of low pass filtering using a filter fraction [FILT &lt;-- FILT + FF(NEW - FILT), where FF is the filter fraction] of 1/16 (multiply by FF is shift right 4 bits), then you get 83% settling in your loop period of 10 ms.  But, no one sample contributes more than about 2.5% to the output.

Yes, there is high-fallutin math to compute all this up front as some of the other answers have shown.  However, the problem with that is usually having to know things about your system you don't know and can't easily measure.  Starting at a reasonable place and then tweaking as necessary is usually simpler and and quicker.  However, you have to actually understand what's going on, else you're just pecking at it in the dark.