Send instructions from andriod app to Nano 33BLE

Hi! My name is Matrucious, and I have recently received my first Nano 33 BLE.

I bought this to start a project where I want to develop an app that connects to the arduino, for sending instructions.
My idea is for example having a solenoid connected to the arduino, and through the app telling it how much to move.
Let's say i have a parameter of 0-60 degrees, I want to send an instruction from the app to tell it to move to 38 degrees.

First of all is this even something the arduino nano 33 ble can do? And second, could anyone please help me get started? I was thinking, for testing purposes in the beginning, just using a breadboard with led outputs for stop, moving, and finished. How do I actually manage to send these types of instructions via an android bluetooth device?

I have tested my arduino by following a tutorial on how to setup, and receving "Hello World!" to the nRF connect app, so i understand that data transfer is possible, however i didn't find anyone sending data to the arduino for it to then do something on a pin-out.

I have never written in the arduino code, so i am a complete beginner, please feel free to directing me to documentation or refrences i should have read.

Thanks for tips and help up front :slight_smile:

Regards
Matrucious

Welcome to the forum.

I can help you with the BLE stuff. However, I think you should also run through a couple of simple examples to learn a few basics. In the IDE go to File → Examples → 01. Basics, 02. Digital, …

As a must do, please check out and make sure you understand the following example.

File → Examples → 02.Digital → BlinkWithoutDelay

The use of delay() is shown in many examples and the source of many issues with more complex sketches and communication stacks.

Regarding BLE, first have a look into the examples that come with the library.

File → Examples → ArduinoBLE

The important things to learn from an application point of view are:

  • there are two sides peripherals/servers (usually a small device) and centrals/clients (often a smartphone or tablet, can be another Arduino)
  • data is stored in characteristics (they can have different sizes and have access rights e.g., read, write, …)
  • characteristics are collected inside services
  • characteristics and services are identified by UUIDs
  • there are 16-bit UUIDs defined by the Bluetooth SiG
  • you can create your own UUIDs, they must be 128-bit random numbers
  • a BLE device sends out advertisements first with some basic information
  • only when connected a client can read and write the characteristics

See if you can identify these concepts in the examples (look at the source code and confirm you see the same thing in the Bluetooth app of your choice) and we take it from there.

Thanks a lot for your help with introduction, and sorry for a late reply. I had some account issues.

I will take a look at the examples, and get back to you when I feel I have a better understanding of what is what.
For experimenting, sending data from my android device, I was thinking of using pfodApp. Just for future reference.

I'll get to learning, and get back to you!

Klaus_K:
Welcome to the forum.

I can help you with the BLE stuff. However, I think you should also run through a couple of simple examples to learn a few basics. In the IDE go to File -> Examples -> 01. Basics, 02. Digital, ...

As a must do, please check out and make sure you understand the following example.

File -> Examples -> 02.Digital -> BlinkWithoutDelay

The use of delay() is shown in many examples and the source of many issues with more complex sketches and communication stacks.

Regarding BLE, first have a look into the examples that come with the library.

File -> Examples -> ArduinoBLE

The important things to learn from an application point of view are:

  • there are two sides peripherals/servers (usually a small device) and centrals/clients (often a smartphone or tablet, can be another Arduino)
  • data is stored in characteristics (they can have different sizes and have access rights e.g., read, write, ...)
  • characteristics are collected inside services
  • characteristics and services are identified by UUIDs
  • there are 16-bit UUIDs defined by the Bluetooth SiG
  • you can create your own UUIDs, they must be 128-bit random numbers
  • a BLE device sends out advertisements first with some basic information
  • only when connected a client can read and write the characteristics

See if you can identify these concepts in the examples (look at the source code and confirm you see the same thing in the Bluetooth app of your choice) and we take it from there.

Klaus_K:

  • there are two sides peripherals/servers (usually a small device) and centrals/clients (often a smartphone or tablet, can be another Arduino)
  • data is stored in characteristics (they can have different sizes and have access rights e.g., read, write, ...)
  • characteristics are collected inside services
  • characteristics and services are identified by UUIDs
  • there are 16-bit UUIDs defined by the Bluetooth SiG
  • you can create your own UUIDs, they must be 128-bit random numbers
  • a BLE device sends out advertisements first with some basic information
  • only when connected a client can read and write the characteristics

See if you can identify these concepts in the examples (look at the source code and confirm you see the same thing in the Bluetooth app of your choice) and we take it from there.

Hi! I posted a reply not too long ago, explaining that I didn't mean for it to take this long to reply, but apparently that reply never went through.

