In pnjunction there is a high concentration of holes on the pside and a high concentration of electrons on the nside. The mobile charge carriers diffuse from high concentration to low concentration so the holes diffuse to the nside and the electrons diffuse to the pside. This establishes a charge imbalance with positive charge on the nside and negative charge on the pside resulting in an electric field that points from n to p. This electric field pushes the holes back to the pside and the electrons back to the nside. At zero applied voltage, the diffusion current is cancelled by the drift current caused by the electric field. There is a voltage drop across the junction in this case which can be calculated by integrating the electric field across the junction. This voltage is called the builtin voltage, $V_{bi}$. In the band diagram there is an offset of $eV_{bi}$ between the valence band on the two sides of the junction. If both sides of the junction are heavily doped, the Fermi energy is close to the valence band on the pside and close to the conduction band on the nside. This makes $eV_{bi}$ nearly equal to the band gap energy $E_g$.
$E_g=$ eV $W=$ μm $x_p=$ μm $x_n=$ μm $V_{bi}=$ V $n_i=$ 1/cm³
Band diagram

The energy associated with the builtin voltage $eV_{bi}$ (orange) is the band gap energy $E_g$ (green) minus the distance between the conduction band and the Fermi energy on the nside (pink) minus the difference between the Fermi energy and the valence band on the pside (blue).
$$eV_{bi} = E_g  (E_c  E_f)_n  (E_fE_v)_p.$$Far from the junction on the pside, the hole concentration is $p= N_v\exp\left(\frac{E_vE_f}{k_BT}\right)\approx N_A$. This can be solved for the energy difference between the valence band and the Fermi energy,
$$(E_fE_v)_p = k_BT\ln\left(\frac{N_v}{N_A}\right).$$Similarly, far from the junction on the nside, the hole concentration is $n= N_c\exp\left(\frac{E_fE_c}{k_BT}\right)\approx N_D$. This can be solved for the energy difference between the conduction band and the Fermi energy,
$$(E_cE_f)_n = k_BT\ln\left(\frac{N_c}{N_D}\right).$$The builtin voltage can then be expresed as,
$$eV_{bi} = E_g  k_BT\ln\left(\frac{N_c}{N_D}\right)  k_BT\ln\left(\frac{N_v}{N_A}\right).$$The expression for the intrinsic carrier density $n_i$ is,
$$n_i^2=N_cN_v\exp\left(\frac{E_g}{k_BT}\right).$$Solving this for $E_g$ yields,
$$E_g = k_BT\ln\left(\frac{N_cN_v}{n_i^2}\right).$$The builtin voltage can then be written as,
$$eV_{bi} = k_BT\ln\left(\frac{N_cN_v}{n_i^2}\right)  k_BT\ln\left(\frac{N_c}{N_D}\right)  k_BT\ln\left(\frac{N_v}{N_A}\right).$$Using the properties of the logarithm, this can be simplied to,
$$\boxed{\quad eV_{bi} = k_BT\ln\left(\frac{N_DN_A}{n_i^2}\right).\quad}$$