There's a bit more than that, the party he's leader of, the Conservative party has been split about the European Union for a long time so he needed to promise a vote on it to keep the eurosceptics in the party happy (UKIP was also growing in influence and played a large part in bringing the referendum to the political foreground) but the thing is he probably didn't think he'd actually have to go through with it because they were in coalition with another party at the time (The Liberal Democrats) who would veto any referendum and the overwhelming opinion was the election after would not give any party a majority government, but it did. (
https://en.m.wikipedia.org/wiki/United_Kingdom_general_election,_2015)
So Cameron was forced to give the referendum and basically rushed one in as soon as possible in fears another migrant crisis would change public opinion.