arduino中使用光敏电阻控制LED灯

具体的连线

int ledPin=12;
int val=0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

int sensorValue=analogRead(A0);
Serial.println(sensorValue);
if(sensorValue>=1000)
{
  digitalWrite(ledPin,HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
delay(1000);
}

其中的sensorValue变量可以自己自由设置,从而控制LED灯亮的方式以及灵敏度。

发布了24 篇原创文章 · 获赞 7 · 访问量 8249

猜你喜欢

转载自blog.csdn.net/yang_zzu/article/details/78266671