Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define vTestPin 0
- #define chargePin 13
- #define dischargePin 11
- #define resistorValue 10000.0F
- #define ADC_RES 1023.0F
- #define V_HIGH 5
- unsigned long startTime;
- unsigned long elapsedTime;
- float microFarads;
- float nanoFarads;
- void setup() {
- pinMode(chargePin, OUTPUT);
- digitalWrite(chargePin, LOW);
- Serial.begin(9600);
- }
- void loop() {
- // ---- Capacitance ----
- // start charging capacitor, and record time
- digitalWrite(chargePin, HIGH);
- startTime = millis();
- // wait for capacitor to charge ~63% (one T = RC)
- while (true) {
- float charge = analogRead(vTestPin);
- elapsedTime = millis() - startTime;
- if (charge > (ADC_RES * .63)) {
- microFarads = ((float)elapsedTime / resistorValue) * 1000;
- break;
- }
- if (elapsedTime > 2000) {
- microFarads = 0;
- break;
- }
- }
- // T = RC -> C = T / R
- elapsedTime = millis() - startTime;
- microFarads = ((float)elapsedTime / resistorValue) * 1000;
- if ((microFarads > 0) && (microFarads < 30)) {
- Serial.println("The electronic component is a capacitor");
- if (microFarads > 1) {
- Serial.print((long)microFarads);
- Serial.println(" microFarads is the value of the capacitor and");
- delay(1600);
- if (microFarads > 10) {
- Serial.println("falls into the category HIGH");
- }
- else if ((microFarads > 1) && (microFarads < 10)) {
- Serial.println("falls into the category MEDIUM");
- }
- else if (microFarads < 1) {
- Serial.println("falls into the category LOW");
- }
- else {
- nanoFarads = microFarads * 1000.0;
- Serial.print((long)nanoFarads);
- Serial.println(" nanoFarads");
- delay(1600);
- }
- }
- }
- if (microFarads > 100) {
- Serial.println("Please insert electronic component");
- }
- float vx = analogRead(vTestPin) * V_HIGH / ADC_RES;
- Serial.print("The electric component is a resistor \n");
- Serial.print("Voltage across the electronic component in Volts = ");
- Serial.println(vx);
- float ix = (V_HIGH - vx) / resistorValue;
- Serial.print("Resistance of the electronic component in Ohms = ");
- if ( ix > 0) {
- float rx = vx / ix;
- Serial.println(((int) rx));
- }
- else {
- Serial.println("infinite!");
- }
- // Allow capacitor to discharge
- digitalWrite(chargePin, LOW);
- pinMode(dischargePin, OUTPUT);
- digitalWrite(dischargePin, LOW);
- while (analogRead(vTestPin) > 0) {
- }
- pinMode(dischargePin, INPUT);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement