Arduino Forum

Using Arduino => Sensors => Topic started by: KevinArduino on May 02, 2013, 07:07 pm

Title: Trouble using cap sensor to light LEDs
Post by: KevinArduino on May 02, 2013, 07:07 pm
Hi all,

I'm new to arduino but I decided its time to jump in and start my first real project.  Eventually I'm going to want to incorporate cap sensors into this project so I figured it wasn't a bad place to start.  I was able to find the cap sense library here:

http://playground.arduino.cc/Main/CapacitiveSensor?from=Main.CapSense (http://playground.arduino.cc/Main/CapacitiveSensor?from=Main.CapSense)

I can see on the serial monitor that the switches are being properly recognized but when I try to use the data to light an LED it is very dim.  If I drive the LED instead with an npn transistor I get the brightness I want.  I therefore assume that my Arduino doesn't have the driving capability to use the cap sensors and light an LED at the same time.  Is this something I'm going to have to accept and add the transistor circuit or am I missing something?  I realize its not a big deal to add a transistor but I figured I might be able to learn something by asking.  Thanks!

-Kevin
Title: Re: Trouble using cap sensor to light LEDs
Post by: Grumpy_Mike on May 02, 2013, 11:08 pm
No the arduino will drive an LED quite well.

My guess is that you forgot to make the LED pin into an output in the setup function.
You need a resistor in line with the LED as well.
Title: Re: Trouble using cap sensor to light LEDs
Post by: KevinArduino on May 06, 2013, 10:37 pm
Quote
My guess is that you forgot to make the LED pin into an output in the setup function.


So you were right that there was something funky going on in the setup and I got it working but just out of curiosity I was wondering if you could add some insight to why this code below that I found on the Arduino site under ForLoop was causing the issue.

for (int thispin = 2; thispin > 13;thispin++){
    pinMode(thispin, OUTPUT);
}

I found this code here:
http://arduino.cc/en/Tutorial/ForLoop (http://arduino.cc/en/Tutorial/ForLoop)

My understanding was that this code put in the setup section would pins 2-13 into outputs.  When I replaced it with this more unwieldy code it fixed everything:

     pinMode(2, OUTPUT);
     pinMode(3, OUTPUT);
     pinMode(4, OUTPUT);
     pinMode(5, OUTPUT);
     pinMode(6, OUTPUT);
     pinMode(7, OUTPUT);
     pinMode(8, OUTPUT);
     pinMode(9, OUTPUT);
     pinMode(10, OUTPUT);
     pinMode(11, OUTPUT);
     pinMode(12, OUTPUT);
     pinMode(13, OUTPUT);

Any idea what was happening?
Title: Re: Trouble using cap sensor to light LEDs
Post by: Grumpy_Mike on May 07, 2013, 04:36 am
You miss read the tutorial.

This code is wrong:-
Code: [Select]
for (int thispin = 2; thispin > 13;thispin++){
   pinMode(thispin, OUTPUT);
}

The loop will never run through the values because the middle statement is always false and the loop only runs while the middle statement is true.
it should be
Code: [Select]
for (int thispin = 2; thispin < 13;thispin++){
   pinMode(thispin, OUTPUT);
}
Title: Re: Trouble using cap sensor to light LEDs
Post by: KevinArduino on May 07, 2013, 03:26 pm
Oh that's right >< I was thinking the middle was the condition for which it would stop.  Thanks a bundle.