Turn a pencil drawing into a capacitive sensor for Arduino

 by alan.chatham
Featured
Touch2.png
Did you know?  You can make pencil drawings reactive to touch for use with your projects!  It's really easy, and gives you a lot of flexibility in making interfaces for whatever microcontroller project you're making.



I put this together as part of my UnoJoy project, where we're using the Arduino as a platform from which to explore how we interact with video games.  You can check out more of our projects for UnoJoy at unojoy.tumblr.com, or more of our other projects at the Exertion Games Lab.
 
Remove these adsRemove these ads by Signing Up

Step 1: Materials

You'll need the following:

A pencil - the softer the lead, the better
Some 1+Megaohm resistors 
Paperclips - make sure they're bare metal, not coated in plastic
Wire
An Arduino
Clear tape (optional)
1-40 of 58Next »
Snoop Lion says: May 20, 2013. 3:38 PM
Awesome article! I am having some trouble with adding multiple different sensors, and I did:

int firstSensor = readCapacitivePin(2);
int secondSensor = readCapacitivePin(3);

but if I touch both sensors at the same time that causes the program to hang. I used the same voltage source and added a resistor from there to pin 4 as well. Any advice?
rbhat1 says: Apr 29, 2013. 11:48 PM
Hello, I have recently tried out this from a using a capsense PCB and apparently the wires at some level also carry a degree of capacitance.Is there anyway to filter the circuit more in order to get a better output?

By the way, this was a really cool project! :)
contactscolored says: Mar 26, 2013. 3:38 AM
amazing
smh08 says: Feb 8, 2013. 2:26 PM
Hey your project is very good and interesting. But when I tried it the LED is always on and when I touch the paper clip or writing it flickers/blink. I tried to change some of the code but it doesn't work. Do you know why I am having this problem? Thank You.
Valtymore says: Jan 27, 2013. 10:54 AM
Hey Alan!
At first great thanks for this article. It has helped a lot!
I have a little problem: everything works very well with the uno, then I tried with a mega2560, I didn't change the code, and it didn't work at all. So I wondered if I should change something in the code ?!
thx !
JensonBut says: Jan 20, 2013. 9:51 PM
Thanks for the great article, and for sharing the great results to your effort
rzahid says: Oct 19, 2012. 6:10 AM
hello.. your project is amazing,, i tried it but my led is constantly glowing.. i dont knw what is the matter,, i checked all connections,, dont knw from where it is gettin the charges.. :/ ..
padorange says: Jun 20, 2012. 12:35 AM
I just try last night but i did not work as a except.
I do not have a 1 Mohm, so i used serial 3x 330Kohms.

But it didn't work detecting anything with pencil draw.
It can detect when i touch directly the paper clip.

I got (return but serial connection) :
14 cycles (when i did not touch anything)
70 to 80 cycles when i touch the paper clip.

I used your code on an Arduino Uno R3 using pin 2 as in your example.
alan.chatham (author) in reply to padorangeJul 13, 2012. 2:08 PM
If you're getting a response by touching the paper clip, then the circuitry is working. There may be an issue with the thickness of your pencil trace - I find that going over the line multiple times is sometimes necessary, and you can test the conductivity of your trace by touching the trace, then, holding the paperclip by the insulated wire, touching the paperclip to different parts of the trace and seeing if it still triggers the sensor.
stringstretcher in reply to alan.chathamOct 18, 2012. 3:32 AM
When I built a Drawdio I experimented a LOT with different pencils, and there is a BIG difference in the way they respond with a circuit like this. I went to the art store and bought soft lead sketching pencils, and a 4b or 6b worked great, much better than a #2 or HB. The problem is not difficult to solve, experiment a bit!
bbbbbbob says: Sep 13, 2012. 2:30 AM
Hi
I'm a beginner in arduino and tried your wonderful project.
But my LED is staying lighted whatever the cutoff. I'm using an arduino MEGA2560. Should it make any difference ?
A last question about the circuit : should there be any connection between the ground of the arduino board and the drawing or myself ?

