Phone

    00852-6915 1330
  • Contents

Catalog

Introduction

How does an LDR work?

How to setup ADC in STM32

Introduction

The majority of streetlights and outdoor lights are typically operated manually. To manually turn on and off lights is not only risky, but it also wastes energy well as the timing of turning on and off is not optimized. Therefore, an optimized, efficient, and automatic light system is needed to efficiently control light brightness and turn on and off them automatically. In this article, a brief introduction to automatic control of light brightness is given as well as its practical implementation using an STM32 microcontroller and a cheap LDR sensor shown in Figure 1 is demonstrated.

ldr-breakout-board

Figure 1 LDR breakout board

In an automatic light control system, a light detection system is employed that senses the light intensity. If the application requires only to turn on and off a light system, then a threshold value of light intensity is set below which the light will turn on, and above it, the light will turn off. However, if the application is to control the light brightness based on the light intensity in the environment, then a PWM-controlled voltage is provided to the automatic light system.

For light detection, a Light Dependent Resistor commonly known as LDR is used. LDR is a sensor whose resistance varies with the intensity of light. This property of an LDR can be used to sense darkness and brightness. Thus, it can be used to automatically control the turn on and off as well as the intensity of the light system. A typical LDR has a maximum resistance value in mega ohms and a minimum resistance value in several kilo ohms.

Materials

1 STM32 F401/F103
2 LDR sensor
3 Potentiometer
4 LED

How does an LDR work?

So, how exactly does an LDR operate? LDR works on the principle of photoconductivity. It is an optical phenomenon in which material conductivity increases when light falls upon it. When light or photon strikes the material, the electrons in the semiconductor material's valence band are stimulated to the conduction band. The incident photons must have energy larger than the bandgap of the semiconductor material to cause the electrons to move from the valence band to the conduction band. Hence as light intensity increases more and more electrons are excited to the conduction band which produces a large number of charge carriers. This means that more current will flow in the circuit, and as a result, the resistance will decrease.

LDR resistance that changes with the intensity of light cannot be read in a microcontroller. To make it readable in a microcontroller the resistance is represented in terms of voltage. For this purpose, a circuit needs to be designed. Many circuits can be used for LDR. These can be based upon MOSFET, BJET, or an amplifier. However, the most commonly used circuit for LDR to convert its resistance into voltage is the voltage divider circuit. In this circuit, two resistors are installed in series. One side is attached to the positive terminal of the battery while the other is attached to the ground. The schematic of the voltage divider is shown in Figure 2. The output of the voltage divider can be fed to another circuit for other purposes such as a comparator i.e LM393. Usually, a comparator is used in on-off operations where the lights are needed to be turned on and off when a threshold value of light intensity is absorbed by the LDR. A typical circuit for the LM393 comparator is shown below.

lm393-comparator-usage-with-ldr

Figure 2 LM393 comparator usage with LDR

The calculation for the voltage divider circuit is pretty easy. Referring to Figure 2, the following equation can be used to measure the output voltage.

 

In this equation, it is assumed that there is no load on the output voltage because that load can affect the output voltage.

The output of the circuit is shown in Figure 2 where the change in resistance changes the voltage at the IN1+ pin of the comparator. As we know the voltage changes with the intensity of light. The circuit gives maximum voltage in complete darkness while minimum voltage when placed in bright light. The ADC of the STM32 controller can be used to sense the change in the voltage while the results obtained via ADC can be used to generate PWM. It is the PWM that generates average voltage and hence controls the intensity of light. In this article, both the manual and automatic light intensity control is demonstrated using an LED light. The program and procedure for automatic and manual light brightness control is same, the only difference is that in automatic light brightness control and LDR is used while in manual mode simple potentiometer is used.

How to setup ADC in STM32

In STM32, ADC can be configured in three different ways. 1) Polling 2) Interrupt 3) DMA.

Polling: In the polling method when ADC conversion starts the CPU operation halts. It is only after the conversion is completed, the CPU resumes working.

Interrupt: The second method is by using the interrupt service routine. When ADC conversion competes, it generates an interrupt during which required functions are executed which in our case is to update the PWM value.

DMA: The third method is to use direct memory access (DMA). In this method, the ADC directly transfers the data to memory bypassing the CPU altogether. This is the most efficient method of all as it does not involve CPU in the ADC operations and keeps it available for other tasks.

In this experiment, we will be using interrupt methods which are both simple and efficient.

Required hardware

hardware

hardware2

Let's build the program step by step

  • Open STM32CubeIDE and start a new project
  • Select an MCU which in our case is STM32F401CD
  • Go to SYS -> Debug and select Serial Wire. Select SystTick in TimeBase Source. Go to RCC-> High Speed Clock and select Crystal/Ceramic Resonator.

program1

program2

 

  • Configure ADC1. Select IN1 and set it to be triggered by software. From the NVIC controller tab check the global interrupt box.

program3

  • Configure Timer 1 in PWM mode with output on CH1. Set the counter period register value to 839 and Prescaler register value to 100. This will ensure 1000 Hz frequency at the output.  The following formula can be used to set PWM frequency

program4

Setting Prescaler value to 99 while the required frequency is 1000 Hz, the ARR value can be calculated as 839.

 

 

  • Finally set the clock frequency to 84 MHz and select HSE as the clock source. And generate the code.

The final code is given below

#include "main.h"
ADC_HandleTypeDef hadc1;
TIM_HandleTypeDef htim1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_TIM1_Init(void);
uint16_t AD_Data = 0;

uint16_t minimumADC = 1000;

uint16_t maximumADC = 3000;
int main(void)
{
  HAL_Init()
  SystemClock_Config();
  MX_GPIO_Init();
  MX_ADC1_Init();
  MX_TIM1_Init();
  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
  while (1)
  {
  HAL_ADC_Start_IT(&hadc1);
  TIM1->CCR1 = ((AD_Data-minimumADC)*840)/maximumADC;
  }
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
    AD_Data = HAL_ADC_GetValue(&hadc1);
}

duty-cycle-in-bright-light

Figure 3 Duty Cycle in Bright Light

duty-cycle-in-low-light

Figure 4 Duty Cycle in Low Light

 

Resources

download Automatics Light.zip

Victoria

Victoria is a highly accomplished technical writer with over 8 years of experience in the semiconductor electronics industry. She possesses a deep understanding of complex technical concepts and a proven ability to translate them into clear, concise, and user-friendly documentation. She is also an excellent communicator and collaborator, with the ability to work effectively with engineers, product managers, and other technical professionals.

Join our mailing list!

Be the first to know about new products, special offers, and more.

Leave a Reply

We'd love to hear from you! Feel free to share your thoughts and comments below. Rest assured, your email address will remain private.

Name *
Email *
Captcha *
Rating:

Kynix

  • How to purchase

  • Order
  • Search & Inquiry
  • Shipping & Tracking
  • Payment Methods
  • Contact Us

  • Tel: 00852-6915 1330
  • Email: info@kynix.com
  • Follow Us

authentication

Kynix

© 2008-2026 kynix.com all rights reserved.