Skip to content

Commit f272fdc

Browse files
Merge pull request #2559 from arduino/pedromsousalima/r4/touch-tutorial
[PXCT-1073] Tutorial from library - Uno R4 Capacitor Tutorial
2 parents 68972fb + 0734280 commit f272fdc

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
58.4 KB
Loading
187 KB
Loading
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
---
2+
title: UNO R4 Capacitive-Touch Tutorial.
3+
difficulty: beginner
4+
description: Learn to use the built-in capacitive sensing capabilities of the Arduino® UNO R4.
5+
tags: [Arduino, Capacitive Sensing, UNO R4]
6+
author: Pedro Lima
7+
hardware:
8+
- hardware/02.hero/boards/uno-r4-wifi
9+
- hardware/02.hero/boards/uno-r4-minima
10+
software:
11+
- ide-v2
12+
- web-editor
13+
---
14+
15+
Capacitive sensing is a technology that detects changes in capacitance to determine the presence or absence of a conductive object, such as a human finger. This principle is widely used in touch-sensitive devices. The Arduino® UNO R4, both the [WiFi®](https://store.arduino.cc/products/arduino-uno-r4-wifi) and [Minima](https://store.arduino.cc/products/arduino-uno-r4-minima) versions, come equipped with built-in capacitive sensing capabilities, making it easier to integrate touch inputs into your projects.
16+
17+
![Sensor Example](assets/Touch_Cover_001.gif)
18+
19+
## Required Hardware
20+
To use the library, you will need one of the compatible boards:
21+
- [Arduino® UNO R4 WiFi](https://store.arduino.cc/products/uno-r4-wifi)
22+
- [Arduino® UNO R4 Minima](https://store.arduino.cc/products/uno-r4-minima)
23+
24+
You will also need:
25+
- Wire
26+
- Something that can affect the electric field to touch (metal objects, materials with moisture, even bananas! Experimentation is key!)
27+
28+
## How Capacitive Sensing Works
29+
30+
Imagine the sensor as creating an invisible electric field around itself. When a conductive object (like your finger) approaches, it's like dropping a stone into a calm pond - the field gets disturbed. The sensor detects this disturbance as a change in capacitance, which is essentially how much electrical charge the system can store.
31+
Here's the process:
32+
33+
1. The sensor electrode creates an electric field by applying a small voltage
34+
2. When you approach or touch the sensor, your body becomes part of the capacitive circuit
35+
3. This increases the capacitance between the sensor electrode and ground
36+
4. The microcontroller detects this capacitance change (often by measuring timing differences) and determines if it's significant enough to register as a "touch"
37+
38+
## Using Capacitive Sensing on the UNO R4
39+
40+
The UNO R4 features a Capacitive Touch Sensing Unit (CTSU) that allows you to use certain pins as capacitive touch inputs. To utilize these capabilities, you can use the [Arduino_CapacitiveTouch library](https://github.com/arduino-libraries/Arduino_CapacitiveTouch).
41+
42+
### Compatible Pins
43+
44+
For both the UNO R4 WiFi® and Minima boards, the compatible pins for capacitive touch are listed in the [Arduino_CapacitiveTouch library documentation](https://github.com/arduino-libraries/Arduino_CapacitiveTouch?tab=readme-ov-file#compatible-pins).
45+
46+
**Arduino® UNO-R4 Minima:**
47+
48+
| Arduino Pin | Touch Sensor Channel (TS#) | Channel Control Index (CHAC idx) | Channel Control Bit Mask (CHAC val) |
49+
|--------------|----------------------------|----------------------------------|-------------------------------------|
50+
| D0 | 9 | 1 | (1 << 1) |
51+
| D1 | 8 | 1 | (1 << 0) |
52+
| D2 | 34 | 4 | (1 << 2) |
53+
| D3 | 13 | 1 | (1 << 5) |
54+
| D8 | 11 | 1 | (1 << 3) |
55+
| D9 | 2 | 0 | (1 << 2) |
56+
| D11 | 10 | 1 | (1 << 2) |
57+
| D13 | 12 | 1 | (1 << 4) |
58+
| A1 (D15) | 21 | 2 | (1 << 5) |
59+
| A2 (D16) | 22 | 2 | (1 << 6) |
60+
| LOVE_BUTTON | 0 | 0 | (1 << 0) |
61+
62+
**Arduino® UNO-R4 WiFi®:**
63+
64+
| Arduino Pin | Touch Sensor Channel (TS#) | Channel Control Index (CHAC idx) | Channel Control Bit Mask (CHAC val) |
65+
|--------------|----------------------------|----------------------------------|-------------------------------------|
66+
| D0 | 9 | 1 | (1 << 1) |
67+
| D1 | 8 | 1 | (1 << 0) |
68+
| D2 | 13 | 1 | (1 << 5) |
69+
| D3 | 34 | 4 | (1 << 2) |
70+
| D6 | 12 | 1 | (1 << 4) |
71+
| D8 | 11 | 1 | (1 << 3) |
72+
| D9 | 2 | 0 | (1 << 2) |
73+
| D11 | 7 | 0 | (1 << 7) |
74+
| D12 | 6 | 0 | (1 << 6) |
75+
| A1 (D15) | 21 | 2 | (1 << 5) |
76+
| A2 (D16) | 22 | 2 | (1 << 6) |
77+
| LOVE_BUTTON | 27 | 3 | (1 << 3) |
78+
79+
80+
### Library Functions
81+
82+
The **Arduino_CapacitiveTouch** library provides several functions to work with capacitive touch inputs:
83+
84+
- **`CapacitiveTouch(uint8_t pin)`** Constructs a capacitive touch sensor for the given pin.
85+
- **`bool begin()`** Initializes the sensor and configures the pin and hardware.
86+
- **`int read()`** Reads the raw sensor value and returns it.
87+
- **`bool isTouched()`** Checks if the sensor is touched based on the threshold.
88+
- **`void setThreshold(int threshold)`** Sets the detection threshold for touch sensitivity.
89+
- **`int getThreshold()`** Retrieves the current detection threshold.
90+
91+
For more detailed usage and examples, refer to the [CapacitiveSensor library documentation](https://github.com/arduino-libraries/Arduino_CapacitiveTouch/blob/main/docs/api.md).
92+
93+
## Detecting Touch Example
94+
95+
### Circuit
96+
97+
Here's a simple example to get you started with capacitive sensing on the UNO R4. For this example, we are connecting a single piece of any conductive material to the pin `D0` on the Board.
98+
99+
![How to connect](assets/HoockupGuideExample.png)
100+
101+
102+
### Code
103+
104+
**Note:** To install the `Arduino_CapacitiveTouch` library, open the Arduino IDE Library Manager, search for "Arduino_CapacitiveTouch" within the Manage Libraries option, and click Install.
105+
106+
```arduino
107+
#include "Arduino_CapacitiveTouch.h"
108+
109+
CapacitiveTouch touchButton = CapacitiveTouch(D0);
110+
111+
void setup() {
112+
Serial.begin(9600);
113+
114+
if(touchButton.begin()){
115+
Serial.println("Capacitive touch sensor initialized.");
116+
} else {
117+
Serial.println("Failed to initialize capacitive touch sensor. Please use a supported pin.");
118+
while(true);
119+
}
120+
121+
touchButton.setThreshold(2000);
122+
}
123+
124+
void loop() {
125+
int sensorValue = touchButton.read();
126+
Serial.print("Raw value: ");
127+
Serial.println(sensorValue);
128+
129+
if (touchButton.isTouched()) {
130+
Serial.println("Button touched!");
131+
}
132+
133+
delay(100);
134+
}
135+
```
136+
137+
![Sensor Example](assets/Touch_Cover_001.gif)
138+
139+
## Creative Project Ideas
140+
141+
Now that you have learned the basics, here are some fun and interesting project ideas:
142+
143+
- **Touch-Controlled Night Light -** Create a lamp that turns on/off with a touch
144+
- **Capacitive Touch Piano -** Use multiple pins to create touch-sensitive keys
145+
- **Smart Home Controller -** Touch different areas to control various devices
146+
- **Interactive Art Installation -** Create touch-responsive visual or audio effects
147+
148+
## Conclusion
149+
150+
Capacitive sensing on the Arduino UNO R4 lets you add intuitive touch controls with minimal hardware. Simply pair the board with the `Arduino_CapacitiveTouch` library. Tune the detection threshold for your environment (such as humidity and nearby electronics), and you can quickly scale from a single-touch button to richer, multi-point interfaces.

0 commit comments

Comments
 (0)