Date: 10/2/2026 Time 15:37:58
const int SENSOR_PIN = PD2; // the sensor input pin
const int MOTOR_PIN = PD3; // the DC motor output pin
void setup() {
pinMode(SENSOR_PIN, INPUT_PULLUP); //sensor pin as an input with the internal pull-up
resistor
pinMode(MOTOR_PIN, OUTPUT); // Set the motor pin as an output.
void loop() {
int sensorState = digitalRead(SENSOR_PIN); //Read the state of the sensor. An NPN sensor pulls
the pin LOW
when a metal object is detected
if (sensorState == HIGH) { // If a metal object is detected,
digitalWrite(MOTOR_PIN, HIGH); // turn the motor ON
} else {
digitalWrite(MOTOR_PIN, LOW); // If no metal object is detected, turn the motor OFF