mirror of
https://github.com/espressif/esp-idf.git
synced 2025-11-27 13:03:51 +00:00
adc: rename adc examples according to hw feature and usage
This commit is contained in:
6
examples/peripherals/adc/single_read/adc/CMakeLists.txt
Normal file
6
examples/peripherals/adc/single_read/adc/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(adc)
|
||||
8
examples/peripherals/adc/single_read/adc/Makefile
Normal file
8
examples/peripherals/adc/single_read/adc/Makefile
Normal file
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := adc
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
55
examples/peripherals/adc/single_read/adc/README.md
Normal file
55
examples/peripherals/adc/single_read/adc/README.md
Normal file
@@ -0,0 +1,55 @@
|
||||
| Supported Targets | ESP32 |
|
||||
| ----------------- | ----- |
|
||||
|
||||
# ADC1 Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
This example shows how to configure ADC1 and read the voltage connected to GPIO pin.
|
||||
|
||||
## How to use example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
* A development board with ESP32 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
|
||||
* A USB cable for power supply and programming
|
||||
|
||||
In this example, we use `ADC_UNIT_1` by default, we need to connect a voltage source (0 ~ 3.3v) to GPIO34. If another ADC unit is selected in your application, you need to change the GPIO pin (please refer to Chapter 4.11 of the `ESP32 Technical Reference Manual`).
|
||||
|
||||
### Configure the project
|
||||
|
||||
```
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
idf.py -p PORT flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
Running this example, you will see the following log output on the serial monitor:
|
||||
|
||||
```
|
||||
Raw: 486 Voltage: 189mV
|
||||
Raw: 435 Voltage: 177mV
|
||||
Raw: 225 Voltage: 128mV
|
||||
Raw: 18 Voltage: 79mV
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
* program upload failure
|
||||
|
||||
* Hardware connection is not correct: run `idf.py -p PORT monitor`, and reboot your board to see if there are any output logs.
|
||||
* The baud rate for downloading is too high: lower your baud rate in the `menuconfig` menu, and try again.
|
||||
|
||||
For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "adc1_example_main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,108 @@
|
||||
/* ADC1 Example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/adc.h"
|
||||
#include "esp_adc_cal.h"
|
||||
|
||||
#define DEFAULT_VREF 1100 //Use adc2_vref_to_gpio() to obtain a better estimate
|
||||
#define NO_OF_SAMPLES 64 //Multisampling
|
||||
|
||||
static esp_adc_cal_characteristics_t *adc_chars;
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
static const adc_channel_t channel = ADC_CHANNEL_6; //GPIO34 if ADC1, GPIO14 if ADC2
|
||||
static const adc_bits_width_t width = ADC_WIDTH_BIT_12;
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
static const adc_channel_t channel = ADC_CHANNEL_6; // GPIO7 if ADC1, GPIO17 if ADC2
|
||||
static const adc_bits_width_t width = ADC_WIDTH_BIT_13;
|
||||
#endif
|
||||
static const adc_atten_t atten = ADC_ATTEN_DB_0;
|
||||
static const adc_unit_t unit = ADC_UNIT_1;
|
||||
|
||||
|
||||
static void check_efuse(void)
|
||||
{
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
//Check if TP is burned into eFuse
|
||||
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
|
||||
printf("eFuse Two Point: Supported\n");
|
||||
} else {
|
||||
printf("eFuse Two Point: NOT supported\n");
|
||||
}
|
||||
//Check Vref is burned into eFuse
|
||||
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) {
|
||||
printf("eFuse Vref: Supported\n");
|
||||
} else {
|
||||
printf("eFuse Vref: NOT supported\n");
|
||||
}
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
|
||||
printf("eFuse Two Point: Supported\n");
|
||||
} else {
|
||||
printf("Cannot retrieve eFuse Two Point calibration values. Default calibration values will be used.\n");
|
||||
}
|
||||
#else
|
||||
#error "This example is configured for ESP32/ESP32S2."
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
static void print_char_val_type(esp_adc_cal_value_t val_type)
|
||||
{
|
||||
if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
|
||||
printf("Characterized using Two Point Value\n");
|
||||
} else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
|
||||
printf("Characterized using eFuse Vref\n");
|
||||
} else {
|
||||
printf("Characterized using Default Vref\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
//Check if Two Point or Vref are burned into eFuse
|
||||
check_efuse();
|
||||
|
||||
//Configure ADC
|
||||
if (unit == ADC_UNIT_1) {
|
||||
adc1_config_width(width);
|
||||
adc1_config_channel_atten(channel, atten);
|
||||
} else {
|
||||
adc2_config_channel_atten((adc2_channel_t)channel, atten);
|
||||
}
|
||||
|
||||
//Characterize ADC
|
||||
adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
|
||||
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, width, DEFAULT_VREF, adc_chars);
|
||||
print_char_val_type(val_type);
|
||||
|
||||
//Continuously sample ADC1
|
||||
while (1) {
|
||||
uint32_t adc_reading = 0;
|
||||
//Multisampling
|
||||
for (int i = 0; i < NO_OF_SAMPLES; i++) {
|
||||
if (unit == ADC_UNIT_1) {
|
||||
adc_reading += adc1_get_raw((adc1_channel_t)channel);
|
||||
} else {
|
||||
int raw;
|
||||
adc2_get_raw((adc2_channel_t)channel, width, &raw);
|
||||
adc_reading += raw;
|
||||
}
|
||||
}
|
||||
adc_reading /= NO_OF_SAMPLES;
|
||||
//Convert adc_reading to voltage in mV
|
||||
uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
|
||||
printf("Raw: %d\tVoltage: %dmV\n", adc_reading, voltage);
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
||||
Reference in New Issue
Block a user