cancel
Showing results for 
Search instead for 
Did you mean: 

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

kunj_2308
Associate II

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);
 
}
 
6 REPLIES 6
Andrew Neil
Evangelist III

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?

TDK
Guru

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".

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

yes it is toggled

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".

@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 ?