0

I want to dim a LED with a Raspberry Pi 3. It works in loop perfect. But how do I dim the LED let's say 50% constantly? Without the script running in a loop. I want to start the python script that dims the LED and then stop it and the LED should still be dimmed. Is that even possible? The problem is, when I keep running the script the whole time, the CPU load of the Raspberry is very high (the whole time).

This is my code with the loop that works. I just want to remove Line 10, but then it doesn't work anymore. I'm a total python beginner.

import RPi.GPIO as IO
import time
IO.setwarnings(False)
IO.setmode (IO.BCM)
IO.setup(19,IO.OUT)
p = IO.PWM(19,100)
p.start(0)
while 1:
    p.ChangeDutyCycle(50)
truemiro
  • 103
  • 1
  • 8

2 Answers2

1

As far as I know you won't be able to get that behavior without continuously running a script. If you want to run your script in the background you should look into tmux. You can get it with

$sudo apt-get install tmux

And then run it with

$tmux

Then run your script. It will continue to run in the background as long as your RPi is turned on.

Edit: Formatting

Myles Hollowed
  • 546
  • 4
  • 16
0

You can do it with a mix of python and hardware. Rather than use a loop (basically PWM) you can use a variable resistance as an analog dimmer.Then you can use the Rpi to increase the resistance digitally and get a dimmer light (not energy efficient and expensive depending on application). For fun here is a really complex (atleast in my book)electronics heavy answer.

kmcodes
  • 807
  • 1
  • 8
  • 20