Analog Input Sampling Rate

I'm using the Bluefruit LE Shield with an Arduino Uno to send analog input data over bluetooth using the UART service.

Here is the relevant code loop on the board:

void loop() {
  val = analogRead(pin);
  // val += 1;

  Serial.print(millis());
  Serial.print(", ");
  Serial.println(val);
  Ble.println(val);
  
  // delay(1);
}

and here is the querying code (python) on my computer:

import time

from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService

delay = 100 # ms
board_name = 'Bluefruit LE Shield'

ble = BLERadio()
while not ble.connected:
    for ad in ble.start_scan(ProvideServicesAdvertisement, timeout=1):
        name = ad.complete_name
        if name == board_name:
            ble.connect(ad)
            break
    ble.stop_scan()

# assert ble.connected, f"{board_name} not found"
try:
    uart = ble.connections[0][UARTService]
except Exception as e:
    print(f"UARTService not found in {board_name}")

count = 0
ct = time.time()
encoding = 'utf-8'
while ble.connected:
    ba = uart.read()
    print(ba)
    try:
        data = int(ba.decode(encoding))
        # if data < 0 or data >= 1024:
        #     raise ValueError()
        print(data, (time.time() - ct), count)
        count += 1
    except:
        pass
    time.sleep(delay/1000)

I expect the Arduino to sample at ~4ms per value as it does initially. Here's a table with time (from millis()) on the left and random analog values on the right:
2368, 420
2371, 420
2375, 417
2378, 415
2382, 416
2385, 418
2388, 417
2393, 415
2402, 415
2413, 411
2424, 412
2436, 412

However, when transmitting the data over bluetooth, it exhibits the following pattern:
17435, 331
17438, 327
17442, 314
17530, 300 <-- 100 ms jump
17533, 313
17539, 310
17542, 296
17630, 278 <-- 100 ms jump
17633, 292
17637, 297
17640, 281

Does anyone know how to address this problem, or how to increase the analog pin sampling rate?

Thanks!

This may shed light on the delay:

Arduino Nano 33 IoT BLE pausing the system for 1.6 sec - Using Arduino / Networking, Protocols, and Devices - Arduino Forum

Essentially, BLE.println() takes you off into RF land and BT code where time is not consistent.

1 Like

this problem has nothing to do with analog sampling rate... it is a communication problem.
To prove it, comment the Ble.println(val); line and test the code on local Serial monitor

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.