In this guide, we will explore how to interface an LDR (Light Dependent Resistor) sensor with the Nucleo Board. The LDR sensor can detect variations in light intensity and is commonly used for applications such as automatic lighting control, brightness adjustment, and ambient light sensing.
The objective of this project is to interface an LDR sensor with the STM32-F103RB Nucleo Board. By achieving this, we will be able to detect changes in light intensity using the LDR sensor and utilize the Nucleo Board to control an LED based on the detected light.
To successfully complete this project, you will need the following components:
STM32-F103RB Nucleo Board
LED
Resistor (220-330 ohms)
LDR Sensor
Breadboard
Jumper wires
Follow the circuit diagram below to interface the LDR sensor with the Nucleo board:
Connect the positive pin of the LED to the PA8 pin and the negative pin to the GND pin of the board.
Connect the signal/output pin of the LDR Sensor to PC13 pin of the STM32-F103RB Nucleo Board.
Also connect the other terminals of the LDR sensor to the STM32-F103RB Nucleo Board.
Follow these steps to configure the .IOC file and set up GPIOs and Clock :
Open the STM32 Cube IDE and start by creating a new STM32 project. Click on the board selector tab in the target selection window.
Enter "NUCLEO-F103RB" as the commercial part number and click on "Next" after selecting the board.
3. Enter the Project Name "LDR" and click on "Finish". After that, click on "Yes" to initialize all the peripherals in default mode.
4. In the Pinout view of the STM32 Cube IDE, locate the PC13 pin. Select the "GPIO_Input" option for PC13 and name it as "LDR". Locate the PA8 pin and select the "GPIO_Output" option for PA8. Name it as "LED".
5. In the Pinout & Configuration menu, navigate to the GPIO section. Select the PC13 pin that is named "LDR". Set the GPIO Pull-up/Pull-down option to "Pull Up" for the PC13 pin. Leave the rest of the configuration settings as default.
6. After finishing the Pinout Configuration, go to the Clock Configuration menu. In the HCLK field, enter "72 MHz" as the desired frequency for the HCLK (system clock). The Integrated Development Environment (IDE) will automatically search for the necessary clock sources and prescalers based on this configuration.
7. Next, locate the gear-like icon on the toolbar and click on it. This will add the configuration settings we did in the GUI to the code.
Now, let's proceed to understand and write the C code to control the LED using Light Sensor . Follow the steps below:
The Device Configuration Tool will automatically open the main.c file for you. In the project, you will find all the necessary pin and port names defined in the Core ⇾ Inc ⇾ main.h file. These definitions will help us easily reference the pins and ports in our code.
Navigate to the main function in the main.c file and paste the following code:
Note: You can download the full project code from the provided link.
int main(void)
{ /* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
uint8_t LDR_val = 0;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
LDR_val = HAL_GPIO_ReadPin(LDR_GPIO_Port, LDR_Pin); // read the value of the button
if (LDR_val)
{
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET); // turn on the LED
}
else
{
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); // turn off the LED
}
}
/* USER CODE END 3 */
}
After pasting the above code, ensure that you have connected your STM32-F103RB Nucleo Board to your PC/Laptop using a USB cable.
Next, click on the "Run and Debug" icon in the toolbar menu of the IDE (Refer to image below ). This will initiate the build process for your project. The IDE will compile the code. Once the build process is successful, the IDE will start flashing the compiled project to the STM32-F103RB Nucleo Board.
Upon successfully running the project, when the ambient light exceeds the threshold, the LED will turn off, and when the light falls below, the LED will turn off.
In this guide, we have explored the process of interfacing an LDR (Light Dependent Resistor) sensor with the STM32-F103RB Nucleo Board. This allows us to achieve automatic control of an LED based on the ambient light levels detected by the LDR sensor. By following the steps outlined in this guide, we have successfully implemented a project that offers various applications such as automatic lighting control and energy-saving mechanisms.