Arduino Nano - Touch Sensor - Servo Motor

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

The same steps are carried out again.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Touch Sensor
1×Servo Motor
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Servo Motor and Touch Sensor

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

Wiring Diagram

The wiring diagram between Arduino Nano and Touch Sensor Servo Motor

This image is created using Fritzing. Click to enlarge image

It should be noted 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 operate the servo motor. The following wiring diagram demonstrates how to connect the servo motor to an external power source.

The wiring diagram between Arduino Nano and Touch Sensor 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.

Arduino Nano Code - Touch 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-touch-sensor-servo-motor */ #include <Servo.h> #define TOUCH_SENSOR_PIN 2 // The Arduino Nano pin connected to the touch sensor #define SERVO_PIN 9 // 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_touch_state; // The previous state of touch sensor int touch_state; // The current state of touch sensor void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. pinMode(TOUCH_SENSOR_PIN, INPUT); // set arduino pin to input mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); touch_state = digitalRead(TOUCH_SENSOR_PIN); } void loop() { prev_touch_state = touch_state; // save the last state touch_state = digitalRead(TOUCH_SENSOR_PIN); // read new state if (prev_touch_state == LOW && touch_state == HIGH) { Serial.println("The sensor is touched"); // 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 your Arduino Nano to a computer using a USB cable.
  • Launch the Arduino IDE, select the correct board type and port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano.
  • Tap the touch sensor multiple times.
  • Check out the servo motor's angle changing.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!