- Internet of Things Projects with ESP32
- Agus Kurniawan
- 416字
- 2021-06-24 16:02:52
Demo 2 - making an Arduino Sketch program with ESP32
In this section, we develop an Arduino program for ESP32 boards. We will use the previous demo, but we still use Arduino software. If you don't have experience with Arduino Sketch, I recommend learning Sketch programming on this site: https://www.arduino.cc/reference/en/. Since ESP32 has two cores (core 0 and core 1), our Arduino program runs one core. You don't need to worry which about core will be used by Arduino. You can verify which core is used with the xPortGetCoreID() function.
- We use the pinMode() function to set ESP32 GPIO pins as input or output. Then, we can write digital values using the digitalWrite() function. Using the previous demo, we can implement the demo using Sketch, as follows:
#define LED1 12
#define LED2 14
#define LED3 26
- Let's set the current_let to 1. That meant that later will start turning the LEDs with LED number 1.
int current_led = 1;
- Every program developed with the help of Arduino IDE will contain the setup() function. Code in setup function will run only on time at the beginning. For now let’s setup the pins that will drives our LEDS as output pins.
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
}
- This is a helper function that will get a led number as parameter and will turn off all the LEDS and based on the value of the input parameter will turn on that LED by using the digitalWrite() function.
void turn_on_led(int led)
{
// turn off all leds
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
switch(led)
{
case 1:
digitalWrite(LED1, HIGH);
break;
case 2:
digitalWrite(LED2, HIGH);
break;
case 3:
digitalWrite(LED3, HIGH);
break;
}
}
- The code in the loop() function will run continuously like in a while(1). For now the code will turn on one LED every second. When the code reach the last LED then will go back to the first one and the process will run forever.
void loop() {
turn_on_led(current_led);
delay(1000);
current_led++;
if(current_led>3)
current_led = 1;
}
- Save the program.
Now, you can set the ESP32 board target and its port, as shown in the following screenshot:

Now, you can compile and upload the Sketch program via Arduino software. If you succeed, you can see the program output as shown in the following screenshot:

If you still get errors, please verify your ESP32 board type and its serial port.
- 24小時學(xué)會電腦組裝與維護(hù)
- Istio入門與實戰(zhàn)
- 計算機(jī)組裝與系統(tǒng)配置
- BeagleBone By Example
- 電腦常見故障現(xiàn)場處理
- scikit-learn:Machine Learning Simplified
- 電腦維護(hù)365問
- 嵌入式系統(tǒng)中的模擬電路設(shè)計
- Mastering Adobe Photoshop Elements
- Blender Quick Start Guide
- 單片機(jī)項目設(shè)計教程
- 微服務(wù)實戰(zhàn)
- 創(chuàng)客電子:Arduino和Raspberry Pi智能制作項目精選
- 基于S5PV210處理器的嵌入式開發(fā)完全攻略
- Nagios系統(tǒng)監(jiān)控實踐(原書第2版)