SKU_DFR1075_FireBeetle_2_Board_ESP32_C6_Basic_Tutorial-DFRobot

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.

More APIs

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.

Serial Port

Mapping is required for initializing ESP32 serial port. Available ports: Serial, Serial1, Serial2

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

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.

Please refer to File-Examples-SD-SD Test.

FAQ

1. What will cause burning error?

How to solve

2. Data cannot be printed on serial port

For any questions, advice or cool ideas to share, please visit the DFRobot Forum.

More Documents