Animals from flies to humans are able to distinguish subtle gradations in temperature and show strong temperature preferences. Animals move to environments of optimal temperature and some manipulate the temperature of their surroundings, as humans do using clothing and shelter. Despite the ubiquitous influence of environmental temperature on animal behaviour, the neural circuits and strategies through which animals select a preferred temperature remain largely unknown. Here we identify a small set of warmth-activated anterior cell (AC) neurons located in the Drosophila brain, the function of which is critical for preferred temperature selection. AC neuron activation occurs just above the fly's preferred temperature and depends on dTrpA1, an ion channel that functions as a molecular sensor of warmth. Flies that selectively express dTrpA1 in the AC neurons select normal temperatures, whereas flies in which dTrpA1 function is reduced or eliminated choose warmer temperatures. This internal warmth-sensing pathway promotes avoidance of slightly elevated temperatures and acts together with a distinct pathway for cold avoidance to set the fly's preferred temperature. Thus, flies select a preferred temperature by using a thermal sensing pathway tuned to trigger avoidance of temperatures that deviate even slightly from the preferred temperature. This provides a potentially general strategy for robustly selecting a narrow temperature range optimal for survival.