esp32系列教程代码-让esp的蓝灯亮起来

发布于 2024-03-16 09:39:52 阅读 227

让蓝灯亮

我们需要查看esp32中的蓝灯是用哪个接口控制的,我的这个esp32是用的GPIO2口,所以我们就让pin2口设置为1即可让蓝灯亮

import machine

pin2 = machine.Pin(2, machine.Pin.OUT)

pin2.value(1)

调整led灯的亮度

from machine import Pin,PWM

led2 = PWM(Pin(2))

led2.freq(1000)

led2.duty(200) # 来控制灯的亮度

将led灯以呼吸灯的方式显示

import time
from machine import Pin,PWM

led2 = PWM(Pin(2))

led2.freq(1000)

while True:
    for i in range (0,1024):
        led2.duty(i)
        time.sleep_ms(2)
    for i in range (1023,-1, -1):
        led2.duty(i)
        time.sleep_ms(2)

arduino 让led灯闪烁

int led_pin = 2;

void setup() {
  // put your setup code here, to run once:
  pinMode(led_pin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(led_pin, HIGH);
  delay(1000);
  digitalWrite(led_pin, LOW);
  delay(1000);
}

arduino 让led灯以pwm调光(呼吸灯)

int led_pin = 2;

void setup() {
}

void loop() {
  for (int i=0; i <= 255; i++){
    analogWrite(led_pin, i);
    delay(10);
  }
}