I think I got the basics down of communicating with the arduino over bluetooth.
However I am struggling with things like allowing bluetooth communication to still happen without the USB being plugged in. For example in the BLE -> Peripherals -> BlinkingLED Button example.. It worked brilliantly, and I could connect to the arduino when I had the USB plugged in, however when i then try to run it off of power through the pins it doesn't seem to broadcast the Bluetooth signal.
I narrowed this down to if I removed the While(!Serial) line, it then broadcasts.. however it doesn't allow me to change the led value anymore, with or without the USB port plugged in.

Is there a way to allow the arduino to only run on power through the pins and still take and execute commands through bluetooth "buttons"? I assume yes, but any example of that?

matrucious:
I narrowed this down to if I removed the While(!Serial) line, it then broadcasts..

Good catch. That's what you have to do when you want to run the sketch without a PC. The line ensures you get to see all prints when you debug your sketch.

matrucious:
however it doesn't allow me to change the led value anymore, with or without the USB port plugged in.

I just tested the ButtonLED example with the latest version of the ArduinoBLE library v 1.2.0 and it works as expected when I comment the while (!Serial);.
I use the BLE Scanner app from bluepixel technology on iOS. Everything (read, write, notify) works.

matrucious:
Is there a way to allow the arduino to only run on power through the pins and still take and execute commands through bluetooth "buttons"? I assume yes, but any example of that?

What do you mean with execute commands?
When the Arduino is powered the processor will start running and execute instructions (unless you keep the reset button pushed or push it twice to set it to Bootloader mode).
When you write to the characteristics a client can read them and when the client writes to the characteristics you can change the behavior of your sketch e.g. light a LED. There is not a lot more to it.

Which power option do you mean with power trough the pins?
There are several options and they should be used once you have your application running because the battery option requires you to cut the jumper bridge below PCB. They are described under FAQ in the store.

https://store.arduino.cc/arduino-nano-33-ble

For tether free testing I use a USB power bank. So, no changes are necessary for experimenting.

Klaus_K:
I just tested the ButtonLED example with the latest version of the ArduinoBLE library v 1.2.0 and it works as expected when I comment the while (!Serial);.
I use the BLE Scanner app from bluepixel technology on iOS. Everything (read, write, notify) works.

Huh.. maybe i just encountered a bug when I tested this. I will try again.

Klaus_K:
Which power option do you mean with power trough the pins?

Sorry for not being clear. I intend to use the VIN pin for power in this project. It is going to be hooked up to a car, and start up whenever the car gives the converter power, that is then in turned hooked up to the VIN pin with 5v and to ground as well of course.

Klaus_K:
What do you mean with execute commands?
When the Arduino is powered the processor will start running and execute instructions (unless you keep the reset button pushed or push it twice to set it to Bootloader mode).
When you write to the characteristics a client can read them and when the client writes to the characteristics you can change the behavior of your sketch e.g. light a LED. There is not a lot more to it.

I based my question on the error I encountered with not being able to change the LED value when running power through the VIN pin, to power the Arduino. I will experiment further with this, and see where I might have encountered a bug, and get back to you.

Now for actually doing this, I will try to take the ButtonLED as a sort of blueprint for what i want. I am going to be controlling a relay with the arduino, and intend on writing, and then using a mobile app(Which i have not yet been able to start writing) to send a signal to open/close the relay for such and such time. For this relay i need power, and ground, as well as a signal input. Which i intend on hooking up to a pin on the arduino. So basically the command I want to send is such as "Open relay for 1.2 seconds, then close it" I've been trying to figure out if this should be done with the pulseIn() function, or if i should just set a delay(duration) in between the two commands.

For practicality i am thinking of setting a duration variable in the app and then sending the command to the arduino and it interpreting my duration variable for it to then use it to a "power pin 1 for (duration)".

Thanks again for your help on this.

Here are a few points for thought:

  • make sure the power from the car is filtered properly, car power systems create a lot of noise that can damage electronics that is not protected, Arduinos are not automotive grade so, it is up to you to protect it. I suspect car power outlets already have some protection, but I am not 100% sure.

  • the relay needs to be powered separately; you should not power it through the Arduino

  • relays need flyback diodes to protect the controlling circuit

  • As I wrote in the first reply, do not use delay and use the BlinkWithoutDelay technique to control timing. Delay will stop the execution of code. Especially for communication stacks do not like to be ignored. They need to manage the protocol in the background.

  • do not think about sending commands, characteristics are more like variables, from your application point of view you write or read a variable and the BLE protocol (GATT) takes care of the rest.

  • the timing should be handled automatically by the peripheral to ensure the function works properly even if BLE is disconnected for some reason or you cannot handle the app anymore e.g., you drop the phone, a call arrives, ...

  • you could still have a characteristic for the timing if you must, setting it would just update the setting but writing to another characteristic starts the sequence

  • the timing characteristic can be abstract e.g., 0% to 100% but the timing behind is from 0.5s to 3.5s, this way you can change the timing later in the Arduino without changing the app

