Let's find the potential energy of an electric dipole in a uniform electric field. We have a dipole inside a uniform electric field as shown in Figure 1 below. The torque produced tends to rotate the dipole in anticlockwise direction.
The force on negative charge is \(F_1\) and on positive charge is \(F_2\). The magnitude of force on each charge is the same. Let the magnitude of one charge is \(q\) and therefore the magnitude of force on each charge is \(F = qE\) where \(E\) is the electric field magnitude. The perpendicular distance between the line of action of forces (shown in dotted line in Figure 1) is \(d\sin \theta \) so the lever arm for each force is the same which is \(\frac{d}{2}\sin \theta \). The magnitude of torque \(\tau \) for each charge is also the same which is \((qE)\left( \frac{d}{2}\sin \theta \right)\). Since both torques tend to rotate the dipole in anticlockwise direction, the net torque magnitude on the dipole is twice the torque magnitude on one of the charges which is:
\[\tau = qdE\sin \theta {\rm{ }} \tag{1} \label{1}\]
The product \(qd\) is another physical quantity called electric dipole moment. The electric dipole moment \(\vec{p}\) has a direction from negative charge to positive charge in an electric dipole. Now in terms of the electric dipole moment, the above expression can be written as
\[\tau = pE\sin \theta \tag{2} \label{2}\]
This is the expression for the cross product of vectors, so in vector form it is \(\vec{\tau }=\vec p \times \vec E\). You need to know the right hand thumb rule of vector product to know the direction of \(\vec \tau\); the curved fingers give the direction of rotation and the thumb gives the direction of \(\vec \tau\) which in this case is perpendicularly towards you. As you can see in Figure 1 and from above equation the torque is zero when \(\theta \) is zero or \(\pi \). When \(\theta =0\), \(\vec p\) and \(\vec E\) are antiparallel which is the position of unstable equilibrium. When \(\theta =\pi \), \(\vec p\) and \(\vec E\) are parallel which is the position of stable equilibrium. And the torque always tends to rotate the dipole in stable equilibrium position.
Now let the torque rotates the dipole through a small angle \(d\theta \) , so the small work done by the torque is \(dW=\tau d\theta \). Since the torque rotates the dipole in anticlockwise direction, that is in the direction of increasing \(\theta \) the work done is positive. The total work done by the torque is obtained by integrating \(dW\) between limits \(\theta_1\) and \(\theta_2\):
\[W = \int\limits_{{\theta _1}}^{{\theta _2}} {\tau d\theta } = pE\int\limits_{{\theta _1}}^{{\theta _2}} {\sin \theta {\mkern 1mu} d\theta } = pE( - cos{\theta _2} + cos{\theta _1})\]
\[{\rm{or,}}\quad W = pE\cos {\theta _1} - pE\cos {\theta _2} \tag{3} \label{3}\]
In the above equation Eq. \eqref{3}, the quantity \(pE \cos \theta\) is the potential energy of the electric dipole. It is denoted by \(U\) and therefore, \(U_1 = pE \cos \theta_1\) and \(U_2 = pE \cos \theta_2\). So, \(W=U_1 - U_2 = -(U_2 - U_1) = -\Delta U\). Therefore work done is the negative of change in potential energy. You know from the conservation of mechanical energy that the work done by gravitational force is also the negative of change in gravitational potential energy. The potential energy of the electric dipole is
\[U = pE \cos \theta \label{4} \tag{4}\]
In anticlockwise direction \(\theta \) increases and the potential energy goes on decreasing until becomes minimum in stable equilibrium position at \(\theta = \pi\). At \(\theta = \pi\), the potential energy is \(U = -pE\) which is the most negative value. In anticlockwise direction the work done is positive; final potential energy is smaller than initial potential energy (\(U_2 < U_1\)) and the negative of change in potential energy is positive. Similarly in clockwise direction that is in the direction of decreasing \(\theta \) the work done is negative; final potential energy is greater than initial (\(U_2 > U_1\)) and the negative of change in potential energy is negative. Note that the torque tends to minimize the potential energy of the dipole towards stable equilibrium position. At \(\theta = 0\) the potential energy is maximum which is \(U = pE\) and zero at \(\theta = \pi /2\). Note that zero potential energy does not mean that the the dipole does not have potential energy but you know that zero is greater than negative values.
If the torque rotates the dipole in clockwise direction (the electric field direction should be exactly opposite to the direction shown in Figure 1) which is in the direction of decreasing \(\theta \), the work done should be positive (the torque is in the same direction of rotation). In this case the final potential energy is greater than initial and therefore the potential energy of the dipole is \(U=-pE\cos \theta \).