If you feel like your attempts at dropping a few (or more) pounds haven’t worked because you keep giving in to those fatty food cravings (hello, pizza and ice cream) and indulging in late-night snacking, you may want to consider whether you’re getting enough shut-eye.

Turns out skimping on sleep can trigger overeating and cravings for fatty foods, say researchers at the University of Pennsylvania. In fact, a new study from University of Colorado found that the female participants who were sleep-deprived for five nights in a row gained about two pounds, perhaps because of late-night snacking .

If you have blood-sugar problems, skimping on sleep can be doubly bad, according to research presented at the March 2015 annual meeting of the Endocrine Society. Researchers at Weill Cornell Medical College in Doha, Quatar, enrolled 522 patients who had been recently diagnosed with Type 2 diabetes. Every patient was required to keep a sleep diary. Those who missed out on even as little as 30 minutes of weekday sleep were 72 percent more likely to be obese compared to those who got enough. A year later, the researchers calculated that for every 30 minutes of weekday sleep debt there was a 17 percent increased risk for obesity and a 39 percent increased risk of insulin resistance.

Related: 6 Safe Ways to Boost a Sluggish Metabolism (and 1 to Avoid)

The sleep-weight link

While the side effects of too little sleep might show up on your waistline, the problem originates in your brain, says Saralyn Mark, MD, an endocrinologist based in Washington, DC, and former senior medical advisor to NASA. “The biggest reason why sleep is correlated with weight gain is that the hypothalamus, the master gland in the brain, is affected by sleep deprivation and levels of hormones become altered,” says. “The hypothalamus produces leptin, our satiety hormone, and ghrelin, which stimulates appetite. With sleep deprivation, ghrelin levels rise so we seek out carbohydrate-rich foods for quick energy, and leptin levels decrease, so we don’t feel as full and the drive for food is increased.”

If you think of your body like a car, sleep is the tune-up that helps keeps us in top-functioning condition. “During sleep, our body restores itself, processing toxins and decreasing our cortisol levels” explains Mark. Cortisol is one of the so-called stress hormones.

Sleep deprivation can affect other hormones, too, including thyroid hormone and insulin. “Thyroid hormone sets the speed for our metabolism and insulin helps us process glucose in our cells,” says Mark.

While it might be okay to have a few sleepless nights, long-term sleep deprivation will take a bigger toll. “Multiple studies have suggested a link between chronic sleep deprivation and increased obesity risk over time,” says Joel A. Rodriguez, MD, a general and bariatric surgeon in San Antonio, Texas. 

Defend yourself against gaining

To keep a lack of sleep from packing on the pounds, try these tips.

Stop sabotaging your sleep. Downing a double latte following dinner or tuning into a nail-gripping crime drama right before bed is just asking for trouble. “You want to reframe that time before you head into bed for sleep,” Mark says. “You should turn off devices as long as possible before retiring for sleep,” she says (Some experts advise powering down at least an hour before bedtime.) “Practice meditation, stretch or take a warm bath,” she says. “These are great ways to reduce stress and promote relaxation.”

As for the ideal temperature for slumber, the National Sleep Foundation recommends setting the thermostat to around 65 degrees.

Aim for at least seven hours. “Sleeping less than seven hours seems to be a pivotal point at which levels of the hormone ghrelin increase, causing increased feelings of hunger, and levels of the hormone leptin fall pretty quickly,” says Georgie Fear, RD, a dietitian in Vancouver and author of the forthcoming “Lean Habits for Lifelong Weight Loss.”

Related: The Lowdown on LED Lightbulbs and Insomnia

Head off food cravings by upping your protein intake. On those days when you end up short on sleep, stay full and fight cravings by eating a higher than usual amount of protein, as well as piling on the fruits and veggies.

Lambeth Hochwald is a New York City-based journalist who covers such topics as family, health and parenting. She is a current contributor to Dr. Oz: The Good Life, Everyday With Rachael Ray and Yahoo Parenting.