Learn how to use the Rd-03D 24GHz mmWave radar sensor to detect and track humans with a Raspberry Pi. Learn how the sensor works, how to connect it to your Pi, how to code with it's library, and have some fun with radar visualisations and a motion-based air hockey game!

Transcript

In this guide, we'll be looking at how to use this radar sensor in your next Raspberry Pi project to detect and track people. We'll be learning how it works and what you can expect from it, how to use it with your Pi, and as a fun little project, we'll be creating a game of air hockey that you control by moving around the room. Let's get into it.

This is a 24-gigahertz millimeter wave radar sensor, and it's really a testament to where technology is at nowadays, as it's an inexpensive device with a lot of things going on under the hood. Lots of math, lots of filtering, lots of very high-level engineering stuff, but we just get a simple UI out of it containing some really helpful information. This emits radio waves and listens for the reflections that bounce back to gather information about what it's seeing, kind of like an ultrasonic sensor. However, this sensor is way more advanced. Not only does it use the time it takes for those radio waves to bounce back to measure the distance to the target, by the way, those radio waves are moving at the speed of light, which, when you think about it, that's not much time to be measuring. It also listens for any shift in frequencies from the Doppler effect to measure how fast the target is moving. Like how when a car drives by, you hear the frequency change, you can actually use that change in frequency to determine the speed of the object. On top of that, it has two receiver antennas on it that are used to detect the angle of the thing that's reflecting it. If you can imagine the reflection coming back at an angle, it's going to hit one of these before it hits the other side. And very importantly, it only detects people. When it sends out those radio waves, you get a lot of things reflecting it back, your chair, the walls, your fridge, your phone, but with some very advanced signal processing, it can figure out what is actually a human by the patterns of it reflecting back. Long story short, your breathing, the subtle movements in your arm, all the tiny little movements in your body creates tiny micro Doppler shifts. And these slight shifts, it creates like a pattern that's very distinctive. And this radar is basically trained to pick that up and go, that's a human. So with this sensor, you'll be able to use 24 gigahertz radio waves to detect a person, how far they are away from a sensor up to eight meters, which is the maximum range, what angle they are at from the sensor is 120 degrees of range on that, and how fast the target is moving. Now that speed is only going to be measuring how fast towards or away the sensor you're moving. It can't measure side to side, only forwards and backwards. Also, one more thing, they don't like to move around. We're not entirely sure why, but we think that when you set it up, it does sort of a room calibration. And if you move it, it throws that calibration out of whack and you get some dodgy readings. Just give it a go and see what's up. It's a bit of fun. And now you know how it works and what it does. Let's go ahead and use it.

To follow along, you're gonna need one of these RD-03D radar sensors and a way to connect it to your Pi. We're gonna be using one of these generic cables, which just plugs into the onboard connector. You're also gonna need a Raspberry Pi single-board computer. We're using the Pi 5 here for the extra processing power, but we also tested this on a Pi 4 without any issues after a slight tweak to the library. More on that later. And of course, for the Pi, you're gonna need a micro SD card to boot off and a way to plug that into a computer so that you can flash it. And you'll also need a keyboard, mouse, monitor, and everything to actually use the Pi as well. And you can find links to everything you need in the written guide, which is linked in the description below. Now, before we begin, the connector cable we're using for this board is generic. So you need to watch out because the colors may not match the actual pins of the board. For example, the red wire isn't power, it's actually the RX pin, and the black wire isn't ground, it's actually the TX pin. Green and yellow are actually the five-volt and ground pins on the cable that I'm using. Just double-check which color corresponds to what pin on the board. Don't trust the coloring convention of the cable. Starting off, let's power the radar by connecting the five-volt pin of the Pi to the five-volt pin of the radar. And we'll also connect a ground pin to the ground pin of the radar. Now our radar is gonna be outputting its data over UART, so connect the TX pin of the radar to pin 15 of the Pi, which is an RX pin. Then connect the RX pin of the radar to pin 14 of the Pi, which is a TX pin. Remember, you connect RX to TX and TX to RX with UART.

