 This is a heart rate and a pulse oximeter sensor. It allows us to track our heart rate over time and how much oxygen is in our blood, which can help us detect things like heart attacks, heart failure, lung cancer, asthma, and more. So in this video, I'm going to show you how these sensors work and how to control them with code. If you enjoyed this video and content like this, please hit the like button, the subscribe button, and tell me in the comments what you'd like to see in a future video. This right here is our pulse oximeter sensor called the Max 30010. There's a bunch of different versions, but they all generally have the same purpose. I also have this one from m5stack.com. This one is just packaged in this little cool plastic thing that can connect to Legos, or you can bolt onto stuff. But the sensor inside of it is the same that we just saw. The sensor itself isn't too complicated. In the middle here, we have that little black piece, and we have three electronics inside of it. We've got a red light, an IR light, and in the middle there, we have a light sensor or a photo detector. The red light is just a normal red light. The infrared light, as we know and use in a bunch of things like our phones, TV remotes, et cetera, is a light that our eyes can't see, but our computers and cameras can. For example, my eyes cannot see this. When I open my phone, my eyes cannot see those flashing lights. Those are IR lights. And then the photo detector is to sense those lights when they're reflected off of our fingers. Now let's talk about that. The way these sensors work is by reflecting these lights onto our skin. So either the wrist if you're wearing a watch, or one of your fingers like you would have at the doctor's office. And then whatever light returns into the sensor, well, we can use that number to calculate how much oxygen is in our blood. I have my watch right here, and I'm just going to go to the heart rate section. And you should see. And then there's a light sensors right there. So when I put it on my wrist, if I mount it like that or on the top, it should be able to measure my heart rate over time. Now we know how to capture our oxygen levels, but we need to capture also heart rate. And how do we do that when we only capture something for a second? Well, what we're going to do is capture a bunch of seconds over time. And then using those numbers, we can get our heart rate. The ratio between the, how much light is being reflected back and how much light is being absorbed in the sensor actually pulses in waves over time. And those waves can be detected and turned into a heart rate. One thing I forgot to mention, and I'll be putting it in the bio, is an article on how to modify these boards if it doesn't work in your situation. Some of these are not meant to be used with arduinos, and you will have to make a little slice, I don't know if I could focus it on camera. You have to make a little slice and then solder up a line here. It's really easy and it took me about 30 seconds to do, but it is something you might have to do if you're using an Arduino board. I'll be sure to put all the instructions on how to do it properly in the bio of this video, but it's definitely something that you should do, or else this might not work. Okay, so we know how the sensor works and we know that if we want to use it with an Arduino board or one of these types of board, we're going to have to make a little modification on it, and I have that link for you in the description. But now we can go and start wiring it up. We're going to need five different cables and your board. The pins we're going to have to use are VIN SCL SDA Int and Ground. So I'm going to start off with the most important ones. I've got a cable here for VIN, which is power. I'm going to be using 5 volts, and then I've got another cable here for ground, which is just the ground on my board. Then I've got on my Arduino, I have SCL and SDA as a port. Depending on your board, just Google where the SDA and SCL ports are. I know on the Arduino UNOS, they're also, I think it's A5 and A4, but in my case, I have dedicated ones. So I'm just going to plug into SCL and SDA. SCL will go into SCL, SDA will go into SCA, and then Int will go into 2 on the Arduino board. That one should be universal. Now that we have it plugged in, I'm going to plug in my Arduino into the computer, and we can get started on writing this code. So open up a new Arduino window, Arduino IDE, and there's one thing we're going to have to do before we start. I'm going to have another link in the description where you need to go into your sketch here, include library, and add a zip library. You're just going to download what I have in the link. This one right here is Winomax 30100. Overwrite that or install it if you don't have it already. It should take a minute, and then you're ready to go. All right, so at the start here, we're just going to have to include two different libraries. One is wire.h, which you should have by default, and the other is the one we just downloaded. I'm going to copy-paste this because it's really long, but that's what it's called right there. Max 30100 and then PulseOximeter.h. After that, we're going to define our reporting period MS, which is just the update time for the heart rate. We'll be using that later, but we're just going to update it here and make it equal to 1,000 milliseconds, which is one second. After that, let's add in a pulse oximeter called pox. So here, we're creating a pulse oximeter object called pox. Then we're going to make a 32-bit integer. We're going to call that TSLastReport. This is going to be the last time we reported a beat. So we're going to store a time in here, but we're going to store it as a 32-bit integer. We need to make a void function called onBeatDetected, which will run when a beat's detected. In here, we're just going to go serial.printLine and we're just going to print the word beat with a exclamation point. All right, then we go to our setup. What I'm going to do is type it out and then I'll explain to you each part. So first thing we're going to do in this setup is we're going to start our serial line so we can print things out and this is going to be our border rate. Then we're going to print initializing the pulse oximeter and we're going to run this little thing here which is going to initialize the sensor and it's going to tell us if it failed or if it succeeded. So before I had that issue with the little resistor inside that I told you guys you need to fix if you want to use it with an Arduino, before I fixed it, it would constantly go to fail to just stay there. Once I cut it up and then soldered that little line, it totally fixed the problem and then it would actually succeed and go through. And if it doesn't succeed, the rest of the code won't go through and you won't be able to use your board. Then you just need to set your current for your IR light. This is the one that has been used online and I'm not sure if any of the other ones are better or not so I just used this one. I don't think there's too much thought to be put behind here if you're just trying to use the sensor for basic stuff. And then we register a callback routine when it detects a beat which is our on-beat detected which means to print the word beat. So the cool thing about this library we're using is it'll do some of the work for us and actually tell us if there's a heartbeat or not. We don't have to sit there and monitor the waveform and be like, oh that's a pulse, that's not a pulse, that is, we don't have to do any of that stuff, it'll do it for us. Then we go to our loop. Now inside the loop we have a couple big things to do. First off, we're going to call our object for our pulse luxe symbol that we made at the start and we're going to call the update method. This is going to read from the sensor. So I'll actually, I'll just put a note for us there because update doesn't totally match with the word reset or read. Now under here is where we're going to do a little bit of printing magic. I'm just going to grab this function right here. So the first thing we're going to do is write an if statement and inside the if statement we're going to subtract milliseconds with the last time we saw a heart rate and our period. Then if that, so then we're going to add this if statement right here. The first thing we're going to do is based off of time and the last time we saw a heart rate for the last time we reported a heart rate we're going to check a couple different things. First we're going to print out the heart rate which we've been able to calculate because of our library and this function right here pox.getHeartRates. So remember I told you they'll do some of the work for you that's what that code is right there. Then we're going to go and print out our SBO2 which is also our oxygen levels our blood oxygen levels and we're going to do that by using again their code which is pox.getSBO2. So it's going to take the reflecting light and it's going to figure out based off of math how much oxygen is in our blood. The more red the more IR or depending on how much of each if they're both equal it's going to give a number out and it's going to do all the work for us we just need to print it out. Then we're going to reset our last reported beats which we did at the top and we're going to loop it all over again. One thing is you don't want to add a delay inside of here because that will mess with your heart rate. So if you're going to add a delay you should do it somewhere else or you should make it that it only checks the heart rate for a certain amount of time and then stops later on. So now that we have our board plugged in and everything wired up the first thing you have to do as always you have to find your board and select it. So we've got Arduino Uno and it's in COM3 I guess I have to do it like this. Okay. Now that we're connected to our board I'm going to do two things I'm first going to open up the serial monitor and then I'm going to upload the code to it. So it says we have a problem with a lowercase serial here. Okay. The code's been uploaded. Let's go to our serial monitor and look at that it's starting to report some stuff. I want it to auto scroll. Okay. So what I'm going to do is I'm going to reset the Arduino clear the chat. So initialized success. Now it's reading levels it's reading zero obviously because nothing's reflecting off the sensor. Now I'm going to take my finger and since this is an exposed board we want to be careful not to touch any of the electronics and short them through our finger because then that will mess with your numbers. So what I'm going to do is just refresh it one more time. I'm going to put my finger on the plastic parts and then put it down on the sensor. Oh it failed. Something must have happened. Let's restart it one more time. Oh it failed again. Something must have happened. Check all your connections. Restart it once more. I can see I have a loose wire because every time I move the board that red light turns off. Just not sure which one. Try again. There we go. Now the light's on. My finger on there. Oh look at that. It says my heart rate is 29 which is not accurate. We saw earlier it was 75. Now it's saying it's 30 but it should fix itself over time. It's going up now 50, 58, 81, 79, 39 and it really has to sit there for a bit and it'll calculate over time and get better. Now it's just stuck at 39. It says my SP02 is 98% which is pretty accurate to what it actually is. That'd be a healthy blood oxygen. It's freaking out again so I'm going to try and get it in a better position. You can imagine if you 3D printed a little case and had it sit tight. See now we're at 69 beats per minute and now I'm going to grab my watch here and I'm going to put it back into heart rate mode. 70. So we measured the last time here. We measured about 69 before I took my finger off. Put my finger back on. We just measured 70 on there. Let's restart the sensor again. Put my finger on the board. So about 70 beats per minute is accurate. So now it's saying zero. Did I disconnect it again? So it takes a minute, takes a minute. 17 beats per minute which is not accurate. 56, 63 slowly climbing up and getting more accurate over time. I think the problem is that it's really hard to use without some proper like protection like this case right here but these are wired up differently because I keep touching all these pins and shorting them through my skin or at least messing with the frequencies because some of these pins like pins IRO and RO are the red light and IR light like hardwired power pin. So if you didn't want to use the magic that they add here and their own code and you wanted to do it strictly based off your own stuff you could just directly use the lights in the sensor. And the problem with that is that if you short those or mess with the frequencies it will drastically change your numbers. There's not many ways I can get my hand on there without touching all of the metal. So if I had to guess that's what's messing with it but sometimes it is pretty accurate saying 98 for the SP02 and then my heart rate's going between 65 and 75. So I'd say that's pretty accurate something like this right here. Definitely some work to be done but that's pretty crazy that for $10 I was able to do that. Well that's how our pulse oximeter sensor works. If you have any questions you want to know anything about the code or you want to know more instructions on how to fix this and make it actually work I'm going to link everything in the description. If you'd like to see in any future videos other sensors ones that you plan on buying ones that you've already bought let me know if I don't have it I'll get it and I'll make a video on it. Please do give the video a like and subscribe to the channel those things mean a world of difference when trying to grow these videos and it means a lot to me. Thanks for watching and I'll see you in the next one.