Intermediate dimensions were introduced to provide a spectrum of dimensions interpolating between Hausdorff and box-counting dimensions for fractals where these differ. In particular, Bedford-McMullen carpets are a natural case for investigation, but until now only very rough bounds for their intermediate dimensions have been found. In this paper, we determine a precise formula for the intermediate dimensions $\dim_{ \theta } \Lambda $ of any Bedford–McMullen carpet $\Lambda $ for the whole spectrum of $\theta \in [0,1]$, in terms of a certain large deviations rate function. The intermediate dimensions exist and are strictly increasing in $\theta $, and the function $\theta \mapsto \dim _{ \theta }\Lambda $ exhibits interesting features not witnessed on any previous example, such as having countably many phase transitions, between which it is analytic and strictly concave. We make an unexpected connection to multifractal analysis by showing that two carpets with non-uniform vertical fibres have equal intermediate dimensions if and only if the Hausdorff multifractal spectra of the uniform Bernoulli measures on the two carpets are equal. Since intermediate dimensions are bi-Lipschitz invariant, this shows that the equality of these multifractal spectra is a necessary condition for two such carpets to be Lipschitz equivalent.