Hi, this is Wayne again with a topic “Add Bluetooth to your Arduino Project – Don Coleman”.
My name is don coleman and i’m going to talk to you today about adding bluetooth to your arduino project. Um. All these slides will be online at dawn.github.io. Slides there’ll be a link in the last slide, so you can download them so really. The goal today is we want to take an arduino and we want to hook it to something like a phone or a computer and uh. So i’m using the arduino uno and a couple of phones here and really we want to be sending data back and forth between the two. So there’s a bunch of ways we can do that, but one of the ways is bluetooth, so bluetooth works well. If your computer, your phone, has bluetooth, which most do and you’re going to be in close proximity to the device, because bluetooth is only about uh 30. Feet is kind of one of the advertised distances there. So we’re going to start off with bluetooth classic, which has been around for a long time and bluetooth classic provides something called serial port profile which basically lets us make a serial port uh over bluetooth. It’S as if we had a big long wire connected between the two devices, we could send data back and forth a little bit later in the presentation.
I will talk to you about bluetooth, low energy and how we can use that so, like i said before, we’re going to be using the arduino uno, a lot of these concepts apply to other uh arduino models and even uh some of the stuff you can do With raspberry pi or beaglebone black, so in order to do bluetooth – oh here i am doing my slides uh hold on there. We go got some slides. So in order to do um bluetooth, we need a bluetooth hardware. So there’s two kinds: there’s breakout boards like those red boards there, which are standalone board, which you wire into your um, which you wire into your arduino and then the other side, is a shield.
The shield is just a fancy board that has pin compatibility with the arduino so that one’s the seed studio, bluetooth, uh radio. So i’m going to be using the adafruit bluefruit radio to do the classic stuff here and the way we communicate between the bluetooth, radio and the arduino is we do a serial connection, there’s a transmit and receive pin on each of those, and we connect that to The uh uno now i’m showing arrows there, but we’re actually going to hook wires up and once we hook the wires up, we have uh there’s a library called software serial and that lets us take any two pins in the arduino and make a virtual serial port. So we’re gon na do serial between the arduino and the bluetooth radio and then we’ll do between the bluetooth radio we’ll do bluetooth over to the phone. The nice thing about this setup is that the hardware handles almost all this for us, so we have a very short arduino sketch here, which you can’t read, but i’m going to go through this in detail.
This sketch or arduino program is just going to increment a counter and when it increments the counter it’s going to send the data over bluetooth. So the first thing we do is we import the software serial library that comes with the base arduino, we take two pins. Six and seven we say we’re going to use those for receive and transmit, and we make an instance of software serial called bluetooth. I also have a counter variable which we’re going to hold the current counter: the setup function in an arduino program. It’S called one time, so we need to do two things: we’re setting up a serial connection, which is like our debugger, our serial out and we’re setting up bluetooth once we set up we’re okay, now the loop part of the arduino program gets called over and over Again so we’re going to print the value to the debug console, so we can see what’s going on we’re going to print the value of our bluetooth, we’re going to increment the counter and then we’ll sleep for one second, so we won’t count too fast. So i’m going to open the serial monitor.
It helps if i plug my arduino in so when i connect the serial, monitor it resets the uh program and then the bluetooth counter just starts counting which in itself isn’t too interesting. But now this computer has been paired with this bluetooth radio. So if i use a program like cool term, which is just a terminal program, i can set the connections and say connect to that radio and if all goes well, we connect and then it starts getting that data. So the one in the back, the window in the background, is this: the debug terminal through the arduino ide, the one in the foreground, is actually getting bluetooth, data and printing it out to the console now that in itself isn’t too exciting.
But what it shows is that we can use the bluetooth essentially as a serial port. So if you’re writing pipe programs in python, you can use pi serial. You can use node serial port for node.js and ruby has a serial port gem. So you can really easily write a program that will run on your computer and it’ll connect to your arduino, completely wirelessly um.
I do a lot of work with mobile phones and phonegap’s, a platform that lets me write mobile native mobile apps with html and javascript. So i wrote a plugin for that. This isn’t necessarily a phonegap talk, but my demos use this plugin that lets us do bluetooth serial from arduino. So for that i have some software, which mirrors my phone really tiny.
In this case, i guess and we’re gon na have to disconnect cool term and then we’re going to launch this program, there’s a little bit of a lag between what happens on my screen and what you see there. So what happens is this goes out and it connects and then it’s receiving that data that the computer was receiving before now, it’s receiving it using javascript and displaying it in a native android app here, and so that was really easy from the arduino side. All we need to do is set up that serial connection and say write the data.
Similarly, we can read the data in which we’ll show in another example, but before we do that, i want to get to bluetooth low energy for a minute we’re using bluetooth classic there before which works. Really good problem is when you want to deal with something like an iphone it, you can’t do bluetooth classic. You have to do bluetooth, low energy.
Fortunately, there’s a lot of new hardware coming out that will support bluetooth, low energy and run on the arduino side. One problem is that bluetooth, low energy doesn’t have serial port profile, so there’s no common way to send data back and forth, but fortunately most of the hardware that i have it provides a uart or a serial-like service. So we can mimic that serial connection over bluetooth, low energy.
So, for the second example we want to have, i have this strip of neopixel lights, which are pretty neat and we want to be able to light the neopixel lights up running a program on the ipod touch here. So we’re going to send some data over the connection, we’re going to send the letter c and then the red, green and blue components of the color. So if i want to turn the lights to be blue, i can send 0-0-255.
So, although this is using a red, bear labs, bluetooth, low-energy radio, it still connects the same way, there’s basically two wires for transmit, and so, as far as my arduino program is concerned, it doesn’t know if it’s running bluetooth classic or bluetooth low energy. We do the same thing we set up software serial and then in the loop we do a little bit different code. We basically go through there and we’re reading this. On the arduino side, we scan to find that letter c.
Once we find that letter c, we parse the next three integers, so we can set a color. So now, if the demos cooperate, okay, unfortunately this isn’t mirroring on the phone. So i’ll have to hold the phone up to show you uh what it looks like. So this is another application written in phonegap and bluetooth.
Low energy works a little bit different instead of pairing with the um instead of pairing with it, you actually go out and you discover and then you connect to services. So in most cases you can use what’s called just works and there’s no pairing at all involved. So i have this app here and it gives me a list of the bluetooth radios. I was only expecting one, so hopefully the one i pick will be my correct radio, and this is where the demos aren’t cooperating.
We’Ll give this one more. Try here hold on switch into my backup phone see if the backup phone will actually connect. Oh well, so, unfortunately, this demo is not cooperating, but what happens here is we send that command? On the phonegap side, we have a very similar api where we say bluetooth serial write and we write the data across and uh if it had worked and now my computer’s not working hold on one second, so if it worked, we would be able to light that Strip of lights out um – hopefully i can get this working after the presentation. If you guys want to come over there and see it, i can run that demo, but for right now, i’m out of time – and i need to let the next speaker go.
I might have time for one quick question: if anybody has questions all right, thank you guys very much and uh build some cool stuff. .