You are on page 1of 1

Experiment 6 code

// Define IR sensor pin and LED pin

int IR_Pin = 9; // IR sensor OUT pin connected to digital pin 2


int ledPin = 13; // LED connected to digital pin 13 (onboard LED)

void setup()

{
pinMode(IR_Pin, INPUT); // Set the IR sensor pin as input
pinMode(ledPin, OUTPUT); // Set the LED pin as output
}

void loop()

{
int irSensorState = digitalRead(IR_Pin); // Read the state of the IR sensor

if (irSensorState == HIGH)

{
// If the IR sensor detects an object, turn the LED on
digitalWrite(ledPin, HIGH);
}

else

{
// If the IR sensor does not detect an object, turn the LED off
digitalWrite(ledPin, LOW);
}
}

You might also like