LED Brightness Control Using Potentiometer

Solving Approach:

How do you plan to solve it?

 

 

Code

/*Paste your code here*/



 int main(void) {

  /* 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();
  MX_ADC1_Init();
  MX_TIM1_Init();

  // Start PWM generation on Timer 1, Channel 1
  // This enables the PWM output on the specified timer channel
  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

  while (1) {
    // Start ADC conversion
    HAL_ADC_Start(&hadc1);

    // Wait for ADC conversion to complete with a timeout of 20ms
    HAL_ADC_PollForConversion(&hadc1, 20);

    // Read the converted ADC value (0-4095 for 12-bit ADC)
    uint16_t adcValue = HAL_ADC_GetValue(&hadc1);

    // Update PWM duty cycle based on ADC reading
    // This creates a direct relationship between analog input and PWM output
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, adcValue);

    // Small delay to stabilize the system and prevent overwhelming the ADC
    HAL_Delay(1);
  }
}

Output

Video

Add video of output (know more)

 

 

 

 

 

Photo of Output

Add a photo of your hardware showing the output.

STM32-Led-brightness-control-circuit

 

 

 

 

Upvote
Downvote

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!