LED fading in with capacitance sensor

I am very new to Arduinos and coding so bare with me.

I am trying to create a circuit where the LED gradually increases in brightness as the user's hand approaches it.

I have created a capacitance sensor and have managed to get the LED to go on and off, I was just wondering how I can make it more of a 'fade' or will I achieve this effect just by upping the resistance?

At the moment I am using 1Mohm resistor and my threshold is very low so the LED turns on when the hand is approx 10cm away from the sensor, there is however a lot of flicker which I'd prefer to get rid of? Will a capacitor do the trick here?

Thank you for your help


Thanks for the video, that's really informative, but I want to use a capacitive sensor that i've already made, not a photosensitive resistor (as it will ultimately be beneath an insulating surface.

What parts of the code would I need to change to make it apply to my circuit?

Thank you :slight_smile:


Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Can you please post a copy of your sketch, using code tags?
Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Hope to help..Tom.... :slight_smile:

At the moment, I’m pretty much following any tutorials I have found, so this is an amalgamation of touch capacitance code for an LED and some fade code. But as I’m a beginner it’s probably not quite right, as I’ve pretty much just played around putting it in different places until I get my (almost desired effect).

As you can see I’m not very confident with this whole process haha

My code is below and regarding the circuit I’m not quite sure how I’d draw it but it’s just the standard LED touch capacitor circuit, with a 1Mega Ohm resistor connected to pin 2, and my capacitive sensor (foil) and the other end going to pin 4, and my led coming from pin 11 to ground.

I’ve almost achieved a fade effect but it’s very very choppy (/flickery) and I’d like to achieve a smoothness like we see using the basic fade example.

I hope this helps you a little and thanks for all the help! :slight_smile:

#include <CapacitiveSensor.h>

// create an instance of the library
// pin 4 sends electrical energy
// pin 2 senses senses a change
CapacitiveSensor capSensor = CapacitiveSensor(4,2);

// threshold for turning the lamp on
int threshold = 30;

// pin the LED is connected to
int ledPin = 11;

int brightness = 0;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by

void setup() {
  // open a serial connection
  // set the LED pin as an output
  pinMode(ledPin, OUTPUT);

void loop() {
  // store the value reported by the sensor in a variable
  long sensorValue = capSensor.capacitiveSensor(30);

  // print out the sensor value

  // set the brightness of pin 9:
  analogWrite(ledPin, brightness);  
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;
  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;}     

  // if the value is greater than the threshold
  if(sensorValue > threshold) {
    // turn the LED on
    digitalWrite(ledPin, HIGH);
  // if it's lower than the threshold
  else {
    // turn the LED off
    digitalWrite(ledPin, LOW);


Have you got a current limit resistor in series with the LED?
Are you using the monitor in the IDE to check your values?

Tom..... :slight_smile:

Hi Tom,

No I'm not using a resistor in series with the LED at the moment because I only have the one! Must have managed to drop that one on my way home the other day. I'll get some more on Monday.

Would that be a cause of the sporadic lighting?

If you are referring to the serial monitor on the Arduino software then yes I am, I can see those, but the do go very fast. So, I slowed them down by creating a delay of 1000 and then the fading didn't seem to work - it was just going on and off as it should (when the value was greater than the threshold)

Oliver :slight_smile:

[Edit: I managed to find my 270 ohm resistor, so I've put that in series with the LED - but it's still very flickery]

Can you post a picture of your project so we can see your layout.
Please the circuit diagram, if you have to trace what you have as you draw it, that may find any problems that have occured, hand drawn photograph is fine.

When using capacitive input, it can become sensitive to noise/interferrence form switching circuits

Tom… :slight_smile:

Okay, I'm not sure if these are any good as I said, I wasn't sure how to draw the circuit correctly, but here's my attempt, with a photo too.

Is the code all okay then? Maybe the flickering is down to the way i've coded it?

Thanks :slight_smile:


Try this connection for your cap sensor, you many have the transmit and sense pins the wrong way.

Tom… :slight_smile:

Actually I think that’s what I have already got, makes much more sense that way.

I think tomorrow I’ll get some bigger resistors and see if that makes the fade more smooth (over a longer distance)