[Hardware Peripheral Use] - ADC

ADC basic concept

ADC is the abbreviation of Analog-to-Digital Converter (Analog-to-Digital Converter), which is an electronic component that converts analog signals into digital signals .

ADCs are widely used in the field of measurement and monitoring, such as converting analog signals such as sound, pressure, and temperature into digital signals for data processing and storage. It is also widely used in industrial automation, medical equipment, robot control and other fields.

insert image description here

How to use ADC

pyb.adc

pyb.adcis the module used to control the ADC in MicroPython. This module provides a set of APIs that can be used to configure and read the input of the ADC.

Commonly used pyb.adcmodule methods are as follows:

  • pyb.ADC(pin): Create an ADC object, pin is the number of the input pin.
  • read(): Read the voltage value input by ADC (between 0-4095).
  • read_timed(buf, tim, mode=ADC.CIRCULAR): Read the voltage value input by the ADC within the specified time and store it in the buffer.
  • read_timed_multi(bufs, tim, mode=ADC.CIRCULAR): Simultaneously read the input voltage values ​​of multiple ADCs and store them into multiple buffers.
  • read_channel(channel, buf=None, n=1, mode=ADC.CIRCULAR): Read the input voltage value for the specified channel. If buf is not specified, it is a single read; otherwise, the specified number of measurements are stored in the given buf.
  • read_timed_dma(buf, tim, mode=ADC.CIRCULAR): Use the DMA controller to read the voltage value input by the ADC within the specified time and store it in the buffer.

When using pyb.adcmodules, you need to pay attention to the following:

  • When reading the voltage value, the maximum value is 4095, which is 3.3V.
  • Different hardware platforms may support different numbers and ranges of ADC pins. For example, on the Pyboard, the supported ADC pins are X19-X22 and X5-X8, while on the ESP8266, all pins can be used for ADC input.

Here is an example showing how to read the ADC input voltage on the Pyboard and output it to the serial port:

import pyb

# 创建一个ADC对象,引脚为X20
adc = pyb.ADC(pyb.Pin.board.X20)

while True:
    # 读取ADC输入的电压值(0-4095之间)
    adc_value = adc.read()

    # 将电压值转换为实际电压
    voltage = adc_value * 3.3 / 4095
    
    # 输出电压值到串口
    print("Voltage: {:.2f}V".format(voltage))
    
    # 等待一段时间后再次读取电压值
    pyb.delay(500)

In the above code, we first created an ADC object and specified the input pin as X20. Then, read the voltage value input by the potentiometer through the method in the loop adc.read()and convert it to the actual voltage value. Finally, output the voltage value through the serial port, and read the voltage value again after waiting for a period of time.

machine.adc

machine.adcIt is a module in MicroPython, which is used to operate the ADC (Analog to Digital Converter) module. The ADC module can convert the analog signal into a digital signal for processing by the microcontroller.

This module provides the following functions:

  • machine.ADC(pin): Create an ADC object and specify the analog input pin. The parameter pincan be any pin that supports analog input, such as machine.ADC(0)using pin A0 as an analog input.

  • ADC.read(): Read the voltage value on the ADC input pin and return a number between 0-4095.

  • ADC.read_u16(): read()Similar to function, but returns an unsigned integer between 0-65535.

  • ADC.atten(atten): Set the attenuation value of the ADC input (Attenuation). The parameter attencan be one of four values: ADC.ATTN_0DB, ADC.ATTN_2_5DB, ADC.ATTN_6DB, ADC.ATTN_11DB. The larger the attenuation value, the wider the voltage range that the ADC input pin can accept.

  • ADC.width(width): Set the data width of ADC. The parameter widthcan be one of two values: ADC.WIDTH_9BIT, ADC.WIDTH_10BIT. The larger the data width, the higher the ADC sampling accuracy.

The following is a simple ADC application example, using machine.adcthe module to read the voltage value of the ADC analog input pin and output it to the serial port:

import machine

adc = machine.ADC(0)
uart = machine.UART(0, 115200)

while True:
    voltage = adc.read() * 3.3 / 4095
    print("Voltage: {:.2f}V".format(voltage))

In the above code, we have created an ADC object and used adc.read()the method to read the voltage value on the analog input pin. Since read()the function returns a number between 0-4095, we need to convert it to an actual voltage value. Finally, we output the voltage value to the serial port.

ADC Available Sensors

ADC is widely used in various sensors, by converting the analog signal output by the sensor into a digital signal, so as to realize the reading and processing of sensor data.

Here are some common sensors that use ADCs:

  1. Temperature sensor: such as LM35, NTC thermistor, etc., can convert the temperature into a corresponding voltage signal, and then read and calculate the actual temperature value through the ADC.
    insert image description here

  2. Photosensitive sensors: such as LDR photoresistors, photodiodes, etc., can convert light intensity into corresponding voltage signals, and then read and calculate the light intensity value through ADC.
    insert image description here

  3. Sound sensors: such as microphones, piezoelectric sensors, etc., can convert sound signals into corresponding voltage signals, and then read and calculate the sound intensity value through ADC.
    insert image description here

  4. Gas sensor: For example, the MQ series gas sensor can convert the gas concentration into a corresponding voltage signal, and then read and calculate the gas concentration value through the ADC.
    insert image description here

The above are just some common sensors that use ADC. In fact, there are more other types of sensors that can also apply ADC technology.
In MicroPython, we can select the appropriate ADC module for data reading and processing according to the characteristics and data format of the specific sensor.

Guess you like

Origin blog.csdn.net/weixin_45020839/article/details/130215033