Cameron ended months of speculation by announcing in a speech the plan for a vote sometime between 2015 and 2018, shrugging off warnings that this could imperil Britain's diplomatic and economic prospects and alienate its allies.
Cameron said Britain did not want to pull up the drawbridge and retreat from the world but that public disillusionment with the EU is at "an all-time high".
"It is time for the British people to have their say. It is time for us to settle this question about Britain and Europe," Cameron said. His Conservative party would campaign for the 2015 election promising to renegotiate Britain's EU membership.
"When we have negotiated that new settlement, we will give the British people a referendum with a very simple in or out choice to stay in the European Union on these new terms; or come out altogether. It will be an in-out referendum."
Whether Cameron will ever hold the referendum remains as uncertain as the Conservatives' chances of winning the next election due in 2015.
They trail the opposition Labour party in opinion polls, and the coalition government is pushing through painful public spending cuts to try to reduce Britain's large budget deficit, likely to upset voters in the meantime.
Cameron's promise looks likely to satisfy much of his own party, which has been split on the issue, but may create uncertainty when events could put his preferred option - a looser version of full British membership - out of reach.
The move may also unsettle other EU states, such as France and Germany. European officials have already warned Cameron against treating the bloc as an "a la carte menu" from which he can pick and choose membership terms.
His speech in London is also likely to raise concerns in the United States, a close ally, which has said it wants Britain to remain inside the EU with "a strong voice".
Nor is it likely to help heal rifts with his pro-European Liberal Democrat junior coalition partners.
Cameron said he would prefer Britain, the world's sixth biggest economy, to remain inside the 27-nation EU but he also made clear he believes the EU must be radically reformed.
A new EU must be built upon five principles, he said: competitiveness, flexibility, power flowing back to - not just away from - member states, democratic accountability and fairness.
The euro zone debt crisis is a main reason why Britain must reassess its relationship with the wider EU, Cameron said, adding that ever-closer union was not Britain's objective.
Cameron said the EU faced three main problems: the debt crisis, competitiveness and faltering public support.
Democratic consent for the EU in Britain was now "wafer-thin", reflecting the results of many opinion polls that have shown a slim majority would vote to leave the bloc and the rise of the UK Independence Party that favours complete withdrawal.
"Some people say that to point this out is irresponsible, creates uncertainty for business and puts a question mark over Britain's place in the European Union," said Cameron. "But the question mark is already there: ignoring it won't make it go away."
Avoiding a referendum would make an eventual British exit more likely, not less, he said. This would risk bottling up resentment towards the EU, compounding people's feeling that "the EU is heading in a direction that they never signed up to".
"Simply asking the British people to carry on accepting a European settlement over which they have had little choice is a path to ensuring that when the question is finally put - and at some stage it will have to be - it is much more likely that the British people will reject the European Union."
Many Britons resent the EU's interference in their daily lives and its "unnecessary rules and regulations", he added.
Cameron's speech has been marked by long delays, diplomatic rows and the postponement due to the Algerian hostage crisis.
"The Curse of TutanCameron's Europe speech" was how one political magazine summed up the repeated delays in a headline over a picture of a golden-faced Cameron superimposed on the death mask of ancient Egyptian pharaoh Tutankhamen.