Arduino Nano - Door Sensor - Servo Motor
This tutorial instructs you how to use Arduino Nano and door sensor to control servo motor. In detail::
- When the door is open, Arduino Nano rotates the servo motor to 90 degrees
- If the door is opened again, Arduino Nano turns the servo motor back to 0 degrees.
The same procedure is carried out again.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
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
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.
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
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.