PWM LED not working

Hi there, I’m trying to set PWM output for my LED but the output is very strange. I tried to change the duty cycle but the output is just on with 100% brightness(duty cycle=32%-100%) or just flash once(duty cycle=0%-31%).

below is my configuration:

shell_command:
  pwm1: 'python /home/hass/pwm1.py'

script:
  pwm:
    alias: PWM LED
    sequence:
      service: shell_command.pwm1    

automation:
- alias: "PWM Light"
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_select.pwm_led
      state: '50%'
  action:
    - service: script.pwm

here is the code for pwm1.py

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)
pin_22 = GPIO.PWM(22, 50)
pin_22.start(50)

Any solution for this?
Thanks

Not sure if I have an answer, but a couple issues I see here.

Be sure that when you post YAML, you use preformatted text (not blockquote) so that we can spot formatting errors. Indentation is significant.

Not a fix, per se, but you can call that shell_command directly without a script.
service: shell_command.pwm1

Take a look at input_select.pwm_led in your states dev tool (icon in left-sidebar, looks like < >) - is the state 50% (including a % sign)?

You can’t use a script as a trigger. That probably threw an error (look in home_assistant.log in the same directory as your config).

What are you trying to use as a trigger? Ignoring the service as a trigger, you’ve got it set so that this automation will trigger only when you change the input_select.pwm_led to a state of 50%.

As a sanity check - what happens when you run “python /home/hass/pwm1.py” from an SSH session? Does it work on it’s own? If not, it won’t work when HASS calls it.

@ ih8gates
Thank you very much and sorry for the late reply.

Yes, the state of the input_select.pwm_led is 50% including a % sign. I’m trying to use input_select as a trigger to run the python script. I have done some modification to the python script and the configuration.yaml file and it is now able to make it run from an SSH session.

shell_command:
  pwm10: 'python /home/pi/pwm10.py'
  pwm100: 'python /home/pi/pwm100.py'

automation:
- alias: "PWM Light 10"
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_select.pwm_led
      state: '10%'
  action:
    - service: shell_command.pwm10

- alias: "PWM Light 100"
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_select.pwm_led
      state: '100%'
  action:
    - service: shell_command.pwm100

pwm10.py

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(29, GPIO.OUT)

p = GPIO.PWM(29, 50)
while True:
  p.start(10)

pwm100.py

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(29, GPIO.OUT)

p = GPIO.PWM(29, 50)
while True:
  p.start(100)

but there’s still some problem. I have multiple similar python scripts that give different pwm output. After running some python scripts, the output gets weirder and the outputs of multiple pwm clashed making the led giving different brigtness at the same time. I think is because the previous processes are not terminated. Is there any way or alternatives that can terminate the unwanted python script or should I modify the python scripts (pwm10.py and pwm100.py) ?

Thanks

I’m not sure I know how to help you with that. So the python script runs constantly to adjust the PWM?

Something to consider, though - you may be able to handle that all as one shell_command. shell_command supports arguments, so you can set it up like:

shell_command:
  pwm: python /home/pi/pwm.py {{brightness}}

Then in your automation,

action:
    - service: shell_command.pwm
      data_template:
        brightness: '100'

That’ll pass brightness=100 to your shell_command.

I’m not a python guy, so I’m not sure how to handle arguments passed through the command line inside your py script.

BUT - this would give you a single script that you’d have to stop rather than several. You’d just need to work out the command to kill a running script.

Thanks again for the solutions and swift reply :smiley:

this is the same issue I was having, were you able to fix it?