ESP32移植Openharmony外设篇(7)土壤湿度传感器YL-69

土壤湿度传感器YL-69

模块简介

简介

YL-69土壤湿度传感器是一种用于测量土壤湿度的传感器,其工作原理为电阻式。传感器的主体为一个具有两个接头的金属探头,可以将其插入到土壤中,接头内部包含有两个金属探针,通过探针与土壤接触来测量土壤的湿度。

当土壤干燥时,土壤的电阻变大,传感器的输出电压也随之增加,反之,当土壤湿度增加时,土壤的电阻变小,传感器的输出电压也随之降低。通过测量传感器输出电压的大小,就能获得土壤湿度的信息。

YL-69土壤湿度传感器的优点是结构简单,成本低廉,易于使用和安装,可以广泛应用于农业、园艺、环境监测等领域。

特性

土壤湿度传感器特性:

(1) 土壤湿度传感器YL-69,表面采用镀镍处理,有加宽的感应面积,可以提高导电性能 ,防止接触土壤容易生锈的问题,延长使用寿命;

(2) 模块采用金手指设计直接插在拓展板上,即插即用,采用type-C口连接土壤湿度探头,使用方便。

(3) 比较器采用LM393芯片,工作稳定。

原理图

搜集资料可以知道,该传感器是通过ADC读取模拟量来判断土壤湿度;又根据原理图可知,Soil_OUT是接在IO_2上的,查询ESP_IDF编程手册的GPIO口汇总图可以看到其对应的ADC组别和通道号。

所以我们使用ADC2通道2进行ADC采集。

同时我们注意到可以通过Soil_Motor控制电机转动(模拟水泵抽水)通过Soil_LED控制LED灯,二者都是通过简单的GPIO口设置高低电平进行控制。

所以初步的思路是通过土壤湿度传感器测量土壤湿度,如果湿度低于阈值,水泵抽水;如果湿度高于阈值,LED灯闪烁示警。

参考代码

adc_soil_example.c
复制代码
/*
 * Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include "cmsis_os2.h"
#include "ohos_run.h"
#include "osal_irq.h"

/* ADC2 Example*/
#include <stdio.h>
#include <stdlib.h>
#include "driver/gpio.h"
#include "driver/adc_common.h"
#include "esp_adc_cal.h"

#define GPIO_MOTOR GPIO_NUM_23
#define GPIO_LED GPIO_NUM_22
//ADC所接的通道
#define ADC2_TEST_CHANNEL ADC2_CHANNEL_2

osThreadId_t g_taskID = NULL;
int read_raw;
int ret;
int count;

//ADC初始化
//ADC_ATTEN_DB_0:表示参考电压为1.1V
//ADC_ATTEN_DB_2_5:表示参考电压为1.5V
//ADC_ATTEN_DB_6:表示参考电压为2.2V
//ADC_ATTEN_DB_11:表示参考电压为3.9V
void adc_Init()
{
	//adc1_config_width(ADC_WIDTH_12Bit);// 12位分辨率
	adc2_config_channel_atten(ADC2_TEST_CHANNEL,ADC_ATTEN_DB_0);// 设置通道2和1.1V参考电压
}

void motor_gpio_init()
{    
  //配置为输出
    gpio_config_t gpio_soil = {
        .intr_type = GPIO_INTR_DISABLE, //关闭中断
        .mode = GPIO_MODE_OUTPUT,        //输出模式
        .pin_bit_mask = 1ULL << GPIO_MOTOR,  //23引脚
        .pull_down_en = GPIO_PULLDOWN_DISABLE,  //关闭下拉
        .pull_up_en = GPIO_PULLUP_ENABLE        //开启上拉
    };

    gpio_config(&gpio_soil);
}

