summaryrefslogtreecommitdiffstatshomepage
path: root/docs/library/pyb.ADC.rst
blob: 33e587f3fa353af9eaf0c1554582e2d7c1602218 (plain) (blame)
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
class ADC -- analog to digital conversion: read analog values on a pin
======================================================================

Usage::

    import pyb

    adc = pyb.ADC(pin)              # create an analog object from a pin
    val = adc.read()                # read an analog value

    adc = pyb.ADCAll(resolution)    # creale an ADCAll object
    val = adc.read_channel(channel) # read the given channel
    val = adc.read_core_temp()      # read MCU temperature
    val = adc.read_core_vbat()      # read MCU VBAT
    val = adc.read_core_vref()      # read MCU VREF


Constructors
------------

.. class:: pyb.ADC(pin)

   Create an ADC object associated with the given pin.
   This allows you to then read analog values on that pin.


Methods
-------

.. method:: adc.read()

   Read the value on the analog pin and return it.  The returned value
   will be between 0 and 4095.

.. method:: adc.read_timed(buf, freq)

   Read analog values into the given buffer at the given frequency. Buffer
   can be bytearray or array.array for example. If a buffer with 8-bit elements
   is used, sample resolution will be reduced to 8 bits.
   
   Example::
   
       adc = pyb.ADC(pyb.Pin.board.X19)    # create an ADC on pin X19
       buf = bytearray(100)                # create a buffer of 100 bytes
       adc.read_timed(buf, 10)             # read analog values into buf at 10Hz
                                           #   this will take 10 seconds to finish
       for val in buf:                     # loop over all values
           print(val)                      # print the value out
   
   This function does not allocate any memory.