Improvements in ingestible electronics with the capacity to sense physiological and pathophysiological states have transformed the standard of care for patients. Yet, despite advances in device development, significant risks associated with solid, non-flexible gastrointestinal transiting systems remain. Here, we report the design and use of an ingestible, flexible piezoelectric device that senses mechanical deformation within the gastric cavity. We demonstrate the capabilities of the sensor in both in vitro and ex vivo simulated gastric models, quantify its key behaviours in the gastrointestinal tract using computational modelling and validate its functionality in awake and ambulating swine. Our proof-of-concept device may lead to the development of ingestible piezoelectric devices that might safely sense mechanical variations and harvest mechanical energy inside the gastrointestinal tract for the diagnosis and treatment of motility disorders, as well as for monitoring ingestion in bariatric applications.