static void adc_test(void)
{
  static uint8_t state = 0;
	adc_Init();
  motor_gpio_init();
	while(1){
    gpio_set_level(GPIO_MOTOR,1);
        switch(state)
        {
            case 0:
            {
                esp_err_t r = adc2_get_raw( ADC2_TEST_CHANNEL, ADC_WIDTH_12Bit, &read_raw);
                if ( r == ESP_OK ) 
                {
                    ret += read_raw;
                    count ++;
                } else if ( r == ESP_ERR_TIMEOUT )
                {            //说明ADC2的引脚已被用为WIFI占用
                    printf("ADC2 used by Wi-Fi.\n");
                    break;
                }
                if(count == 10)
                {
                    state = 1;
                }
            }
            case 1:
            {
                printf("MQ2 adc value is %d\r\n",ret/count);
                state = 0;
                ret = 0;
                count = 0;
            }
        }
		osDelay(100);
	}
}

static void adc_task(void)
{
    osThreadAttr_t attr;
    attr.name = "adc_test";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024;
    attr.priority = 26;
    g_taskID = osThreadNew((osThreadFunc_t)adc_test, NULL, &attr);
    if (g_taskID == NULL) {
        printf("Failed to create Test ADC thread!\n");
    }
}

OHOS_APP_RUN(adc_task);
BUILD.gn
复制代码
# Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//kernel/liteos_m/liteos.gni")
module_name = get_path_info(rebase_path("."), "name")
kernel_module(module_name){
    sources = [
        "adc_soil_example.c",
    ]
    include_dirs = [
        "//drivers/hdf_core/framework/include/platform/",
        "//drivers/hdf_core/framework/include/utils/",
        "//drivers/hdf_core/framework/support/platform/include/adc",
        "//drivers/hdf_core/adapter/khdf/liteos_m/osal/include/",
        "//drivers/hdf_core/framework/include/core/",
        "//drivers/hdf_core/framework/include/osal/",
        "//drivers/hdf_core/interfaces/inner_api/utils",
        "//device/soc/esp/esp32/components/driver/include",
        "//device/soc/esp/esp32/components/esp_adc_cal/include",
        "//device/soc/esp/esp32/components/driver/esp32/include",
    ]
}

编译并烧录

在源码根目录下使用hb工具对写好的代码进行编译

选择mini级系统

同理 产品选择esp公司下的esp32

选择完毕后在源码根目录下执行hb build -f 进行编译

编译完成后会有如下界面,并且编译后的代码固件位于:out\esp32\esp32

实验现象

按下ESP32开发板上的EN键,即可观察到实验现象:

将纸巾沾湿包裹住土壤湿度探头,可以看到读取到的adc值明显升高,实验成功!

相关推荐
Georgewu23 分钟前
【HarmonyOS】鸿蒙应用开发Text控件常见错误
harmonyos
TESmart碲视42 分钟前
HKS201-M24 大师版 8K60Hz USB 3.0 适用于 2 台 PC 1台显示器 无缝切换 KVM 切换器
单片机·嵌入式硬件·物联网·游戏·计算机外设·电脑·智能硬件
Georgewu2 小时前
【HarmonyOS】富文本编辑器RichEditor详解
harmonyos
花落已飘2 小时前
STM32中实现shell控制台(shell窗口输入实现)
stm32·单片机·嵌入式硬件
TDengine (老段)3 小时前
TDengine STMT2 API 使用指南
java·大数据·物联网·时序数据库·iot·tdengine·涛思数据
牵牛老人4 小时前
Qt处理USB摄像头开发说明与QtMultimedia与V4L2融合应用
stm32·单片机·qt
宇钶宇夕5 小时前
针对工业触摸屏维修的系统指南和资源获取途径
单片机·嵌入式硬件·自动化
和风化雨6 小时前
stm32的三种开发方式
stm32·单片机·嵌入式硬件
kanhao1006 小时前
三态逻辑详解:单片机GPIO、计算机总线系统举例
单片机·嵌入式硬件
zhanshuo9 小时前
鸿蒙应用调试与测试实战全指南:高效定位问题,性能优化必备技巧+实用代码示例
harmonyos