Introduction: Introduction to ESP32 Programming

About: Do you like technology? Follow my channel on Youtube and my Blog. In them I put videos every week of microcontrollers, arduinos, networks, among other subjects.

Today’s video will give an introduction to programming ESP32 using the Arduino IDE. I'll show you here how to program an ESP32, with 19-pins on each side, as an Arduino. But also, I’ll show the differences between this type of microcontroller and the Arduino itself. So, in addition to introducing ESP32 using the Arduino IDE, I'll give you a practical example of how to do a digital reading and digital performance.

Step 1: Demonstration

Step 2: Resources Used


1 push-button type switch

1 10k ohm resistor

1 relay module

1 Protoboard

Step 3: What Is ESP32?

The ESP32 is a series of Soc's (System on Chip), developed by Espressif Systems. They contain a Tensilica Xtensa LX6 microprocessor, WiFi, and integrated Bluetooth.

It was developed to have low consumption and low cost, and it is a very interesting option for makers or developers of products.

It can be found in some variations, from the chip going through modules and development boards.

To begin, the use of development boards tends to be the best option, since it involves the minimal amount of electronics for maintenance of the ESP32, which (although very small) is already available.

There is also the ease of using USB-Serial converters on these development boards that make the communication process with ESP32 much easier.

Development boards can also be found on a wide range of models, some very similar and others with very specific facilities.

Step 4: The Development Board With ESP WROOM 32

For this lesson, we will use the NodeMCU ESP WROOM 32 development board.

It is important that when using a development board, we pay attention to how this board provides the pins of the module for external use in its pin headers.

Step 5: Circuit

In this circuit, we will detect the push of a button through the GPIO18 (pin 36 of the board) and activate the relays of the module through the GPIO32 and 33 (pins 7 and 8) respectively.

Step 6: ​Source Code

Source Code: Statements

I set the pin 18 to the button. Relay modules 32 and 33 are connected to the relay modules. I have here the variable to store the state of the second relay.

//Pinos ligado no botão
int btn1 = 18; //Pinos que são ligados no módulo de relés int rele1 = 33; int rele2 = 32; //variável para guardar o estado do segundo relé boolean rele2_Ativo = false;

Source Code: Setup ()

The setup () function runs once before the Loop. We set the button pins as input and the relay pins as output. We then open the serial port, setting the data rate to 9600 bps.

//A função setup() é executada uma vez antes do loop
void setup() { //Seta os pinos dos botões como entrada pinMode(btn1, INPUT); //Seta os pinos dos relés como saída pinMode(rele1, OUTPUT); pinMode(rele2, OUTPUT); //Abre a porta serial, definindo a taxa de dados para 9600 bps Serial.begin(9600); }

Source Code: Loop ()

Here, the loop () function is executed after setup and is repeated continuously. If button 1 is pressed, the message will be displayed in quotation marks on the serial monitor as "Button pressed." We turn on relay 1 and invert the state of variable rele2_Active. We change the state of relay 2, wait 1 second (1000 ms), and turn off relay 1.

//A função loop() é executada após o setup e é repetida continuamente
void loop() { if (digitalRead(btn1) == HIGH) //Caso o botão 1 foi pressionado { //Exibe no monitor serial a mensagem entre aspas Serial.println("Botão pressionado"); //Ligamos o relé 1 digitalWrite(rele1, HIGH); //Invertemos o estado da variável rele2_Ativo rele2_Ativo = !rele2_Ativo; //Mudamos o estado do relé 2 digitalWrite(rele2, rele2_Ativo ); //Aguardamos 1 segundo (1000 ms) delay(1000); //Desligamos o relé 1 digitalWrite(rele1, LOW); } }

Step 7: Doing the UpLoad of Code

With the IDE open, open the file with the source code by double clicking on the .ino file or through the File menu.

With ESP32 connected to USB, select the menu Tools => Board: "ESP32 Dev Module"

Still in the Tools menu, select the COM port on which the ESP32 is connected

Click the UPLOAD button. The IDE will compile the code and attempt to start the connection to the ESP32

When the message "Connecting ....._____.....___" appears; press the IO0 button on the ESP32 to enable it for recording.

After recording, we will now check the execution, click the button to open the Serial Monitor, and then set the baud rate to 9600.

Press the button, in addition to activating the relays, and the following message should appear on the Serial Monitor

Step 8: Files

Download the files: