Write_analog in microPython on BBC micro: the bit does nothing

I have a simple one line of micro-bit javascript that works, but the same line in microPython doesn't.

I have a potentiometer connected to a micro bit and the readings from the potentiometer are sent to a pin attached to the LED to control the brightness of the LED. Javascript code

        pins.analogWritePin(AnalogPin.P1, pins.analogReadPin(AnalogPin.P0))

      

which is the result of the block code, works great and the brightness of the LED changes with the change of the potentiometer.

But using a similar line microPython

    pin1.write_analog(pin0.read_analog())

      

differs not in the intensity of the LED.

Any ideas on what's going on here?

+3


source to share


2 answers


Your micropitton line of code is fine. Separately, it works for me to read one and one and as a result proportionally reduces the LED connected to pin 1 and ground one time . But it makes sense to consider.

Are you re-executing the line in a closed loop without sleep (and no other time-consuming operations)? Like this?

#This will not work
from microbit import *
while True:
    pin1.write_analog(pin2.read_analog())

      

When I test the above, the LED is off. This is resolved simply by adding 50-100 milliseconds sleep to the cycle.

#This will work
from microbit import *
while True:
    pin1.write_analog(pin2.read_analog()) 
    sleep(50)

      

Please note that this is the same as the solution above from an analog user.



pin1.write_analog(v)

does not actually set the analog pin voltage, it simulates it using a PWM formula that is high for a percentage of its cycle, proportional to your desired "analog" voltage, and low for the rest of the cycle.

I suspect that you do not allow time for this wave to complete the cycle before you call again write_analog()

, and therefore you will never see the whole waveform. It is possible that the PWM wave starts at a maximum again every time it is called write_analog()

- I don't know. If so, that would mean that you are seeing a signal that is constantly going high, so just 3.3v is constant. Not what you want.

Leaving to sleep between calls to the same analog_write () will give you the dullness you expect. (Alternatively, find another way to avoid calling analog_write () as often.)

Maybe your javascript version doesn't have this problem because it is slow enough to repeat the PWM simulation before you change it again.

Please note that you can speed up the formation of the PWM signal, giving it a period of 1 ms: pin1.set_analog_period(1)

. This improves somewhat in my testing, but not as good as just leaving more time between calls write_analog

. You still need to leave at least 1ms between calls.

Here's the documentation on micro: bit IO and PWM pins http://microbit-micropython.readthedocs.io/en/latest/pin.html

+1


source


I got the following micro-python running on micro-bit. I connected pin 0 to a resistor bridge with a potentiometer on one side so that I can vary the voltage on pin 0. I connected pin 1 to an LED with a resistor in series with ground. I also measured the voltage on pin 0 and pin 1 with a Fluke multiplexer and they match well. I see that the intensity of the LED changes when I adjust the potentiometer.



from microbit import *

while True:
    pin1.write_analog(pin0.read_analog())
    sleep(100)

      

+1


source







All Articles