Most heritable behavioral traits have a complex genetic basis, but few multigenic traits are understood at a molecular level. Here we show that the C. elegans strains N2 and CB4856 have opposite behavioral responses to simultaneous changes in environmental O(2) and CO(2). We identify two quantitative trait loci (QTL) that affect this trait and map each QTL to a single-gene polymorphism. One gene, npr-1, encodes a previously described neuropeptide receptor whose high activity in N2 promotes CO(2) avoidance. The second gene, glb-5, encodes a neuronal globin domain protein whose high activity in CB4856 modifies behavioral responses to O(2) and combined O(2)/CO(2) stimuli. glb-5 acts in O(2)-sensing neurons to increase O(2)-evoked calcium signals, implicating globins in sensory signaling. An analysis of wild C. elegans strains indicates that the N2 alleles of npr-1 and glb-5 arose recently in the same strain background, possibly as an adaptation to laboratory conditions.