Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import machine 

2from sensor import Sensor 

3 

4 

5class SensorADC(Sensor): 

6 

7 """Sensor, measures an amount from a pin with ADC """ 

8 

9 def __init__(self, pin_num, range_min=0, range_max=4000, average_converging_speed=1 / 2, attenuation=6): 

10 

11 """ constructor. 

12 :param pin_num: number pin that will read value, it has to be ADC, analog to digital converter 

13 :param range_min: min value of sensor 

14 :param range_max: max value of sensor 

15 :param average_converging_speed: speed the average goes towards the last measure (range 0-1) """ 

16 super().__init__(range_min, range_max, average_converging_speed) 

17 self.pin = machine.ADC(machine.Pin(pin_num)) 

18 if attenuation == 0: 

19 self.pin.atten(machine.ADC.ATTN_0DB) # maximum input voltage of 1.00v 

20 if attenuation == 2.5: 

21 self.pin.atten(machine.ADC.ATTN_2_5DB) # maximum input voltage of 1.34V 

22 if attenuation == 6: 

23 self.pin.atten(machine.ADC.ATTN_6DB) # increase reading voltage to 2V 

24 if attenuation == 11: 

25 self.pin.atten(machine.ADC.ATTN_11DB) # maximum input voltage of 3.6V, going near this boundary may be damaging to the IC! 

26 

27 def raw_measure(self): 

28 """measures, updates average, returns raw measure.""" 

29 return self.pin.read() 

30