I have been trying to send analog data (applied pressure on fsr sensor) every 1 s via BLE using Xiao BLE nrf52840 and ArduinoBLE but nothing prints out on serial monitor when I connect a battery.
When trying the LED example the "LED" service is found using nrf-connect mobile and I can turn on and off the builtin led on the mc connecting a battery.
Does anyone have an idea how to send analog data via BLE as can be done with the LED example where some text is printed out on the screen without connecting a usb cable?
You will have to explain more about this.
What "screen"?
Are you referring to the Serial monitor, or a central device like a phone?
If you are talking about the Serial monitor, be certain that the sketch does not contain this line, as it will stop the sketch until there is a serial connection over USB.
while (!Serial);
Can you post the code that works as you want, and the code which does not work correctly without the USB connection.
I want to, when applying pressure with for example a thumb on a fsr sensor that is connected to analog pin A0 on the microcontroller Xiao BLE nrf52840 that these registered analog signals/data should be printed out on serial monitor in Arduino IDE.
I can do this with the code when a usb c cable is connected to the microcontroller and laptop but when I connect a battery as power source nothing show in the serial monitor, I can only find it and connect to it in the app nrf-connect mobile.
// Include all libraries needed
#include <ArduinoBLE.h>
//#include <HardwareBLESerial.h>
// Get HardwareBLE instance.
//HardwareBLESerial &bleSerial = HardwareBLESerial::getInstance();
// Define constants
int fsrPin = A0;
float pressureValue_11;
float pressureValue_12;
float D = 0.02120; // Diameter fsr sensor in meter från rsonline.se
float Pi = 3.14159;
float Area_sensor;
float P_pascal_11;
float Pressure_12;
int fsrReading; // the analog reading from the FSR resistor divider
int fsrVoltage; // the analog reading converted to voltage
unsigned long fsrResistance; // The voltage converted to resistance
unsigned long fsrConductance;
long fsrForce; // The resistance converted to force
long int t1; // variables to register thw time the fsrble function e.g. the sensor readings run.
long int t2;
// -------------------------------------------------------------------------
// Define BLE services, charteristics etc.
BLEService sensorService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
// Define BLE charetiristics
//BLEUnsignedCharCharacteristic sensorValueChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify | BLEWriteWithoutResponse);
BLEUnsignedCharCharacteristic sensorValueChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // | BLEWriteWithoutResponse);
// Setup code, to run once.
void setup() {
//Serial.begin(9600);
//while (!Serial); // make the sketch wait for the Serial Monitor to open. Comment out to send data as soon as device connected
// to BLE on app nrf-connect fo mobile.
// Define sensor as input.
pinMode(fsrPin, INPUT);
// begin BLE communication
if (!BLE.begin()) {
Serial.begin(9600);
//Serial.begin(115200);
while (true){
Serial.println("Failed to initialize BLE");
delay(1000);
}
}
// set advertised local name and service UUID:
BLE.setLocalName("FSR Sensor");
BLE.setAdvertisedService(sensorService);
// add the characteristic to the service
sensorService.addCharacteristic(sensorValueChar);
// add the service to the "BLE bulletin board".
BLE.addService(sensorService);
// Initial characteristic value
sensorValueChar.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE FSR Sensor");
// To register for how many ms fsrble runs, althought not how long the sensor is pressed!!!
t1 = millis();
fsrble();
t2 = millis();
//Serial.print("Time taken by the task: ");
//Serial.print(t2-t1);
//Serial.println(" milliseconds");
}
// Send data from fsr sensor via BLE to computer.
void fsrble() {
// When the Arduino converts this analog voltage (fsrPin) to a digital value (sensorValue), it converts it to a 10-bit number between 0 and 1023.
// read the FSR sensor value.
int sensorValue = analogRead(fsrPin);
// Assign the analogue value read from fsrPin to the BLE charateristic sensorValueChar.
sensorValueChar.writeValue(sensorValue);
Serial.print("FSR Sensor Value, analog reading: ");
Serial.println(sensorValue);
// Analog voltage reading ranges from about 0 to 1023, function map take these values and map them to the voltage range of Xiao board, which is
// 0V to 3.3V (= 3300mV).
fsrVoltage = map(sensorValue, 0, 1023, 0, 3300);
//Serial.print("Voltage reading 1 in mV = ");
//Serial.println(fsrVoltage1);
if (fsrVoltage == 0 ) {
Serial.println("No pressure sensor 1");
Serial.print("FSR resistance in ohms 1 = ");
Serial.println(fsrResistance);
} else {
// The fsr voltage = Vcc * R / (R + FSR) where R = 1K and Vcc = 3.3V.
// so FSR Resistance = ((Vcc - V) * R) / V
fsrResistance = 3300 - fsrVoltage; // fsrVoltage is in millivolts so 3.3V = 3300mV.
fsrResistance *= 1000; // 1 K resistor
fsrResistance /= fsrVoltage; // fsrResistance is measured in Ohms.
Serial.print("FSR resistance in ohms = ");
Serial.println(fsrResistance);
// General function for the force vs resistance curve: 𝑦 = a*(x^−𝑏). REF: https://www.sensitronics.com/pdf/Sensitronics_FSR_101.pdf.
// R = a * (fsrForce1^-b)
// When the force is applied to the fsr sensor the resistance changes with approximately 1/R = C, therefore the conductance is used for
// approximate the applied force.
fsrConductance = 1000000; // Measure conductance in micromhos (uS).
fsrConductance /= fsrResistance;
Serial.print("Conductance in microMhos: ");
Serial.println(fsrConductance);
if (fsrConductance <= 1000) {
fsrForce = fsrConductance / 80;
Serial.print("Force in Newtons: ");
Serial.println(fsrForce);
Serial.print("\n");
// Area of contact surface of sensor.
Area_sensor = Pi * ((D/2) *(D/2));
// Calculate the pressure in Pascal with registered voltage and area of contact surface of sensor.
P_pascal_11 = fsrForce / Area_sensor;
Serial.print("Pressure 11: ");
Serial.print(P_pascal_11);
Serial.println(" Pa");
// Convert the pressure from kPa to mmhg.
Pressure_12 = P_pascal_11 * 0.00750062;
Serial.print("Pressure 12: ");
Serial.print(Pressure_12);
Serial.println(" mmhg");
} else {
fsrForce = fsrConductance - 1000;
fsrForce /= 30;
Serial.print("Force in Newtons: ");
Serial.println(fsrForce);
Serial.print("\n");
// Calculate the pressure in Pascal with registered voltage and area of contact surface of sensor.
P_pascal_11 = fsrForce / Area_sensor;
Serial.print("Pressure 11: ");
Serial.print(P_pascal_11);
Serial.println(" Pa");
// Convert the pressure from kPa to mmhg.
Pressure_12 = P_pascal_11 * 0.00750062;
Serial.print("Pressure 12: ");
Serial.print(Pressure_12);
Serial.println(" mmhg");
}
}
Serial.println("--------------------");
// Data is printed on Serial Monitor every 1000 ms = every 1 s.
delay(1000);
//delay(4000);
}
void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
central.poll();
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
fsrble();
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
// Prints out how many ms fsrble runs, although not how many ms the sensor is pressed!!!!
Serial.print("Time taken by the task: ");
Serial.print(t2-t1);
Serial.println(" milliseconds");
}
}
When connected to the battery, is the usb cable still in place? It definitely needs to be there as it supplies both power and the connection to the monitor.
If you use a simple test sketch like "blink" with some Serial printing added, can you see output in the monitor with the battery connection?