With everything plugged in now, go ahead and use another computer to flash Pi OS onto your micro SD card. Ensure you select the correct board, and it's probably a smart idea to use a 64-bit version of Pi OS. As always, the installation process will wipe all data on your micro SD card. Once it's flashed, slot it into your Pi and boot it up. Run through the first-time setup however you want. There really aren't any important settings here. When you get to your desktop, head to your menu and then preferences, and go on over to Raspberry Pi configuration, interfaces, and turn on that serial port. Very important step, which is telling your Pi to enable UART communication on the GPIO pins, and we'll want to reboot after doing so. Once rebooted, head on over to the written guide link below to download the zip folder containing our library for the radar, as well as all the other demo codes that we'll be looking at. I'm just gonna go ahead and extract that to here, and then I'm just gonna drag it to my desktop so we can find it easily. Beautiful. Now, something very important before we get into to everything. This library is designed to work with the Pi 5, which has a different way of handling UART than previous Pi boards. If you are using a Pi 4 or something else, you're going to need to open up rd03d in Thonny or whatever you want to use. And at the top here, where we set up the UART, instead of ttyam0, it's going to be ttys0. Save it, close it. That's the only thing you need to do. Again, only do this if you are not using a Pi 5. If you've got a Pi 5, leave it as it originally comes. Let's start by opening up the demo usage code here. I'm just using Thonny because it's my preferred IDE of choice.

 I'm just going to go through and pick apart this script because it kind of shows you how to use the library and the sensor. So starting off, we import that library that you might have just modified there. And it's very important that that Python file is in the same directory or file location as any of the scripts that you're going to be running. And then we go ahead and create an instance of our radar. Now, what that's going to do is the library goes out and it sets up all the UART communications and starts working with the radar. It just kind of handles everything behind the scenes. The radar can actually detect up to three people at the same time. And by turning on the set multi-mode to true, you're enabling it to do so. If you set it to false, it's only going to return data for one target. Now just a heads up, while it's in single target mode, it is pretty darn accurate and it is on the ball most of the time. But turning on multi-target mode can start to introduce some issues. For example, if you have a large flat surface like the wall and the TV that we have behind us, it might actually reflect off that, bounce onto you, bounce back, and then bounce onto the radar. And it kind of thinks that there's an extra person in the room, even though there might be one. It also struggles a bit if two people are really close together; it might pick them up as one target. Look, we're leaving it in there because, depending on your environment, it might work really well. Depending on your environment, it might work really bad. We're going to go ahead and set it to false because we only want to track one target. By the way, if it's in single target mode and there's two people, it's only going to detect the person that is giving the strongest return, which is usually the closest person. All right, now we get to the meat of it. First, let's call radar.update. This is going to tell the library to go ahead and grab the latest information coming out of our radar sensor. And if it is valid, it's going to return true. And if there's an error, it's going to return false. And by putting it with this if statement here, not only are we telling it to grab the latest information, but we're also checking, did it get a valid reading? And if there is, we're going to do something with it inside of it. If else, we're going to say there was something wrong with it. It's just a bit of nice condensed error handling. So after we call .update, that library is going to hold that information and it's going to hold it until we call update again, which is then going to grab the latest information. To actually get that information into our script, we're going to call .getTarget. And because we can track up to three targets at once, we can get the information of up to three targets. We've currently got it up in single target mode, or if it's not detecting two and three targets, it's just going to return zeros. So now all of that information is stored in this target one or two and three variables. To actually get it out, we're going to have to just call target1.distance, which gets the distance out. If we use target1.angle, we'll get the angle out and we can do the same thing for the speed, the X and the Y. And that's just, you know, how do we actually get that specific information from all that data stored under the target one variable. And in this case, we're obviously just printing it out.

