forked from timwaizenegger/raspberrypi-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKY-053_RPi_AnalogDigitalConverter.py
94 lines (74 loc) · 3.08 KB
/
KY-053_RPi_AnalogDigitalConverter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#
#!/usr/bin/python
# coding=utf-8
#############################################################################################################
### Copyright by Joy-IT
### Published under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
### Commercial use only after permission is requested and granted
###
### KY-053 Analog Digital Converter - Raspberry Pi Python Code Example
###
#############################################################################################################
# Dieser Code nutzt die ADS1115 und die I2C Python Library fuer den Raspberry Pi
# Diese ist unter folgendem Link unter der BSD Lizenz veroeffentlicht
# [https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code]
from Adafruit_ADS1x15 import ADS1x15
from time import sleep
# Weitere benoetigte Module werden importiert und eingerichtet
import time, signal, sys, os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Benutzte Variablen werden initialisiert
delayTime = 0.5 # in Sekunden
# Adresszuweisung ADS1x15 ADC
ADS1015 = 0x00 # 12-bit ADC
ADS1115 = 0x01 # 16-bit
# Verstaerkung (Gain) wird ausgewaehlt
gain = 4096 # +/- 4.096V
# gain = 2048 # +/- 2.048V
# gain = 1024 # +/- 1.024V
# gain = 512 # +/- 0.512V
# gain = 256 # +/- 0.256V
# Abtasterate des ADC (SampleRate) wird ausgewaehlt
# sps = 8 # 8 Samples pro Sekunde
# sps = 16 # 16 Samples pro Sekunde
# sps = 32 # 32 Samples pro Sekunde
sps = 64 # 64 Samples pro Sekunde
# sps = 128 # 128 Samples pro Sekunde
# sps = 250 # 250 Samples pro Sekunde
# sps = 475 # 475 Samples pro Sekunde
# sps = 860 # 860 Samples pro Sekunde
# ADC-Channel (1-4) wird ausgewaehlt
adc_channel_0 = 0 # Channel 0
adc_channel_1 = 1 # Channel 1
adc_channel_2 = 2 # Channel 2
adc_channel_3 = 3 # Channel 3
# Hier wird der ADC initialisiert - beim KY-053 verwendeten ADC handelt es sich um einen ADS1115 Chipsatz
adc = ADS1x15(ic=ADS1115)
Button_PIN = 24
GPIO.setup(Button_PIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)
#############################################################################################################
# ########
# Hauptprogrammschleife
# ########
# Das Programm liest die aktuellen Werte der Eingang-Pins
# und gibt diese in der Konsole aus
try:
while True:
#Aktuelle Werte werden aufgenommen
adc0 = adc.readADCSingleEnded(adc_channel_0, gain, sps)
adc1 = adc.readADCSingleEnded(adc_channel_1, gain, sps)
adc2 = adc.readADCSingleEnded(adc_channel_2, gain, sps)
adc3 = adc.readADCSingleEnded(adc_channel_3, gain, sps)
# Ausgabe auf die Konsole
print "Messwert Channel 0:", adc0, "mV "
print "Messwert Channel 1:", adc1, "mV "
print "Messwert Channel 2:", adc2, "mV "
print "Messwert Channel 3:", adc3, "mV "
print "---------------------------------------"
# Reset + Delay
button_pressed = False
time.sleep(delayTime)
except KeyboardInterrupt:
GPIO.cleanup()