The Nano 33 BLE board can be programmed using the popular Python® programming language. More specifically, it supports OpenMV's fork of MicroPython, where MicroPython is an implementation of the Python® language, designed to run on microcontrollers. In this article, you will find a lot of sample scripts that will work directly with your Nano 33 BLE, such as general GPIO control, reading data from the IMU module and testing Bluetooth® Low Energy connection.
If you are looking for information related to the similar Nano 33 BLE Sense board, you can refer to the Nano 33 BLE Sense Python® Guide.
This guide does not cover the installation of OpenMV and MicroPython on your board. Please refer to Getting started with OpenMV and Nano 33 BLE for a detailed guide.
Below you will find a lot of useful examples that can be loaded to your Nano 33 BLE board. Many of these examples were extracted from the OpenMV repository, where you can find many useful examples for other boards as well.
In this article, you will only find examples for the Nano 33 BLE board. For more information on how to use delays, read and write to pins, please refer to the Python® with Arduino main article.
The pinout for the Nano 33 BLE and the NRF52840 microcontroller varies greatly. For example, if we are to use
D2
according to the Arduino pinout, we would actually need to use pin 43.1# Defining "D2" on the Nano 33 BLE2p0 = Pin(43, Pin.OUT)
In the MicroPython port of the Nano 33 BLE board, the pinout is the same as the Nordic NRF52840 (the microcontroller). You will find a GPIO Map below this section that explains how to address the different pins.
Before you start using the board's pins, it might be a good idea to check out the table below to understand the relationship between Arduino's pinout and the NRF52840's pinout.
Arduino | nRF52840 |
---|---|
TX | 35 |
RX | 42 |
D2 | 43 |
D3 | 44 |
D4 | 47 |
D5 | 45 |
D6 | 46 |
D7 | 23 |
D8 | 21 |
D9 | 27 |
D10 | 34 |
D11 | 33 |
D12 | 40 |
D13 | 13 |
D14/A0 | 4 |
D15/A1 | 5 |
D16/A2 | 30 |
D17/A3 | 29 |
D18/A4 | 31 |
D19/A5 | 2 |
D20/A6 | 28 |
D21/A7 | 3 |
To read the analog pins on the Nano BLE , we can choose from the following pins:
4
5
30
29
31
2
28
3
To define them, we need to import the
machine
module, and define the pin as follows: 1import machine2
3adc_pin = machine.Pin(29)4adc = machine.ADC(adc_pin)
To read the analog pin, simply use:
1reading = adc.read_u16() #16-bit resolution (0-65535)
The below script will read the
A3
pin on the board and print the value in the terminal.1import machine2import time3
4adc_pin = machine.Pin(29) # A35adc = machine.ADC(adc_pin)6
7while True:8 reading = adc.read_u16() 9 print("ADC: ",reading)10 time.sleep_ms(500)
There are 3 different LEDs that can be accessed on the Nano BLE: RGB, the built-in LED and the power LED.
They can be accessed by importing the
LED
module, where the RGB and built-in LED can be accessed.1from board import LED2
3led_red = LED(1) # red LED4led_green = LED(2) # green LED5led_blue = LED(3) # blue LED6led_builtin = LED(4) # classic built-in LED (also accessible through pin 13)
To access the power LED we need to import the
Pin
module.1from machine import Pin2
3led_pwr = Pin(41, Pin.OUT)
Blink all RGB lights every 0.25 seconds.
1from board import LED2import time 3
4led_red = LED(1)5led_green = LED(2)6led_blue = LED(3)7
8while (True):9 10 # Turn on LEDs11 led_red.on()12 led_green.on()13 led_blue.on()14
15 # Wait 0.25 seconds16 time.sleep_ms(250)17 18 # Turn off LEDs19 led_red.off()20 led_green.off()21 led_blue.off()22
23 # Wait 0.25 seconds24 time.sleep_ms(250)
The classic blink example! Blink the built-in LED every 0.25 seconds.
1from board import LED2import time 3
4led_builtin = LED(4)5
6while (True):7 8 # Turn on LED9 led_builtin.on()10
11 # Wait 0.25 seconds12 time.sleep_ms(250)13 14 # Turn off LED15 led_builtin.off()16
17 # Wait 0.25 seconds18 time.sleep_ms(250)
Access the
accelerometer
, magnetometer
, and gyroscope
data from the LSM9DS1 IMU module.1import time2import lsm9ds13from machine import Pin, I2C4
5bus = I2C(1, scl=Pin(15), sda=Pin(14))6lsm = lsm9ds1.LSM9DS1(bus)7
8while (True):9 #for g,a in lsm.iter_accel_gyro(): print(g,a) # using fifo10 print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_accel()))11 print('Magnetometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_magnet()))12 print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_gyro()))13 print("")14 time.sleep_ms(500)
This example allows us to connect to our board via our phone, and control the built-in LED. We recommend using the nRF Connect applications.
After loading the script below, your board should be listed as "Nano 33 BLE" in the list of available devices. You need to pair in order to control the built-in LED.
1# Use nRF Connect from App store, connect to the Nano and write 1/0 to control the LED.2
3import time4from board import LED5from ubluepy import Service, Characteristic, UUID, Peripheral, constants6
7def event_handler(id, handle, data):8 global periph9 global service10 if id == constants.EVT_GAP_CONNECTED:11 pass12 elif id == constants.EVT_GAP_DISCONNECTED:13 # restart advertisement14 periph.advertise(device_name="Nano 33 BLE", services=[service])15 elif id == constants.EVT_GATTS_WRITE:16 LED(1).on() if int(data[0]) else LED(1).off()17
18# start off with LED(1) off19LED(1).off()20
21notif_enabled = False22uuid_service = UUID("0x1523")23uuid_led = UUID("0x1525")24
25service = Service(uuid_service)26char_led = Characteristic(uuid_led, props=Characteristic.PROP_WRITE)27service.addCharacteristic(char_led)28
29periph = Peripheral()30periph.addService(service)31periph.setConnectionHandler(event_handler)32periph.advertise(device_name="Nano 33 BLE", services=[service])33
34while (True):35 time.sleep_ms(500)
In this article we have gone through a selection of scripts that will help you control your Nano BLE board, via the OpenMV IDE. Feel free to check out our Python® with Arduino boards article, where you can find guides to other boards, useful links to learn Python® and more.