0

Can't get built in Cap sensing working using Arduino IDE
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Can't get built in Cap sensing working using Arduino IDE

by miktek1 on Sun Sep 19, 2021 8:05 pm

Hopefully this post will benefit others having the same general issue.
I'm having issues understanding how to use the built in Touch sensors on the newer Trinket M0 +3vdc.
The background is that I have developed my code on the Arduino Uno using the Arduino IDE which I prefer to use instead of Circuit Python. I used the Capacitive Sense library (#include <CapacitiveSensor.h>) and also used external resistors on the Arduino Uno and everything worked great.

The challenge I have is trying to adapt that part of the code to the Trinket M0. I have tried to use the
CircuitPython Cap Touch example in the Circuit Python Essentials which does not use the library that worked with the Arduino and used external resistors. I’m sure part of the problem is that this is a Circuit Python based example (please direct me to any example that uses C instead).

So here’s my attempt at “adapting” the code to C and the errors I’m getting. The Circuit Python example uses libraries , time, board and touchio but I could not find these in the Library Manager. Maybe these are already included and don’t need to be downloaded?
I would really like to not use external resistors on the Trinket MO. Just trying to turn on the internal Led when a touch is sensed on A0. Very similar to the example provided.

Lastly, I should say that I have been successful in setting up the Trinket M0 to work with the Arduino IDE and uploading sketches, and controlling things with Digital IO etc.
Thanks in advance for any help on this.

Here's the example I tried to adapt from:
"""CircuitPython Essentials Capacitive Touch example"""
import time
import board
import touchio

touch_pad = board.A0 # Will not work for Circuit Playground Express!
# touch_pad = board.A1 # For Circuit Playground Express

touch = touchio.TouchIn(touch_pad)

while True:
if touch.value:
print("Touched!")
time.sleep(0.05)

Here's my attempt to convert it away from Circuit Python to the Arduino IDE environment:

//CircuitPython Essentials Capacitive Touch example
import time
import board
import touchio

touch_pad = board.A0 // Will not work for Circuit Playground Express!
touch = touchio.TouchIn(touch_pad)
int led = 13; // blink 'digital' pin 1 - AKA the built in red LED

void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:

if (touch) {
//print("Touched!")
digitalWrite(led, HIGH); }
time.sleep(0.05)
}

And here's the compile errors I get: (I'm also a little confused why it says my variable led was not declared because I think I did that.

Arduino: 1.8.15 (Windows 10), Board: "Adafruit Trinket M0, Small (-Os) (standard), TinyUSB, Off"

sketch_sep19dadapt:2:1: error: 'import' does not name a type

2 | import time

| ^~~~~~

C:\Users\Tami\AppData\Local\Temp\arduino_modified_sketch_715250\sketch_sep19dadapt.ino: In function 'void setup()':

sketch_sep19dadapt:12:9: error: 'led' was not declared in this scope

12 | pinMode(led, OUTPUT);

| ^~~

C:\Users\Tami\AppData\Local\Temp\arduino_modified_sketch_715250\sketch_sep19dadapt.ino: In function 'void loop()':

sketch_sep19dadapt:18:9: error: 'touch' was not declared in this scope

18 | if (touch) {

| ^~~~~

sketch_sep19dadapt:20:22: error: 'led' was not declared in this scope

20 | digitalWrite(led, HIGH); }

| ^~~

sketch_sep19dadapt:21:5: error: 'time' was not declared in this scope; did you mean 'time_t'?

21 | time.sleep(0.05)

| ^~~~

| time_t

exit status 1

'import' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

miktek1
 
Posts: 11
Joined: Tue Mar 16, 2021 9:00 pm

Re: Can't get built in Cap sensing working using Arduino IDE

by mikeysklar on Mon Sep 20, 2021 1:45 pm

It looks like you are mixing in CircuitPython code with Arduino code. eg. import is a keyword only for CircuitPython #include would be traditional C for Arduino and the header files will have different names.

