Earthquakes are frequently accompanied by electromagnetic (EM) anomalies. These anomalies are thought to be caused by earthquakes but the generation mechanism is still unclear. The piezoelectric effect has been proposed as a possible mechanism but the EM responses to earthquakes due to such an effect has not been well understood. In this article, we study the EM signals generated by an earthquake source due to the piezoelectric effect. We develop a semi-analytical method to solve the seismic and EM fields in a 3D layered model and conduct numerical simulations to investigate the characteristics of the EM fields. The results show that the earthquake can generate two kinds of EM signals. One is the early-EM signal which arrives earlier than the seismic wave. The other is the co-seismic EM signal accompanying the seismic wave. For an earthquake the co-seismic electric field can reach ~10 μV/m and the magnetic field can reach ~10-4 nT. We also study the sensitivity of the co-seismic EM fields to the rock conductivity. The results show that the co-seismic EM fields are mainly affected by the conductivity of the shallow layer, and they are also affected by the conductivity of the deep layer when the top layer is thin.