X
Home > Blog > STEM for ESP32 > HOW to Complete LED BLINK with ESP32

HOW to Complete LED BLINK with ESP32

By r December 16th, 2024 126 views

1. Overview

KD2080

The traffic light is the signals that commands the traffic operation, which is generally composed of red, green and yellow. Red indicates prohibited, green indicates permitted, and yellow indicates a warning. This traffic light module is composed of red LED, yellow LED and green LED, whose control principle is consistent with that of white LED.

2. Parameters

Operating voltage: DC 3.3 ~ 5V

Operating current: 40 mA

Maximum power: 0.2 W

Operating temperature: -10°C ~ +50°C

Dimensions: 48 x 24 x 18 mm (without housing)

Positioning hole: Diameter of 4.8 mm

Interface: Telephone socket

3. Principle

a6

Schematic diagram of traffic light module

We know from the schematic that the traffic light module is assembled by red, yellow, and green LED modules. So when we control this module, we are actually controlling LEDs.

4. Wiring Diagram

When using the traffic light module, connect it to white ports on the board.

IOT-white

Connect this module to socket 9. According to the pin sequence, the red LED is connected to pin io17, the yellow to io18, and the green to io19.

When a high level (1) is input to the pin of the module, the corresponding color lights up; When low (0) is entered, the corresponding color is off.

b1

5. Test Code

For LED BLINK project, only the red LED is adopted whose control pin is IO17.

/*  
  Project name: LED_BLINK 
  Function: LED BLINK
  Author: keyestudio    
  Hardware connection:  
    - Connect RED LED to digital port 17  
  Library:  
    - none (no need to import additional library)  
  Cautions:  
    - Ensure the LED is connected to digital port IO17  
    - Before uploading test code, please correctly connect to the development board and port  
*/    
#define RED_LED_PIN 17   //Connect the red LED to port IO17
  
void setup() {  
  // Initialize the digital port to output  
  pinMode(RED_LED_PIN, OUTPUT);  
}  
  
void loop() {  
  // LED on
  digitalWrite(RED_LED_PIN, HIGH);  
  // delay 500ms  
  delay(500);  
  // LED off
  digitalWrite(RED_LED_PIN, LOW);  
  // delay 500ms  
  delay(500);  
}

6. Test Result

After uploading the test code, the red LED flashes.

d1

7. Code Explanation

7.1 #define RED_LED_PIN 17

#define is used to set constant (an unchanging value). Syntax: #define constantName value

  • constantName: the name of the macro to define

  • value: the value to assign to the macro

Please refer to the official website for details: #define | Arduino Documentation

7.2 setup()

When executing the code, setup() function will be called to initialize variables and pin modes, or enable libraries. It only runs once each time the Arduino board is powered on or reset.

Please refer to the official website for details: setup() | Arduino Documentation

7.3 pinMode(RED_LED_PIN, OUTPUT);

pinMode() sets the specified pin to input / output / pull-up. Syntax: pinMode(pin, mode)

  • pin: the Arduino pin number to set the mode of.

  • mode:INPUT,OUTPUT, orINPUT_PULLUP

Please refer to the official website for details: pinMode() | Arduino Documentation

7.4 loop()

Test codes in ‘{ }’ of the loop() function will always be executed. They are a loop.

Please refer to the official website for details: loop() | Arduino Documentation

7.5 digitalWrite(RED_LED_PIN,HIGH)

digitalWrite() controls specified pins output HIGH or LOW. Syntax: digitalWrite(pin, value)

  • pin: the Arduino pin number

  • value: HIGH or LOW

Please refer to the official website for details: digitalWrite() | Arduino Documentation

7.6 delay(500)

delay() is used where waiting is required in the program. Syntax: delay(value)

value: Delay time value (unit: ms), 1S = 1000mS, 1mS = 1000 uS. Generally we use mS.

Please refer to the official website for details: delay() | Arduino Documentation

HOW to Make a Motion Alarm with Kidspico
Previous
HOW to Make a Motion Alarm with Kidspico
Read More
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Next
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Read More
Message Us