This post was finally edited by Yuban 10032 at 15:54 on June 1, 2024
Digression
Arduino is very simple to implement ADC functions, so in recent articles, the functions of peripherals like IIC, USART, SPI, etc. We all achieve it quickly. And use the sensor corresponding to the protocol to realize the Demo function.
What we want to achieve today is to use the ADC function on this development board to read the voltage output of the gas quality sensor and send it to the upper computer through the serial port.
text
According to the schematic diagram, there are 8 pins on this development board that can be used for ADC input, as shown in the figure below
The data manual also clearly describes the current ADC, to the effect that 12 bit ADC has 8 channels
Now let's start the coding part. Sparkfun actually provides us with Demo examples. We can open the official demo through the following figure
The codes are as follows:
#include <Arduino.h> #include <Adafruit_TinyUSB.h> // for Serial int adcin = A5; int adcvalue = 0; float mv_per_lsb = 3600.0F/1024.0F; // 10-bit ADC with 3.6V input range void setup() { Serial.begin(115200); while ( ! Serial ) delay(10); // for nrf52840 with native usb } void loop() { // Get a fresh ADC value adcvalue = analogRead(adcin); // Display the results Serial.print(adcvalue); Serial.print(" ["); Serial.print((float)adcvalue * mv_per_lsb); Serial.println(" mV]"); delay(100); }
But actually you notice that the current ADC conversion is performed at 10 bit resolution. The official data manual provides 12 digit ADC.
So at this time, we need to adjust the ADC resolution so that it can run at 12 bit resolution.
At this time, we need to query the Arduino API analog Part of the description of the resolution (I will take a detailed look at the API description in this part, which is definitely not for water articles!)
For the Analog part, Arduino provides five APIs, namely:
- Read the converted value of a Pin( analogRead() )
- Set the read resolution( analogReadResolution() )
- Set reference voltage( analogReference() )
- Write the specified voltage of a PIN bit in PWM mode( analogWrite() )
- Set Write Resolution( analogWriteResolution() )
The above roughly means that the default is ten bit ADC. You can set the ADC bit supported by your board through this API. If you set the resolution of 12 to 16 bit ADC, the result of analylogread() will be affected by the approximation
So let's modify the original program function to support a 12 bit resolution. We just need to set the resolution to 12 bits before reading the ADC value, and then according to 2 ^ 12=4096 Set the calculated value
#include <Arduino.h> #include <Adafruit_TinyUSB.h> // for Serial int adcin = A5; int adcvalue = 0; float mv_per_lsb = 3600.0F/4096.0F; // 12-bit ADC with 3.6V input range void setup() { Serial.begin(115200); while ( ! Serial ) delay(10); // for nrf52840 with native usb analogReadResolution(12); } void loop() { // Get a fresh ADC value adcvalue = analogRead(adcin); // Display the results Serial.print(adcvalue); Serial.print(" ["); Serial.print((float)adcvalue * mv_per_lsb); Serial.println(" mV]"); delay(100); }
After modification, short the ADC to 3.3. If the mv you read is basically equal to the value of 10 bit resolution ADC, the test result is correct
Effect demonstration:
June 1 (1)
In the next section, we will study how to send the converted data through Bluetooth