3. ESP32 development board NodeMcu-32S lighting and button control

LED circuit analysis

(After reading the article, if you need code or schematics and other documents, you can leave a message in the mailbox below)

In this issue, before we turn on and off the LED on the board, we still need to analyze the circuit of the LED on the board so that we can control it.
Insert picture description here
We zoom in to see the circuit of the LED part.

Insert picture description here
One end of the LED is connected to the GPIO2 of the ESP32 module through a current-limiting resistor, and the other end is grounded. Then, if there is a voltage difference between the positive and negative ends of our GPIO2 port output, the light will be on, and the output low level is 0V at both ends. Lights off

LED programming

Those who have been exposed to arduino programming may be familiar with the interface. If not, you can refer to the GPIO control function in the arduino community to view the function.

Light up the LED

Controlling the LED light can also be said to be the process by which we control the state of the GPIO port. In the process of controlling the level change of the GPIO port, we need to use the following functions

pinMode(pin, mode)

Function: Set the state of the GPIO port

The parameter pinspecifies the GPIO port that needs to be set. The
parameter modespecifies the mode of the GPIO port. The following three parameters are available
INPUT: Input mode
OUTPUT: Output mode
INPUT_PULLUP: Input pull-up mode

digitalWrite(pin, value)

Function function: You can use this function to make the GPIO port output high or low level

The parameter pinspecifies the GPIO port whose level needs to be set. The
parameter valuespecifies the level status of the port output. The following two parameters are available
HIGH: high level
LOW: low level

Next, we open the arduino IDE and start writing our first program. Click the downward arrow in the figure below to save the program. I saved it to the program folder in the data.
Insert picture description here
There are two projects under the default project below. function void setup()andvoid loop()

void setup()It is the program initialization function, which will be executed once when the power is turned on. After the execution is completed, the program will enter the loop()function and no longer execute setup()until the program restarts again.

void loop()As a function of cycle executed after executing the program setup()after the initialization function is executed, loop()the program code will complete execution after time from loop()the code in the beginning of the re-run, this means that we will always function in the code of the implementation cycle continues

Our light only needs to execute the state of GPIO2 once, and the program is relatively simple. We refer to the programming ideas below

Insert picture description here

The final code is as follows

void setup() {
    
    
  // put your setup code here, to run once:
  pinMode(2,OUTPUT);  //配置GPIO2端口模式为输出模式
  digitalWrite(2,HIGH);  //配置GPIO2端口为高电平,灯亮
}

void loop() {
    
    
  // put your main code here, to run repeatedly:

}

Next, we select the corresponding development board and the corresponding port and click the arrow in the upper right direction to burn, which is the same as the previous burning

Insert picture description here
After burning, you can see the blue light on the board will light up
Insert picture description here

LED blinking

After we light up the LED, we further implement the LED flashing program. The flashing program contains the code to turn on and off. We have another function here to control the speed of the LED flashing. You can also find the reference function in the arduino community.

delay( ms)

Function function: delay

The parameter is the msduration, the unit is milliseconds, and the data typeunsigned long

We write a program that the light flashes once a second, and the flashing time delay()is controlled by a function. For the program, we refer to the following ideas to write

Insert picture description here

Next, the new project, save, program editing, and code download are the same as the part where we light up the LED. We save the program to the data and the project name is LED_twinkle

code show as below:

void setup() {
    
    
  // put your setup code here, to run once:
  pinMode(2,OUTPUT);  //将GPIO2配置为输出模式
}

void loop() {
    
    
  // put your main code here, to run repeatedly:
  digitalWrite(2,HIGH);  //GPIO2输出高电平 灯亮
  delay(500);  //保持GPIO2高电平500毫秒,即灯亮500ms
  digitalWrite(2,LOW);  //GPIO2输出低电平 灯灭
  delay(500);  //保持GPIO低电平500毫秒,即灯灭500ms
}

After burning the program, you can see the blue light of the board flashing

Insert picture description here

Button control LED on and off

Next, we control the LED light by pressing the button, the light is on when the button is pressed, and the light is off when the button is released.

Just like controlling the LED lights, before using the buttons on the board, let’s look at the buttons in the schematic.
Insert picture description here

It can be seen that the button in the schematic diagram is grounded. VCCAfter the resistor is pulled up , it is connected to the GPIO0port. When the button is not pressed, the GPIO0port is pulled up by the resistor 3.3V, and the port level is high. After pressing the button, press the button the GPIO0down to GNDthe port level is low, we only need to detect the GPIO0level of the port state can determine whether the button is pressed
on GPIO0mode of the port is configured to the configuration we ensure that the pull-input ports can be pulled low level
since it is detected The level input state of the port, we need to use a detection function

digitalRead(pin)

Function function: read the pin status, and return the status value, return 1 is high level, 0 is low level

The parameter pinis the port whose status needs to be detected

Then write the program according to the thinking below

Insert picture description here

New project, save, program editing, we save the program to the data, the project name is key_LED

code show as below

void setup() {
    
    
  // put your setup code here, to run once:
  pinMode(2,OUTPUT);  //配置LED所在端口为输出模式
  pinMode(0,INPUT_PULLUP);  //配置按键所在端口为上拉输入模式
}

void loop() {
    
    
  // put your main code here, to run repeatedly:
  if(!digitalRead(0))  //如果按键按下,即GPIO0的端口状态为低电平
  {
    
    
    digitalWrite(2,HIGH);  //灯亮
    }
  else
  {
    
    
    digitalWrite(2,LOW);  //灯灭
    }
}

Insert picture description here

to sum up

In this issue, learn how to control the output level of the GPIO port on the ESP32 through a program that controls the LED lights, and learn how to read the input state of the GPIO port through a key-press program, and simply learn the level control of the GPIO port by the ESP32. Functions such as serial port, time control, etc. can be found in the function reference of the arduino community . In the next article, we will learn about the wifi of ESP32 and the opening, closing and connection of the hotspot function. The following is the
Insert picture description here
information required for the function reference interface in arduino , Program files can be chatted privately or leave a message in the mailbox below

Guess you like

Origin blog.csdn.net/qq_42250136/article/details/108579350