If you want to develop with CircuitPython on your Trinket M0 you will not be using the Arduino IDE at all. Maybe start here?

https://learn.adafruit.com/welcome-to-circuitpython

mikeysklar
 
Posts: 5873
Joined: Mon Aug 01, 2016 8:10 pm

Re: Can't get built in Cap sensing working using Arduino IDE

by miktek1 on Mon Sep 20, 2021 4:14 pm

Thanks for the reply.
Yes, import is a Circuit Python reference. I did try those libraries using the #include but it can't find the touchio libraries.
And I did look at using Circuit Python in the beginning and although it looks straightforward to get started with I'm much more experienced with C and I also learned that Circuit Python doesn't have some of the control structures like Switch Case that C does. And all my other code is working in C

If I try to simplify my question down,
If I want to use the built in Cap Sense on the Trinket M0, will it work by using the #include <CapacitiveSensor.h> library from the Arduino IDE? And then use the external resistors?
Or what other way or what libraries would I use since touchio and other board libraries are not available in the library manager within the Arduino IDE?

Thanks,

miktek1
 
Posts: 11
Joined: Tue Mar 16, 2021 9:00 pm

Re: Can't get built in Cap sensing working using Arduino IDE

by adafruit2 on Tue Sep 21, 2021 11:50 am

Arduino and circuitpython languages are completely different, so you can't just copy and paste code from one to the other
arduino you can use freetouch, such as here
https://github.com/adafruit/Adafruit_Fr ... etouch.ino
https://learn.adafruit.com/gemma-color- ... e/software

adafruit2
Site Admin
 
Posts: 20283
Joined: Fri Mar 11, 2005 7:36 pm

Re: Can't get built in Cap sensing working using Arduino IDE

by miktek1 on Sat Sep 25, 2021 1:43 pm

Thought I would try again to make some progress in trying to get the Cap Sense functionality using the Arduino IDE with the Trinket M0. Latest one +3VDC)
Summary background.
I developed my code using the Arduino Uno with the <CapacitiveSensor.h> library. Touch Sensing worked great.
I want to get similar functionality using the Trinket M0 with programming in the Arduino IDE, not Circuit Python.
I have successfully used the Blink program and other DIO control with my Trinket M0.
I realize that Circuit Python and Arduino C code are completely different.

Here's my current code attempt to use Cap Sense on pins 0(Send) and 2(Receive) Which have a 1M ohm resister across them.
But there is a Complier error and it seems the code is not mapping the pins correctly to the Trinket.
I'm trying to avoid starting over with everything by learning CP as I would like to develop projects using the Arduino with C and then transferring to the Trinket.

Any advice on the compile errors?
Thanks,

CODE

#include <CapacitiveSensor.h>
#include <Adafruit_FreeTouch.h>
int ledPin = 13; // blink 'digital' pin 1 - AKA the built in red LED

CapacitiveSensor cs_0_2 = CapacitiveSensor(0,2); //4.7M Resistor between pins 0(Send) and 2(Receive),
unsigned long csSum;

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
CSread();
delay(5);
}

void CSread() {
long cs = cs_0_2.capacitiveSensor(50); //a: Sensor resolution is set to 80
if (cs > 2500) {
csSum += cs;
//Serial.println(cs);
if (csSum >= 2500) //c: This value is the threshold, a High value means it takes longer to trigger
{
digitalWrite(ledPin, LOW);

if (csSum > 0) { csSum = 0; } //Reset
//cs_0_2.reset_CS_AutoCal(); //Stops readings
}
} else {
digitalWrite(ledPin, HIGH);
csSum = 0; //Timeout caused by bad readings
}
}

COMPILE ERRORS

