Hello Im working on a project where the goal is to use one arduino mkr 1010 to send a signal (say a square wave) with a frequency that I write into the Monitor. This signal should then be sent to another arduino mkr 1010 which should simply make a LED blink with that frequency. I wish to connect them with BLE, but so far with no success. From what I can see is the following: the peripheral does work if it is connected with USB (i have tested with the lighBlue app), but if i disconnect the USB and apply a external powersource, then I cant find the peripheral. Im very new to using arduinos so I do more or less copy examples and adjust them. My circuit is very simple, but it would not surprise me if that is the error, when the usb is not connected my battery is connected to Vin and ground, and the LED is connected from pin 2 to ground
peripheral code:
#include <ArduinoBLE.h>
int ledPin = 2;
char* deviceServiceUuid = "19B10010-E8F2-537E-4F6C-D104768A1214";
char* deviceServiceCharacteristicUuid = "19B10011-E8F2-537E-4F6C-D104768A1214";
BLEService ledService(deviceServiceUuid);
// create switch characteristic and allow remote device to read and write
BLEByteCharacteristic ledCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);
void setup() {
pinMode(ledPin, OUTPUT);
BLE.begin();
// set the local name peripheral advertises
BLE.setLocalName("LED");
// set the UUID for the service this peripheral advertises:
BLE.setAdvertisedService(ledService);
// add the characteristics to the service
ledService.addCharacteristic(ledCharacteristic);
// add the service
BLE.addService(ledService);
ledCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
}
void loop() {
BLE.poll();
char ledValue = digitalRead(ledPin);
if (ledCharacteristic.written()) {
// update LED, either central has written to characteristic or button state has changed
if (ledCharacteristic.value()) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
}
The end goal is ofcourse to have the second arduino become the central (this arduino will be connected with USB) so far the code for the central is mainly for testing if i can even get a connection. So the central is only sending a value which the peripheral reads (I have connected a LED on the peripheral which then turns on or off)
#include <WString.h>
#include <ArduinoBLE.h>
const char* deviceServiceUuid= "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";
const int pin = 2;
void setup() {
Serial.begin(9600);
while (!Serial);
if (!BLE.begin()) {
Serial.println("* Starting Bluetooth® Low Energy module failed!");
while (1);
}
Serial.println("Bluetooth® Low Energy Central - LED control");
// start scanning for peripherals
BLE.scanForUuid(deviceServiceUuid);
}
void loop() {
BLEDevice peripheral = BLE.available();
if (peripheral) {
// discovered a peripheral, print out address, local name, and advertised service
Serial.print("Found ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();
if (peripheral.localName() != "LED") {
return;
}
// stop scanning
BLE.stopScan();
controlLed(peripheral);
// peripheral disconnected, start scanning again
BLE.scanForUuid(deviceServiceUuid);
}
}
void controlLed(BLEDevice peripheral){
Serial.println("- Connecting to peripheral device...");
if (peripheral.connect()) {
Serial.println("* Connected to peripheral device!");
Serial.println(" ");
} else {
Serial.println("* Connection to peripheral device failed!");
Serial.println(" ");
return;
}
Serial.println("- Discovering peripheral device attributes...");
if (peripheral.discoverAttributes()) {
Serial.println("* Peripheral device attributes discovered!");
Serial.println(" ");
} else {
Serial.println("* Peripheral device attributes discovery failed!");
Serial.println(" ");
peripheral.disconnect();
return;
}
BLECharacteristic Blink = peripheral.characteristic(deviceServiceCharacteristicUuid);
if (!Blink) {
Serial.println("* Peripheral device does not have BLINK characteristic!");
peripheral.disconnect();
return;
} else if (!Blink.canWrite()) {
Serial.println("* Peripheral does not have a writable blink characteristic!");
peripheral.disconnect();
return;
}
while (peripheral.connected()) {
String str = Serial.readString();
float val = str.toFloat();
Blink.writeValue((byte)val);
Serial.println((byte)val);
Serial.println("* Writing value to Blink characteristic done!");
Serial.println(" ");
}
Serial.println("- Peripheral device disconnected!");
}