From The Penguin History Of Latin America, by Edwin Williamson (Penguin, 2003), Kindle pp. 229-230:
Brazil’s passage to independence, however, was not without its risks of political catastrophe. Though the attachment to monarchy was very strong, there had emerged here and there a considerable feeling for republicanism, as attested by the Inconfidência mineira of 1788–9 and intermittent republican revolts since. In the event of a sufficiently grave crisis of royal authority, these republican sympathies could have cohered to challenge the Catholic monarchy of Portugal. Such a possibility arose in 1820, when events in the Peninsula again placed the Crown in difficulties. After the defeat of Napoleon in 1814 Portugal had been ruled by a Regency Council in the absence of the king, but in late 1820 a series of revolts by liberals led to the establishment of a government committed to a constitutional monarchy. A Cortes was called in Lisbon to draw up a constitution modelled on the 1812 constitution of Cadiz, and the king was summoned to Portugal by the liberal government.
In Brazil there was extensive sympathy for the liberal revolution and John VI came to accept the principle of a constitutional monarchy, but he was torn as to whether or not he should return to Lisbon, fearing that he might lose Brazil if he did, or else Portugal if he did not. Finally, he decided to go back, but he left behind his son Dom Pedro as prince regent in Brazil. Thus the Portuguese monarchy put out an offshoot in its most important overseas colony in an attempt to span the political rift that was opening up between Brazil and the mother country.
That rift was to widen into an unbridgeable gulf once it became evident to the Brazilian delegates at the Lisbon Cortes that the peninsular liberals were determined to return Brazil to its colonial status prior to 1808. The liberal government proposed to cancel the political equality of Brazil with Portugal and the freedom of trade which the king had decreed for Brazil when he had first arrived in Rio. This the Brazilians would not countenance and, when the Lisbon government recalled the prince regent in October 1821, the Brazilians urged him to ignore the order. Perversely, Lisbon was pushing the mostly reluctant Brazilians towards some kind of separation, but it was still unclear what form this separation would take and how it might come about. At this juncture, in the final months of 1821, a political crisis arose which could have led to one of a number of outcomes – even to a republic, for which there was considerable support among radical liberals.
It was Dom Pedro’s chief minister, José Bonifácio de Andrada e Silva, a conservative monarchist who had spent over thirty years in the service of the Crown in Portugal, who steered Brazil towards independence. On 9 January Dom Pedro had declared that he would stay in Brazil, thereby asserting his autonomy from Lisbon. After his appointment a week later, José Bonifácio edged the country along an independent path, allowing indirect elections for a constituent assembly and disregarding orders from Lisbon. The final break with Portugal came when the Lisbon government tried once again to assert its authority over Brazil by recalling the prince regent. On 7 September 1822, on the banks of the River Ipiranga near São Paulo, Dom Pedro finally rejected Portugal and proclaimed the independence of Brazil.
After his famous Grito de Ipiranga the prince regent was crowned emperor and the former colony became a constitutional monarchy in its own right. Portuguese troops in various captaincies in the north and north-east put up violent resistance to independence, but by 1824 the whole territory had been won for Dom Pedro’s regime. In the following year Portugal, under pressure from Britain, recognized the independent state of Brazil; Britain also extended recognition, in return for a promise from Brazil to abolish the slave-trade and a commercial treaty which accorded imports from Britain a preferential tariff.