Arduino: using ADC (8) on ESP32

purpose

Using a ADC (analog-digital conversion) an analog signal to the real world, such as temperature, pressure, sound, image or the like, it needs to be converted more easily stored, processed and transmitted in digital form. Using a DAC (digital to analog conversion) the digital signal into an analog signal, so that they can be external (or other non-digital systems) identification.

DAC

Basics

The following is based on the use DAC code, the code burned into the module, to realize the digital signal into an analog signal.

#include "Arduino.h"

void setup()
{
    dacWrite(20, 100); //IO20 DAC输出 100*3.3V/255≈1.294V
}

The main function

  • void dacWrite(uint8_t pin, uint8_t value)
    Output voltage of the corresponding pin

Sample Code

Slightly ^ ^

ADC

Basics

ADC is based on the use of the following code, the code burned into the module, to realize the analog signal into a digital signal.

#include "Arduino.h"

void setup()
{
    Serial.begin(115200);
    Serial.println();

    float vtmp = analogRead(34); //从IO34 利用 ADC 获取电压
    printf("%.3f",vtmp);
}

void loop()
{

}

The main function

========= ========= The following is a sampling blocked

  • uint16_t analogRead(uint8_t pin)
    Gets the IO port of the analog voltage data (which will block until acquisition is complete)

  • void analogReadResolution(uint8_t bits)
    Setting a reading resolution analog data, ranging from 1 to 16, default is 12. If is between 9 and 12, the hardware will set resolution is equal, otherwise the value will be moved

  • void analogSetWidth(uint8_t bits)
    ADC sampling resolution settings, ranging from 9 to 12, the default is 12

  • void analogSetCycles(uint8_t cycles)
    Setting a single sampling period, ranging from 1 to 255, the default is 8

  • void analogSetSamples(uint8_t samples)
    Single sample provided the actual sample number, ranging from 1 to 255, default 1;
    this is equivalent to increase the sensitivity of the ADC is provided, for example the value 2, then the data is obtained by sampling two times of real data

  • void analogSetClockDiv(uint8_t clockDiv)
    Set ADC clock division factor, ranging from 1 to 255, default 1

  • void analogSetAttenuation(adc_attenuation_t attenuation)
    Set global ADC input attenuation value ADC_0db, ADC_2_5db, ADC_6db, ADC_11db, the default is 11db

When VDD_A as 3.3V:

condition Maximum range
0dB 1.1V
2.5dB 1.5V
6dB 2.2V
11dB 3.9V (3.3V maximum voltage can be collected)
  • void analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation)
    Set a separate IO port input attenuation

========= ========= The following is a sample non-blocking

  • bool adcAttachPin(uint8_t pin)
    Connect the pin to the ADC (analog mode will also clear any other possible open)

  • bool adcStart(uint8_t pin)
    Start ADC conversion on the pin connections

  • bool adcBusy(uint8_t pin)
    Check the ADC conversion is in progress

  • uint16_t adcEnd(uint8_t pin)
    Get the conversion result (if not completed will wait)

Sample Code

Obstruction sampling

#include "Arduino.h"
#include "math.h"

const float R1 = 10000.0; //10K
const float T2 = (273.15 + 25.0);    
const float Bx = 3950.0;    // B值
const float Ka = 273.15;

void setup()
{

  Serial.begin(115200);
  Serial.println();
}

int tempCount(float vtmp)       // 温度转换
{
  float Rt;
  float temp;
  float v0 = (vtmp * 3.9) / 4095.0;

  Rt=(3.3-v0)/v0*R1;
  temp = Rt/R1 ;
  temp = log10(temp); 
  temp /= Bx;         
  temp += (1 / T2);
  temp = 1 / (temp);
  temp -= Ka;
  return temp;
}

void loop()
{
  float vtmp = analogRead(34); //IO34 ADC获取电压
  float temp = tempCount(vtmp);

  Serial.printf("T:%f\n", temp);
  Serial.println();
  Serial.printf("C:%f\n", vtmp);
  Serial.println();
  Serial.printf("V:%.3fV\n", vtmp * 3.9 / 4095);
  Serial.println();

  delay(1000);
}

Non-blocking sampling

#include "Arduino.h"
#include "math.h"

const float R1 = 10000.0; //10K
const float T2 = (273.15 + 25.0);
const float Bx = 3950.0; // B值
const float Ka = 273.15;

void setup()
{

  Serial.begin(115200);

  
  while (adcAttachPin(34) != 1)
  {
  }
  Serial.printf("Successful Connection !");
  Serial.println();
  while (adcStart(34) != 1)
  {
  }
  Serial.printf("ADC Open Successfully !");
  Serial.println();
}

int tempCount(float vtmp) // 温度转换
{
  float Rt;
  float temp;
  float v0 = (vtmp * 3.9) / 4095.0;

  Rt = (3.3 - v0) / v0 * R1;
  temp = Rt / R1;
  temp = log10(temp);
  temp /= Bx;
  temp += (1 / T2);
  temp = 1 / (temp);
  temp -= Ka;
  return temp;
}

void loop()
{
  float vtmp; // ADC获取电压
  float temp;

  if (adcBusy(34) == 0)
  {
    vtmp = adcEnd(34);
    temp = tempCount(vtmp);

    Serial.printf("T:%f\n", temp);
    Serial.println();
    Serial.printf("C:%f\n", vtmp);
    Serial.println();
    Serial.printf("V:%.3fV\n", vtmp * 3.9 / 4095);
    Serial.println();

    delay(1000);
  }
}


phenomenon

The test results of sampling and blocking non-blocking almost sampling, the difference is whether the connection in non-blocking sampling detects success

Here Insert Picture Description

to sum up

ADC and DAC in the Arduino not difficult to implement. DAC main function only void dacWrite(uint8_t pin, uint8_t value)in the ADC, more functions, but also blocking and non-blocking sampling sampling two ways.

Guess you like

Origin blog.csdn.net/weixin_43474408/article/details/91445800