Thanks again for the great project and your help.
Julien
techiebot says: Jul 21, 2012. 12:25 PM
I got the circuit to work first try, but only by touching the paperclip. I think I need a better pencil to make it work with the graphite. (I tried various values to increase sensitivity but no go with graphite).

However, I have some Bare conductive paint from Sparkfun and it works great! I just painted a line and the circuit reacts all the way down. I am thinking of making an interactive popup book and touching the lines would be the way to interact with the page.
NelloB says: Jun 10, 2012. 7:36 PM
This is a nice little project apart from a couple of basic problems.

1. How about an explanation? How does this work exactly? How does that map to your code?

2. A non-handwritten circuit diagram would be nice

3. Why paperclips? Surely any wire is sufficient, or did I miss something. Hard to say without an explanation. See point #1.

My son has built this, and it works. What did he learn? How to download a file from the internet, and decipher a hand-drawn circuit diagram. What does he know about capacitve sensors? The same as when he started.
techiebot in reply to NelloBJul 21, 2012. 12:22 PM
Hi NelloB-
I hope that this doesn't sound at all snotty - but as a long-time teacher, I felt like replying...
Instructables are (to my mind) meant to be like a bit of a show and tell. It's like having a ton of geeky friends who stop by and show off what they are working on. Most of us have multiple projects going in various stages of successful operation.

I love it that your son built this circuit. But stopping there kind of misses the point. After I built it, I became curious. What does a capacitor really do? Can a stack of coins be a capacitor? So I started to experiment. I tried putting various metals on the trace - then I tried touching the + and - terminals of a 9volt battery - then I tried touching a pencil to it the lead tip, and also the metal eraser holder - then I tried different plastics - wire attached to the cement floor - and on and on. The point of a circuit like this is what can or can't it do? How could I use it? How can I learn more about capacitance by experimenting with it.

Sorry - teacher mode - anyway I hope that your son and others take the circuit and do amazing things with it! And learn by doing...
jabelone in reply to NelloBJun 10, 2012. 9:12 PM
**Just for the record coming from a 14 yo**

1. See step 2 in this Instructable it says this:

There are 3 ideas going on here:
1 - When you touch a conductive object, you create a certain degree of capacitance. This increases the ability of the conductive material you touched to store a charge.

2 - We can create a cheap capacitive sensor by measuring how long it takes for a piece of conductive material to go from a grounded state to a higher potential state when pulled up to that higher state through a resistor. The higher the capacitance, the longer it will take the conductive material to be pulled up to the high state.

3 - Graphite conducts electricity. You may have seen this before in the Drawdio project, where they use a pencil trace to create sound.

Therefore, if we take our conductive pencil line, pull it to ground, then try and pull it to a higher state, we can measure how long it takes, and if we're touching the drawing, it will take longer to get to a high state than usual. Conveniently, we can use the Arduino to automatically pull our trace to ground on and off. If we attach a pull-up resistor to our line, when the ground connection inside the Arduino is turned off, everything attached to the pin will start to be pulled up to VCC. So, our circuit diagram will look something like the above. We use an external pull-up resistor rather than the Arduino's internal pin pull-up resistors since we need much more resistance than the 30K ohm internal ones, otherwise, the resistance of the graphite tends to dominate and prevent the sensor from working.


2. The hand-drawn schematic as you are supposed to call it is very neat and very easy to understand if you know how to read them.

3. Paperclips are very easy to "clip" onto paper and conduct electricity so basically they are just extending the wire to the graphite.  That is why they are used.

4. What did he learn? See point #1

Be thankful someone has taken hours of their time to put together such a very easy to follow and useful Instructable for your benefit, not theirs.

Next time do a little research before posting a comment like this:
 
"What did he learn? How to download a file from the internet, and decipher a hand-drawn circuit diagram. What does he know about capacitve sensors? The same as when he started."
jabelone says: Jun 4, 2012. 2:53 AM
Hello, awesome idea.
I have converted my UNO into a joystick and can't upload sketches. So how would I be able to use this capacitive touch sensor to act as if I am pressing a button on the UNOjoy joystick?
alan.chatham (author) in reply to jabeloneJun 4, 2012. 10:28 AM
Thanks for trying out UnoJoy! This capacitive sensor technique goes great with UnoJoy, but you'll have to upload a new sketch onto your Arduino.

