Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
The usage of DAC
The purpose of the DAC is the opposite of that of the ADC. The DAC converts a digital signal into an analog signal for output.
Remember the music when NyBoard is turned on? It is using PWM to make music sound which uses high-speed switching to adjust the duty cycle to output voltage.
Compared with PWM, the DAC will directly output the voltage without calculating the duty cycle. ESP32 integrates a 2-channel 8-bit DAC with a value of 0-255. The voltage range is 0-3.3V. Therefore, the formula for calculating the output voltage of the DAC is as follows:
The demo is as follows:
Application of ADC which is variable gain on BiBoard (ESP32)
The instructions of ADC on BiBoard
The 34, 35, 36 and 39 pins of the ESP32 module support input only. We configure it as an analog input port on BiBoard, which makes it convenient for developers to connect 4 foot sensors.
The usage of analog input analog-to-digital converter (ADC) on BiBoard is the same as the basic Arduino UNO, but the accuracy is higher (12 bits, UNO is 10 bits), and a programmable gain amplifier is added to make the ADC work in the best range.
When a 1V voltage signal is input, if 12bit access is used according to the normal configuration, the reference voltage is equal to the power supply voltage (3.3V): the corresponding output is 0~1241, a large part of the ADC range will be wasted, resulting in inaccurate data. When we configure the programmable gain, we can make the 1V input signal fill almost the entire ADC range, and the accuracy and resolution are greatly improved.
This demo uses 4 inputs, respectively configured as: 0/2.5/6/11 decibel amplification gain, it should be noted that the default configuration of ESP32 Arduino is 11 decibel amplification gain.
We use "analogSetPinAttenuation(PIN_NAME, attenuation)" to configure the gain of a single input pin, or use "analogSetAttenuation(attenuation)" to configure the gain of all analog input pins.
In the actual test, when the 1V standard voltage is input, the ADC values are: 3850/2890/2025/1050. In future productions, the ADC range can be changed by changing the ADC gain without the replacement of the reference voltage source.
Operate the GPIO port of BiBoard
There is no separate GPIO port on BiBoard, but the multiplexed serial port 2 (pin 16, 17) or the PWM pin of the unused PWM servo interface can be used as GPIO port. The GPIO port is also relatively simple to use. After configuring the input and output mode, the usage is exactly the same as that of Arduino UNO. You can use any IO control program of Arduino UNO, just change the number of IO .
There are 2 serial ports,which are separately located on 2 expansion sockets (P16, P17) ,on BiBoard.
The serial port 1 on the P16 can be connected to the USB downloader and the external serial device. Please do not use the downloader and the external serial device at the same time. The serial port voltage division will lead to communication errors.
In the Arduino demo, Serial represents the serial port 0, Serial1 represents the serial port 1.Serial and Serial1 send to each other.
MPU6050 is the most widely used 6-axis gyroscope, which can not only measure 3-axis angular velocity and 3-axis acceleration more accurately, but also use the built-in digital motion processor (DMP) for hardware based attitude fusion calculation. So novices can use it very conveniently. For this reason, we also use MPU6050 gyroscope.
There are many demos of MPU6050 on Arduino UNO, the most famous is jrowberg's I2Cdev and MPU6050DMP library:
Unfortunately, this library cannot be run directly on BiBoard based on ESP32. We found the ported library on Github, which is easy to use. This library adds the definition of PGMSpace for the ARM and ESP series, adds the calibration function, and removes the FIFO overflow processing function (friends who are interested can use Beyond Compare for code comparison). The library contains I2Cdev and MPU6050, the address and compressed package are as follows:
After the download is complete, create a MPU6050 folder under Documents/Arduino/library, and copy the library files in the compressed package into it. The library of this modified MPU6050 is also compatible with ARM and AVR, so if you have the original I2Cdev and MPU6050 libraries in your computer, you can delete them.
We can use the official MPU6050_DMP6 demo.
The usage of EEPROM is the same as Arduino UNO, there are two operations: read and write.
Read:
I2C address of EEPROM
The internal address of EEPROM (the address for storing data)
Read data
Write:
I2C address of EEPROM
The internal address of EEPROM (the address for storing data)
Write data
In the BiBoard demo, the address of EEPROM on the I2C bus is 0x54, and the capacity is 8192Bytes (64Kbit). We sequentially write a total of 16 values from 0 to 15 in the EEPROM from the first address, and then read them for comparison. Theoretically, the data written in EEPROM and the data read from the corresponding address should be the same.
In the NyBoard factory test, we also use this method, but it is more complicated. We will use a fixed list to fill the EEPROM and read it out for comparison.
Note: the EEPROM operations, especially write operations, are generally not put into the loop() loop. Although the EEPROM is resistant to erasing (100,000 times), if a certain block is frequently written in the loop, It will cause the EEPROM to malfunction.
The sample code mainly demonstrates the mutual forwarding of information between the Bluetooth serial port and the serial port, which is derived from the official demo of ESP32, which is simple and easy to understand. So the description mainly explains the concepts that appear in the code.
At present, the main Bluetooth protocols are divided into two categories, traditional Bluetooth (HS/BR/EDR) based on RFCOMM and Bluetooth low energy (BLE) based on GATT.
Traditional Bluetooth is faster and has many specific application protocols, such as audio-oriented A2DP, Bluetooth serial port SPP, etc. However, the power consumption is high, and access to Apple devices requires MFi (Made For iOS) chips and certification.
Bluetooth Low Energy (BLE) can define various GATT profiles by itself, and it is also equipped with commonly used profiles (such as device information, battery, etc.). It has low power consumption and is widely used. It can be used on Apple devices. The disadvantages is that it is slower than traditional Bluetooth. Bluetooth low energy is mostly used on devices with low data volume but sensitive to power consumption, such as bracelets/smart watches/beacons.
This demo uses the SPP protocol based on traditional Bluetooth, which comes with all serial port protocols. When the computer or Android phone is connected and paired, a serial port number will be automatically generated in the system for communication, and the experience is not much different from that of a normal wired serial port.
The bluetooth low energy serial port will be demonstrated in the next chapter. In essence, it is a profile configured with a serial port and requires host software support.
Bluetooth Low Energy (BLE, Bluetooth Low Energy) serial port pass-through is widely used. On Apple's iOS platform, Classic Bluetooth requires MFi certification to connect with Apple's iOS devices. The Bluetooth low energy device does not have this restriction.
The protocol stack and principle of Bluetooth low energy will not be repeated here, there are many related articles and videos. In short, the bluetooth service is provided in the form of a profile, and there are N characters with independent IDs (UUID) under the profile of each service. Each character has different permissions (read, write, notify, indicate). After the user defines the character and combines it with the authority, a complete service can be provided.
What BLE pass-through is actually to establish a BLE Service, and there are 2 characters under this profile.
One for TX (transmit data) and one for RX (receive data). For this they have different permissions. The following code is to create a new service and character:
Next are two callback functions, which are performed when there is a connection and when there is a write RX character:
Finally, the main loop is the control of the connection, which determines whether there is a connection and whether it is disconnected.
For the complete code, see the example of the official library: ble_uart, and the debugging tool can use LightBlue.
ESP32 File System SPIFFS Configuration Guide
On BiBoard (ESP32), in addition to the regular program area and boot area, we use the file system in the Flash partition.
The role of a file system with independent partitions is as follows:
Save the data at the specified address and will not be deleted due to re-update (such as calibration data, gait data)
No external SD card needed, saving hardware resources
Common file systems include Windows NTFS, exFAT, and Linux log file systems Ext and XFS. But in the embedded field, these large file systems are too large. We use the lightweight SPIFFS (SPI Flash File System), an embedded file system for SPI NOR flash devices, and support functions such as wear leveling and file system consistency checking.
Because of its light weight, the biggest feature of SPIFFS is that it does not support tree directories, that is, all files are stored in the same layer. SPIFFS provided by ESP32 has the following features:
Currently, SPIFFS does not support directories, it produces a flat structure. If SPIFFS is mounted under /spiffs, then creating a file with the path /spiffs/tmp/myfile.txt will create a file called /tmp/myfile.txt in SPIFFS, instead of myfile.txt in the directory /spiffs/tmp.
It is not a real-time stack. One write operation might take much longer than another.
For now, it does not detect or handle bad blocks.
You can create/save and delete files with your own Arduino code, but the operation is cumbersome. You need to put data or even binary files into Arduino Sketch and create files by running the program.
However, there is a very useful tool that can directly upload files from the computer to the file system. Although it is slightly more troublesome than the "drag-and-drop" copy of the "removable storage", whether it is MP3 audio or HTML web files, all can be easily uploaded to flash memory. Let's learn how to use this plugin.
Please install Arduino IDE (Version: 1.8.* ) and the ESP32 support package of Arduino IDE (refer to Chapter 3.2.1 of the BiBoard Quick Start Guide.).
Download the compressed package of the ESP32FS plug-in at:
Go to the "Arduino" directory and open the "tools" folder
C:\Users\{YourUserName}\Documents\Arduino\tools
Unzip the downloaded .zip folder to the Tools folder. You should have a similar folder structure:
C:\Users\{YourUserName}\Documents\Arduino\tools\ESP32FS\tool\esp32fs.jar
Finally, restart the Arduino IDE.
To check whether the plug-in has been successfully installed, open the Arduino IDE. Select your ESP32 development board (ESP32 Dev Module), go to "Tools", and then check if there is an "ESP32 Sketch Data Upload" option.
To upload files to the ESP32 file system, follow the steps below:
Create an Arduino project (e.g.: Test.ino) and save
To open the project directory, you can use the "Sketch - Show Sketch Folder" option
Inside this folder, create a new folder named "data"
In the "data" folder, you should put the file which you want to save into the SPIFFS file system. e.g., create a .txt file that contains some text named "test_example". as following:
Please click "Tools - ESP32 Sketch Data Upload" in the Arduino IDE
When you see the "SPIFFS Image Uploaded" prompt message, the file has been successfully uploaded to the SPIFFS partition.
The demo of the file system SPIFFS_Test.ino(C:\Users\{YourUserName}\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.*\libraries\SPIFFS\examples\SPIFFS_Test
)comes from the official ESP32 without modification. The code implements the basic operation of "addition, deletion, modification, and check", and provides an SPI flash IO test program.
If necessary, it is recommended to directly use the code of the demo to operate ESP32 SPIFFS.
BiBoard is equipped with an infrared sensor, which is connected to the 23rd pin. The use of infrared is exactly the same as which is on Arduino UNO based on AVR.
First download the 2.6.1 version of the IRremote library, you need to manually select the 2.6.1 version. Because the infrared-related codes have changed in later versions, if you use the 3.X version, the commands will not be translated. In order to be compatible with our previous products, we decided to use the 2.6.1 version after testing.
When using NyBoard, in order to ensure that the code can be compiled smoothly, we need to remove unnecessary code in the IRremote library, that is, remove the encoder/decoder that we don't use, and only keep the NEC_DECODER, which is the 38KHz signal decoder in NEC format.
Due to the flash memory capacity of BiBoard is “huge”, we don’t need to remove unnecessary code in the IRremote library.
Finally, a demo is attached, which accepts infrared signals and prints via the serial port. You can also use official demo for testing.
The flash memory of the ESP32 board has 16M, and the range of the storage address expressed in hexadecimal is 0x0-0x01000000.
This is the partition table that has been configured by the system, as shown in the figure below:
The storage location of this partition table file on the computer:
C:\Users\{YourUserName}\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.*\tools\partitions\large_spiffs_16MB.csv
It can be seen from the above partition table: APP0 area and APP1 area are 4.5M each; the data area is SPIFFS, and the size is 6.9M.
But in the Arduino IDE, this configuration is not included in the hardware partition configuration options of the ESP32 Dev Module:
We need to add this configuration to the ESP32 Dev Module.
Open the development board configuration file:
C:\Users\{YourUserName}\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.*\boards.txt
Locate the name of the development board: esp32.name=ESP32 Dev Module, as shown in the figure below:
The line of text in the ESP32 Dev Module partition configuration in the configuration file:
Add the following 3 lines of text below this line:
The following explains the meaning of the three lines of text:
The name of the ESP32 partition configuration, we named it BiBoard V0 (4.5M APP with OTA /6.9 MB SPIFFS), or it can be replaced with other names you are familiar with.
The partition configuration file information is the file large_spiffs_16MB.csv . You can also write a partition file to adjust the file size of the APP and data area.
This line of text specifies that the maximum upload program size is 4685824 bytes.
Let's try to compile a simple program to test whether the above configuration is set successfully.
Reopen the Arduino IDE, we can see the BiBoard just configured:
After compiling the program, the result is as shown in the figure below:
Compilation is complete, using 213KB of Flash (4%), and the maximum usable size is 4,685,824 bytes.
In this passage, “4685824 bytes” is specified in the third line of text just added to the configuration file.
If you use Arduino IDE 2.0.*, the partition option may not appear automatically. To fix:
Select File > Quit from the Arduino IDE menu to close all active Arduino windows and quit the process.
Delete the "User data" folder:
Windows:
Linux:
macOS:
Restart the Arduino IDE.
The custom board options menus should now reflect any changes that were made to boards.txt
.
So far, you have completed the configuration of the development board with the largest flash memory space in Arduino IDE.
The ESP32 used by BiBoard is different from the 328P used by UNO. Because the PWM of ESP32 uses the matrix bus, it can be used on unspecified pins.
The PWM of ESP32 is called LED controller (LEDC). The LED PWM controller is mainly used to control LEDs, and it can also generate PWM signals for the control of other devices. The controller has 8 timers, corresponding to 8 high-speed channels and 8 low-speed channels, totaling 16 channels.
Compared with UNO, directly use "analogWrite()" to input any duty ratio between 0-255. The PWM control of ESP32 on BiBoard is more troublesome. The parameters that need to be controlled are as follows:
Manual selection of PWM channels (0-15) also improves the flexibility of the use of pins
The number of bits of the PWM waveform determines the resolution of the duty cycle of the PWM waveform. The higher the number of bits, the higher the accuracy.
The frequency of the PWM waveform determines the speed of the PWM waveform, the higher the frequency, the faster the speed.
The frequency of the PWM waveform and the number of bits are relative, the higher the number of bits, the lower the frequency. The following example is quoted from the ESP32 programming manual:
For example, when the PWM frequency is 5 kHz, the maximum duty cycle resolution can be 13 bits. This means that the duty cycle can be any value between 0 and 100%, with a resolution of ~0.012% (2 ** 13 = 8192 discrete levels of LED brightness).
The LED PWM controller can be used to generate high-frequency signals, enough to clock other devices such as digital camera modules. Here the maximum frequency can be 40 MHz, and the duty cycle resolution is 1 bit. In other words, the duty cycle is fixed at 50% and cannot be adjusted.
The LED PWM controller API can report an error when the set frequency and duty cycle resolution exceed the hardware range of the LED PWM controller. For example, if you try to set the frequency to 20 MHz and the duty cycle resolution to 3 bits, an error will be reported on the serial port monitor.
As shown above, we need to configure the channel, frequency and number of bits, and select the output pin.
Step 1: Configure the PWM controller
Step 2: Configure the PWM output pins
Step 3: Output PWM waveform
In the demo, we choose IO2 as the output pin, connect IO2 to an LED, and you can observe the effect of the LED breathing light.
There is a demo named testMP3 in the "OpenCatEsp32/ModuleTests" file directory. The main function of this demo is to play .mp3 files stored in the SPIFFS file system. Users can choose to play different .mp3 files by inputting 0~6 numbers (.mp3 file index number) in the serial monitor.
Before compiling the demo, please (, development board esp32 2.0.*, library , ).
After compiling, use the latest SPIFFS file upload plugin to upload the .mp3 file to the SPIFFS file system partition of BiBoard. For details, please refer to the chapter .
Note:
Arduino IDE 2.0 cannot add the large_spiffs_16MB (4.5MB APP with OTA/6.93MB SPIFFS) configuration option currently.
The in Arduino IDE 1.8.* is written in Java. And Arduino IDE 2.0 is written in a different language (TypeScript + Golang), so the previous upload plugin cannot be used in Arduino IDE 2.0. There is no support for the Arduino IDE 2.0 SPIFFS file upload plugin currently.
So it is recommended that you temporarily install and use the to upload sketch and .mp3 file.
Of course, you can also use VS Code + PlatformIO to upload the sketch and .mp3 files. For details, please refer to the following documents:
Before uploading the sketch and .mp3 files, please move the "data" folder to the project root directory, store the code files in the "src" directory, and configure the partition option "board_build.partitions" in the platformio.ini in the project root directory, as shown in the following figure:
The Arduino demo of ESP32 provides the function of OTA (updating/uploading a new program to ESP32 using Wi-Fi)
The configuration of our BiBoard is 16MB Flash, and the specific partitions are as follows:
OTA mainly operates OTA data areas, namely APP1 and APP2 areas. The principle is:
BiBoard runs the firmware with OTA function, at this time, the boot points to the APP1 area.
The OTA command is sent to ESP32 via Wi-Fi, and the binary file of the upgrade program is transferred to the APP2 area.
If the transmission of APP2 is completed and the verification is successful, OTAdata points to the APP2 area, and the next time it starts from the updated firmware area (APP2), the APP1 data is retained. Next time, OTA will write to APP1 area to overwrite the old firmware.
If the transmission of APP2 is not completed due to a network transmission error, because APP2 has not passed the verification, OTAdata does not point to the APP2 area. The program in the APP1 area will still be executed after the reset is started, and the damaged APP2 area will be completely erased and overwritten during the next OTA.
In the demo, first configure WiFi, and configure the WiFi mode as STA (Station, base station mode). Enable the WiFi function and pass in the account password "WiFi.begin(ssid, password);"
When the Wi-Fi is successfully connected, the IP address will be printed via the serial port; if the connection is wrong, the ESP32 will restart.
In the demo, you can configure the port number, the OTA key or the hash of the key, and the area and type of the OTA (commented by default).
The following are several code snippets similar to callback functions, which are used to judge the state of each stage of OTA.
After configuring according to the demo, call "ArduinoOTA.handle();" in the loop function. The following analogWrite function is to distinguish the effects of different firmware updates (by changing the value).
The first time you use the serial port to download, the python tool "esptool" is called. You can use OTA after the download is complete. In the port options, you will find an extra port based on the IP address, which is the OTA address.
Select this address, the lower right corner is the IP address of ESP32 Dev Module on your BiBoard (192.168.1.178)
At the same time, a warning will pop up: "Serial monitor is not supported on network ports such as 192.168.1.178 for the ESP32 Dev Module in this release".
The ESP32 OTA of Arduino is only suitable for updating the program and cannot complete the serial port debugging work. If you need to debug BiBoard, please connect the USB-C interface.
Download the program, as shown in the figure.