Capacitive medium range sensor with STM8

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++);
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)

• Show your circuit and explain how you got your data. Some sort of USB scope? What is your scope's impedance? Did you use a 10x probe? Something like a 1 Meg probe impedance could easily form a voltage divider with a weak current source used for such a thing. – Chris Stratton Sep 10 '20 at 23:53
• It is likely that your busy-delay loops are screwing up everything. Use hardware timers instead. – Lundin Sep 11 '20 at 7:35
• @Lundin why do you think that? – max_s Sep 11 '20 at 10:02
• @ChrisStratton I used PicoScope 2204A with 1x probe. I added the circuit to the post. – max_s Sep 11 '20 at 10:08
• @max_s Because input may happen at any time and not just when it is convenient for your program to read it. – Lundin Sep 11 '20 at 10:09