#include <stdio.h>
#include "driver/uart.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h" // Include the ESP-IDF logging system
#define UART_NUM UART_NUM_2 // Use UART2
#define TXD_PIN 17 // UART TX pin
#define RXD_PIN 16 // UART RX pin
#define LED_PIN 38 // GPIO pin connected to the LED
void app_main() {
// Disable ESP32 system logs from interfering with UART
esp_log_level_set("*", ESP_LOG_NONE);
// UART configuration
uart_config_t uart_config = {
.baud_rate = 115200, // Standard baud rate
.data_bits = UART_DATA_8_BITS, // 8 data bits
.parity = UART_PARITY_DISABLE, // No parity
.stop_bits = UART_STOP_BITS_1, // 1 stop bit
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE // Disable flow control
};
// Apply UART configuration to UART2
uart_param_config(UART_NUM, &uart_config);
// Set TX and RX pins, leaving RTS and CTS as unused
uart_set_pin(UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
// Install UART driver
esp_err_t uart_driver_status = uart_driver_install(UART_NUM, 1024, 0, 0, NULL, 0);
if (uart_driver_status != ESP_OK) {
printf("Failed to install UART driver: %s\n", esp_err_to_name(uart_driver_status));
return;
}
// Configure LED GPIO pin as output
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
// Main loop: send '1' and '0' alternately, toggling the LED
while (1) {
// Send '1' to indicate LED ON
char data = '1';
uart_write_bytes(UART_NUM, &data, 1);
printf("Master sent: %c\n", data);
// Turn LED ON
gpio_set_level(LED_PIN, 1);
vTaskDelay(pdMS_TO_TICKS(5000)); // 5-second delay
// Send '0' to indicate LED OFF
data = '0';
uart_write_bytes(UART_NUM, &data, 1);
printf("Master sent: %c\n", data);
// Turn LED OFF
gpio_set_level(LED_PIN, 0);
vTaskDelay(pdMS_TO_TICKS(5000)); // 5-second delay
}
}
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1