-
Notifications
You must be signed in to change notification settings - Fork 0
/
pycodes100_12_ADC Temperature Sensor.py
66 lines (64 loc) · 1.56 KB
/
pycodes100_12_ADC Temperature Sensor.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
#https://newdigitals.org/2024/02/24/100-basic-python-codes/#adc-temperature-sensor
SYSVOLT = 5
ADC_RESOLUTION = 4095
MAX_VOLT = 2.442
MIN_VOLT = 0
MIN_TEMP = -50
MAX_TEMP = 50
def adc_raw_value(v):
if(v >= MIN_VOLT and v <= MAX_VOLT):
ADC = (v*(ADC_RESOLUTION/SYSVOLT))
return round(ADC)
else:
return None
def adc_to_c(x):
if x == 0:
return -50
else:
return ((adc_to_c(x-1)) + 0.05)
def sensor_temp(v):
print(f"SENSOR READ: {v} Volt")
ADC = adc_raw_value(v)
if ADC is None:
print("Voltage is not within the sensor range")
else:
print("**********************\nAnalog to Digital Convertion\n**********************")
print(f"Analog: {v}\nDigital: {ADC}")
print("**********************")
print(f"Temperature is: {round(adc_to_c(ADC))}")
sensor_temp(MIN_VOLT)
SENSOR READ: 0 Volt
**********************
Analog to Digital Convertion
**********************
Analog: 0
Digital: 0
**********************
Temperature is: -50
sensor_temp(MAX_VOLT)
SENSOR READ: 2.442 Volt
**********************
Analog to Digital Convertion
**********************
Analog: 2.442
Digital: 2000
**********************
Temperature is: 50
sensor_temp(1.221)
SENSOR READ: 1.221 Volt
**********************
Analog to Digital Convertion
**********************
Analog: 1.221
Digital: 1000
**********************
Temperature is: 0
sensor_temp(1.5)
SENSOR READ: 1.5 Volt
**********************
Analog to Digital Convertion
**********************
Analog: 1.5
Digital: 1228
**********************
Temperature is: 11