habe ein Kapazitätsmessgerät zum Messen von kleinen Kapazitäten gebastelt.
Nachbau der Schaltung auf Platine zur Messung über die Soundkarte:
http://www.elexs.de/radio4.htm
Erklärung zur Berechnung der Kapazität am NE555 als astabile Kippstufe:
http://www.elektronik-kompendium.de/sit ... 310131.htm
Code: Alles auswählen
#/usr/bin/env python
# -*- coding: utf-8
import tkinter as tk
import numpy as np
from functools import partial
import pyaudio
WIDTH = 270
HEIGHT = 100
RA = 1 # kOhm
RB = 5 # kOhm
RESISTANCE = (RA + RB * 2) * 0.69
class Capacitance_NE555(object):
def __init__(self,
resistance,
rate = 44100,
frames_per_buffer = 1024):
self.py_audio = pyaudio.PyAudio()
self.resistance = resistance
self.rate = rate
self.frames_per_buffer = frames_per_buffer
self.cap_reference = None
def __enter__(self):
return self
def __exit__(self, *args):
self.release()
def start_stream(self):
self.stream = self.py_audio.open(format= pyaudio.paInt16,
channels = 1,
rate = self.rate,
frames_per_buffer = \
self.frames_per_buffer,
input = True)
def stop_stream(self):
self.stream.stop_stream()
self.stream.close()
def get_frequency(self, stream_data):
past_mean = False
f = 0
for data in stream_data:
if data >= np.mean(stream_data) and past_mean== False:
past_mean = True
f=f+1
elif data <= np.mean(stream_data):
past_mean = False
return f / (len(stream_data) / self.rate)
def measure(self):
data = list()
for i in range(0, int(self.rate/ self.frames_per_buffer)):
result = 1 / self.get_frequency(
np.frombuffer(
self.stream.read(1024),
dtype=np.int16)
) / self.resistance * 100000000
result = result - self.cap_reference if self.cap_reference else\
result
data.append(result)
if self.cap_reference:
return np.mean(data)
else:
self.cap_reference = np.mean(data)
def release(self):
self.stream.stop_stream()
self.stream.close()
self.py_audio.terminate()
class CapacitanceUI(tk.LabelFrame):
UPDATE_INTERVAL = 2000
CALIBRATION = 50
def __init__(self, parent, capacitance_ne555, width, height):
tk.LabelFrame.__init__(self, parent, text = "CAPACITANCE",
relief = "solid")
self.capacitance = capacitance_ne555
self.width = width
self.height = height
self.parent = parent
self.calibration = True
self.display_conf = {"cap" : (
self.width / 2,
self.height / 2,
"{0:2.2f} pf",
"system 18 bold",
"blue",
"cap"),
"info" :(
self.width / 2,
self.height / 2,
"{0}",
"system 18 bold",
"magenta",
"info")}
self.display = tk.Canvas(self, width = width, height = height,
bg="cyan")
self.display.pack(padx = 5, pady = 5)
self.update_cap_display(0, "cap")
self.start_button = tk.Button(self, text="START", width = 7,
command = self.measure)
self.start_button.pack(side = tk.LEFT, padx = 30)
self.calibrate_button = tk.Button(self, text = "CALIBRATE", width = 7,
command = partial(self.measure, True))
self.calibrate_button.pack(side = tk.RIGHT, padx = 30)
self.measure(True)
def update_cap_display(self, text, tag):
width, height, text_conf, font, color, tag = self.display_conf[tag]
self.display.delete(tag)
self.display.create_text(width, height, text = text_conf.format(text),
font = font, fill = color,tag = tag)
def measure(self, calibrate = False):
self.display.delete("all")
self.update_cap_display("", "info")
self.capacitance.start_stream()
if calibrate:
self.capacitance.cap_reference = None
frequency = self.capacitance.measure()
display_text = ["CALIBATRED", "info"] if calibrate else\
[frequency, "cap"]
self.update_cap_display(display_text[0], display_text[1])
def release(self):
self.parent.destroy()
def main():
root = tk.Tk()
root.title("CAPACITANCE")
with Capacitance_NE555(RESISTANCE) as capacitance_ne555:
capacitance_ui = CapacitanceUI(root, capacitance_ne555, WIDTH, HEIGHT)
capacitance_ui.pack(expand=tk.YES, padx=5, pady=5)
root.protocol("WM_DELETE_WINDOW",capacitance_ui.release)
root.mainloop()
if __name__ == '__main__':
main()