Skip to content

Instantly share code, notes, and snippets.

@DedeHai
Last active October 5, 2023 03:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DedeHai/3d3d9854d9c6a329496ff6d7e4b23e0c to your computer and use it in GitHub Desktop.
Save DedeHai/3d3d9854d9c6a329496ff6d7e4b23e0c to your computer and use it in GitHub Desktop.
/*
Capacitive Sensing methods using Arduino
Visit https://bashtelorofscience.wordpress.com/ for a description and the circuit required to make this work.
Please see https://www.arduino.cc/en/Reference/PortManipulation for an explanation on the direct I/O access used in this sketch.
note:
the assembler command
__asm__("nop\n\t");
is used in the sketch to do single clock (62ns) delays.
MIT License
Copyright (c) 2019 Damian Schneider
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
uint16_t zerovalue_transfer;
uint16_t zerovalue_overflow;
uint16_t zerovalue_resistor;
byte sense = 0;
void setup() {
//start serial connection
Serial.begin(115200);
Serial.println("capacitive sensor");
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
delay(1);
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
delay(1);
pinMode(4, INPUT);
long sum_transfer = 0;
long sum_oveflow = 0;
long sum_resistor = 0;
for (byte i = 0; i < 16; i++)
{
sum_transfer += chargetransfermethod();
sum_oveflow += filteroverflowmethod();
sum_resistor += resistormethod();
}
zerovalue_transfer = sum_transfer / 16;
zerovalue_overflow = sum_oveflow / 16;
zerovalue_resistor = sum_resistor / 16;
}
float lowpass = 0;
void loop() {
//print each sensing methods with an offset to make all values positive
Serial.print(resistormethod() - zerovalue_resistor + 10020);
Serial.print("\t");
Serial.print(chargetransfermethod() - zerovalue_transfer + 10000);
Serial.print("\t");
Serial.println(filteroverflowmethod() - zerovalue_overflow + 9980);
//
//Serial.println(chargetransfermethod());
//Serial.println(filteroverflowmethod());
// Serial.println(resistormethod());
delay(2);
}
//algorithm to do capacitive sensing using the charge transfer method. Pins 5 and 6 on the arduino are used
uint16_t chargetransfermethod(void)
{
uint16_t counter = 0;
cli(); //disable interrupts to get an exact timing
while (counter < 10000)
{
DDRD &= ~(1 << DDD6); //pinMode(6, INPUT);
PORTD |= (1 << PD5); //digitalWrite(5, HIGH);
DDRD |= (1 << DDD5); //pinMode(5, OUTPUT);
while ((PIND & B01000000) == 0); //wait for pin 6 to charge up
DDRD &= ~(1 << DDD5); //pinMode(5, INPUT); //make the large capacitor floating
PORTD &= ~(1 << PD5); //digitalWrite(5, LOW); //disable the pullup resistor
DDRD |= (1 << DDD6); //pinMode(6, OUTPUT); //discharge the small sensing cap, ground the large cap
__asm__("nop\n\t"); //for voltage to stabilize
//the above code without any direct port manipulation (much slower)
// pinMode(6, INPUT);
// digitalWrite(5, HIGH);
// pinMode(5, OUTPUT);
// while (PIND & B01000000 == 0); //wait for pin 6 to charge up
// pinMode(5, INPUT); //make the large capacitor floatin
// digitalWrite(5, LOW); //disable the pullup resistor
// pinMode(6, OUTPUT); //discharge the small sensing cap, ground the large cap
if (PIND & B00100000) //check if large cap is charged already
break;
__asm__("nop\n\t"); //wait for the small cap to fully discharge
__asm__("nop\n\t");
// __asm__("nop\n\t");
counter++;
}
sei(); //re-enable interrupts now
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
//delay(1);//wait for discharge
// Serial.print("counter = ");
//Serial.println(counter, DEC);
//delay(5);
// Serial.println((int)lowpass, DEC);
return counter;
}
uint16_t filteroverflowmethod(void)
{
pinMode(4, INPUT);
uint32_t counter = 0;
sense = 0;
cli();
//pre-charge to speed up the method
PORTD = B00001000; // digitalWrite(3,HIGH);
for (int i = 0; i < 50; i++) //value for the loop is found by testing
{
__asm__("nop\n\t"); //wait one clock
}
PORTD = B00000000; // digitalWrite(3,LOW);
//"small" capacitive probe size
while (sense == 0 && counter < 10000)
{
PORTD = B00001000; // digitalWrite(3,HIGH);
__asm__("nop\n\t"); //wait one clock
PORTD = B00000000; // digitalWrite(3,LOW);
counter++;
sense = PIND & B00010000; //sense = digitalRead(4);
}
//"medium" capacitive probe size
while (sense == 0 && counter < 20000)
{
PORTD = B00001000; // digitalWrite(3,HIGH);
__asm__("nop\n\t");//wait one clock
__asm__("nop\n\t");//wait one clock
PORTD = B00000000; // digitalWrite(3,LOW);
counter++;
sense = PIND & B00010000; //sense = digitalRead(4);
}
//"large" capacitive probe size
while (sense == 0 && counter < 50000)
{
PORTD = B00001000; //digitalWrite(3,HIGH);
__asm__("nop\n\t"); //wait one clock
__asm__("nop\n\t"); //wait one clock
__asm__("nop\n\t"); //wait one clock
__asm__("nop\n\t"); //wait one clock
// __asm__("nop\n\t"); //wait one clock
PORTD = B00000000; //digitalWrite(3,LOW);
counter++;
sense = PIND & B00010000; //sense = digitalRead(4);
}
sei();
digitalWrite(3, LOW);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
return counter >> 4; //reduce noise by removing 4 LSB
}
uint16_t resistormethod(void)
{
digitalWrite(8, LOW);
digitalWrite(9, LOW);
pinMode(9, INPUT);
pinMode(8, OUTPUT);
digitalWrite(8, HIGH);
uint16_t counter = 0;
sense = 0;
cli();
while (sense == 0)
{
counter++;
sense = digitalRead(9);
}
sei();
pinMode(9, OUTPUT); //discharge the capacitor now
digitalWrite(8, LOW);
return counter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment