Arduino Nano - Door Sensor - Servo Motor

This tutorial instructs you how to use Arduino Nano and door sensor to control servo motor. In detail::

The same procedure is carried out again.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Door Sensor
1×Servo Motor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Servo Motor and Door Sensor

If you are unfamiliar with servo motors and door sensors (including pinouts, how they work, and how to program them), the following tutorials can help:

Wiring Diagram

The wiring diagram between Arduino Nano and door sensor servo motor

This image is created using Fritzing. Click to enlarge image

Please note that the wiring diagram shown above is only suitable for a servo motor with low torque. In case the motor vibrates instead of rotating, an external power source must be utilized to provide more power for the servo motor. The below demonstrates the wiring diagram with an external power source for servo motor.

The wiring diagram between Arduino Nano and door sensor servo motor external power

This image is created using Fritzing. Click to enlarge image

Please do not forget to connect GND of the external power to GND of Arduino Nano.

Arduino Nano Code - Door Sensor Controls Servo Motor

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-door-sensor-servo-motor */ #include <Servo.h> #define DOOR_SENSOR_PIN 3 // The Arduino Nano pin connected to the door sensor's pin #define SERVO_PIN 2 // The Arduino Nano pin connected to the servo motor Servo servo; // create servo object to control a servo int angle = 0; // The current angle of servo motor int prev_door_state; // The previous state of door sensor int door_state; // The current state of door sensor void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); door_state = digitalRead(DOOR_SENSOR_PIN); } void loop() { prev_door_state = door_state; // save the last state door_state = digitalRead(DOOR_SENSOR_PIN); // read new state if (prev_door_state == LOW && door_state == HIGH) { Serial.println("The door-opening event is detected"); // change angle of servo motor if (angle == 0) angle = 90; else if (angle == 90) angle = 0; // rotate the servo motor to the angle position servo.write(angle); } }

Detailed Instructions

  • Connect an Arduino Nano to your computer using a USB cable.
  • Open the Arduino IDE, select the appropriate board and port.
  • Copy the code provided and open it in the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Bring a magnet close to the reed switch, then move it away again. Do this multiple times.
  • Check out the servo motor's rotated angle.

Video Tutorial