Alrighty, I'm just going to go ahead and mount that sensor facing towards me with some BluTack. We're going to go ahead and run that code. As you can see, we've got data printing out. Let's just quickly go through it. Distance, really straightforward. It's the distance between you and the sensor. Angle is the angle between you and the sensor, but it's measured from down the middle line of the sensor. So right now I'm kind of in front of it directly. If I move this way, it should go into the negative degrees. And if I move this way, it should go into the positive degrees. And it can measure up to negative 60 degrees that way and positive 60 degrees that way. We've also got the speed, which is in centimeters a second. That's just what the sensor default outputs at. I move towards the sensor, it's going to be a negative. If I move away from the sensor, it's going to be a positive. And remember, this is only away or towards the sensor. If I move side to side like so, you probably shouldn't be seeing anything. Forwards and backwards, we're going to be getting it reading like so. And then we've got the X and the Y, which is a little bit more tricky. Imagine like a straight line coming out of the sensor, the same line that we're using to measure positive and negative angles. The X is simply a measurement of how far away you are from this line. So if I move this way, we should get a negative X reading because I'm some distance away from the line in this direction. And if I move this way, we should get a positive reading because I'm this amount away from the middle line. It's essentially how far away I am from the middle line of the sensor. And the Y is a very similar concept. Imagine a wall that the sensor is placed on. It's just a measurement of how far away you are from the wall. So let's say I was one meter away from the wall, standing right in front of the sensor. The distance is going to read one meter and the Y is also going to read one meter. But let's say I took a few steps to the Y is still going to read one meter because I'm one meter away from the wall, but the distance is probably going to increase. So there is a slight difference there. Y is the distance from you to the wall, no matter where along the wall you are, and distance is the distance from you to the sensor.

Sweet, we've got that data. Let's go ahead and build something cool with it. We're going to go ahead and open up radar visualization. And this code is going to take that data coming from the demo usage code and use Pygame to draw a neat little radar kind of visualization. Go ahead and run that. And we get this nice little visual radar display. We've got all of our information there. That's just what we were printing to our shell. And we're just displaying it here, which is a bit easier for us humans to visualize rather than, you know, raw numbers having something to see. And I can probably get up here and move around. As you can see, that ring there is about two meters and each of those rings is a two meter mark. And I can probably move back and move to the side. I move around to the other side and move all the way around. And that little area you're seeing is the speed measurement. Obviously, the bigger it is, the faster you're moving. This is not only just a little bit of fun, but it's a great tool if you're planning on using these sensors, because just having that visualization really helps with any sort of debugging.

Just a tangent while we're here, some really quick tips on actually using this thing. First of all, if you are mounting it to detect people, anywhere between chest height to probably just a little bit above head height is the right place to put it. It's kind of got the best results for detecting people. Like we mentioned before, you should also be aware that the radio waves can bounce off walls and other flat surfaces. Like I said, I was behind the sensor and it was picking me up while bouncing off the walls. So if you are getting readings when no one is in the room, it's not a ghost. Trust me, it might be the reflection of a wall or something. If no one is around or near the sensor, it will very reliably not pick anyone up. And also, if you are using multiple of these, be aware that they will interfere with each other. So ensure that they're far enough away from each other if you are using them in multiple rooms.

 All right, let's kick it up a notch and do something fun with it. Go ahead and open up the air hockey script in Thonny and go ahead and run that. And let's take a look at what we've got. This is similar to the last one where we used Pygame to visualize where we are, but it's also going to be running a slightly jank game of air hockey against an AI opponent, which half of the time is comically bad and the other half of the time cheats like some god, I don't really know. It's a bit ridiculous sometimes. Do something, please. And of course, you move around your area to control your piece. It's a great bit of fun and it's a really cool example of what can be achieved with such a simple and inexpensive sensor. Also, there are a whole heap of settings to play around with at the top of the code. And the important ones are these two, which will set the play area your sensor will track you in to control your piece.Now, we aren't going to go over the 600 lines of code because this isn't a guide on how to use Pygame. I'm also not an expert on Pygame either, so I got a large language model like ChatGPT or Claude actually both to help me write this code. If you want to make something like this, LLMs are probably a good way to get some help in doing so.

Well, that about wraps us up here. You now know how to add a radar sensor to your Pi and get the location and speed of people in a room, which is pretty impressive for such an inexpensive piece of hardware. If you do use this to make something cool or you need a hand with anything we covered in this video, feel free to head on over to our community forums and post about it. We're all makers over there and we're happy to help. Till next time though, happy making.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.