Arduino CDS 센서, Analog read


Analog Sensor Read

CDS Sensor

Analog read

  • https://www.arduino.cc/reference/ko/language/functions/analog-io/analogread/
#define cdsPin A0

void setup() {
	Serial.begin(9600);
}

void loop() {
	int cds_data = analogRead(cdsPin);
	Serial.print("cds in: ");
	Serial.println(cds_data);
	delay(100);
}

Digital read

  • https://www.arduino.cc/reference/ko/language/functions/digital-io/digitalread/
#define cdsPin 8

void setup() {
	Serial.begin(9600);
	pinMode(cdsPin, INPUT);
}

void loop() {
	int cds_data = digitalRead(cdsPin);
	Serial.print("cds in: ");
	Serial.println(cds_data);
	# 1 HIGH
	# 0 LOW

	delay(100);
}

CDS Sensor + LED

CDS Direct LED Controll


CDS Read + LED Controll

#define cdsPin A0
#define ledPin 13

void setup() {
	Serial.begin(9600);
	pinMode(ledPin, OUTPUT);
}

void loop() {
	int cds_data = analogRead(cdsPin);
	Serial.print("cds in: ");
	Serial.println(cds_data);

	if(cds_data > 350){
		Serial.print("LED On");
		digitalWrite(ledPin, HIGH);
	} else {
		Serial.print("LED Off");
		digitalWrite(ledPin, LOW);
	}
	delay(100);
}

Relay Controll

Arduino 5v : Relay VCC(+)
Arduino GND: Relay GND(-)
Arduino 8pin : Relay IN & S & D(DATA)

#define relayPin 8
void setup() {
	Serial.begin(9600);
	pinMode(relayPin, OUTPUT);
}

void loop() {
	Serial.print("Relay On");
	digitalWrite(relayPin, HIGH);
	delay(1000);
	Serial.print("Relay Off");
	digitalWrite(relayPin, LOW);
	delay(1000);
}

CDS Sensor + Relay Controll

#define cdsPin A0
#define RelayPin 8

void setup() {
	Serial.begin(9600);
	pinMode(RelayPin, OUTPUT);
}

void loop() {
	int cds_data = analogRead(cdsPin);
	Serial.print("cds in: ");
	Serial.println(cds_data);

	if(cds_data > 350){
		Serial.print("Relay On");
		digitalWrite(RelayPin, HIGH);
	} else {
		Serial.print("Relay Off");
		digitalWrite(RelayPin, LOW);
	}
	delay(100);
}

Delay & Millis diff

  • https://www.arduino.cc/reference/ko/language/functions/time/millis/
unsigned long start_time;
unsigned long sleep_time = 60 * 1000;

void setup() {
	Serial.begin(9600);
	start_time = millis();
}
void loop() {
	unsigned long crrent_time = millis();
	Serial.print("Start Time: ");
	Serial.println(start_time);
	Serial.print("Crrent Time: ");
	Serial.println(Crrebt_time);
	
	delay(1000);
	if(crrent_time - start_time > sleep_time){
		delay(sleep_time);
	}
}

Ref

  • https://zelkun.tistory.com/entry/010-Arduino-아두이노-CDS-Sensor-조도센서
  • https://zelkun.tistory.com/entry/011-Arduino-아두이노-CDS-Sensor-Module-조도센서모듈
  • https://zelkun.tistory.com/entry/018-Arduino-아두이노-relay-module-control-릴레이-모듈-제어