Arduino for BPI:bit 8 : how to use ADC

Using an ADC(analog-to-digital conversion) to convert real-world analog signals, such as temperature, pressure, sound, or images, into digital forms that are easier to store, process, and transmit. Digital signals are converted to analog signals using DAC(digital to analog conversion) so that they can be recognized by the outside world (people or other non-digital systems).

DAC

main function : void dacWrite(uint8_t pin, uint8_t value)

code:

#include "Arduino.h"

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

ADC

main function:

Blocking the sampling

  • uint16_t analogRead(uint8_t pin)
  • void analogReadResolution(uint8_t bits)
  • void analogSetWidth(uint8_t bits)
  • void analogSetCycles(uint8_t cycles)
  • void analogSetSamples(uint8_t samples)
  • void analogSetClockDiv(uint8_t clockDiv)
  • void analogSetAttenuation(adc_attenuation_t attenuation)
  • void analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation)

code:

#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);
}

Nonblocking sampling

  • bool adcAttachPin(uint8_t pin)
  • bool adcStart(uint8_t pin)
  • bool adcBusy(uint8_t pin)
  • uint16_t adcEnd(uint8_t pin)

code:

#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);
  }
}


The results of blocking sampling and non-blocking sampling are similar, except that the connection is detected in non-blocking sampling