Arduino Nano - Touch Sensor - Piezo Buzzer
This tutorial instructs you how to use Arduino Nano and touch sensor to control piezo buzzer. In detail:
- Arduino Nano produces a sound when the touch sensor is touched.
- Arduino Nano ceases producing a sound when the touch sensor is not activated.
- Arduino Nano plays a melody when the touch sensor is touched.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Piezo Buzzer and Touch Sensor
If you are unfamiliar with piezo buzzer and touch sensor (including pinout, functionality, and programming), the following tutorials can help:
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
If you do not use a breadboard, wire it like this:

This image is created using Fritzing. Click to enlarge image
Jumper wires can come loose when you move the project. Simply plug the Arduino Nano into a Screw Terminal Expansion Board for Arduino Nano. The pins do not change. Just connect each wire to the matching screw terminal:

This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Nano and other components.
Arduino Nano Code - Simple Sound
Detailed Instructions
- Connect your Arduino Nano to the computer using a USB cable.
- Launch 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.
- Place your finger on the touch sensor and keep it there for several seconds.
- Listen to the sound emitted by the piezo buzzer.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!
Arduino Nano Code - Melody
Detailed Instructions
- Copy the code and open it with the Arduino IDE.
- Create the pitches.h file On Arduino IDE by:
- Either click on the button just below the serial monitor icon and choose New Tab, or use Ctrl+Shift+N keys.
- Give file's name pitches.h and click OK button
- Copy the below code and paste it to the created pitches.h file.
- Click the Upload button on Arduino IDE to compile and upload the code to the Arduino Nano.
- Activate the touch sensor.
- Hear the tune from the piezo buzzer.


Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!