今天简单的用光照传感器做毕业设计的一个小部分,先简单的记录一下吧!
大致完成的内容是光照传感器检测环境的光照强度,当检测的数据超过一定的数值(环境较暗的情况),自动点亮LED,否则LED不亮
首先准备的工具:
Arduino UNO R3开发板
光照传感器
LED
杜邦线
连接线
Arduino开发板 光照传感器
3.3V------------------VCC
GND-----------------GND
A0---------------------AO
Led灯正极(长)接13,负极(短)接GND
准备差不多了,上代码!
这里环境越暗,读取的模拟值越大,当数据超过60,就会自动打开LED小灯。可以根据环境的不同状态调整参数。
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
int a=60;
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 10000, 0);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
if(outputValue>=a)
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}
delay(1000);
}
毕业设计的路好漫长啊…等我下次更新吧~~