Animal development is coupled with innate behaviors that maximize chances of survival. Here, we show that the prothoracicotropic hormone (PTTH), a neuropeptide that controls the developmental transition from juvenile stage to sexual maturation, also regulates light avoidance in Drosophila melanogaster larvae. PTTH, through its receptor Torso, acts on two light sensors--the Bolwig's organ and the peripheral class IV dendritic arborization neurons--to regulate light avoidance. We found that PTTH concomitantly promotes steroidogenesis and light avoidance at the end of larval stage, driving animals toward a darker environment to initiate the immobile maturation phase. Thus, PTTH controls the decisions of when and where animals undergo metamorphosis, optimizing conditions for adult development.