top of page

EMBEDDED SYSTEMS

Welcome to KUS-BLOG, I started this blog to share my embedded system projects which I  did in my spare time. Explore my site and all that I have to offer; perhaps KUS-BLOG will ignite your own passions as well.

Home: Welcome
Home: Blog2
Search

Interfacing Soil Sensor with stm32-Discovery

Kusala Bandara

Updated: Nov 8, 2019


I used "Gikfun Capacitive Soil Moisture Sensor " to measure the plant's soil moisture level. I wired this moisture sensor to STM32Discovery board as follows,


STM32Discovery Moisture Sensor

=========================================

GND GND

3V3 VCC

PA1 A0


This program converts Analog voltage to Digital voltage using ADC. Depending on the Digital Voltage you get, you could detect if the soil is dry or wet. If water is needed you could turn on a water pump until desired soil moisture level is reached.



void Enable_Clock(void);

void Config_GPIO(void);

void ADC_Config();

float Read_ADCVal();

float ADCValue;


int main(void)

{

Enable_Clock();

Config_GPIO();

ADC_Config();

While(){

//Reading Soil Voltage

ADCValue=Read_ADCVal();

}

}


void Enable_Clock(){

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);

}


void Config_GPIO(){

//ADC1

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(GPIOA,&GPIO_InitStructure);

}

void ADC_Config(void){

ADC_InitTypeDef ADC_InitStructure;

ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;

ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;

ADC_InitStructure.ADC_NbrOfChannel=1;

ADC_InitStructure.ADC_ScanConvMode=DISABLE;

ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;

ADC_InitStructure.ADC_ExternalTrigConv =ADC_ExternalTrigConv_None;

ADC_Init(ADC1,&ADC_InitStructure);

ADC_Cmd(ADC1,ENABLE);

ADC_ResetCalibration(ADC1);

while(ADC_GetResetCalibrationStatus(ADC1));

ADC_StartCalibration(ADC1);

while(ADC_GetCalibrationStatus(ADC1));

ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_1Cycles5);

ADC_SoftwareStartConvCmd(ADC1, ENABLE);


}













951 views0 comments

Recent Posts

See All

Comments


CONTACT

Thanks for submitting!

Glowing Keyboard
Home: Contact
bottom of page