I am newbie in the electronics world and I tried best to build a device on my own using Arduino Nano IoT 33. The device works perfectly fine, when connected to USB and my bluetooth starts fine and can pair with the mobile app I have built.
When I use a Lipo battery (7.4 v 300 mah 30C), BLE won't start.
My code for the setup() is as below
BLEService xxx("<uuid_1>");
BLEFloatCharacteristic xxx_characteristic("uuid_2", BLERead | BLENotify);
void setup() {
Serial.begin(9600);
Serial.println("BLE Starting");
while (!Serial);
// begin BLE initialization
if (!BLE.begin()) {
while (1);
}
BLE.setLocalName("xxx");
BLE.setAdvertisedService(xxx);
repZZ.addCharacteristic(xxx_characteristic);
BLE.addService(xxx);
// start advertising
BLE.advertise();
Serial.println("BLE Started... Waiting to connect");
}
Am I choosing the right battery? I connected battery to VIN and GND pins on the Nano IoT 33.
This line will make your program stay in an infinite loop until the serial port is opened in Serial Monitor. The reason that is useful is because on native USB boards like yours, the board is not reset when you open Serial Monitor. That means any serial output printed between the time the program starts running and when you get Serial Monitor open will be lost.
In applications where missing that serial output would be a problem, it's useful to add this code to the sketch in order to make the program wait for the Serial Monitor before running.
However, if you are wanting to run your program when the board is not connected to Serial Monitor, you must remove that while loop to allow the rest of the sketch to run.
If you want to remove all the serial code from your sketch, that is fine. But I wouldn't recommend removing that line alone because the behavior of calls to functions like Serial.println is uncertain if you haven't done the expected initialization.
We could look into the source code of the "Arduino SAMD Boards (32-bits ARM Cortex-M0+)" core used by your Nano 33 Iot board, and it is likely we would see that removing the Serial.begin call is harmless, but there is no assurance that code wouldn't be changed in a later release of the platform.
That makes sense. Is there a way to verify based on which power source my Arduino is turned on and I can have a conditional use of while(!Serial); This way I can use same code to test and use.