Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe

LDR(Light dependent resistor)/Photoresistor as light detector using stm32 microcontroller

By EG Projects May 19, 2019

In this tutorial i am going to teach you how to make a simple light detector with 32-bit stm32 microcontroller. LDR(light dependent resistor) is used as a light detector/sensor in the tutorial. Its a simple project and you might have made it before in your electronics class using 555 timer ic. Using a microcontroller to achieve the same task/logic is not difficult. In 555 timer circuit, internal hardware of the 555 timer detects the strength of the light and opens the output channel to switch on the light. In 555 timer ic every thing is hardware driven. But now in microcontroller case we can control many variables(light intensity, switching frequency etc) through software. Microcontroller can be costly if we only want to detect light and switch on the bulb. But if we want to carry out some other tasks on detection of visible light like sending a text message, updating server and maintaining log then microcontroller is necessary in this case.  

I used Stm32CubeMx code configurator and keil uvision 5 ide for code writing and compilation. If you have no previous experience of Stm32CubeMx and keil ide then i suggest you to first take the getting started tutorial with Stm32CubeMx and keil. 

Light detector switch with Stm32 microcontroller and Photoresistor

Microcontroller which i am going to use in diy project is stm32f103c8t6. Its a cortex M1 32-bit microcontroller. I purchased a pre-assembled cheap development board, on which stm32f103 resides. The board cost me about 4$. Pre-assembled boards are easy to work with and saves time to make circuit on breadboard. In the circuit diagram of the project you can see the stm32f103 development board.

Stm32f103 Configurator – Stm32CubeMx

To initialize the stm32f103 pins, registers, clock, pull up/down resistors i used Stm32cubeMx. Stm32cubemx is an initialization platform by stmicroelectronics. Developers can initialize any stm32 series microcontroller graphically using Stm32CubeMx and by pressing a single button stm32cubemx can generate HAL libraries code for the input configuration.

Light detector Input/Outputs

Our light detector detects the visible light and on the intensity of light microcontroller will decide whether to switch on the bulb or not. So an LDR(light dependent resistor)/Photoresistor is an input to our system and bulb is output.

So we need one single gpio(general purpose input/output pin) as input(for light sensor) and one as output(for controlling light bulb). Stm32f103 Port-A Pin#10 is used as input and Port-C Pin#13 is used as output. Stm32f103 used pins are shown below.

Light sensor with stm32 microcontroller pin settings

Light sensor with stm32 microcontroller pin settings

Stm32CubeMx Pin Configuration for Light sensor project

In the Pin configuration window. I named the pins and activated pull up and down resistors. For both the input and output pins their internal pull down resistors are activated. Below picture is from stm32cubemx code configutator pin configuration settings window.     
Light detector using stm32 microcontroller pin settings

Light detector using stm32 microcontroller pin settings

Street Light detector switch – Circuit diagram

LDR(Light dependent resistor) is connected to Port-C Pin#13 of microcontroller. Other leg of photoresistor is connected to active high power in series with a resistor to limit current. Note that Pin#13 internal resistor is pulled down. Stm32 works on 3.3 volts. I supplied 5 volt at input and used 4.7 k resistor to limit the current and bring the voltage down to 3.3 volts. So now our input pin is in save voltage range.

Port-A Pin#10 is our output pin. Through output pin i am controlling a transistor. Transistor is used to switch heavy loads. A 12 volt relay is connected to collector pin of transistor. Relay is activated and deactivated by controlling the base of transistor. An AC powered bulb is connected to relay output pins. So now when we switch on or off the transistor we are actually switching the bulb. Diode across the coil of the relay is protecting the circuit from induced and back emf(electro motive force). Note output pin is also internally pull down.      

Autonomous Light detector with stm32 microcontroller and photoresistor

Light detector with stm32 microcontroller and photoresistor

Light detector code

Code is simple only three statements fulfills our desired logic. I am checking if light is present and its falling on the light sensor. When light falls on the LDR the resistance of LDR decreases and current starts flowing through the circuit. Stm32 code statement which identifies this logic(light falling) is 
​if(HAL_GPIO_ReadPin(LdrPin_GPIO_Port, LdrPin_Pin)==GPIO_PIN_RESET)  

Now if light is not falling on the sensor or the intensity of light is not enough to break the reverse barrier of LDR(light dependent resistor) then switch on the bulb. The statement which switches on the bulb is   
HAL_GPIO_WritePin(Bulb_GPIO_Port, Bulb_Pin,GPIO_PIN_SET)   

else if light is present and falling on the ldr, also lights intensity is enough to forward bias the ldr then switch off the bulb. The statement which switches off the bulb is 
HAL_GPIO_WritePin(Bulb_GPIO_Port, Bulb_Pin,GPIO_PIN_RESET)

The main code of the project is present in the main() function while() loop. While loop is continuously running so our logic is continuously checked. Project can be used as autonomous street light switch. At output instead of a bulb we can attach a water motor to irrigate farm lands by inverting the logic. So when their is light, switch on the water pump and switch off the pump when their is no light.   

Future Work:
One can connect the system with internet, cloud and create a log/database of switching on and off the light. Another feature can also be included to switch the bulb remotely through a web connection. Other sensors can also be introduced in the project and a remote corp field can be monitored and autonomously controlled with the system. Such as watering the corps, monitoring temperature, controlling pumps and lights etc. 

I made a nice smart garden weather project using a WiFi enabled esp8266 controller. Check out the project below.

Smart Garden with Esp8266 IOT Board

Code/Files


Filed Under: Microcontroller Projects, STM32

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

Submit a Guest Post

submit a guest post

EE TECH TOOLBOX

“ee
Tech Toolbox: Power Efficiency
Discover proven strategies for power conversion, wide bandgap devices, and motor control — balancing performance, cost, and sustainability across industrial, automotive, and IoT systems.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions.

  • CMOS inverter VTC DC sweep issue virtuoso
  • Regarding the Frequency Range of DAC Dynamic Characteristics
  • Interfacing Flash chip to FPGA
  • Lighting bus needed: 2kW at 40Vdc
  • ADMIN: PLEASE FIX/REMOVE THE ROBOT TEST AS I CAN'T GET INTO THE FORUM

RSS Electro-Tech-Online.com Discussions

  • JLCPBC are using a different shipping company = less $$$$$$$$
  • Manually actuate fuel tank selector solenoid
  • strange laptop problem
  • Help please! BLDC driver circuit using the IR2136s and the STP80NF06 MOSFETS
  • need two ICs

Featured – Real Time Hardware Filter Design

  • Practical implementation of bandpass and band reject filters
  • Practical application of hardware filters with real-life examples
  • A filter design example
  • Types of filter responses
  • What are the two types of hardware filters?
  • What are hardware filters and their types?

Recent Articles

  • GigaDevices introduces 32-bit MCUs with integrated DSP and FPU support
  • Grinn introduces 8-core SBC supporting AI-ready embedded development
  • EPC’s 100 kHz BLDC inverter supports high-efficiency motion control
  • Melexis announces 5 W smart driver to supports sensorless FOC operation
  • STMicroelectronics’ motion sensor simplifies industrial IoT system design

EE ENGINEERING TRAINING DAYS

engineering
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe