Nano BLE Sense

MicroPythonを使用し、Nano BLE Sense固有の機能の使い方を学びます。


Author:Karl SöderbyLast revision:2024/12/18

このガイドでは、シリアルプロトコルや利用できる内蔵センサーといった、Nano BLE Senseボード固有の情報を紹介します。

インストール方法は、以下のリンクを参照してください。

ピンアウト

Nano BLE Senseのピンアウトを以下に示します。

i
製品の詳細は、ハードウェア製品ページを参照してください。

ボード固有機能

Nano BLE Senseは、MicroPythonからアクセスできるボード固有機能が多くあります。

  • 内蔵LED: ボード上の小さい1ピクセルLED
  • RGB LED: rgbの値を設定することで制御可能な、簡易なRGBピクセル
  • マイク(MP34DT05): 音声サンプルを録音するためのマイク
  • ジェスチャーセンサー(APDS9960): 周辺光と距離の測定
  • 大気圧センサー(LPS22): 気象アプリケーション向け大気圧の測定
  • 気温と湿度(HTS221(Rev1)、HS3003(Rev2)): 気温と相対湿度の測定
  • IMU(LSM9DS1(Rev1)、BMI270 + BMM150(Rev2)): ジャイロスコープと加速度データの測定

RGB LED

RGBピクセルを使うには、123番ピンを制御します。以下は、メインカラーを順に点滅させる例です。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from board import LED
import time 

led_red = LED(1)
led_green = LED(2)
led_blue = LED(3)

while (True):
   
    # Turn on LEDs
    led_red.on()
    led_green.on()
    led_blue.on()

    # Wait 0.25 seconds
    time.sleep_ms(250)
    
    # Turn off LEDs
    led_red.off()
    led_green.off()
    led_blue.off()

    # Wait 0.25 seconds
    time.sleep_ms(250)

内蔵LED

典型的な点滅の例です。内蔵LEDを0.25秒間隔で点滅させます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from board import LED
import time 

led_builtin = LED(4)

while (True):
   
    # Turn on LED
    led_builtin.on()

    # Wait 0.25 seconds
    time.sleep_ms(250)
    
    # Turn off LED
    led_builtin.off()

    # Wait 0.25 seconds
    time.sleep_ms(250)

IMU (LSM9DS1、BMI270 + BMM150)

IMUモジュールからのaccelerometermagnetometergyroscopeデータにアクセスします。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import time
import imu
from machine import Pin, I2C

bus = I2C(1, scl=Pin(15), sda=Pin(14))
imu = imu.IMU(bus)

while (True):
    print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*imu.accel()))
    print('Gyroscope:     x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*imu.gyro()))
    print('Magnetometer:  x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*imu.magnet()))
    print("")
    time.sleep_ms(100)

気温と湿度(HTS221)

HTS221センサー(Nano 33 BLE Sense)の気温湿度の値にアクセスします。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import time
import hts221
from machine import Pin, I2C

bus = I2C(1, scl=Pin(15), sda=Pin(14))
hts = hts221.HTS221(bus)

while (True):
    rH   = hts.humidity()
    temp = hts.temperature()
    print ("rH: %.2f%% T: %.2fC" %(rH, temp))
    time.sleep_ms(100)

気温と湿度(HS3003)

HS3003センサー(Nano 33 BLE Sense Rev2)の気温湿度の値にアクセスします。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import time
from hs3003 import HS3003
from machine import Pin, I2C

bus = I2C(1, scl=Pin(15), sda=Pin(14))
hts = HS3003(bus)

while True:
    rH   = hts.humidity()
    temp = hts.temperature()
    print ("rH: %.2f%% T: %.2fC" %(rH, temp))
    time.sleep_ms(100)

大気圧(LPS22)

LPS22センサーの大気圧の値にアクセスします。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import time
import lps22h
from machine import Pin, I2C

bus = I2C(1, scl=Pin(15), sda=Pin(14))
lps = lps22h.LPS22H(bus)

while (True):
    pressure = lps.pressure()
    temperature = lps.temperature()
    print("Pressure: %.2f hPa Temperature: %.2f C"%(pressure, temperature))
    time.sleep_ms(100)

周辺光(APDS9960)

APDS9960センサーの周辺光の値にアクセスします。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from time import sleep_ms
from machine import Pin, I2C
from apds9960.const import *
from apds9960 import uAPDS9960 as APDS9960

bus = I2C(1, sda=Pin(13), scl=Pin(14))
apds = APDS9960(bus)

print("Light Sensor Test")
print("=================")
apds.enableLightSensor()

while True:
    sleep_ms(250)
    val = apds.readAmbientLight()
    print("AmbientLight={}".format(val))

近接(APDS9960)

APDS9960センサーの近接の値にアクセスします。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from time import sleep_ms
from machine import Pin, I2C

from apds9960.const import *
from apds9960 import uAPDS9960 as APDS9960

bus = I2C(1, sda=Pin(13), scl=Pin(14))
apds = APDS9960(bus)

apds.setProximityIntLowThreshold(50)

print("Proximity Sensor Test")
print("=====================")
apds.enableProximitySensor()

while True:
    sleep_ms(250)
    val = apds.readProximity()
    print("proximity={}".format(val))

マイク(MP34DT05)

以下の例は、OpenMVのフレームバッファウインドウ(右上隅)で使えます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import image, audio, time
from ulab import numpy as np
from ulab import scipy as sp

CHANNELS = 1
SIZE = 256//(2*CHANNELS)

raw_buf = None
fb = image.Image(SIZE+50, SIZE, image.RGB565, copy_to_fb=True)
audio.init(channels=CHANNELS, frequency=16000, gain_db=80, highpass=0.9883)

def audio_callback(buf):
    # NOTE: do Not call any function that allocates memory.
    global raw_buf
    if (raw_buf == None):
        raw_buf = buf

# Start audio streaming
audio.start_streaming(audio_callback)

def draw_fft(img, fft_buf):
    fft_buf = (fft_buf / max(fft_buf)) * SIZE
    fft_buf = np.log10(fft_buf + 1) * 20
    color = (0xFF, 0x0F, 0x00)
    for i in range(0, SIZE):
        img.draw_line(i, SIZE, i, SIZE-int(fft_buf[i]), color, 1)

def draw_audio_bar(img, level, offset):
    blk_size = SIZE//10
    color = (0xFF, 0x00, 0xF0)
    blk_space = (blk_size//4)
    for i in range(0, int(round(level/10))):
        fb.draw_rectangle(SIZE+offset, SIZE - ((i+1)*blk_size) + blk_space, 20, blk_size - blk_space, color, 1, True)

while (True):
    if (raw_buf != None):
        pcm_buf = np.frombuffer(raw_buf, dtype=np.int16)
        raw_buf = None

        if CHANNELS == 1:
            fft_buf = sp.signal.spectrogram(pcm_buf)
            l_lvl = int((np.mean(abs(pcm_buf[1::2])) / 32768)*100)
        else:
            fft_buf = sp.signal.spectrogram(pcm_buf[0::2])
            l_lvl = int((np.mean(abs(pcm_buf[1::2])) / 32768)*100)
            r_lvl = int((np.mean(abs(pcm_buf[0::2])) / 32768)*100)

        fb.clear()
        draw_fft(fb, fft_buf)
        draw_audio_bar(fb, l_lvl, 0)
        if CHANNELS == 2:
            draw_audio_bar(fb, r_lvl, 25)
        fb.flush()

# Stop streaming
audio.stop_streaming()

通信

Nano BLE Senseは、I2CとUART、SPIをサポートします。それらの使い方を以下に示します。

I2C

Nano BLE SenseのI2Cバスは、A4/A5ピンを通じて利用できます。以下は利用例です。

1
2
3
4
5
6
7
from machine import Pin, I2C

# Initialize I2C with SCL on A5 and SDA on A4
i2c = I2C(0, scl=Pin(5), sda=Pin(4))
devices = i2c.scan()

print("I2C devices found:", devices)

UART

Nano BLE Senseは、D0/D1ピンでUARTをサポートします。以下は利用例です。

1
2
3
4
5
6
7
8
9
from machine import UART

# Initialize UART on pins 16 (TX) and 17 (RX)
uart = UART(1, baudrate=9600, tx=16, rx=17)

# Send and receive data
uart.write("Hello from Nano BLE Sense!")
data = uart.read()
print("Received:", data)

SPI

Nano BLE Senseは、以下のピンでSPIをサポートします。

  • (CIPO): D12
  • (COPI): D11
  • (SCK): D13
  • (CS): 任意のGPIO(A6/A7を除く)

以下は利用例です。

1
2
3
4
5
6
from machine import Pin, SPI

# Initialize SPI with SCK on pin 18, MOSI on pin 23, and MISO on pin 19
spi = SPI(1, baudrate=1000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))

print("SPI initialized")

オリジナルのページ

https://docs.arduino.cc/micropython/board-examples/nano-ble-sense/

最終更新日

August 16, 2026

inserted by FC2 system