In file included from C:\Users\Tami\Documents\Arduino\sketch_sep19b_touchexamplefromyoutube\sketch_sep19b_touchexamplefromyoutube.ino:1:
C:\Users\Tami\Documents\Arduino\libraries\CapacitiveSensor/CapacitiveSensor.h:97:2: error: 'IO_REG_TYPE' does not name a type
97 | IO_REG_TYPE sBit; // send pin's ports and bitmask
| ^~~~~~~~~~~
C:\Users\Tami\Documents\Arduino\libraries\CapacitiveSensor/CapacitiveSensor.h:98:11: error: 'IO_REG_TYPE' does not name a type
98 | volatile IO_REG_TYPE *sReg;
| ^~~~~~~~~~~
C:\Users\Tami\Documents\Arduino\libraries\CapacitiveSensor/CapacitiveSensor.h:99:2: error: 'IO_REG_TYPE' does not name a type
99 | IO_REG_TYPE rBit; // receive pin's ports and bitmask
| ^~~~~~~~~~~
C:\Users\Tami\Documents\Arduino\libraries\CapacitiveSensor/CapacitiveSensor.h:100:11: error: 'IO_REG_TYPE' does not name a type
100 | volatile IO_REG_TYPE *rReg;
| ^~~~~~~~~~~
exit status 1
Error compiling for board Adafruit Trinket M0.

miktek1
 
Posts: 11
Joined: Tue Mar 16, 2021 9:00 pm

Re: Can't get built in Cap sensing working using Arduino IDE

by adafruit2 on Sat Sep 25, 2021 1:48 pm

why are you combining two libraries? you can just use freetouch, there's an example, run that example.

adafruit2
Site Admin
 
Posts: 20283
Joined: Fri Mar 11, 2005 7:36 pm

Re: Can't get built in Cap sensing working using Arduino IDE

by miktek1 on Sat Sep 25, 2021 9:07 pm

Do you mean this example from the library info? I hope not because this example code is full of Serial print references which are not supposed to work with Trinket M0

https://github.com/adafruit/Adafruit_Fr ... etouch.ino

Because it has compile errors also.

c:/users/tami/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: sketch\sketch_sep25a.ino.cpp.o: in function `setup':
C:\Users\Tami\AppData\Local\Temp\arduino_modified_sketch_692080/sketch_sep25a.ino:9: undefined reference to `Adafruit_USBD_CDC::begin(unsigned long)'
c:/users/tami/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\Tami\AppData\Local\Temp\arduino_modified_sketch_692080/sketch_sep25a.ino:11: undefined reference to `Adafruit_USBD_CDC::operator bool()'
c:/users/tami/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\Tami\AppData\Local\Temp\arduino_modified_sketch_692080/sketch_sep25a.ino:25: undefined reference to `Serial'
c:/users/tami/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: sketch\sketch_sep25a.ino.cpp.o: in function `loop':
C:\Users\Tami\AppData\Local\Temp\arduino_modified_sketch_692080/sketch_sep25a.ino:56: undefined reference to `Serial'
collect2.exe: error: ld returned 1 exit status
exit status 1

Please let me know any other examples.
I have read through all the "Programming with Arduino IDE sections and the CP sections.
Hoping not to have to use CP.

thanks.

miktek1
 
Posts: 11
Joined: Tue Mar 16, 2021 9:00 pm

Re: Can't get built in Cap sensing working using Arduino IDE

by adafruit2 on Sat Sep 25, 2021 10:21 pm

may need to update your board support packages and all libraries

adafruit2
Site Admin
 
Posts: 20283
Joined: Fri Mar 11, 2005 7:36 pm

Re: Can't get built in Cap sensing working using Arduino IDE

by miktek1 on Thu Oct 07, 2021 10:46 pm

trinketschematic.PNG
schematic
trinketschematic.PNG (43.13 KiB) Viewed 62 times


I thought I would try and tie off this post since I feel I'm so close to getting Cap sensing working. I've updated all libraries.
Here's the latest status and appreciate any input on how to understand a solution to the latest glitch.