One of the great things about UnoJoy is that the process is totally reversible - if you plug the UnoJoy Arduino back into your computer and put it into DFU mode again (by shorting the same pins you did when converting it into a joystick), then run the 'TurnIntoAnArduino.bat' or .command program.

If you want some sample code that uses this graphite touch sensor as joystick input, you can give this code a try:
http://code.google.com/p/unojoy/source/browse/#hg%2FExamples%2FDrawnController

Let me know if you need any more help!
jabelone in reply to alan.chathamJun 4, 2012. 2:08 PM
Thanks, but I can't upload sketches to the UNOjoy, so how would I put that sample code onto it? The only way I can upload code is to revert it back to proper Aduino but then it doesn't act as a Joystick. So exactly how would I be able to use the capacitive t
alan.chatham (author) in reply to jabeloneJun 4, 2012. 6:25 PM
So, UnoJoy is a two part system -

1. Code that's running on the ATmega328p chip that reads in whatever input you want, then sends serial data to the communications chip.

2. A firmware for the communications chip which translates the serial data into joystick signals.

So, when developing your controller code for UnoJoy, you'll put your Arduino into the Arduino mode so you can upload code to the ATmega328p chip, then put the Arduino back into the UnoJoy mode, where it turns the serial data that the UnoJoy code on the ATmega328p is sending out into joystick signals.

While it's in Arduino mode, but running code that uses the UnoJoy library, the tool in the UnoJoyProcessingVisualizer folder takes in the serial data that the Arduino communication chip is passing through and provides a visualization of what sort of signals the code is putting out.
jabelone in reply to alan.chathamJun 5, 2012. 1:56 AM
Ok, thanks, i thought that might be the case but wasn't to sure.
I am trying to make a platform game using Unity and then make a "Touch Controller" for the character and actions.

So... to confirm...
1) Upload Sample Capacitive Touch code to arduino (arduino mode)
2) Change into Unojoy and plug into pc for game controller (UNOJoy mode)
3) Play games with a "magic piece of paper."
alan.chatham (author) in reply to jabeloneJun 5, 2012. 2:13 AM
Yep, that should get you close to where you need to be. I'd recommend using the UnoJoyProcessingVisualizer while working on the Arduino code, so the process will be:

1) Upload code to Arduino (Arduino mode)
2) Open up UnoJoyProcessingVisualizer.exe (Arduino Mode)
3) Test controller's functionality (Arduino mode)
4) Repeat steps 1-3 until satisfied with performance
5) Change into UnoJoy then plug into PC (UnoJoy mode)
6) Play games with 'magic paper'

Good luck!
jabelone in reply to alan.chathamJun 5, 2012. 3:42 AM
Thank-you very much for all of your help. Would I be able to use a normal resistor or do I have to use this "pull up resistor." Sorry about my lack of understanding, I am just getting into electronics. :)
alan.chatham (author) in reply to jabeloneJun 5, 2012. 5:50 AM
No problem!

So, a 'pull up' or 'pull down' resistor is a term that we use for a resistor of relatively high value (like 10K, 20K, 1M ohm) that connects a path to the voltage source or ground, respectively. We call it a 'pull up' or 'pull down' resistor because it puts a light pressure that 'pulls' the level on the line up or down if there's nothing else connected to the line.

Commonly, we'll see this used with switches - when the switch is closed, it will directly connect the pin to ground, so the level of the pin goes to 0. However, when the switch is released, the voltage on the line will bring the line back up to 1.
Heck, wikipedia is probably way better at explaining it than me:
http://en.wikipedia.org/wiki/Pull_up_resistor

