Introduction
This tutorial introduces the basics of using FireBeetle 2 ESP32-C6 and Beetle ESP32-C6.
Click here to find more details about FireBeetle 2 ESP32-C6.
Click here to find more details about Beetle ESP32-C6.
Click here to view the advanced tutorial for FireBeetle 2 ESP32-C6 and Beetle ESP32-C6.
Click here to look for information about Arduino programming.
Battery voltage measurement
Voltage calculation code
void setup() {
// initialize serial communication at 115200 bits per second:
Serial.begin(115200);
//set the resolution to 12 bits (0-4096)
analogReadResolution(12);
}
void loop() {
// read the analog / millivolts value for pin 0:
int analogValue = analogRead(0);
int analogVolts = analogReadMilliVolts(0);
// print out the values you read:
Serial.print("ADC analog value = ");
Serial.println(analogValue);
Serial.print("ADC millivolts value = ");
Serial.print(analogVolts);
Serial.println("mV");
// Please adjust the calculation coefficient according to the actual measurement.
Serial.print("BAT millivolts value = ");
Serial.print(analogVolts * 2);
Serial.println("mV");
Serial.println("--------------");
delay(500);
}
PWM Output
The PWM function of ESP32 needs to be declared in advance.
ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution)
Description: set the PWM signal properties
Parameter:
- pin: tselect LEDC pin.
- freq: select frequency of pwm.
- resolution: select resolution for LEDC channel.(range is 1-14 bits)
ledcWrite(uint8_t pin, uint32_t duty)
Description:output PWM signal
Parameter:
- pin: select LEDC pin.
- duty: select duty to be set for selected LEDC pin.
Sample Code
The ESP32 PWM can be freely mapped to other ports to output. The sample code below demonstrates the steps for configuring that. The LED will show a fade-in/fade-off effect when everything is set up.
/*LED fading example
*
*/
const int ledPin = 15; // Actual LED pin for outputting PWM signal
//Setting PWM properties
const int freq = 5000;//PWM frequence
const int resolution = 8;//8-bit resolution
void setup(){
ledcAttach(ledPin, freq, resolution);
}
void loop(){
//LED fade in
for(int dutyCycle = 0; dutyCycle <= 100; dutyCycle++){
// changing the LED brightness with PWM
ledcWrite(ledPin, dutyCycle);
delay(15);
}
//LED fade off
for(int dutyCycle = 100; dutyCycle >= 0; dutyCycle--){
// changing the LED brightness with PWM
ledcWrite(ledPin, dutyCycle);
delay(15);
}
}
/*LED fading example
*
*/
const int ledPin = 15; // Actual LED pin for outputting PWM signal
void setup(){
}
void loop(){
//LED fade in
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
analogWrite(ledPin, dutyCycle);
delay(15);
}
//LED fade off
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
analogWrite(ledPin, dutyCycle);
delay(15);
}
}
Interrupt
The interrupt of ESP32 can be freely allocated.
pinMode(GPIO,INPUT_PULLUP);
Description: external interrupt pin definition
Parameters:- GPIO: the pin that ESP32 is going to use as an interrupt
- INPUT_PULLINGUP: set as pull-up mode
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
Description: External interrupts
Parameters:- pin: the Arduino pin number.
- ISR: the ISR to call when the interrupt occurs; this function must accept no parameters and return nothing. This function is sometimes called an an interrupt handler.
- mode: defines when the interrupt should be triggered. Three constants are predefined as valid values.
- CHANGE: trigger the interrupt pin when the pin level changes
- RISING: trigger the interrupt pin when the pin level changes from low to high
- FALLING: Trigger the interrupt pin when the pin level changes from high to low
detachInterrupt(digitalPinToInterrupt(pin))
Description: disable the given interrupt.
Parameters:- pin: the interrupt pin to be disabled
Interrupts ()
Description: Re-enables interrupts (after they’ve been disabled by nointerrupts(). Interrupts allow certain important tasks to happen in the background and are enabled by default. Some functions will not work while interrupts are disabled, and incoming communication may be ignored. Interrupts can slightly disrupt the timing of code, however, and may be disabled for particularly critical sections of code.noInterrupts ()
Description: Disables interrupts (you can re-enable them with interrupts()). Interrupts allow certain important tasks to happen in the background and are enabled by default. Some functions will not work while interrupts are disabled, and incoming communication may be ignored. Interrupts can slightly disrupt the timing of code, however, and may be disabled for particularly critical sections of code.
Serial Port
Mapping is required for initializing ESP32 serial port. Available ports: Serial, Serial1, Serial2
- Serial1.begin(baud, config, rxPin, txPin);
Description: Serial1 initialization
Parameters:- baud: baud rate
- config: data bit and stop bit setting
- rxPin: RX pin
- txPin: TX pin
Serial1.begin(9600,SERIAL_8N1,/*rx =*/0,/*Tx =*/1);
Servo
ESP32 doesn't support driving servo through the Servo library. Please search and install the ESP32Servo library in Sketch->Include Library->Manage Library to drive servo.
SD Library
SD Class
begin(cspin) Description: Initializes the SD library and SD card. This begins use of the SPI bus (digital pins 11, 12, and 13 on most Arduino boards; 50, 51, and 52 on the Mega) and the chip select pin, which defaults to the hardware SS pin (pin 10 on most Arduino boards, 53 on the Mega). Note that even if you use a different chip select pin, the hardware SS pin must be kept as an output or the SD library functions will not work. Parameter:
- cspin:the Arduino pin connected to the chip select line of the SD card.
Return: boolean type. True on success; false on failure
exists() Description: Test whether a file or directory exists on the SD card. Syntax: SD. exists(filename) Parameter:
- filename: the name of the file to test for existence, which can include directories (delimited by forward-slashes, /)
Return: boolean type, true if the file or directory exists, false if not
open()
Description: Opens a file on the SD card. If the file is opened for writing, it will be created if it doesn't already exist (but the directory containing it must already exist). Syntax: SD.open( filename) SD.open(filename,mode) Parameter:- filename: the file name to open, which can include directories (delimited by forward slashes, /)
- mode (optional): the mode in which to open the file, defaults to FILE_READ - byte.
- FILE_READ: open the file for reading;
- FILE_WRITE: open the file for reading and writing.
Return: a File object referring to the opened file: if the file couldn't be opened, this object will evaluate to false in a boolean
remove() Description: Remove a file from the SD card. If the file didn't exist, the return value is unspecified, so it is better to use SD. Exists (file name) to detect whether the file exists before removing the file. Syntax: SD. remove( filename) Parameter:
- filename:the name of the file to remove, which can include directories (delimited by exclamatory mark, !)
Return: boolean type. True if the removal of the file succeeded, false if not.
mkdir(filename) Description: Create a directory on the SD card. Parameter:
- filename: the name of the directory to create, with sub-directories separated by forward-slashes, /
Return: boolean type. True if the creation of the directory succeeded, false if not.
rmdir(filename) Description: Remove a directory from the SD card. The directory must be empty. Syntax: SD.rmdir( filename) Parameter:
- filename: the name of the directory to remove, with sub-directories separated by forward-slashes, /
Return: booleantype. True if the removal of the directory succeeded, false if not.
File Class
The file class provides the function of reading/writing files. The function of this class is very similar to that of the serial port related functions used before. The member functions are as follows.
available()
Description: Check if there are any bytes available for reading from the file. Syntax: file. available()
Parameter:- file: an instance of the File class
Return: the number of bytes available
close()
Description: Close the file, and ensure that any data written to it is physically saved to the SD card. Syntax: file. close()
Parameter:- file: an instance of the File class
Return: none
flush()
Description: Ensures that any bytes written to the file are physically saved to the SD card. This is done automatically when the file is closed. Syntax: file.flush
Parameter:- file: an instance of the File class
- Return*: none
peek() Description: Read a byte from the file without advancing to the next one. Parameter:
- file: an instance of the File class
Return: The next byte (or character), or -1 if none is available.
position( )
Description: Get the current position within the file (i.e. the location to which the next byte will be read from or written to). Syntax: file. position()
Parameter:- file: an instance of the File class
Return: the position within the file
print()
Description: Print data to the file, which must have been opened for writing. Syntax: file. print(data)file. print(data,BASE)
Parameter:- file: an instance of the File class
- data:the data to print (char, byte, int, long, or string)
- BASE(optional): the base in which to print numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).
Return: byte number to be transmitted.
println()
Description: Print data, followed by a carriage return and newline, to the File, which must have been opened for writing. Syntax: file. println(data) file.println(data,BASE)
Parameter:- file: an instance of the File class
- data (optional): the data to print (char, byte, int, long, or string)
- BASE (optional): the base in which to print numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).
Return: byte number to be transmitted.
seek()
Description: Seek to a new position in the file, which must be between 0 and the size of the file (inclusive). Syntax: file. seek( pos)
Parameter:- file:an instance of the File class
- pos: the position to which to seek
Return: true for success, false for failure (boolean)
size()
Description: Get the size of the file. Syntax: filue. size()
Parameter:- file:an instance of the File class
Return: the size of the file in bytes
read()
Description: Read from the file. Syntax: file.read Parameter:- file:an instance of the File class
Return: The next byte (or character), or -1 if none is available.
write()
Description: Write data to the file.
Syntax: file. write(data)file. write(buf,len)
Parameter:- file:an instance of the File class
- data:the byte, char, or string (char*) to write
- buf: an array of characters or bytes
- len:the number of elements in buf
Return: the number of bytes written
isDirectory()
Description: Reports if the current file is a directory or not Syntax: file.isDirectory()
Parameter:- file: an instance of the File class
Return:boolean. True if the file is a directory, false if not
openNextFile()
Description: Reports the next file or folder in a directory. Syntax: file.openNextFile()
Parameter:- file: an instance of the File class that is a directory
Return: the next file or folder in the path
rewindDirectory()
Description: Back to the first file in the directory Syntax: file.rewindDirectory()
Parameter:- file: an instance of the File class.
Return: None
Please refer to File-Examples-SD-SD Test.
FAQ
1. What will cause burning error?
- There is no delay or too short delay in Loop.
The USB cannot be recognized by the PC as some functions are incorrectlly called.
How to solve
- Press Boot, press RST and release both, then try burning again.
2. Data cannot be printed on serial port
- Check if the USB CDC is enabled
- Check print information using other serial debugger.
For any questions, advice or cool ideas to share, please visit the DFRobot Forum.