当前位置:网站首页>STM32 project practice (1) introduction and use of photosensitive resistor

STM32 project practice (1) introduction and use of photosensitive resistor

2022-07-01 12:08:00 Proficient in embedded

Preface

In this article, we will take you to understand the use of photoresistors

One 、 Introduction to photosensitive resistance

Here is 4 Physical drawing of line photosensitive resistance
 Insert picture description here
Product wiring instructions :
1、VCC Connect the positive pole of the power supply 3.3-5V
2、GND Connect the negative pole of the power supply
3、DO TTL Switch signal output ( Configured for output mode )
4、AO Analog signal output (ADC Sampling channels )

Photosensitive resistance is mainly used to detect the surrounding light intensity , Light intensity changes, then ADC The sampled value will also send changes .

Two 、cubeMX To configure

We won't talk more about simple configuration, mainly ADC Configuration of sampling
What I'm using here is ADC1 passageway 4 So photoresistor A0 It should be connected to the corresponding ADC On the channel .
 Insert picture description here
By the way D0 Output mode for simple detection of light intensity .
 Insert picture description here

3、 ... and 、 Code instructions

//IO Definition of mouth 
#define D0_SET() HAL_GPIO_WritePin(D0_GPIO_Port, D0_Pin, GPIO_PIN_SET)
#define D0_RESET() HAL_GPIO_WritePin(D0_GPIO_Port, D0_Pin,GPIO_PIN_RESET)


//ADC Sampling value 
typedef struct
{
    
  u32 adc_vol;//ADC Measured value 
  float vol;// Actual voltage value 
  
}RESIST;

HAL_ADC_Start_IT(&hadc1);// Interrupt mode on ADC
//ADC Callback function 
void  HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
    
   	 resist_data.adc_vol=HAL_ADC_GetValue(hadc);// obtain ADC Conversion result 
     resist_data.vol=resist_data.adc_vol*5/4095;// Convert to voltage value 
}

// Test function when the voltage is greater than 2.5 when D0 Output on low-level photoresistor LED The indicator light is on 
void Resist_Test(void)
{
    
  if(resist_data.vol>2.5)
  {
    
    D0_RESET();
  }
  else
  {
    
    D0_SET();
  }
}

while(1)
{
    
	Resist_Test();
}

summary

The use of photoresistors is actually ADC Use of sampling , If you are not familiar with ADC If sampling, you can see my previous article :ADC sampling

原网站

版权声明
本文为[Proficient in embedded]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160034599002.html