At any rate, this project here relies on the fact that, after the pin switched to ground (which the Arduino does in the code), it takes a certain amount of time for the line to go back high, so we need a big value on our pull-up resistor (like 1M ohm) so that it takes a relatively long time for the voltage to go from 0 to 5v.
jabelone in reply to alan.chathamJun 10, 2012. 3:54 AM
Hello again, I am very sorry for constantly bugging you. I have had another problem. I have hooked it all up right but I just cannot get the code right (been trying for days). Could you please email me a sample code or upload it to your wiki then comment, so that I can use the "Capacitive Touch Sensor" in UNOJoy mode to make a windows game controller. Thank-you very much in advanced and I promise I won't bug you again after this. :) Thanks, Jabelone. (btw email is temp12@jabelone.com )
alan.chatham (author) in reply to jabeloneJun 10, 2012. 6:43 PM
No problem! There's some example code for the drawn sensor controller here:

http://code.google.com/p/unojoy/source/browse/#hg%2FExamples%2FDrawnController

I hope it helps!
jabelone in reply to alan.chathamJun 11, 2012. 1:17 AM
Thank-you for the code but when I have the sensors connected to +5 on the board it doesn't work at all. When I connect the sensors to ground they only work when I touch the paperclip not the drawing.
limbo says: May 27, 2012. 2:42 PM
Hey that a paper with hyperlink on it! Awesome!
jabelone in reply to limboJun 10, 2012. 3:55 AM
lol :)
ghostbust555 says: Jun 6, 2012. 12:13 PM
I'm thinking portable handwritten keyboard!? How freaking cool would that be
jabelone in reply to ghostbust555Jun 7, 2012. 3:57 AM
it would be cool, good idea!
styleatjoe says: Jun 4, 2012. 9:56 PM
for more than one sensors on paper please hint the coding..
alan.chatham (author) in reply to styleatjoeJun 4, 2012. 10:14 PM
The sample code here has a function, readCapacitivePin(int pinToMeasure)

If you put in different values for that, like

int firstSensor = readCapacitivePin(2);
int secondSensor = readCapacitivePin(3);

you can read whatever Arduino pins you want, as many as you'd like.
matthew_x says: Jun 3, 2012. 10:12 PM
I connected all things right,but it just keep come out with:
"Capacitive Sensor on Pin 2 reads: 0"
This project is awesome,but how to put it right?
Please give me a hand.
alan.chatham (author) in reply to matthew_xJun 4, 2012. 12:29 AM
What is the value of the resistor you have between the pin and 5v?
matthew_x in reply to alan.chathamJun 4, 2012. 2:53 AM
Oh,I'm so sorry,I only have one about 400 Ω,now I know it's not enough.
So,if the value is too low ,it will come out [Capacitive Sensor on Pin 2 reads: 0],right?
By the way, how a 1mΩ resistor look like ? I'll got some.
alan.chatham (author) in reply to matthew_xJun 4, 2012. 10:39 AM
Yep! If the value on the resistor is too low, it pulls up the voltage on the line super-fast. The number that you see printed is actually the number of processor cycles it takes for the pin to go from grounded to 2.5v or so (the level at which a pin goes from thinking a voltage is LOW to HIGH). The larger the resistor, the longer it takes for the line to be pulled up to that level.
0_Nvd_0 says: Jun 1, 2012. 6:06 AM
Are you sure that it is capacitive or is it the A.C hum picked up by the body?

Where are you touching the ground node with your body to act as the second terminal of the capacitor?
skrubol in reply to 0_Nvd_0Jun 1, 2012. 8:30 PM
Capacitors can be 1 terminal devices. A couple examples of 1T caps are the toroid on the top of a tesla coil, and the ball sometimes on the end of antennas (though the ball usually primarily serves to just make antennas less eye pokey-outy.)
0_Nvd_0 in reply to skrubolJun 2, 2012. 6:36 AM
I do not agree.

There is a ground reference that I am thinking about. One way is that USB power ground to the Arduino is connected to the same ground on which the user is standing.
skrubol in reply to 0_Nvd_0Jun 2, 2012. 9:37 AM
Then it would not work while wearing sneakers.
1-40 of 58Next »
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!