1、项目说明
基于RT-Thread+瑞萨RA6M4 采集土壤湿度信息 ,并通过iic 在OLED SSD1306屏幕上显示。
2、项目结构
3、详细设计
3.1 读取传感器数据
3.1.1 传感器介绍
3.2 读取数据
使用ADC读取模拟量
ADC配置可参考【开发板评测】Renesas RA6M4开发板之ADC、GPIO
3、OLED显示
添加SSD1306软件包
需要0.96寸I2C驱动的OLED屏幕进行动态显示,SDA—-p511(p50b);SCL—-p512(p50c),公式首先将p去掉还有三位,以p511为例,最后转换的数字为 “(5x16x16)+(1x10)+1”算出来为 1291 转换为16进制为0x50b。注:在程序里的话需要将字符转为数字。
配置项如下
4、代码实现
soil_moisture.c
/*
- Copyright (c) 2006-2021, RT-Thread Development Team
- SPDX-License-Identifier: Apache-2.0
- Change Logs:
- Date Author Notes
- 2022-07-18 gerhardt the first version
/
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <string.h>
#include <stdio.h>
#include "ssd1306.h"
#define ADC_DEV_NAME "adc0" / ADC 设备名称 /
#define ADC_DEV_CHANNEL 0 / ADC 通道 /
int collection(){
rt_adc_device_t adc_dev;
rt_uint32_t value, vol;
rt_err_t ret = RT_EOK;
//初始化ssd1306
ssd1306_Init();
/ 查找设备 /
adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
if (adc_dev == RT_NULL)
{
rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
return RT_ERROR;
}
while(1){
/ 使能设备 /
ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
/ 读取采样值 /
value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
rt_kprintf("the value is :%d \n", value);
/ 转换为对应电压值,3.3V对应12位最大值4096,数据精度乘以100保留2位小数 /
vol = value * 330 / 4096;
rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
/*
- 湿润土壤:0-2000
- 干燥土壤 2000-4096
/
if (value<2000){
ssd1306_Fill(Black);
ssd1306_SetCursor(18, 0);
ssd1306_WriteString("soil moisture:", Font_7x10, White);
ssd1306_SetCursor(40, 26);
ssd1306_WriteString("moist", Font_11x18, White);
ssd1306_UpdateScreen();
} else {
ssd1306_Fill(Black);
ssd1306_SetCursor(18, 0);
ssd1306_WriteString("soil moisture:", Font_7x10, White);
ssd1306_SetCursor(45, 26);
ssd1306_WriteString("dry", Font_11x18, White);
ssd1306_UpdateScreen();
}
rt_thread_mdelay(1000);
}
/ 关闭通道 /
ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
return ret;
}
/ 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(collection, Soil moisture collection);
soil_moisture.h
/*
- Copyright (c) 2006-2021, RT-Thread Development Team
- SPDX-License-Identifier: Apache-2.0
- Change Logs:
- Date Author Notes
- 2022-07-19 gerhardt the first version
/
#ifndef SRC_SOIL_MOISTURE_H_
#define SRC_SOIL_MOISTURE_H_
int collection(void);
#endif / SRC_SOIL_MOISTURE_H_ */
5、视频演示
原作者:Gerhardt1024
|