I am trying to replicate the behaviour of this library: https://playground.arduino.cc/Main/CapacitiveSensor/ with an STM8, but I am not able to do it. I want to detect the proximity of the human hand from about 10 cm. I don't have sensibility, i can only detect a strong pression on the capacitive sensor (aluminium foil) and even the heartbeat, but no the proximity. With this library I had a perfect beahiour. I am using a 1 Mohm resistor.
This is a piece of code:
disableInterrupts()
GPIOD->ODR &= 0b11111101; // sendPIN D1 to LOW
GPIOE->DDR &= 0b11111011; // receivePIN E2 to INPUT (=0)
GPIOE->DDR |= 0b00000100; // receivePIN E2 to OUTPUT (=1)
GPIOE->ODR &= 0b11111011; // receivePIN E2 to LOW
// delayMicroseconds(10)
enableInterrupts();
for ( t = 0; t < 1000; t++);
disableInterrupts()
GPIOE->DDR &= 0b11111011; // receivePIN E2 to INPUT
GPIOD->ODR |= 0b00000010; // sendPin High
enableInterrupts();
for ( t = 0; t < 1000; t++);
//getADCValue();
In = (GPIOE->IDR & GPIO_Pin_2);
while (In == 0) {
In = (GPIOE->IDR & GPIO_Pin_2);
total++;
}
//stopTime = TIM3->CNTRH;
disableInterrupts();
GPIOE->ODR |= 0b00000100; // receivePIN E2 to HIGH
GPIOE->DDR |= 0b00000100; // receivePIN E2 to OUTPUT (=1)
GPIOE->ODR |= 0b00000100; // receivePIN E2 to HIGH
GPIOE->DDR &= 0b11111011; // receivePIN E2 to INPUT (=0)
GPIOD->ODR &= 0b11111101; // sendPIN D1 to LOW
enableInterrupts();
for ( t = 0; t < 100; t++);
In = (GPIOE->IDR & GPIO_Pin_2);
while (In != 0) {
In = (GPIOE->IDR & GPIO_Pin_2);
total++;
}
In the image you can see the two pins, and you can see the blue one cannot reach the 3.3 V treshold, and it does not change much with aluminium foil touch.
Is there a better way to build a proximity sensor?
Thank you
EDIT 1: This is my circuit, extremely simple:
Instead of clamp is attached the aluminium foil. I did use the 1x probe.
EDIT 2: Ok, the probe was acting as voltage divider as you say. I also the function Delay that is connected to TIM4 in the STM8 I am using, event if I don't think it was actually a problem. I removed the probe and it works better. Actually the program works, but I can't have a good sensing range (looks to be 4-5 cms). Is there any way to increase the sensitivity? I tried reading the ADC after a defined and certain time, but it looks to have the same sensibility. Maybe it is the maximum? I Arduino and CapacitiveSensor I had a very long range (something like 20 cms)