Raspberry Pi Pico 2 W Tutorial: Bluetooth Proximity Sensing

Raspberry Pi Pico 2 W Tutorial: Bluetooth Proximity Sensing

Show Video

hey everyone Ralpour here and today we're going to show you how to get started with the Raspberry Pi Pico2W device This is the next generation of the Raspberry Pico If you're familiar with that the Raspberry Pi Pico device was a kind of like a response to the Arduino done by the Raspberry Pi Foundation with multiple cores running micro Python Packs a lot of power in a really tiny chip Uh we did some tutorials on that in the past This is the next generation and we're also looking at the wireless version with Bluetooth B 5.2 So grab your Raspberry Pi Pico 2 device because we're about to show you an example of how to use proximity sensing with Bluetooth We're also going to get you started up and running So grab your device join me Let's get started We're going to first start with showing you how to load MicroPython onto your Raspberry Pi Pico 2 And you know you could use uh C++ for your device We're going to be using MicroPython Uh it's really easy to get started especially a lot of people who are familiar with Python So I felt like this was kind of like an easier entry point into playing with these embedded devices So first of all uh you got to get your device Of course the Pi Pico2 this is the W version So the wireless version and that comes with its own image over here the Pico2W as you can see micropython.org/d download/pipco2_w So this is the W version That's really really important distinction Don't make that mistake You'll download the UF2 file What we're going to do is we turn on so we unplug our device We hold on this little boot select button right here And as we're pressing it down we plug in our device And what will happen is it will open up as an empty folder over here So when you do that you load that UF2 file directly onto here and you just literally drag and drop it and then it will restart as a MicroPython device So I'm going to download this You can see right here Go ahead and download that It takes you know a little bit of time It's it's not too big Um and then you take that load it on the put it in that folder So going to do that And then my device is going to restart Once it copies that over it's going to restart the actual PI Pico It's no longer going to show up as a um as like almost like a flash drive It's going to it's now going to turn on as a comm port So if I go to terrierm and I set up as serial and then this is the device You can do this by looking at the vendor ID and product ID the vid and pid But in this case I know that I know these other two uh com devices are my Bluetooth whatever and this is the last com device that exists So I know that this is my device And so now as you can see over here there is a shell interpreter So I can just say help And as you see over here do that again help And welcome to micro python It tells me you know what's what's the um uh I can say machine whatever I want and start doing things like uh here print uh hello There you go So this is called the ripple This is like the shell that we can run directly This is over serial from uh USB as a USB serial communications port a comp port So I'm in Windows I'm using a comp port if this if you'd be in Linux or Mac it would be like a dev uh TTY USB or something like that Okay so now we have our device up and running and we're able to now start with some examples So we're ready to dive into some examples I already have some in my public repository which I will pull up here But one thing that I do want to say is that in previous examples what I've done is I have put together some demonstrations that use the IDE that is recommended with uh the Raspberry Pi Pico which is the Thonnie IDE I actually started moving away from that um because I a prefer to use VS Code Uh as you can see I'm already I'm using Copilot things like that I use AI for pretty much everything that I do but also most importantly is I um I I like the feel of the environment and this is what I'm comfortable with And so I don't want to be locked into some IDE that's meant directly for that chip or for that environ or for that uh you know development kit I want to use the ID that I'm comfortable with VS Code is pretty um universal in that they have a lot of extensions that support a lot of these embedded devices like Arduinos and things like that So with that said there is actually an extension called the Raspberry Pi Pico extension This is relatively new It's really nice It was not around when I first did the tutorial with the original Raspberry Pi Pico So I actually really like it It's really nice So if you go here to extensions you type in Raspberry Pi Okay Raspberry Pi Pico the official VS Code extension from the Raspberry Pi Foundation So this is great Um what happens is when you get this all set up you install that extension and then you can do a new C++ project or a new MicroPython project So I selected a new Micro Python project It opened up a new window and then gave me even gave me a blink project which is really nice I've modified it slightly and I also have pushed my code to my public gitlab [Music] gitlab.com/mbbedded-designs/micropython-blut-example Uh you can also just go to gitlab.com/amopore and look at some of

