Lab Day 7

During lab today, our assigment was to create a sensor. I decided that I would create a capacitive sensor that would turn an LED on when pressed. I chose to do this because it is related to and helpful for my final project.


To begin, I soldered a wire onto a small copper plate, and then placed black tape over it. I then cut a small piece of cardboard and cut some copper tape on it creating space for a LED and ultimatley circuit. It is important that each leg of the LED is experiencing a different voltage, or else a short circuit will be created. This is why I cut the copper so that there was a space in between the two strips to allow for the anode and cathode of the LED to experience different voltages. To put the LED on the carboard and copper tape, I took a little bit of solder and attached it to the copper tape. I do not think that the other side of the copper tape is conductive so I made sure that it was attahced to the conductive part. Next, I soldered two wires onto the other end of the cardboard with the copper. I soldered a red wire to the anode side of the LED as this wire will insert into digital pin 13 on the arduino (where it will be powered). I then soldered a black wire to the cathode end of the LED as this wire will insert into the ground pin on the arduino.
LED Copper Circuit no Resistor
Next, I added a resistor to this circuit because I did not want the power to overload the LED, possibly breaking it. I cut a little piece of the copper on the cathode side of the LED so that the different ends of the resistor would have different voltages. Again, I did this for the same reason that I did it for the LED: to prevent a short circuit. I soldered one end of the resistor (the one on the copper) and then twisted the other part around the wire. I latered found out that this way that I attached the latter end of the resistor was problematic. Because it was not soldered it was creating an intermittent connection, ultimatley preventing the LED from lighting up when the copper plate was pressed.
LED Copper Circuit with Resistor
Then, before using the capacitor, I tested that the LED would be able to be powered by the 5V and ground of an arduino. For the first LED that I used, this did not work. I got out the multimeter to trouble shoot this problem. I put pins from the multimeter into the arduino (pins 5V and ground) and saw a voltage drop across that, so I knew that the arduino was not the problem. I then used aligator clips in the multimeter to clip to each leg of the LED. Again, I found that there was a voltage drop of roughly 5V across the LED. From this, I determined that the LED was the problem. So, I soldered another LED to the copper tape, and it worked!
LED Copper Circuit with Resistor Lit Up
Next, I attached the red wire on the anode of the LED to digital pin 13 and the black wire on the cathode to ground. I then put one end of a 1 Mega-Ohm resistor into digital pin 7, and then the resistor and the wire soldered to the copper plate in digital pin 5. Originally, I just twised the resistor around the wire of the capacitor to attach both to digital pin 5, but again as I learned from the previous resistor, this created an intermitten connection. Once the 1 Mega-Ohm resistor was soldered, I put all the wires into the arduino as explained above.
LED Copper Circuit with Capacitor
Next, it was time to write the code. I decided that I would start from the code we used in class to creat a capacitive touch sensor and then modify it from there. The code for the simple capacitive touch sensor can be found here. Also, make sure to download the capacitive library to use this code. I used a conditional statement to get the LED to turn on when the value on the capacitance was above a certain value (1000). I got this value from opening the serial monitor when the regular touch sensor code was uploaded to my arduino and estimating the value that was produced when the copper plate was moderatley touched. Here is my code below.

#include <CapacitiveSensor.h>

CapacitiveSensor Sensor = CapacitiveSensor(7, 5);

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  long sensorValue = Sensor.capacitiveSensor(300);
  Serial.println(sensorValue);
  delay(10);
  if (sensorValue > 1000)
  { digitalWrite(LED_BUILTIN, HIGH);
  }
  else {
    digitalWrite(LED_BUILTIN, LOW);
  }
}


Once I successfully uploaded this code, my sensor ran as intended!


Next, I decided that I wanted to try to adjust the brightness of the LED based on the value outputed from the serial monitor. To get an idea of how to do this, I looked at the fade example in arduino. To begin, I looked at the value on the serial monitor and set five ranges of values that I planned to use. The first one started with a value of roughly twenty and went to 2000 – the others adding another 2000 for each range (ending with a range of 8000 to 30000 as the maximum value I was able to create from pressing the copper tape was about 25,000). I then used 51 as the value for the first range and added another 51 for each range. This, however, failed to work. I found that there must be a maximum value that the LED needs to be able to light up and I concluded that that value was somewhere between 125 and 150. I spent the rest of the lab, changing values around with the hopes of seeing some increasing brightness, but I was unsuccessful in this.

Then, as per suggestion of Souyma, I used the map function. This, however, was also unsuccessful. To check that this LED was actually capable of showing varriance in brightness, we uploaded the fade program to my arduino, and we were able to see the fade happen. At this point, I did not have much time left in class so I decided to move on from this as it was not directly related to my final project; instead, it was just something cool I wanted to try out. My code with the map function and with the conditional statements is below for reference to provide a better picture of what I was working on.

#include <CapacitiveSensor.h>

CapacitiveSensor Sensor = CapacitiveSensor(7, 5);

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  unsigned long sensorValue = Sensor.capacitiveSensor(300);
  Serial.println(sensorValue);
  if (sensorValue >= 20 && sensorValue <= 2000)
  { analogWrite(LED_BUILTIN, 51);
  }
  if (sensorValue > 2000 && sensorValue <= 4000)
  { analogWrite(LED_BUILTIN, 102);
  }
  if (sensorValue > 4000 && sensorValue <= 6000)
  { analogWrite(LED_BUILTIN, 153);
  }
  if (sensorValue > 6000 && sensorValue <= 8000)
  { analogWrite(LED_BUILTIN, 204);
  }
  if (sensorValue > 8000 && sensorValue <= 30000)
  { analogWrite(LED_BUILTIN, 255);
  }
  else {
    digitalWrite(LED_BUILTIN, LOW);
  }
}



#include <CapacitiveSensor.h>

CapacitiveSensor Sensor = CapacitiveSensor(7, 5);

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  unsigned long sensorValue = Sensor.capacitiveSensor(300);
  sensorValue = map(sensorValue,0,25000,1,255);
  analogWrite(LED_BUILTIN,sensorValue);
  Serial.println(sensorValue);
}


Calibration for this sensor was a little bit difficult. Because my sensor only has the two capabilities: on and off, the calibration is unusual. Originally, I was going to average the values whent the sensor was not pressed, and then average the values when the sensor was pressed. I would then create a graph from these two values. I then decided, that I would add a third conditon: when the finger is hovering above the plate. What I did was find the range of values when the capacitor is not pressed at all:[0,9]. Find the range of values while my finger is just hovering above the capacitor [17,48]. And finally, find the range of values when my finger was pressing the capacitor with a resonable pressure [938, 1146]. The state of the capacitor for each of the three scenarios is displayed below.

Capacitor
Capacitor with Finger Hovering
Capacitor Pressed


Next, I created a graph in Excel to display this data. The scale for this is poor, but I ran out of time in class and did not have a chance to fix it. 1 corresponds to when the capacitor is not pressed at all. 2 corresponds to when my finger was hovering over the capacitor. And 3 corresponds to when my finger was pressing the capacitor. The blue line above each point corresponds to the values that are on the serial monitor when the capacitor is pressed (or not pressed) in the three varrying amounts.
Excel Graph