His father, King John VI, was in charge of Portugal at the time. Why did Pedro defy him and side with the Brazilians?
TLDR: Pedro didn’t defy his father, he defied the Portuguese parliament. It was a gamble, made by both of them, to make the House of Bragança the royal house of both places, since war seemed inevitable due to economic reasons. In the end it paid off, because that’s exactly what happened.
The long version:
The Portuguese Royal Family fled Napoleon’s invasion of Portugal in 1808 and went to Brazil.
In Europe, Portugal became then a battle zone between French and British interests, with the British being allies of Portugal. There were Portuguese insurrections at the same time as the Spanish resistance, and the French tried to repress these with violence. In the end, the British-Portuguese army defeated the French and evicted then from Portugal and the Iberian Peninsula, but this took years and led to much damage and suffering.
In Brazil, D. Joao VI formed his government in Rio de Janeiro, Brazil’s colonial capital. One of the most important things he did was open the Brazilian ports to friendly nations, especially the UK, which received lower tariffs. This measure was meant to compensate the British for their support, since British ships escorted the Royal Family to Brazil. Brazil was a closed colony, following the mercantilist ideas of the time (it’s called colonial pact), and only Portugal had the privilege of trading with it. This meant that each time a Brazilian wanted to export sugar or import a piece of cloth from Europe, for example, he had to go through Portuguese intermediaries, who all levied a price on it. A good chunk of the Portuguese economy depended on this. When Portugal lost its access to overseas markets due to the French invasion (it was blocked along with all of French occupied Europe by the British navy), the Portuguese merchant class and all who depended on it took a blow. But even when the French were expelled the old prosperity didn’t come back, because now Brazilians could do business directly with all other European nations, most importantly the UK, which had a lower tariff than even Portugal. So the Portuguese intermediaries lost their business.
The Portuguese also had the grievance of being ruled from a king that lived overseas (something unheard of in Europe), who hadn’t shared their suffering and being ruled locally by a foreigner, William Carr Beresford, a British military official (Irish to be more precise). He was officially appointed by D. Joao VI to rule Portugal in his name, but his appointment actually came because the British army was responsible for expelling the French, and he was one of the officials under the Duke of Wellington at the time. The Portuguese military disliked having to report to a foreigner like Beresford, so conspiracies to oust him were being made. These conspiracies took advantage of the secret societies of the time, which were mostly Freemasons, who had liberal ideas, and so there was a mix of ideas of independence and liberalism. Some of the conspiracies were caught, and the conspirators harshly punished by Beresford, but eventually a revolt took place in Porto, the second biggest Portuguese city, and this revolt ousted Beresford from power. The rebels gave voice to many grievances the Portuguese had, especially being ruled locally by a foreigner, being ruled from a king that resided from overseas and not having the colonial pact with Brazil anymore.
The revolution of Porto of 1820 was a liberal revolution. Much like the French revolution, it at first took the shape of convening a parliament (that hadn’t gathered in centuries) and trying to make the country a more constitutional monarchy instead of an absolute one. The Brazilians at first were very happy with the revolution. Many had liberal ideas and wanted a constitutional monarchy as well. D. Joao VI had elevated the Brazilian colony to the status of united kingdom of Portugal and Algarve, so they would be represented in the Cortes as well (the same way Scotland was represented in the British parliament). The problem was in how much representation in the Cortes, the Portuguese parliament, they would actually have.
Brazil and Portugal had about the same population size, around 3-4 million. The difference was that 1/3 of the Brazilian population was made up of slaves. The question whether slaves should be represented any way in the parliament is a very tough one, because slave-owning territories would then be underrepresented against territories where there were no slaves. This same question arose in the US Congress and the matter was solved with each slave counting as 2/3 free men for the question of how many deputies they should have in Congress, even if the slaves never voted. In the Portuguese revolution the matter was settled in such a way that slaves would not count at all. So the Brazilians would be outnumbered by 3 to 2 in the Cortes.
So the complaint that the Portuguese merchant class had, of having lost its Brazilian markets, was going to be put forward to the Cortes one day. And the Portuguese would have their way, for democracy can be the dictatorship of the majority when minority rights aren’t protected. And in this case the Portuguese didn’t think the Brazilians had the right to free trade with other countries, they had to go back to the colonial pact. And this would be the matter that would case the independence war between the two countries, since the Brazilians profited more from free trade, without Portuguese intermediaries.
D. Joao VI was an able politician, even if later historiography didn’t paint him in such a way. He understood the political currents he was facing. When the Cortes called him back to Portugal he could feel that a conflict between Portugal and Brazil could arise in the future, due to the colonial pact. So he told his older son, D. Pedro, to stay in Brazil. He was thinking in more dynastic terms rather than in personal ones. There was a way that the House of Bragança could be the royal house of both places (as it indeed came to be), and so he made his gamble. His son, D. Pedro, played the part. He kind of liked Brazil, unlike his younger brother, D. Manuel, who hated it with a passion. So he could live in Brazil for an indeterminate time, and could be used by the Brazilian elite as a focal point of independence and unity at the same time as he used them by making sure he’d be their leader and that their monarchs would belong to the House of Bragança.
D. Joao VI returned to Portugal in 1821 at the request of the Cortes. Before he left, he named his son, D. Pedro, Prince Regent of Brazil (something that the Cortes hadn’t given the explicit liberty to do, but then there was a transition from absolute monarchy to constitutional monarchy). D. Pedro then took many Brazilian-friendly decisions, like lowering taxes and elevating the Brazilian military officers to the same status as the Portuguese ones. The Cortes then asked D. Pedro to return. All the others in the royal family had returned except him, and he was taking many decisions that didn’t go through parliament before (and that probably would be denied by the Portuguese majority). Brazilian government should be transferred from the Prince Regent to a junta appointed by the Cortes. D. Pedro defied the Cortes request then in a day that’s called Dia do Fico (Day of the Staying) in Brazil, January 9th 1822. He didn’t defy his father. He was asked by many Brazilian leaders to stay, due to the reasons mentioned above. He would be the focus of Brazilian independence, an independence made in a conservative way, a way that would try to avoid the fragmentation that was seen in the Spanish colonies, and that would take the interests of the Southeastern Brazilian elites over the elites from more distant provinces.
The Cortes ordered again D. Pedro to return. In the months before he had unilaterally passed a law that said that the Corte’s rulings would only count in Brazil after they were reviewed by the Brazilian government (him and the Brazilian elites that formed his cabinet). The Cortes said that if he didn’t comply there’d be military invasion. D. Pedro answered that by officially declaring independence. The Brazilian independence war would start then in September 1822.