At the atomic level, the piezoelectric effect occurs due to the alignment of electric dipoles within a material. When mechanical stress is applied, the structure of the material changes, causing a shift in the alignment of these dipoles and generating an electric charge. Conversely, when an electric field is applied, it can alter the alignment of the dipoles, causing mechanical deformation.