In the Python led dimmer switch project I multiplied the pot_value with 3.3/1023 to get the dimmer voltage, I wanted to switch a led to go \”ON\” when the voltage is bigger then 2.5 V. I wanted to do this with a if statement.. I tried many ways but dit not suceed.I tried it with led1 = machine.Pin(34, machine.Pin.OUT, value=1) Do you have solutions how t o set a led on.
Hi.
I’m sorry, I’m not sure that I understood your question.
To turn an LED ON, set it as an output
led = Pin(34, machine.Pin.OUT)
Then, set it to ON like this:
led.value(1)
Let me know if this is what you’re looking for.
It it’s not. Please clarify your question.
Regards,
Sara
In the Project Led dimmer Switch I wanted to put in a extra led that will go ON when potmeter voltage is > 2.3V and go Off when voltage < 2,3V. In the cource it is not clear how to do this with comands.
Start your code herefrom import machine from machine import Pin, PWM, ADC from time import sleep led = machine.Pin(32, machine.Pin.OUT) frequency = 5000 led = PWM(Pin(5), frequency) pot = ADC(Pin(34)) pot.width(ADC.WIDTH_10BIT) # 1023 pot.atten(ADC.ATTN_11DB) # full range (0 - 3.3)V while True: pot_value = pot.read() print(pot_value) potvoltage = (pot_value)*3.3/1023 print(potvoltage) if pot_value < 15: led.duty(0) else: led.duty(pot_value) if potvoltage > 2.3 led.value(1) if potvoltage < 2.3 led.value(0) sleep(0.1)
Hi.
Try this for your loop
while True:
pot_value = pot.read()
print(pot_value)
potvoltage = (pot_value)*3.3/1023
print(potvoltage)
if pot_value < 2.3:
led.duty(0)
elif potvoltage > 2.3:
led.value(1)
else:
led.duty(pot_value)
Alternatively, to put the LED completely on, you can pass the maximum value for the duty cycle, which is 1023, like this:
led.duty(1023)
I hope this helps.
Regards,
Sara
I dit it on a differand way instead of setting the led to ON and.OFF I gave the command print (“voltage lower then 2.3V” and print (“Votage bigger then 2,3V”) This was working correct and when I added led.value(0) and led.value(1) I got this time the error. Antribute ERROR:’PWM’ object has no Antribute.
. On the solution you gave me I get on the line with led.value(1) a error but no explanation
Hi.
I’m sorry for that issue. You are right about getting that error.
You’ve defined the LED as a PWM object. So, you can’t control it with the .value() method.
You need to control it using PWM.
To turn the LED on set the PWM duty cycle to the maximum value:
led.duty(1023)
To turn the LED off set the PWM duty cycle to zero.
led.duty(0)
I hope this is clear.
Regards,
Sara
Hi Sara,
I don’t understand Antribute ERROR:’PWM’ object has no Antribute value() and your last advice controling with iPWM is not clear at all.
I dit this program with arduino one last year and had no problem.at all.
Hi again.
In your code, when you write this:
led = PWM(Pin(5), frequency)
You are creating a PWM object in MicroPython. PWM objects have these attributes: GPIO, frequency and duty cycle. But it doesn’t have a value attribute.
On the other hand, when you use this line:
led = Pin(5, machine.Pin.OUT)
You are creating a Pin object. Pin objects accept several attributes and the value() attribute is one of them.
However, in your code. You created a PWM object. So, you can’t use .value() to set it to on or off.
Because you’re using PWM to control the LED, you can turn it on or off using the duty cycle. A duty cycle of 100% means the LED is fully on. The range of the duty cycle is 0 to 1023. So, 1023=100% duty cycle.
So, using this line, turns the LED fully on:
led.duty(1023)
On the other hand, 0% duty cycle turns the LED is off.
led.duty(0)
I hope this is clear now.
Regards,
Sara
Hi Sara,
Thank you for the good advice and it is working correct and I now understansd it, why it did not work.
jan.