my activity Um you'll see it's over here I've been pushing to that So in this example I have uh the Blinky LED which came kind of stock uh with their project and then the Bluetooth scanner which we're going to get to in a little bit But first let's just prove that we can do uh LED blinking So I have my device uh connected So what you can do is once you open up VS Code you can plug it in it'll automatically detect it But if it doesn't you can go right down here as you saw and it'll say device uh disconnected or device connected There's a run command as you see down here as well This is going to run the code that you have open So in this case I have a blink.py open This is using micro python So if you see from machine import pin this will not run on your device This will run on on your uh Windows machine or Linux machine This will only run on target on your device So let's get it to blink an LED So we use most importantly the library is machine So machine.pin is what sets the pins just like you do with Arduinos or with the Raspberry Pi Uh we need to use that library that controls the DIOS the pins the GPIOs uh the pins on this device So in this case this is attached to an onboard LED that you will see in a second flash So we're going to run this example So let's clear this And I'm going to run this example So LED starts flashing So if you can see right here in the corner it's starting to flash It's a little fuzzy but you can see that flashing here Let's see if we can get a little closer for you So that LED is flashing right there Okay So we've demonstrated that we can flash it We've demonstrated that MicroPython is running on the device and that uh we can now really start developing So again I'm going to hit the stop button right over here And now it stopped So again we have our environment set up We're using VS Code We're using our AI assistant on the side We are writing code We're running it directly on target No need for any external ways to send files back and forth No need to download any extra idees or interpreters All we did was we just installed the extension We ran the example over here We uh import we did a new microython project and uh we just ran it all the way through and ran the uh simple LED toggle project So next we're going to get into something a little bit more complicated This is a Bluetooth scanning example So we're now at the Bluetooth scanning example We're going to do an overhead projection of this uh in real time a real live demo with uh with devices So you're going to see that next but I just want to go through the code really quickly I also have a readme that's in the repository as you saw before In my repository on GitLab uh as you can see I go through quite in detail um how it all works and and what's going on I also have the written article if you want to follow that as well with all the links and more details in the tutorial This is just kind of giving you a quick overview and showing you how to get up and fast and and running really quickly So I've got my Raspberry Pi Pico uh plugged in but we're going to demonstrate that in a little bit Let me just go through the code first and then we're going to demonstrate how to actually run this So first of all few different things There's a little bit more involved here than just blinking an LED We need to be able to access the Bluetooth library The purpose of this example is what we're going to be doing is we're taking this little Raspberry Pi Pico device and it is going to perform proximity sensing against a designated Bluetooth device So first we're going to discover anything that's nearby and then we're going to be able to kind of track when something gets close So what does this mean So for example let's say I have my phone and we're going to show a phone actually as the example but let's say I have my phone and I walk into my house and I want my house my smart home or my smart whatever it is these days uh I want my smart insert place here to detect that I've now come home right or or that I've entered my car or that I've climbed into my treehouse whatever it is So this little guy right over here can do that for us So what it is is it it's almost like sniffing or scanning for devices that are close by And it can see that I am I am close by because it can see my phone being detected So it sees oh there's this specific Bluetooth MAC address that just showed up within the proximity of let's say 5T 2T 1 ft whatever it is That I specify with an RSSI That's a done in DB units And that is basically a proximity sensing that tells me oh this phone is close by This phone is probably sitting in AR's pocket Therefore AR is close by So how does this work We are going to um we're going to basically take the proximity sensing and then apply some some logic So first of all we set a constant value of a very close RSSI I don't want it to go haywire because it can detect things from 90 ft away I I don't want to detect stuff from that far I just want to detect things that are very very close So I chose a value of -50 dB which is really really close I mean just like maybe a foot And so what we do here is we first set up the Bluetooth device the Bluetooth BLE and then uh we set up a an event handler So we say that we want to do scanning So tell my Bluetooth device to start scanning and when it finds something in this event that IQ scan result which I've set as a constant value So when a device is found I want you to do this action So basically what we're doing think of it as like interrupts uh that you have in uh in very low-level embedded devices This is kind of the same thing We're just saying go scan and then while you're scanning if you find if you find a device then start applying this logic So in this logic we say oh okay if you find a device and that device is within this range then I want you to do the following So we have two modes we have discovery and we have track So when you set discovery mode what that does is that basically will advertise on the print line which we'll see shortly in the demo That'll print out in the terminal and say hey I've discovered this device with this MAC address it is this far away in DB Uh and and it's just going to constantly print that out Then uh I have a track mode which I change So you say if you find this device so if the address that we're looking for matches uh the address that we've we're basically we found So if we found this address and it matches the specified target MAC address that we're looking for meaning my phone then I want you to say the target device has been found nearby Uh and it prints out what the RSSI the the DB the value is terms of proximity and then it will light up an LED So then so and and that happens later on So again in my run mode I activate my Bluetooth I set up that event as almost like an interrupt right And then I run in a loop So in my loop I basically scan uh with a 1 second duration and scanning for every 30 milliseconds So we we don't want to scan indefinitely because then I can't do other things which you'll see in a second So I scan and then I kind of check things and then I sleep for 100 milliseconds and then I scan again So this is what the loop is doing So scan right that with that event handler that I talked about and then if we find that the uh LED needs to be updated or not updated meaning like the device has moved further away then we'll get this kind of uh we'll get this last scene uh last scene boolean which is uh this this uh last scene indicator to basically say like the device has been gone for a certain amount of of time therefore turn off the LED meaning I move my phone close to the device it turns on the LED because it sees that that device is there I move my phone away and within a short period of time that I've specified I say "Oh you know what Uh within uh 500 milliseconds if that phone is gone within like longer than 500 milliseconds turn off the LED." I do this because I don't want the I won't want the LED turning on turning off turning on turning off Gets a little confusing So I get a little delay So you can kind of see that in the demo That's where the extra logic is So then in my main function I instantiate my object either in discovery or in scanning mode So discovery mode right now the default is basically going to do a printout statement when I put devices close by Then when I put my phone close by in a quiet area meaning there's nothing else that's printing in a quiet area I get that the target MAC address and then I paste it in here and then instantiate this one instead of this one So hopefully that makes sense You're going to see in the demo in a live demo that will will explain a lot of things Again you can read through the readme for a little bit more details So let's go through the demo of running the actual code And what we're going to do is we're going to first run in discovery mode as discussed and then grab the MAC address then run in tracking mode So let's go ahead and run So if you see down here we click on the run button And now I'm going to move my phone As you can see here in the corner I have the Raspberry Pi Pico device plugged in I'm going to move my phone close by And you see it's starting to print If you see that we have the MAC address right here So if I pull it away it'll stop printing Okay So now what I'm going to do is grab this And again I'm grabbing the MAC address versus looking on the settings on my phone because the iPhones they scramble the MAC address Apple scrambles the MAC address in order to create more kind of like security So I am taking the scrambled MAC address There's obviously easier ways to do this but I'm going to do it a little differently So I'm going to take that scrambled value So this is just like a very basic idea on how to test this So now let's go ahead and just kind of kill that and start again So I'm going to comment this guy So now I'm going to track against this scrambled um MAC address Okay So now going to run the script again So track we're scanning for devices in track mode So now watch you're going to see the LED turn on Oh there you go And then if I move it away see if you just saw right there the LED just turned off So let's do it again I'm going to move my phone closer LED just went on If you see over there I'm going to move my phone away and the LED should turn off See And again LED and then LED should go off So as you can see here we demonstrated the ability to detect devices close proximity uh to our Raspberry Pi Eco So then you can create an action like turn on your lights or say hello to you or turn on a motion sensor you know turn on some sort of camera do a whole bunch of things when it detects certain devices close by That's the uh demo just to demonstrate some of the B and some really cool capabilities in a very little bit of code uh for using MicroPython and the Raspberry Pi Pico 2W So today we looked at the Raspberry Pi Pico2W device This device was capable of showing us how to do proximity sensing with Bluetooth to be able to detect when we would get close to the device For example when you're entering your home and you've got your smartphone in your pocket and you want to be able to tell it when you're coming home to do something maybe turn on a light or turn on a speaker create your own smart device You can do that with this little guy right here We also showed you how to get started from Fresh a completely brand new device how to blink the LED And that was pretty much a wrap If you enjoyed this video please check out the rest of the videos in this channel Give this video a like a thumbs up and subscribe to this channel so you can see more of the videos that we work on for these embedded devices Thank you so much for watching and we'll see you soon [Music] [Music]

2025-03-22 12:44

Show Video

Other news

Future-Ready Tech: AI and accessibility 2025-04-07 18:15
How to Prepare for a Python Interview: A Complete Guide 2025-04-05 23:34
I Tested Every "Unbreakable" Product 2025-04-05 17:20