Madara didn't actually give it to Nagato. When Nagato was young, Madara transplanted the Rinnegan onto Nagato, without his knowledge.

Madara had achieved the Rinnegan near to end of his physical life. So he decided that would have to die someday as his body was nearing the end. As a plan to be revived later on, he transplanted his eyes onto an Uzumaki individual.