Once again, my project was motivated by my curiosity to explore interesting and unfamiliar territory. I decided to see if I could make my own capacitive sensor and then use it in a project of some sort. After numerous fiascoes with Arduino and computers, I was able to get the capacitive sensor to work and then implemented it into a fishing game. It was a highly frustrating but really fun project to do.
Right above is the circuit diagram for a simple capacitive sensor. The human’s hand and a rectangular piece of conductive material form a capacitor. The output the measures the time it takes for that capacitor to discharge. The closer the hand and coductive material are together, the longer it takes to discharge. Also, the higher the resistor between pins A & B, the more sensitive the sensor is.
The picture on the right shows the actual sensor I built. The white alligator clips connect pins A and B together through a resistor. The yellow aligator clip is the output sensor connected to one half of the capacitor. The black alligator clip is ground and is held by the human user.
The major problems I had with the sensor was the connections. At first, I just wrapped the resistor through the actual holes in the Lilypad, but it was very wiggly and not secure at all. The alligator clips work better but they tend to disconnect from the Lilypad, or touch each other when they are close, causing a short circuit.
My next big step was to modify the code. Now that the sensor worked, I wanted to use it to light up LEDs. There are three sets of code that had to be added to the capacitive sensor sample code that we were given.
This bit of code is put under the void setup() section (Serial.begin(9600) is already in the sample code). Basically, it is naming pins 2-5 as output pins for my LEDs. Pins are automatically inputs so you need to declare them as outputs. You can have as little or as many as you want; I used four pins because I had four colours of LEDs.
This next bit of code is under the void setup() section (sensorValue=mysensor.capsense(30) is already in the sample code). This is used to limit the numbers that the capacitive sensor was showing in the serial monitor, because of too much fluctuation. So anything less than 100 becomes 0 and anything more than 1275 becomes 1275.
This last bit of code is also under the void setup() section. It is pretty easy to understand, just very annoying to write up. This is what controls which LEDs light up at which level of distance.
The first if;else phrase controls pin 2 (red LED). When the sensor values (that have been simplified to (100-1275) read 500 or more, the red LED goes on. If the value is less than 500, the red LED is off.
The second if;else phrase controls pin 3 (yellow LED). When the sensor values read 200 or more, the yellow LED goes on. If the value is less than 200, the yellow LED is off.
The next if;else phrase controls pin 4 (green LED). When the sensor values read 150 or more, the green LED goes on. If the value is less than 150, the green LED is off.
The last if;else phrase controls pin 5 (blue LED). When the sensor values read 100 or more, the blue LED goes on. If the value is less than 100, the blue LED is off.
In short, the LEDs light up in this order as you get closer: blue, green, yellow, red.
The moment each LED turns on can be changed by messing around with the sensorValues. It all depends on how you want to use the sensor.
For more detail: Fish Sensing
JLCPCB – Prototype 10 PCBs for $2 (For Any Color)
China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w