Code For 8.1: Setup Pinmode Serial Begin
Code For 8.1: Setup Pinmode Serial Begin
1
int ldrPin = A0; // LDR analog input
int ledPin = 7; // LED output
int lightValue = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
lightValue = analogRead(ldrPin); // read light level
Serial.println(lightValue);
delay(500);
}
This code sets up an LDR sensor to measure light intensity and control an LED based on
the readings. The LDR is connected to analog pin A0, while the LED is connected to digital
pin 7. In the loop(), the Arduino reads the analog value from the LDR and prints it to the
Serial Monitor. If the light level is below 400 (meaning it’s dark), the LED is turned on;
otherwise, it’s turned off. The delay(500) adds a short pause between each reading to make
the output more stable and readable.