0% found this document useful (0 votes)
2 views1 page

Source Code

The document contains an Arduino code snippet for controlling a DC motor based on the detection of a metal object using a sensor. The sensor is connected to pin PD2 and the motor to pin PD3, with the sensor configured as an input with an internal pull-up resistor. The motor is activated when the sensor detects a metal object and deactivated otherwise.

Uploaded by

Isaac Kimaru
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Source Code

The document contains an Arduino code snippet for controlling a DC motor based on the detection of a metal object using a sensor. The sensor is connected to pin PD2 and the motor to pin PD3, with the sensor configured as an input with an internal pull-up resistor. The motor is activated when the sensor detects a metal object and deactivated otherwise.

Uploaded by

Isaac Kimaru
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like