Skip to content

Exercise 01: Control Blue LED on GPIO2 (Strobe Light Effect)

Mohamad Ariffin Zulkifli edited this page Apr 18, 2021 · 1 revision

There is 1x blue LED labelled as LED on-board, as circled on the image above. The circuit of this LED is an active-low circuit, as shown in the schematic below, which the negative (-ve) terminal of this LED is connected to GPIO2, therefore we have to pull the GPIO2 to LOW state, in order to complete the circuit, then the light of the LED will turn ON. Otherwise, by pulling the GPIO2 to HIGH state, the light of the LED will turn OFF.

This is quite unusual behaviour, but it is good to learn how an electronic circuit could behave either way. Whereas, the usual circuit design is active-high circuit, where the positive (+ve) terminal of the LED connected to the GPIO2, as in the schematic below, instead of the negative (-ve) terminal of the LED connected to GPIO2, as in the schematic above. According to this type of circuit, we need to pull GPIO2 to HIGH state, to turn ON the LED, otherwise pull GPIO2 to LOW state, to turn OFF the LED.

Since, the blue LED circuit on Hibiscus Sense is active-low, we will program it as below:

Complete Sketch

void setup() {
  pinMode(2, OUTPUT); // declaring GPIO2 as an OUTPUT pin.

}

void loop() {
  // strobe light effect, such on the aeroplane.
  digitalWrite(2, LOW);
  delay(100);
  digitalWrite(2, HIGH);
  delay(100);

  digitalWrite(2, LOW);
  delay(100);
  digitalWrite(2, HIGH);
  delay(2000);
}

Detail Sketch Explanations

The LED is connected to GPIO2, we need to configure GPIO2 as OUTPUT.

pinMode(2, OUTPUT);

In the void loop() function, the program start by turn ON the LED, as the ciruit is active-low, we use LOW state on GPIO2 to complete the LED circuit. Then delay() function to pause the program in milliseconds. Vice versa to turn OFF the LED.

digitalWrite(2, LOW);
delay(100);
digitalWrite(2, HIGH);
delay(100);

digitalWrite(2, LOW);
delay(100);
digitalWrite(2, HIGH);
delay(2000);

Now, we can upload the complete sketch to ESP32, then observe the output. By observation, the output of the LED only have 2 states, which are ON and OFF, thus this output signal known as DIGITAL OUTPUT.

Interesting facts! This exercise is mimicking the strobe light on an aeroplane during the night.

🚀 Tutorial Improvement & Suggestions