Skip to main content
Associate II
August 6, 2024
Solved

Controlling the brightness of the led using the PWM and optocoupler and 0 -10 volts led driver

  • August 6, 2024
  • 7 replies
  • 3567 views

I am currently using the STM32F103C8 board, in this, I connect the OPTOCOUPLER  input to the PA6 PIN of stm32 which is the PWM pin, and at the output side I connect the optocoupler's output to the driver's 10v + pin and optocoupler's output with 10v - pin, and I also generate the PWM signal using timer 3 ,and in this i want to control the led brightness using the pwm dutycycle but this is not proper working . give me suggestions what should i do in this case?

 

 

 

this is the pwm initialization code

 

void PWM_Init(void) {
    // Initialization struct
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    TIM_OCInitTypeDef TIM_OCInitStruct;
    GPIO_InitTypeDef GPIO_InitStruct;
    
    // Step 1: Initialize TIM3
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    
    // Configure TIM3 base
    TIM_TimeBaseInitStruct.TIM_Prescaler = 144;
    TIM_TimeBaseInitStruct.TIM_Period = 499;
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
    TIM_Cmd(TIM3, ENABLE);
    
    // Step 2: Initialize PWM
    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
 
    TIM_OCInitStruct.TIM_Pulse = 0;
    
    // Initialize OC1
    TIM_OC1Init(TIM3, &TIM_OCInitStruct);
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
    
    // Initialize OC2
    TIM_OCInitStruct.TIM_Pulse = 0;
    TIM_OC2Init(TIM3, &TIM_OCInitStruct);
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
    
    // Initialize OC3
    TIM_OCInitStruct.TIM_Pulse = 0;
    TIM_OC3Init(TIM3, &TIM_OCInitStruct);
    TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
 
// Initialize OC4
TIM_OCInitStruct.TIM_Pulse = 0;
    TIM_OC4Init(TIM3, &TIM_OCInitStruct);
    TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
    
    // Step 3: Initialize GPIOA (PA6, PA7) and GPIOB (PB0)
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
 
    // Initialize PA6 as push-pull alternate function (PWM output) for LED
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    // Initialize PB0 as push-pull alternate function (PWM output) for LED
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
 
}
 
This topic has been closed for replies.
Best answer by mƎALLEm

@kunj_2308 wrote:

BLUEPILL board


Hello,

ST resources are only dedicated to supporting genuine ST products. We are not committed to ensuring that clones/fakes products work properly with the firmware we provide.

We suggest to purchase a genuine products and purchase them from known and trusted distributors.

Thank you for your understanding.

7 replies

Andrew Neil
Super User
August 6, 2024

Please see the posting tips for how to properly post source code, etc:

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

 


@kunj_2308 wrote:

I am currently using the STM32F103C8 board


What "STM32F103C8 board" ?

 


@kunj_2308 wrote:

I connect the OPTOCOUPLER 


What optocoupler?

 


@kunj_2308 wrote:

 this is not proper working


What, exactly, does that mean?

So what investigation / testing / debugging have you done to find out what's wrong?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
kunj_2308Author
Associate II
August 6, 2024

okay , thanks for your guidance

BLUEPILL board

optocoupler PC817  

i find that if i give the voltages according to that i am not getting the voltage difference of 10v

Andrew Neil
Super User
August 6, 2024

@kunj_2308 wrote:

okay , thanks for your guidance


See point #3 - provide a schematic of your setup.

Some good, clear photos may also help ...

 


@kunj_2308 wrote:

if i give the voltages according to that i am not getting the voltage difference of 10v


Sorry, I don't understand that.

What voltages? Where? According to what? Difference relative to what?

Again, this would be a lot clearer on a schematic!

 


@kunj_2308 wrote:

optocoupler PC817  


This: https://global.sharp/products/device/lineup/data/pdf/datasheet/PC817XxNSZ1B_e.pdf ?

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
TDK
August 6, 2024

If you initialize the pin as a GPIO output and toggle it, does it control the LED as expected? If not, there's a hardware issue.

> TIM_OCInitStruct.TIM_Pulse = 0;

This will not produce a PWM signal, but will produce a DC low signal (or high depending on settings).

 

"If you feel a post has answered your question, please click ""Accept as Solution""."
kunj_2308Author
Associate II
August 6, 2024

yes it is toggled

TDK
August 6, 2024

Did you change the TIM_Pulse value to something nonzero? 100 for example.

"If you feel a post has answered your question, please click ""Accept as Solution""."