# Arduino Capacitive Sensor

Professor Dawn asked us to create a circuit, using foil or some kind of resistant materials. Also it was key to use the Capacitive Sensor code.

The Arduino library didn’t have the Capacitive code in its library,  so it needed to be downloaded,  using the links from the Arduino website,  I posted it below.

http://arduino.cc/en/Guide/Libraries

I had to learn how to import zip library. After making sure the Arduino has the Capacitive sensor added in the library. I searched different circuits I can try and create on my own. So I challenged myself to do :

# A Pencil drawing into a capacitive sensor for Arduino

My bill of materials were really simple.

• Arduino uno
• paperclip (must be bare, not coated in plastic )
• A pencil
• A paper
• 3 Wires
• scotch tape (optional )
• 1 megaohm resistor
• 1 led light

The circuit is suppose to work by attaching a wire to a paperclip,  attaching the paperclip to the paper, and wherever you write on the paper it’s suppose to react to your touch and the Led should light up. Whatever you write you must make sure that all the words are in a straight line and connected.  You can see what I drew below.  As you notice it goes in a straight line, from where the paper clip is attached to where the pencil drawing begins.

So what I’ve learned from pencils is that  Pencil lead is actually graphite, a form of carbon.

Graphite can conduct electricity due to the vast electron delocalization within the carbon layers. ( a phenomenon called aromaticity). These valence electrons are free to move, so are able to conduct electricity. However,  the electricity is only conducted within the plane of the layers.

So now we’ve learned why the pencil can be used. The next step was coding.

For this you can check out the two videos I made. I had to install the zip import into the library,  which simply means we added Arduino into the library, I ran into trouble because I had downloaded the capacitive sensor, but it wasn’t actually in the library.  So what happened was the code was verifying,  but it would not upload.  So when I went back and checked to see if I saw capacitive sensor in the library,  I went back and corrected it.

So this is the original code I was tackling with

```// Pin for the LED
int LEDPin = 13;
// Pin to connect to your drawing
int capSensePin = 2;
// This is how high the sensor needs to read in order
//  to trigger a touch.  You'll find this number
//  by trial and error, or you could take readings at
//  the start of the program to dynamically calculate this.
int touchedCutoff = 60;

void setup(){
Serial.begin(9600);
// Set up the LED
pinMode(LEDPin, OUTPUT);
digitalWrite(LEDPin, LOW);
}

void loop(){
// If the capacitive sensor reads above a certain threshold,
//  turn on the LED
digitalWrite(LEDPin, HIGH);
}
else {
digitalWrite(LEDPin, LOW);
}

// Every 500 ms, print the value of the capacitive sensor
if ( (millis() % 500) == 0){
Serial.print("Capacitive Sensor on Pin 2 reads: ");
}
}

//  Input: Arduino pin number
//  Output: A number, from 0 to 17 expressing
//          how much capacitance is on the pin
//  When you touch the pin, or whatever you have
//  attached to it, the number will get higher
//  In order for this to work now,
// The pin should have a 1+Megaohm resistor pulling
//  it up to +5v.
// This is how you declare a variable which
//  will hold the PORT, PIN, and DDR registers
//  on an AVR
volatile uint8_t* port;
volatile uint8_t* ddr;
volatile uint8_t* pin;
// Here we translate the input pin number from
//  Arduino pin number to the AVR PORT, PIN, DDR,
//  and which bit of those registers we care about.
if ((pinToMeasure >= 0) && (pinToMeasure <= 7)){
port = &PORTD;
ddr = &DDRD;
pin = &PIND;
}
if ((pinToMeasure > 7) && (pinToMeasure <= 13)){
port = &PORTB;
ddr = &DDRB;
bitmask = 1 << (pinToMeasure - 8);
pin = &PINB;
}
if ((pinToMeasure > 13) && (pinToMeasure <= 19)){
port = &PORTC;
ddr = &DDRC;
bitmask = 1 << (pinToMeasure - 13);
pin = &PINC;
}
// Discharge the pin first by setting it low and output
delay(1);
// Make the pin an input WITHOUT the internal pull-up on
// Now see how long the pin to get pulled up
int cycles = 16000;
for(int i = 0; i < cycles; i++){
cycles = i;
break;
}
}
// Discharge the pin again by setting it low and output
//  It's important to leave the pins low if you want to
//  be able to touch more than 1 sensor at a time - if
//  the sensor is left pulled high, when you touch
//  two sensors, your body will transfer the charge between
//  sensors.

return cycles;
}

Here is a diagram of the actual  circuit built Here are some more pictures of my circuit  After my code was uploaded,  I still couldn't get it to work. I checked to make sure the Led was working, As you see my LED was working.

I think their some theory is the way the wires are set up.

One wire is in 5v that's your power, the 2nd wire is in pin 2, instead of going in the ground pin, this is how the wires are set up according to the code. So I will do more trouble shooting.  But the code is correct and went through,  that's half the battle I believe.

Below is a video of how the circuit is suppose to work I hope you enjoy.

```