Capacitive Sensor on a standalone AtMega328P

Hi,

I'm working on an standalone AtMega328P project that uses a capacitive sensor as a button, but I'm getting stuck right now. The project does work without problems on an arduino board, but as soon as I upload on a standalone AtMega328P the capacitive sensor is no longer working. The project is an LED lamp. The code is on github here. Also here is my schematic:

I'm currently using the <CapacitiveSensor.h> library with a 50MΩ resistor between pin 4 and 2 with a 100pF capacitor to ground. I was able to get consistant readings with the board, but I get the error value "-2" with the AtMega328P.

If someone can tell me: is it only possible to use capacitive sensor this way with a standalone microcontroller, if not, what can be done? Also, if someone is able to tell me why it cannot work on the microcontroller as a standalone, I'm still very interested in understand how they differ and how to mitigate this.

Capacitive sensors can be sensitive to the grounding plane of your board (or standalone construction). You might try using a star point ground design - where all system grounds are wired directly to the power ground point (rather than all grounds wired together at various points around the board and then occasionally or at one point connected to the power ground. To a dc engineer, this doesn't make sense sometimes (aren't all ground points the same at the same potential?) Yes. but when there are switching signals that act like AC (clocks, PWM any other switched signals), these introduce ac components to the circuit board and cause ac imparted behaviors, such as ground loops, eddy currents and oscillations that can affect capacitive sensors.

It may be in your case that the Arduino board ground plane profile is optimized to minimise such interference, but that your standalone board design doe not account for it. When measuring, your scope (or the negative lead of your meter) should (in most cases) be directly connected to system ground as well.

Thinking of grounds in general, do ensure that all system grounds on all connected systems are connected together to a common system ground point. Many experimenters have been undone by not interconnecting all grounds to a common point.

If you're open to alternatives, I have very good experience with the ADCtouch library. It doesn't require any external components, but I've used a series resistor for good measure. a 10k one should work fine if your hardware allows it.

I'll look into it. Might be a solution. I used the library for no other reason that it was the first I found that had a solution for my goal.

Ok, I'm just so f*ing dumb... I plugged the capacitive touch in A2 and A4 not the digital pins. Makes a lot of sense why I was getting a time out error (-2).

Wouldn't you know it, it works perfectly when you wire it correctly. That'll teach me to check my wiring before posting next time.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.