Grid impedance has a significant impact on the small-signal stability and control of grid-connected power converters used for connecting multiple distributed energy resources and in various motor drive applications. To apply advanced control schemes, such as adaptive control, real-time grid impedance is a necessary parameter that ensures stability margins of the system during operation. For real-time grid-impedance measurement purpose, broadband frequency-response measurement techniques based on the pseudorandom binary sequence can be used. In this article, a computationally effective perturbation signal quadratic residue binary (QRB) sequence is proposed for real-time grid impedance measurement. Unlike the conventional perturbation signals, the QRB sequence has low crest factor and less perturbation duration resulting in minimal disturbance during impedance measurement. To extract the grid impedance in real-time, the QRB sequence is added to the reference d-axis and q-axis currents of the battery energy storage system (BESS). Next, perturbed voltage and current signals are extracted in the abc-frame and converted to the d-q-frame, and grid impedance is assessed. Subsequently, the system stability is analyzed using the generalized Nyquist stability criterion. Later, an adaptive control algorithm is proposed and applied to the energy storage system (ESS) to enhance stability margins of the system based on the measured grid impedance. The effectiveness of the proposed perturbation sequence for impedance measurement and stability improvement algorithm are tested on a standard CIGRE microgrid system in the MATLAB/Simulink simulation environment. Experiments are conducted on a reduced scale 3 kVA laboratory testbed and results are presented to validate the efficacy of the proposed perturbation sequence and stability improvement algorithm. © 1972-2012 IEEE.