I planned on using a CPT 12v to 5v converter, that deliveres 3 amps. I'm thinking this should give stable enough power.

And the relay i'll be using is a KY-019, and from what i've seen people usually plug this straight up to the arduino. However if this is bad practice for actual deployment I will find a solution for this.

Klaus_K:

  • you could still have a characteristic for the timing if you must, setting it would just update the setting but writing to another characteristic starts the sequence

  • the timing characteristic can be abstract e.g., 0% to 100% but the timing behind is from 0.5s to 3.5s, this way you can change the timing later in the Arduino without changing the app

So for example in the app I can have a slider from 0-100 and a "send" button. This then sends the amount of time to open the relay for, based on the percentage you mean?

matrucious:
I planned on using a CPT 12v to 5v converter, that deliveres 3 amps. I'm thinking this should give stable enough power.

I did not mean stability and power. The issue is noise and voltage surges. Have a look into the following Wikipedia page. Especially the last part.

There are users in the forum that destroyed their Arduinos. This might be worth a read too. There are more but I found that one first.

matrucious:
And the relay i'll be using is a KY-019

So, that is a module as far as I can see. It looks like they have the components on the PCB to safely use the relay. I found one that said 5V and might work with 3.3V. The Nano 33 BLE uses 3.3V signals.

Do you have a link to the module you would like to use?

matrucious:
So for example in the app I can have a slider from 0-100 and a "send" button. This then sends the amount of time to open the relay for, based on the percentage you mean?

Yes, when you change the slider a characteristic is set in the Arduino to a value from 0 to 100. It then calculates the time and uses that for control. The calculation is simple because there is a function for that called map().

https://www.arduino.cc/reference/en/language/functions/math/map/

When you touch the button another characteristic is set which starts the sequence. The characteristic could change the value back when done to allow the app to change the color of the button. Or you create a write only characteristic for the button. Lots of choices. :slight_smile:

Do you have a link to the module you would like to use?

Heres a link to the moduels i've opted for. I did order a set of the ky-019, but got an email a month later saying they were unavailable.

Now for the power problem, i think something like this would help a lot to keep the power very stable and not have any surges affect the arduino:

https://au.grandado.com/products/dc-converter-module-12v-to-5v-3a-15w-usb-output-power-adapter?_pos=251&_sid=86ce707dc&_ss=r

I have began looking into creating the app, and I think I will be using flutter to develop and test the app. Haven't gotten far, yet, but I greatly appreciate the help and understanding part of this. It makes planning a lot easier, and allows me to streamline the process.

matrucious:
Now for the power problem, i think something like this would help a lot to keep the power very stable and not have any surges affect the arduino:

Why do you think that? I did not see any datasheet.

Hi!

A bit of an update on this. Sorry for taking so long with this in general. But I sat down and started learning about flutter blue, and that took me a while to get down, on how to build it from scratch… not flutter blue the library specifically but flutter as a whole, to actually build an app, and then be able to send ble instructions via my self programmed app.

I’m fairly certain I got what i want for the Arduino aspect of things.
I just ended up using the Callback LED example, and just developing what i wanted from there. Including a BLEIntCharacteristic, to get the amount of millisecs to use in the variable, and including millisDelay, to set an I/O pin high for that amount of seconds.
Which works just brilliantly!

The struggle I’m having right now is just developing the Flutter side of things, being able to connect, and then communicate with the UUIDs i’ve set, to change what I want to change.

I’m fairly certain I’m close to being done.

Thanks for the concerns, and help with the physical aspect, aswell as pointing me in the right direction with what I should learn, programming wise.

Now, if i were to develop what I’ve done so far into a product… Arduino being open source, is there a way I could basically make the nano 33 ble, just with less I/O pins? I just want the microcontroller, ble unit, and maybe 6 digital I/O pins, and a way to upload the code, of course.
Anyone think this is possible, or even cost effective?

This is all hypothetical, so far. I wouldn’t realistically have the capital to make this into a product.

Thanks!