1
\$\begingroup\$

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

enter image description here

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:

enter image description here 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)

\$\endgroup\$
7
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Sep 10, 2020 at 23:53
  • \$\begingroup\$ It is likely that your busy-delay loops are screwing up everything. Use hardware timers instead. \$\endgroup\$
    – Lundin
    Commented Sep 11, 2020 at 7:35
  • \$\begingroup\$ @Lundin why do you think that? \$\endgroup\$
    – max_s
    Commented Sep 11, 2020 at 10:02
  • \$\begingroup\$ @ChrisStratton I used PicoScope 2204A with 1x probe. I added the circuit to the post. \$\endgroup\$
    – max_s
    Commented Sep 11, 2020 at 10:08
  • \$\begingroup\$ @max_s Because input may happen at any time and not just when it is convenient for your program to read it. \$\endgroup\$
    – Lundin
    Commented Sep 11, 2020 at 10:09

1 Answer 1

1
\$\begingroup\$

The 1 megaohm source resistor and comparable input impedance of your scope with a 1x probe form a voltage divider preventing the test signal from reaching more than about half value.

Remove the scope probe - this circuit is too sensitive. If you must try to probe use a 10x probe, but it would be better that you have nothing extraneous connected to the test node at all.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.