Skip to content
← Back to Community
How to turn on and off a light with python ?
Profile icon
Manceau9

What I am trying to code is not (or at least I dont think it is) very hard. I am trying to code a simple program to turn on and off a light. I will later connect it with an Arduino to turn on and off a light with my program. I need the command to control the light/LED, if you know it. I also would like to know how to give a time limit to the lighting time.

If you can help me thank you !

P.S. : I am exactly trying to make a code to make optic morse. I got the code to translate text to morse, but now I need to translate morse code into light signals. Once I will now how to turn on and off a light/LED, I think it will be easier.

Voters
Profile icon
Manceau9
Comments
hotnewtop
Profile icon
Xarton

Hi, OP. An interesting thread. I am not a programmer and don’t understand the whole process. I just wanted to ask whether it will be possible to use a similar program to turn on and off led strip light from https://www.vont.com/product/smart-strip-lights-led-strip-lights/? They look cool in my room and I like them but I wanted to be able to turn them off and on automatically at certain time.
For example, I usually go to bed at 10 pm, and fall asleep around 10:30 pm. Is it possible that the lights turn off themselves at that time that was set by me? I am sorry if it’s a stupid question, I just want to know if it’s possible.

Profile icon
RYANTADIPARTHI

no this is not possible in repl.it. you could try it in other languages though.

Profile icon
Manceau9

@RYANTADIPARTHI If I try python but not on repl.it would that work ? And is there websites that could translate python into another language ?

Profile icon
Coder100

you can't do that on repl.it, and no this is not easy at all.
First, you need access to the hardware, the easiest lang to do this would be something like C/C++, but if you are using raspberry pi, you could also use node.js.

Profile icon
Manceau9

@Coder100 What do you mean by hardware ?

Profile icon
Coder100

access to the light itself @Manceau9

Profile icon
Manceau9

Ok, thanks ! @Coder100

Profile icon
InvisibleOne

Raspberry pi?

Profile icon
Manceau9

@InvisibleOne I am going to try to use an Arduino, but if I can't then raspberry pi yeah.

Profile icon
InvisibleOne

I don't know ardiuno but on a raspberry pi it would be

from gpiozero import LED led1 = LED(17) # the number should be positive pin number led1.on() led.off()

@Manceau9

Profile icon
Manceau9

@InvisibleOne Thank you very much ! I'll use pi instead of arduino.

Profile icon
InvisibleOne

Np, I have a pi 4b (and an old 3b that doesn't work) and it's pretty cool, that was one of the main reasons I learned python so I could mess around with my pi. @Manceau9

Profile icon
Manceau9

@InvisibleOne That was the last part I needed to finish the code but I did not know how it worked (I am quite new). I was hesitating between pi or arduino but you convinced me, thanks. If you are interested I'll show you the final projet with the LED/lamp connected. If I get it to work it'll translate text into optic morse (light signals).
P.S. Can a pi be transportable ?

Profile icon
InvisibleOne
Profile icon
Manceau9

That is a lot a questions I am asking sorry but I have one more. Do you know how to program the duration of the lighting (can you tell the LED to stay on for 1 second for exemple). @InvisibleOne

Profile icon
Manceau9

Will do when I am done ! @InvisibleOne

Profile icon
InvisibleOne

Yeah, you just use sleep, like this

import time from gpiozero import LED led = LED(17) led.on() time.sleep(1) led.off()

@Manceau9

Profile icon
Manceau9

Thank you again so much, it's going to help me a lot ! @InvisibleOne

Profile icon
InvisibleOne