0

I am having difficulty using a servo motor with the bbc-microbit v2. I am programming in MicroPython using Mu (1.1.0.alpha.2), and I have followed instructions and sample code from Microbit and Sparkfun. I keep getting a "ValueError: invalid period" when I use the command "set_analog_period". I can get the exact same set-up and similar program to work, without any errors, when using the Microbit MakeCode editor.

Here is the sample code (slightly edited for length) that is taken directly from https://support.microbit.org/support/solutions/articles/19000101864-using-a-servo-with-the-micro-bit:

from microbit import * 
pin0.set_analog_period(20)

while True: 
    pin0.write_analog(75)
    sleep(1000)

The REPL outputs:

Traceback (most recent call last):
  File "main.py", line 2, in <module>
ValueError: invalid period
MicroPython v1.13 on 2020-12-21; micro:bit v2.0.0-beta.3 with nRF52833
SNW
  • 1
  • 1

1 Answers1

1

I also failed to get the mu editor to run correctly with v2 of the micro:bit. Instead of mu, I use the online micropython editor here: https://python.microbit.org/v/2

This can be downloaded from the project's GitHub site and used offline. The GitHub site is here:

https://github.com/bbcmicrobit/PythonEditor

To get this to run in Linux (you don't mention which OS you are using), you need to set up a udev rule:

Create the file: /etc/udev/rules.d/50-microbit.rules

with this content: SUBSYSTEM=="usb", ATTR{idVendor}=="0d28", MODE="0664", GROUP="plugdev"

You then need to add your username to the plugdev group:

To add your username to the plugdev group: sudo usermod -aG plugdev <user>

To have this change recognised by the system, we need to restart the udev rules: sudo udevadm control --reload-rules

The PythonEditor then works in Chrome for me. The first time I flash a script takes a while, I suspect the first flash writes a micropython interpreter hex file to the board.

Oppy
  • 2,662
  • 16
  • 22