Arduino Nano 33 IoT - Touch Sensor

This guide shows you how to use the Arduino Nano 33 IoT with a touch sensor (which is also called a touch switch or touch button).

Arduino Nano 33 IoT Touch Sensor

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Touch Sensor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter 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 Touch Sensor

Touch Sensor Pinout

A touch sensor has three pins.

  • GND pin: Connect this pin to ground (0V).
  • VCC pin: Connect this pin to the power supply (5V or 3.3V).
  • SIGNAL pin: This pin sends out a signal. It is LOW when not touched and HIGH when touched. Connect this pin to one of the input pins on the Arduino Nano 33 IoT.
Touch Sensor Pinout

How Touch Sensor Works

  • When you do not touch the sensor, the SIGNAL pin is low.
  • When you touch the sensor, the SIGNAL pin is high.

Arduino Nano 33 IoT - Touch Sensor

We can hook the touch sensor's signal pin to one of the Arduino Nano 33 IoT's input pins. Then, we use the board's program to check if the touch sensor is active.

Wiring Diagram between Touch Sensor and Arduino Nano 33 IoT

The wiring diagram between Arduino Nano and 33 IoT Touch Sensor

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. Although these pins can be used as digital input pins, it is recommended to avoid using them for digital input. If you must use them, do not use internal or external pull-down resistors for these pins

How To Program Touch Sensor

  • This code sets a pin on the Arduino Nano 33 IoT (like pin D2) as a digital input using the pinMode() function.
pinMode(2, INPUT);
  • Checks the current condition of an Arduino Nano 33 IoT pin using the digitalRead() function.
int inputState = digitalRead(D2);

Touch Sensor - Arduino Nano 33 IoT Code

This code checks the touch sensor and shows its status on the Serial Monitor.

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-touch-sensor */ #define SENSOR_PIN 2 // The Arduino Nano 33 IoT pin connected to the sensor's SIGNAL pin of touch sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the Arduino Nano 33 IoT's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: int state = digitalRead(SENSOR_PIN); // print state to Serial Monitor Serial.println(state); }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and send the code to your Arduino Nano 33 IoT board.
  • Touch the sensor with your finger, then lift your finger off.
  • Open the Serial Monitor to see the result, which should look like the image below.
COM6
Send
0 0 0 1 1 1 1 1 1 0 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

How to detect the state change from LOW to HIGH

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-touch-sensor */ #define SENSOR_PIN 2 // The Arduino Nano 33 IoT pin connected to the sensor's SIGNAL pin of touch sensor int prev_state = LOW; // The previous state from the input pin int touch_state; // The current reading from the input pin void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the Arduino Nano 33 IoT's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: touch_state = digitalRead(SENSOR_PIN); if (prev_state == LOW && touch_state == HIGH) Serial.println("The sensor is touched"); // save the the last state prev_state = touch_state; }

Video Tutorial

Learn More

※ 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!