Hello,

I found this code for Arduino. It needs no additional part to measure a 
capacitor value, just plug it between two pins of the microcontroller.

const int OUT_PIN = A2;
const int IN_PIN = A0;

//Capacitance between IN_PIN and Ground
//Stray capacitance is always present. Extra capacitance can be added to
//allow higher capacitance to be measured.
const float IN_STRAY_CAP_TO_GND = 24.48; //initially this was 30.00
const float IN_EXTRA_CAP_TO_GND = 0.0;
const float IN_CAP_TO_GND  = IN_STRAY_CAP_TO_GND + IN_EXTRA_CAP_TO_GND;
const int MAX_ADC_VALUE = 1023;

void setup()
{
  pinMode(OUT_PIN, OUTPUT);
  //digitalWrite(OUT_PIN, LOW);  //This is the default state for outputs
  pinMode(IN_PIN, OUTPUT);
  //digitalWrite(IN_PIN, LOW);

  Serial.begin(115200);
}

void loop()
{
  //Capacitor under test between OUT_PIN and IN_PIN
  //Rising high edge on OUT_PIN
  pinMode(IN_PIN, INPUT);
  digitalWrite(OUT_PIN, HIGH);
  int val = analogRead(IN_PIN);

  //Clear everything for next measurement
  digitalWrite(OUT_PIN, LOW);
  pinMode(IN_PIN, OUTPUT);

  //Calculate and print result

  float capacitance = (float)val * IN_CAP_TO_GND / (float)(MAX_ADC_VALUE - val);

  Serial.print(F("Capacitance Value = "));
  Serial.print(capacitance, 3);
  Serial.print(F(" pF ("));
  Serial.print(val);
  Serial.println(F(") "));

  while (millis() % 500 != 0)
    ;    
}

I translated it to Jal, I just adapted the pins to use, and  converted floating 
values to integer, since I don't need them for my application

Include 16f877a

Pragma Target CLOCK 20_000_000
Pragma Target OSC hs
Pragma Target WDT disabled
Pragma Target BROWNOUT enabled
Pragma Target PWRTE disabled
Pragma Target LVP disabled
Pragma Target CPD disabled
Pragma Target WRT disabled
Pragma Target DEBUG disabled
Pragma Target CP disabled

-- include delay
include print
include format

-- Désactive les convertisseurs A/D
enable_digital_io()
--
-- Mesure en haute résolution
const bit ADC_HIGH_RESOLUTION = high
-- On va utiliser un seul canal analogique
const byte ADC_NCHANNEL = 1
-- Pas de tension de référence externe
const byte ADC_NVREF = ADC_NO_EXT_VREF

include adc
adc_init()

-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
-- % Traitement du port RS232
-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
-- -- Définition des valeurs du port série
const USART_HW_Serial     = TRUE   -- TRUE = RS232, FALSE = SPI
const Serial_HW_Baudrate  = 9600
include Serial_Hardware
Serial_HW_Init

-- some aliases so it is easy to change from serial hw to serial sw.
alias serial_write is serial_hw_write
alias serial_read is serial_hw_read
alias serial_data is serial_hw_data
alias serial_data_available is serial_hw_data_available
-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
-- % FIN Traitement du port RS232
-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

print_cls_ansi(serial_data)
print_string(serial_data,"Debut programme\r\n")
print_crlf(serial_data)

alias OUT_PIN is pin_B7
alias OUT_PIN_DIRECTION is pin_B7_direction
alias IN_PIN is pin_A0
alias IN_PIN_DIRECTION is pin_A0_direction

--Capacitance between IN_PIN and Ground
--Stray capacitance is always present. Extra capacitance can be added to
--allow higher capacitance to be measured.
const word IN_STRAY_CAP_TO_GND = 0 --initially this was 30.00
const word IN_EXTRA_CAP_TO_GND = 0
const word IN_CAP_TO_GND  = IN_STRAY_CAP_TO_GND + IN_EXTRA_CAP_TO_GND
const word MAX_ADC_VALUE = 1024

OUT_PIN_DIRECTION = OUTPUT
-- OUT_PIN = LOW  --This is the default state for outputs
IN_PIN_DIRECTION = OUTPUT
-- IN_PIN = LOW

var word val = 0
var dword capacitance = 0

forever loop
    -- Capacitor under test between OUT_PIN and IN_PIN
    -- Rising high edge on OUT_PIN
    IN_PIN_DIRECTION = INPUT

    OUT_PIN = HIGH
    val = adc_read_high_res(0)
    OUT_PIN = LOW

    -- Clear everything for next measurement
    IN_PIN_DIRECTION = OUTPUT

    -- Calculate and print result
    capacitance = (val * IN_CAP_TO_GND) / (MAX_ADC_VALUE - val)

    print_string(serial_data, "Capacitance Value = ")
    print_dword_dec(serial_data, capacitance)
    print_string(serial_data, " pF (")
    print_word_dec(serial_data, val)
    print_string(serial_data, ")\r\n")

    _usec_delay (1_000_000)

end loop

Unfortunately, it doesn't work at all, ADC doesn't see any change with several 
values of capacitors, I can't figure out why... I obviously miss something, but 
what?
Any idea will be welcomed!

Link to the original project:
https://wordpress.codewrite.co.uk/pic/2014/01/21/cap-meter-with-arduino-uno/

-- 
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jallib/ad650a5f-d052-4470-a3c5-120156c01f3a%40googlegroups.com.

Reply via email to