 Welcome back, and thank you for joining me for Part 2 of my Halloween 2018 project (part 1 is here if you missed it)

Last time out I introduced the project, which you can read here if you missed it.

Having decided on making a low-level capacitive sense circuit after seeing a tutorial, I then couldn't find it again! Even worse, trying to find anything that would work with an RPi only revealed results that worked on microcontrollers, with the note "if you want to work with a Pi, you will need to rewrite the library". Brilliant.

Fortunately, having looked into the principle of how the circuit works, I managed to get my head around it enough that I wrote an example code that worked!

## The Basics

The circuit is really simple, an output joined to an input using a high-value resistor. The sense line is then attached on the input side of the resistor. I added an LED to the output just so I had some visual debugging to know when it was working or not.

All the circuit does is send a signal from the output pin to the input pin. The overall capacitance of the circuit defines how long the input will take to respond to the output, and that's where the code comes in...

## The Code

```import time                     #Required for time functions, including "wait" for a time and getting the time
import RPi.GPIO as GPIO         #Required for GPIO control and reading

gpio_in = 17                    #GPIO pin that will be used as the reading or input line
gpio_out = 27                   #GPIO pin that will output the square wave
pulseTime = 0.1                 #Time that the pulse spends time high or low, technically wave length divided by 2
touchLimit = 0.05               #Touch limit, not used in this example, but will later be the threashold we use to trigger the events.

GPIO.setmode(GPIO.BCM)          #Set the mode of GPIO numbering to BCM (there are other options, so be careful which pin you connect to which)
GPIO.setup(gpio_in, GPIO.IN)    #Set the mode of the pin we called "gpio_in" (17) to input
GPIO.setup(gpio_out, GPIO.OUT)  #Set the mode of the pin we called "gpio_out" (27) to output. For what I needed, I didn't have to set the internal pull up resister. Check your pi model and GPIO model!

def my_callback(gpio_in):                 #Name and define the function that will be called when the input goes high
timeDelay=time.time()-lastTime        #Calculate the time between the output being set high and now.
print timeDelay                       #Putput the calculated time on the screen.

GPIO.add_event_detect(gpio_in, GPIO.BOTH, callback=my_callback)          #Listen for the input GPIO changing state , and trigger the function we defined above

while True:                              #Set up the continuous loop to output the sqaure wave
time.sleep(pulseTime)                #Wait for the pulse time
GPIO.output(gpio_out, GPIO.HIGH)     #Set the output high
lastTime=time.time()                 #Save the time that the output went high so we can compare it to the input in the function above.
time.sleep(pulseTime)                #Wait for the pulse time
GPIO.output(gpio_out, GPIO.LOW)      #Set the output high
lastTime=time.time()                 #Save the time that the output went high so we can compare it to the input in the function above.```

All the code does is output the response times on the screen, longer times are when the capacitance is high, shorter times are when the capacitance is low. It is pretty easy from there to set the response time and set some actions!

Next time I'll start the permanent build and finish off the code off for all the elements of my project!