In this work, we present a systematic procedure to design piezoelectric transducers by simultaneously optimizing the host structure and the electrode layout. The technique allows maximizing any electromechanical coupling of output efficiency of the transducer. Either the output current collected at the electrodes when a mechanical force is applied (sensors), or the in-plane displacement when a given voltage is applied to the electrodes (actuators) can be optimized. We introduce a new idea to avoid the typical problem in topology optimization of the appearance of gray areas, getting finally 0-1 designs, some of which have been manufactured. Also, mathematical demonstration of reciprocity of the piezoelectric effect is shown. Many MEMS-based actuators like microgrippers, surface probes, or micro-optical devices can be optimized following this procedure. A similar approach has been demonstrated previously in modal sensors/actuators, although restricted to the design of the electrode layout for a given structure. The novel method shown here allows the simultaneous optimization of both shapes, for electrode and structure in the static case.