How to change the intensity of LED using python script

How can I change the intensity of led using analogWrite() method with respect to a change in light intensity measured from LDR sensor?

I have connected LDR to A0 pin and the leds to 1,2, and so on pins. Now I want to change the intensity of leds using analogWrite() method. Is it possible while the leds are connected to 1…4 pins or I have to connect it to A0 pin along with the LDR sensor?

@rahul.mukherjee93

Pins 0 to 4 are PWM enabled. That means, all 5 of the pins can be used to send write analog data.

Syntax - analogWrite(‘A’,‘B’)

where A = any pin no. from ( 0 - 4 )
and B = any value between ( 0-255 )

Example - analogWrite('3','155')

1 Like

Thanks for your help… it was helpful.

Can u guide me one more time!!! Is there any command in python to switch off the output pins all at a time?

You cannot really switch off the output pins. But you can use the digitalWrite(‘X’,‘LOW’) for all pins under a function, and run the function in the code. It will do the job.

def TurnOff():
    digitalWrite('A','LOW')
    digitalWrite('B','LOW')
    digitalWrite('C','LOW')
    digitalWrite('D','LOW')

Then call the TurnOff() function wherever you look necessary.

1 Like

Thanks for the solutions provided.

How you tried digitalMultiWrite? It is used to control multiple pins in a single command.

I am not sure if we have added it in the python library, but you can make an API call to get it done.

@yeshwant.naik
No, it’s not in the boltiot package. Should I share a snippet that handles the MultiWrite function using python?

digitalMultiWrite is currently not working in python.

@rahul.mukherjee93
Yeah its not present in the package. But do you really wanna use it?