Arduino for ODROID-GO - Blue LED and PWM

  • Refer to the Arduino official documents. This provides useful common functions with great instructions.
  • Refer to the ESP32 official programming guide. Most of the ESP32 specific functions are introduced here.
  • We don't need the odroid_go.h library for this step.
  • You can implement a “breathing” effect also by using fading functions in the ledc.h. However, this will not be dealt with in this guide.

We will learn how to control the blue status LED on the board and how to adjust the PWM value to give a breathing effect to the LED with Arduino.

Let's make the LED blink continuously.

Open a new sketch by pressing the shortcut CTRL-N.

You're able to control the LED very easily.
The pin number for the LED is 2. Define that with Preprocessor.

First, we have to set the pin to output mode. Use the pinMode() function to do that.

#define PIN_BLUE_LED 2
 
void setup() {
  // put your setup code here, to run once:
 
  pinMode(PIN_BLUE_LED, OUTPUT);
}
 
void loop() {
  // put your main code here, to run repeatedly:
}

Now the pin is ready to use.

You can set the pin signal level by using the digitalWrite() function. This function has to be in loop() to run repeatedly.
The delay() function is used to slow down the rate of blinking.

#define PIN_BLUE_LED 2
 
void setup() {
  // put your setup code here, to run once:
 
  pinMode(PIN_BLUE_LED, OUTPUT);
}
 
void loop() {
  // put your main code here, to run repeatedly:
 
  digitalWrite(PIN_BLUE_LED, HIGH);
  delay(500);
  digitalWrite(PIN_BLUE_LED, LOW);
  delay(500);
}

Press CTRL-U to compile and upload the sketch.
Then, you can see the blue LED blinking.

By adjusting the analog output value, known as PWM, we can make the LED show a breathing effect.

Arduino gives us wrappers that help control the GPIO pins as well as the PWM features.
Generally, these PWM functions are analogRead() and analogWrite(), but they are not yet available in ESP32 so we should use ledcRead() and ledcWrite() functions to control the LEDs instead. The way to control PWM values in ESP32 is current different from other Arduino boards.
These functions use the LED PWM hardware feature of ESP32 and are located in ledc.h (LED Control).

In ESP32, the LED PWM is composed of 16 independent channels, and we can configure duty cycles with a resolution and wave period by accessing these channels.

So, let's get it started.

First of all, we should choose a channel to attach the LED to. Channels 0 to 7 are available. We will use channel 1.
Define the channel and the blue LED through a Preprocessor macro.

#define PIN_BLUE_LED  2
#define PWM_CHANNEL   1
 
void setup() {
  // put your setup code here, to run once:
}
 
void loop() {
  // put your main code here, to run repeatedly:
}

Set the pin mode of the LED to output.

#define PIN_BLUE_LED  2
#define PWM_CHANNEL   1
 
void setup() {
  // put your setup code here, to run once:
  pinMode(PIN_BLUE_LED, OUTPUT);
}
 
void loop() {
  // put your main code here, to run repeatedly:
}

Attach the LED pin to the channel we defined.
Next, setup the channel to operate in 12kHz, 8 bit resolution.

#define PIN_BLUE_LED  2
#define PWM_CHANNEL   1
 
void setup() {
  // put your setup code here, to run once:
  pinMode(PIN_BLUE_LED, OUTPUT);
 
  ledcAttachPin(PIN_BLUE_LED, PWM_CHANNEL);
  ledcSetup(PWM_CHANNEL, 12000, 8);
}
 
void loop() {
  // put your main code here, to run repeatedly:
}

Lastly, add the breathing code into the loop() function.
We defined a variable called pinVal as a global variable to prevent allocating new memory repeatedly in the loop() function.
Type of pinVal is unsigned char since the PWM value ranges only 0-255.

#define PIN_BLUE_LED  2
#define PWM_CHANNEL   1
 
unsigned char pinVal = 0;
 
void setup() {
    // put your setup code here, to run once:
 
    pinMode(PIN_BLUE_LED, OUTPUT);
    digitalWrite(PIN_BLUE_LED, HIGH);
 
    ledcAttachPin(PIN_BLUE_LED, PWM_CHANNEL);
    ledcSetup(PWM_CHANNEL, 12000, 8);
}
 
void loop() {
    // put your main code here, to run repeatedly:
 
    for (; pinVal > 0; pinVal--) {
        ledcWrite(PWM_CHANNEL, pinVal);
        delay(3);
    }
    for (; pinVal < 255; pinVal++) {
        ledcWrite(PWM_CHANNEL, pinVal);
        delay(3);
    }
}

Press CTRL-U to compile and upload the sketch.
Then you can see the blue LED breathing.

The complete example is available as follows:

Click the Files → Examples → ODROID-GO → LED or LED_PWM menu to import and press CTRL-U to compile/upload.