以下是一个简单的Arduino程序,用于控制ESP32上的继电器:

#include <WiFi.h>

// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

// Set the GPIO pin number for the relay
const int relayPin = 4;

void setup() {
  Serial.begin(115200);
  pinMode(relayPin, OUTPUT);

  // Connect to Wi-Fi network
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  // Turn on the relay
  digitalWrite(relayPin, HIGH);
  Serial.println("Relay turned on");
  delay(5000);

  // Turn off the relay
  digitalWrite(relayPin, LOW);
  Serial.println("Relay turned off");
  delay(5000);
}

这个程序假设你已经连接到Wi-Fi网络,并且将继电器连接到ESP32的GPIO 4引脚。程序中的循环会交替打开和关闭继电器,每次保持5秒钟。你可以根据需要更改这些延迟时间

用Arduino写一个esp32控制继电器的程序

原文地址: https://www.cveoy.top/t/topic/cKFs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录