LoveButton Touch Sense library for R4 Minima and R4 WiFi

I've been playing with the CTSU unit on the new R4 boards. On the Minima in particular the Love Pin (the little heart shaped blob of solder on the back) is connected to one of the touch sensor pins. So I thought it would be cool to make a touch sensitive button out of it.

Once I had it working I put it together into a library. You can find the code here or it should be in the Library Manager tomorrow.

All you need for this is a 1uF ceramic capacitor. Other values may work, but might need some experimentation. 1uF is what I had when I set it up. Place this cap between pin 10 and Ground keeping the leads as short as you can so it's not sticking way up.

The example just uses the touch button to toggle the built-in LED on pin 13. If you touch the Love Pin then the led state will toggle on and off.

The class is pre-instantiated. All you do is call love.begin() in setup once and then use love.read() just like you would digitalRead a pin.

The example is pretty simple:

#include "LoveButton.h"

bool oldTouch = false;
uint8_t ledState = LOW;

void setup() {
  pinMode(13, OUTPUT);
  love.begin();  // Start the capacitive sensor
}

void loop() {
  // read the touch sensor and store the result
  bool touch = love.read();
  if (touch && !oldTouch) {
    // if there's a new touch.
    // toggle the led state
    ledState = 1 - ledState;
    digitalWrite(13, ledState);
  }
  oldTouch = touch;  // save the state for next time
  delay(50);         // for debounce a little
}

It should be noted that this library ONLY works with the UNO-R4 Minima. The Love Pin on the WiFi board is connected to a different pin that is not touch sensitive.

There's also a love.debug() method that will return a char* with the raw numbers and the difference. The difference (labelled diff) is the number that I'm using to tell if there's a touch or not. Currently I have a default threshold of 23000, but there is a setThreshold method that you can use to change it if you find that you get different numbers.

There's a Debug.ino sketch in the examples that will just print out the raw numbers every half a second so you can watch and see what sort of threshold you need.

Let me know what you think...

5 Likes

I just pushed a new version 1.2 that adds support for the UNO-R4 WiFi.

Note: The TSCAP pin is pin 7 for the WiFi so the capacitor goes between pin 7 and ground on the WiFi.

Thanks to Winnie S. for the pull request. I've tested it on both the WiFi and Minima.

I posted a video showing how the library works.

Thanks for putting this together! Was quite fun to play around with.

Having trouble though determining if there is still the capacitive touch input option in the Renesas boards as in the previous boards through the GPIO pins? Is the CTSU accessible through a more practical interface than the lovely love button?

You have to look at the peripheral functions for the individual pins. Only some pins on the chip go to the CTSU and of that group only a small number are broken out on the UNO-R4. And it's different between the R4-WiFi and the Minima.

This post in the Deep Magic thread has section 1.7 of the hardware manual broken out by pin number on the Arduino. The last column for each entry is the CTSU. Each board has at least one option for TSCAP and then several options for pins that can be used.

The library itself only uses the Love Pin, and is meant more as an example than anything else. When time allows one of the projects I'd like to hit (if nobody else does it first) would be a proper library for the CTSU.

Here's a link to an explainer for the code in LoveButton. From here you could probably figure out how to change the pin to something else.

Thanks for the reply! I'd like to try to tackle customizing the library for a direct interaction with the breakout pins (would it be Minima pin 10 for its role in the TSCAP?) but I hope you get to it before me :slight_smile: