Scanning electron microscope (SEM) and its variations, such as critical dimension SEM (CD-SEM) and electron beam inspection (EBI-SEM), are getting increasingly critical in process control in very large-scale integration circuits (VLSI) manufacturing. For rapid capturing of image, the patterned wafer surface needs to be maintained in a range smaller than the depth of focus (DoF) of the electron beam. A triangulation optical system is used to sense the wafer surface for the rapid leveling of the wafer height in real time. An optical grating illuminated by a LED light source is projected onto the wafer and further imaged to a camera. The wafer height is calculated from the displacement of the optical grating image. However, pattern variation under the measurement area of the wafer may affect the transmission ratio of the light to the sensor and further the measurement accuracy. To achieve a high precision measurement result, an automatic light intensity adjustment system is developed. A computer records the optical grating image and the image intensity is analyzed. If the grating image intensity in the bright area is not in the desired range, a control software will convert the delta into the LED driving current increment or decrement, and the LED light output will be adjusted accordingly so that the image intensity is brought back to the expected range and keep there for the entire process. The LED feedback control experiment shows that the system remains at the target grey level without noticeable variation when the system transmission ratio varies by a factor of 7.5 times.