nRF52840 Development with Arduino and CircuitPython

Pages
Contributors: jimblom
Favorited Favorite 4

CircuitPython Examples

New platform? New programming language? Time to blink some LEDs! Copy the code below to start blinking the blue LED on pin 7:

language:python
import time
import board
from digitalio import DigitalInOut, Direction, Pull

led = DigitalInOut(board.P0_07)
led.direction = Direction.OUTPUT

while True:
    led.value = False
    time.sleep(0.5)
    led.value = True
    time.sleep(0.5)

Then create a new file named code.py and paste the above code into it. (Or you can download blinky.py and rename it "code.py" if you prefer.) Note that CircuitPython is expecting a Python file explicitly named "code.py" for its Python application. If your file is named something else, it won't run.

After the file loads, you should be presented with that comforting blinking light.

Want to change up the time delay? Feel free to edit the "code.py" script directly on the CIRCUITPY drive. After you save, it should reload the script and run it anew.

Here's another example pulling in button-support:

language:python
import time
import board
from digitalio import DigitalInOut, Direction, Pull

led = DigitalInOut(board.P0_07)
led.direction = Direction.OUTPUT

button = DigitalInOut(board.P0_13)
button.direction = Direction.INPUT
button.pull = Pull.UP

while True:
    if not button.value:
        print("Button pressed!")
        led.value = True
    else:
        led.value = False

    time.sleep(0.01)

After saving that, you should be able to toggle the LED by tapping the pin 13 button.

Troubleshooting

As noted, CircuitPython support for the nRF52840 is still in a very early release stage. Here are a few tips for escaping some of the more common pitfalls.

Debugging the Python

If you're a seasoned Python developer, or really done any Python work at all, you know how critical the terminal can be to tracing-back errors. Luckily a debugging port is built in to CircuitPython too!

CircuitPython enumerates as a serial port on your computer -- something in the form of COM## for Windows or /dev/tty.usbmodem###### on Mac. Find the port, then open up your favorite serial terminal setting the baud rate to 115200.

Debug terminal using Tera Term

The terminal also features a REPL (read-eval-print loop) interface, which you can access by pressing enter in the terminal. That comes in especially handy for the next issue...

File Corruption

File corruption issues are not uncommon in CircuitPython. Maybe Windows doesn't like the way you ejected the drive. If you're having issues opening or even deleting "code.py", try this:

Follow the instructions above to open your CircuitPython terminal. Then press Enter to enter REPL mode.

Then type these two instructions:

language:python
>>> import storage
>>> storage.erase_filesystem()

This will delete all files contained within CircuitPython's filesystem. You should now be able to drag a new code.py on and keep going.


For more troubleshooting tips, check out the CircuitPython Troubleshooting Guide.