Example Code for Arduino-GPIO

Last revision 2026/01/31

This article provides comprehensive example codes for Arduino GPIO usage, focusing on digital write/read, analog read, and PWM output with the FireBeetle 2 ESP32-E, enabling users to control LEDs and read sensor values.

1. Digital Write Pin

Hardware Preparation

Wiring Diagram

DFR0654-LED wiring diagram

Sample Code

When the digital pin outputs high, the LED turns on.

int ledPin = D7; //Define LED pin 
void setup(){
   pinMode(ledPin, OUTPUT); 
   digitalWrite(ledPin, HIGH);
}

void loop(){
}

Result

When the program is uploaded, the blue LED lights up.

2. Digital Read Pin

Hardware Preparation

Wiring Diagram

DFR0654-Button wiring diagram

Sample Code

The on-board LED lights up when the button is pressed, and turns off when released.

int buttonPin = D7;   //Define button pin  
int ledPin = D9;     //Define LED pin 
int buttonState = 0;  //Variable for reading button statu

void setup(){
   pinMode(buttonPin, INPUT); 
   pinMode(ledPin, OUTPUT);
}

void loop(){
   buttonState = digitalRead(buttonPin); //Read the status of button value 

   if(buttonState == HIGH){    //If the button is pressed 
     digitalWrite(ledPin,HIGH);
   } else{
     digitalWrite(ledPin,LOW);
   }
}

Results

When the program is uploaded, press down the button connected to pin D7, the on-board LED turns on; Release, it goes off.

3. Analog Read Pin

Hardware Preparation

Wiring Diagram

DFR0654-ADC wiring diagram

Other Preparation Work

Analog-to-Digital converters (ADC) translate analog signals, real world signals like temperature, pressure, voltage, current, distance, or light intensity, into a digital representation of that signal. This digital representation can then be processed, manipulated, computed, transmitted or stored. FireBeetle 2 ESP32-E has a 12-bit ADC with a max output of 4095.

Note: The ADC on ESP32 can only measure around 2.5V instead of 3.3V, as shown in the example below.

Sample Code

The program below can be used to read the sensor module connected to the pin IO15/A4, and then print out the real-time analog angle sensor readings and the detected voltage on serial monitor.

int sensorPin = A4;   //  Define analog angle sensor pin
int sensorValue = 0;

void setup(){
   pinMode(sensorPin, INPUT); 
   Serial.begin(9600);   //Initialize serial port
}

void loop(){
   sensorValue = analogRead(sensorPin);    //Read the sensor value on analog pin A4           
   Serial.printf("sensor value: %d\n", sensorValue);  //Print the read sensor value  
   Serial.printf("voltage: %.3fV\n", sensorValue * 3.26 / 4095);  //Print the detected voltage
   delay(100);
}

Result

Open the serial monitor and you can see the current value from the angle sensor printed in it. When the angle sensor is rotated, the printed value changes accordingly. It can be observed that when the sensor value is 4095, the detected voltage is about 3.3V.

DFR0654-ADC result

4. PWM Output (Analog Write)

Hardware Preparation

Other Preparation Work

PWM (Pulse Width Modulation) is a very effective technique to control analog circuits using the digital output of MCU. It is widely used in many fields such as lighting control, motor speed control, measurement, communication, power control and conversion.

The PWM controller of ESP32 has 16 independent channels, which can be configured to generate PWM signals with different properties. All pins that can be used as outputs can serve as PWM pins (GPIO 34 to 39 cannot generate PWM).

In the Arduino IDE, PWM output is defined as analog output. When using Arduino for PWM LED dimming, you should follow the steps below:

  1. First, select a PWM channel. There are 16 channels from 0 to 15.

  2. Then, set the PWM signal frequency. For LEDs, a frequency of 5000 Hz is suitable.

  3. Set the duty cycle resolution of the signal, ranging from 1 to 16 bits. Here an 8-bit resolution is used. And we can use the value between 0 and 255 (2 to the power of 8) to control the LED brightness.

The sample code below will demonstrate how to drive the onboard LED using PWM to display a breathing light effect. No external hardware connection is required for this example.

Sample Code

Function: Drive the onboard LED using PWM to display a breathing light effect.

const int ledPin = D9; //Define LED Pin
const int freq = 5000;   //Set PWM signal frequency
const int ledChannel = 0;   //There are 16 channels from 0 to 15, set to PWM channel 0
const int resolution = 8;    //Set the duty cycle resolution of the signal, from 1 to 16 bits. Select 8-bit resolution here, ranging from 0 to 255

void setup(){
  ledcSetup(ledChannel,freq,resolution);
  ledcAttachPin(ledPin,ledChannel);    //Set the pin for outputting PWM signals and the channel for generating PWM signals

}

void loop(){
  for(int dutyCycle = 0;dutyCycle <= 255;dutyCycle++){
    ledcWrite(ledChannel,dutyCycle);   
    delay(15);
  }
  for(int dutyCycle = 255;dutyCycle >= 0;dutyCycle--){
    ledcWrite(ledChannel,dutyCycle);
    delay(15);
  }
}

Result

After the program is uploaded successfully, the light intensity of the on-board green LED appears to rise and fall in a manner that resembles human breathing.

Was this article helpful?

TOP