Summary and latest problem statement is that I found a simple Arduino C program example that was applicable and modified it and it works!
I am using the code below with the Adafruit_FreeTouch.h library

// Capacitive touch demo using FreeTouch. Trinket M0 version.
// Note: the pin silkscreened 1 is Analog Pin 0.

#include "Adafruit_FreeTouch.h"

Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(A0, OVERSAMPLE_4, RESISTOR_0, FREQ_MODE_NONE);
int led = 13; // blink 'digital' pin 1 - AKA the built in red LED
int FAN = 2; //

void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(FAN, OUTPUT);
digitalWrite(led, LOW);
digitalWrite(FAN, LOW);
delay(50);
// Initialize A0 as a touch sensor
if (! qt_1.begin())
//Serial.println("Failed to begin qt on pin A0");
digitalWrite(led, HIGH); // Turn on the led
}
void loop()
{
int counter;
int result = 0;
counter = millis();
result = qt_1.measure();
if (result > 950) {
digitalWrite(FAN, HIGH); // Turn on the FAN
digitalWrite(led, HIGH); // Turn on the led
}
else {
digitalWrite(FAN, LOW); // Turn off the FAN
digitalWrite(led, LOW); // Turn off the led
}
delay(50);
}

I have attached the circuit I've put together on my proto board.

I can touch the foil I've connected to a wire from A0 and it turns on the fan and the built in LED just fine.
BUT, the glitch is that it only works reliably When the USB cable is connected to my laptop.
When the USB cable is disconnected, It goes into an intermittent mode where the output #1 is very unstable which constantly switches the fan and led off and on randomly.
I have tried different series resisters, different Oversample parameters and Freq Modes.
the 12volts supply is a simple wall transformer that has 1.25A max output. This feeds a DC-dc converter for the 5VDC going into the BAT of the Trinket.
I've checked every ground and wire many times.
One other point is that I arrived at the 950 value threshhold purely by trial and error. I'm still looking for some good reference material on how to use that statement for the touch parameters.

Any suggestions? I feel so close.

Thanks.
Attachments
trinketproto.jpg
trinketproto.jpg (110.54 KiB) Viewed 62 times

miktek1
 
Posts: 11
Joined: Tue Mar 16, 2021 9:00 pm

Re: Can't get built in Cap sensing working using Arduino IDE

by miktek1 on Thu Oct 07, 2021 11:21 pm

Sorry there was an error on my schematic. I don't really have 5V tied to Ground.
Attachments
trinketschematic.PNG
trinketschematic.PNG (8.79 KiB) Viewed 60 times

miktek1
 
Posts: 11
Joined: Tue Mar 16, 2021 9:00 pm

Re: Can't get built in Cap sensing working using Arduino IDE

by adafruit2 on Fri Oct 08, 2021 12:09 pm

that isnt surprising, the 12V motor is incredibly noisy and its going to mess with readings of all sorts, you could try powering the trinket seperately with a battery pack or just use the threshold you found

adafruit2
Site Admin
 
Posts: 20283
Joined: Fri Mar 11, 2005 7:36 pm

Re: Can't get built in Cap sensing working using Arduino IDE

by miktek1 on Fri Oct 08, 2021 12:34 pm

I can understand how fans can be noisy.
However, are you saying the Trinket is particular susceptible to noise?
And I'm not sure how that fits in with my experience of :

1) Having no issue with Arduino Uno being powered with a DC-DC converter output that also uses the 12VDC source from wall transformer to power the fan
2) The Trinket works fine when connected through USB cable to laptop.

Many applications and projects for these chips need to be able to be plugged in to a line voltage.
Battery power is not feasible in these cases. Although it may be a good experiment to try to see if it really solves the issue.

miktek1
 
Posts: 11
Joined: Tue Mar 16, 2021 9:00 pm

Please be positive and constructive with your questions and comments.