Using an Arduino with Python LESSON 3: Passing Data From Arduino to Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter with toptechboy.com coming to you today from the mighty river now and i'm here today with episode number three in our incredible new tutorial series where you're going to learn how to make python and arduino work together what i'm going to need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always i want to give a shout out to you guys who are helping me out over at patreon it is your support and your encouragement that keeps this great content coming you guys that are not helping out yet take a look down in the link there down in the description there is a link over to my patreon account think about hopping on over their hand hooking a brother up but enough of this shameless self-promotion let's jump in and talk about what i am going to teach you today now last week what we did is we got our python environment configured so that we can come in and start writing the python code and getting python and arduino working together so if you haven't done that already make sure to go back to lesson number two and do that and then today what we're going to do is we're going to make our first program where we write a program in arduino to make arduino do something and then pass that data from arduino over to python and then do some simple something in python like print now in the end we'll be doing some really cool stuff on the arduino and passing the data over and then animating it with some really really cool animations but today we're just going to take that first step how do we get data from arduino over to python okay are you guys ready to go i hope you are so let me move over here to my screen view let me get out of your way and then i need you guys to go ahead and fire up your arduino ide and so i'm gonna get one going here and i'm gonna go ahead and make this nice and big so you can see it sorry i didn't have this big to begin with but then we're just going to start by writing a very simple program that will just generate some very simple data and then what we're going to do is we're going to then pass that data over to python so we're going to take the data in arduino arduino is going to pass it python's going to grab it and then python is going to display it and so really this is a very important lesson because this is just the very core the very foundation of what we're trying to do which is to pass data from arduino to python and so we have our new slick bare minimum set up here and so we are ready to code gotta warn you guys i have not coded in uh arduino in about a year i've been coding in python so i might forget a semicolon here and there and i might forget to declare my variables but we'll get it all figured out okay what i want to do is i want to make a simple counter and so we're going to come up here and be good boys and girls and declare our variables and so i'm just going to make an int which is count i'm going to set it equal to 1 to start with don't forget your friend mr semicolon and then i'm going to have a delay in here so i'm just going to call it dl dl and let's set that to a thousand milliseconds okay that looks good now we're going to come down in our void setup and what we're going to do is we're going to need to start our serial our serial monitors we're going to do a serial.begin this should be very familiar to you we're going to live a little dangerously 9 600 has always kind of been the go-to baud rate but we're going to go on up to a 115 200 just because we can and then we'll put in our semicolon there and so now what we're going to do in our void loop is we're just going to count okay we are just going to count and then what i am going to do is i am going to do a serial dot print excuse me i'm going to go ahead and do a print ln what am i going to print count okay and then uh let's go ahead and make it a little bit more interesting and so i'm going to serial print count okay serial print count and then i am going to serial dot print ln so this will send it to a new line and i'm going to serial print ln and then we are going to count in mississippi's okay how many of you growing up learn to count in mississippis one mississippi two mississippi three mississippis now here in africa they count in tanzania's it's 1 tanzania 2 tanzania 3 tanzania leave a comment down below how did you learn to count young did you do anything silly like one mississippi 2 mississippi or was that just a southern thing all right so now with this could it really be that simple could it really be that simple well let's run this now one thing i will remind you is you need to come in and make sure you're right on on the right com port you notice that i am on com3 you'll be on calm something else but you got to make sure that you're hooked up to the right com port with your arduino also i should show you that i will be sporting today the most excellent sparkfun red board with the little attached breadboard that's a live view so you're going to be able to see a live view of my arduino while you're able to see my code window now i know you're saying hey i thought you said to use the arduino the super starter kit from elegu yes i'm using the components from the lagoo super starter kit but the arduino itself i'm just using the red board it's absolutely identical in every way to the uh to the uh arduino that comes in the super starter kit then you say why are you using this well i think the red just looks a little bit sharper than the black one so i really like the red the second thing i like is is that as i'm doing a build and filming i like that the uh that the breadboard is kind of glued down on the same base as the arduino so it'll keep things a little more stable as i'm filming for my build okay those are the reasons i'm telling you that i'm doing this but the real reason do you want to know the real reason that i'm using the red board i got all the way over here to africa and in all my stuff that i moved over let's see where can you see this i did not bring one of those little types of usb cables okay i didn't get over here with that kind of larger size usb cable i got over here with a medium sized cable and lots of small cables but in going through all of my stuff i actually can't hook to that arduino but it's okay because for the other reasons this red board will really work and as far as coding goes indeed it really is identical enough of this chit chat let's jump in and let's see if this program will run now we are going to return to our earlier most excellent tradition of holding our breath while we compile our code ah gotta save it all right so i will call this i think i'll call this pass one just like i did there let's see will that work okay now that didn't work i'm sorry i'm just i've just installed this here and so uh plus i've always found this just to be a little bit awkward okay so i'm going to erase this one okay and then i'm going to save this as pass data one i always find it kind of awkward how it wants to put it inside of a folder and all that kind of stuff all right so now it's called pass data one it looks like things are happy we will all hold our breath together so this will work it looks like it worked okay so is it doing anything well to see if it's doing anything let's fire up our serial monitor and then it should start over here now make sure that you're set on the same baud rate here as you set over here yeah you guys can see that let me move it over there ah it's stuck on one mississippi why is it not incrementing it's not doing what i want well it's not doing what i want but it's doing what exactly what i told it and what did i forget to do i forgot to increment my counter so count is equal to count plus one don't forget the semicolon all right now let's download this thing again and see if it's going to work okay okay now let's open up our serial monitor and let's see if this thing is working oh wow that is going really fast okay so what we need to do with that is we need to put a delay in and we set up delay but we didn't apply it so let's say uh delay and how much do we want to delay by dl all right and then our semicolon and now we're going to download this thing people get so mad when i say download instead of upload it's just in my mind i am in the pc and i'm sending the data down into the arduino and so you guys can get mad or argue or whatever you want but that's just the way i look at it i look at it as a download okay one mississippi two mississippi three mississippi 4 mississippi all right we got it working now what on earth does that have to do with how do we pass data over to python well we pass it just by printing it but as we print it and we're throwing it to the serial monitor instead of opening up the serial monitor python is going to jump in and on that same com port is going to catch it now here's a pro tip and this will make your life much easier if you learn this if you have your serial monitor open python cannot go out and get the data because your serial monitor has locked up that com port so for this to work you have to remember make sure that your serial monitor is closed just a little pro tip there okay so we are running here remember it is still running while we're going about our business here now we need to go do some python coding so i need you to fire up the most excellent visual studio code and then we are going to come over here to this explorer view and then remember we are working in the pi arduino we're working in the pi arduino folder and then what we're going to do is we're going to create a new program and then that is going to be let's call it pass data pass data dash one dot py and the dot py is kind of important all right if you guys remember last week we already in the last week's video we already set up all of our libraries and so our library should be there and ready to run but the first thing that we're going to need to do is close this explorer view okay and then what i'm going to do is i'm going to import time because we might need to put a delay in here somewhere so i'm going to go ahead and import time and then i'm also going to import cereal now while we're calling it cereal it's really the pie cereal library that we installed last week and then we're not getting in bad habits of putting semicolons on the end of commands in python that would be very bad but easy enough to do okay now what we need to do this is really the core of what you're going to learn today we're going to create a python object and that python object is going to be the com port so we're going to set up an object that we can go and grab data from the object and the object is going to be that com port and so we can name it whatever we want but i'm going to name mine arduino data because that's what it is i'm going to get my data there and that is going to be equal to serial dot wait for it serial with a kepler cases that really bugs me import serial lowercase dot serial uppercase okay and then what we got to do is we got to tell it what com port we are on inside of single quotes we were on com three now notice no space com3 if you're on com5 on the arduino then it's com5 here but make sure that whatever this is is the port that your arduino is on and now i got to give it the baud rate and the baud rate has to match so that was 115 200 no comma all right now i have created an object that i can now read data from all right now this is also very key it can take a little while a small amount of time to initialize that uh that com port and if you try to go read from it before it is set up then you're going to get some very strange behavior so you need to give it time to set up and so just to be safe i will say time dot sleep and i will wait one second now if you're a guy who's interested in this type of thing you could see like how low of a number i could use here and it would still work i will tell you sometimes you can put no delay at all and it might work but putting a delay is really the way to make sure that it will work okay now what we want to do is we want to constantly we want to constantly read the data now on arduino we've got that void loop that's always looping but we don't have that on python so we need to create an infinite loop and we'll say while and while what while true okay when is true true true is always true so this will create an infinite loop okay while true i think that will work i don't like that little squiggly underline there i don't know why it keeps giving it to me do i have anything there [Music] okay it's just going to be there i don't know why it's there right now what we have to do we can't go out and just read that serial port because there might not be any data there there might not be any data there so what i need to do is i need to wait so i'm going to say while and this is just to wait while what while wow are our duo we know data that's that object i created while arduino data dot in waiting okay arduino data dot in weighting open close while that's equal equals zero all right so what does that mean it goes to our object arduino data it looks to see if there's any data they're waiting and if there's data then the amount of data is not going to be zero if arduino dot in weighting equal equals zero that means there's what there's no data so what do we want to do we just want to continue to loop so then we're going to pass so it's going to sit there and just keep looping until there's data then once there's data what we have to do is we have to drop out of that while loop we've got to drop out of the while loop because now we are ready to read and so now i need a variable to read so i'm going to call my variable data packet like that data packet and that is equal to arduino dot arduino data that's our object dot what read line like that arduino data why did it not like that arduino data did i misspell arduino arduino i hope you guys were yelling at me and now that one i did wrong too it would work but you just got to spell it the same way all the time and it's preferable to spell it correctly okay so we have arduino data in waiting arduino data read line and so now i actually have a data packet and so the first thing that we can do is just try to print that data packet so i'm going to say print date [Music] packet and so the interesting thing will be here just are we getting data okay now we need to come back and look at our arduino program okay make sure there's no serial monitor it looks like there is no serial monitor so we're going to come back over here and i think that we are ready to run this program and then let's see what happens on the python side now you got to know all this time while we're sitting here chit chatting and programming and having our coffee arduino has been passing data the whole time but once we go out to that serial port then we will start reading okay so now we run it and what's going to happen look at that boom shazam one mississippi two mississippi three mississippi four mississippi also you can see when we went out and connected to the uh when we went out and connected to that com port it reset the arduino so arduino was certainly up to a very high number of counting but when we connected to it it restarted the program and then it started from one why are we happy we're happy because we're getting data from arduino to python i think we should go back and start doing the double chest bump again leave a comment down below do you think we should be celebrating our small successes in programming with a double chest pot i think so okay we're happy but why are we not happy what is this b colon nonsense and what is this slash r slash in nonsense those two things we don't like okay what is the b the b is kind of getting down in the data structure that when we when arduino sent it that is specifying that it's being sent as bytes okay and then it's but when we're reading it we're reading the b and now that is kind of messing us up so it's good that it's there as we read it but now when we have it we want to strip that b off of there if that makes sense and then we also don't like the slash r slash n what is the slash r slash n well on the arduino side remember we did a print and then a print ln well that print ln will put the slash r and the slash n on the end of it the slash r is like a carriage return and the slash n is like a new line and so that's what puts it to the new line when we do a print ln and that's great but we really don't want that as part of our data so let's see if we can take care of those two little annoying things here so i've got the print data this time what i'm going to do is i'm going to come in and i am actually going to convert that from a byte to a string and so i'm going to say now data packet data packet is equal to data packet is equal to the string value of data packet the string value of data packet and then i've got to give it a format if i'm going to make it a string what type of string it needs to be a utf-8 okay i can make a whole video on what utf-8 means but just understand that when you throw something in arduino to the com port and then you grab it in python you gotta put it as a string utf-8 and now you'll start having something that looks more like data that you can really work with so let's go ahead we don't have to do anything on the arduino side because the arduino the program is still running the arduino side the program is still running so we'll jump down here and run the python side look at that one mississippi two mississippi three mississippi uh-huh count in mississippi's all right okay now what is one thing that we really don't like though okay what is one thing that we really don't like and one thing that we really don't like is it's skipping two lines it's skipping two lines now why is that because the data packet itself has that slash r and slash n and then when i do a print in python that goes to the next line so it's like we're going to the next line twice and so how do we do that what we want to do is we want to strip off that dash r and that dash in okay and then what we can do is we can just get a nice clean data packet at that point and so what we can do is i can come in and say data packet is equal to data packet okay and then what i want to do is a dot strip and then what do i want to strip off i want to strip off the slash r slash in like that i think that ah now i forgot i forgot i need to i need to take these two out i got to make sure which direction those two were going if it was the forward slash or the backslash and if i do this we should be able to see it on there again okay it is yeah that's right i got it right forward slash forward slash okay so now this then should strip it off and now when i print data set data packet it should work there it is okay now you see no extra line because i got rid of that extra carriage return line ending now you really want that carriage return in line ending in there because when i do a read when i do a data packet equal arduino data dot read line it reads until it finds that character so when you send it from arduino you really want to do the print ln so you end up with that slash r slash n okay and then that tells you when to stop reading but then you can see what our program here is doing it's sitting and waiting for data when there's data it reads it it reads it and so till it sees the slash r slash n it reads it in and then it takes that byte character off and it takes the extra character return off and then things work great okay that is really good okay let's see if we can do something a little bit more complicated all right let's see if we can do something a little bit more complicated we're going to come back over to the arduino side okay and this time what we are going to do is let's just say instead of count let's say that i have an x is equal to 1 and then let's have an int and let's make it y is equal to 2 and then let's have a int and let's have z is equal to 3 okay and then let's come down here and then uh as we we're gonna keep our delays and keep other things the same here and then what we're just gonna do is on our incrementing we're just gonna get rid of all this stuff here and then i'm just gonna say x is equal to x plus 2 all right and then i'm going to say y is equal to y plus 5 and then i'm going to say z is equal to z plus four i'm just trying to generate data four different numbers so kind of like four different channels of data how would we send all of that data kind of like a little bundle of data of three variables x y and z well what i would do is i would do serial dot print okay i would do serial.print and then what would i print i would print x okay and then i would do serial dot print and i need to put a delimiter a data delimiter so i'm going to put a comma like that okay and you might have to use double quotes here actually we'll do that to be safe okay and now what am i going to do i'm going to do a serial dot print and now i'm going to print y okay and now i'm going to do a serial dot print and then that is going to be another comma a comma okay and then serial dot print and then z okay now i don't need to right i don't need to put a comma after the z because this will just be x comma y comma z new line and so it should read x comma y comma z and then a new line but what we want to do here is we want to go ahead and run this on this side and then what we want to do is we want to look on the serial monitor just making sure that we're generating data like we would expect before we jump over to the python side so we're going to look here and 377 ooh okay what's wrong that last one needed to be what this one needed to be of what print ln to go to the next line because python would just keep reading reading reading looking for that end line it would never work okay it would never find one so it would just hang so let's go ahead here and do it okay let's read the serial monitor okay so you see nice behaved data we're passing chunks of data we're passing chunks of data all right so now what would we want to do we got to kill the serial monitor but the program will keep running now coming over here now what do we want to do well we still want to wait for the data we still want to read it we still want to strip off the uh the byte business convert it to a string then we want to take off that slash r slash n okay now let's just run this this should print i mean this should give us kind of like the three numbers over here like we had it over there if i'm thinking about it right is a good start okay 377 very good okay what is the one problem that we have here that is a what that is a string if we're going to play with those numbers we need them to be numbers and so how would we turn those things into numbers well what we need to do is we need to come over here and we need to split we need to say that the split packet we need to split that packet and put it in a new variable split packet is going to be equal to day data packet data packet dot what split we need to split it on what split it on the comma all right and we're going to use double quotes here okay we're going to split it on the comma all right so what is this going to do split data is then going to be an array and the array is going to have three values and it's going to have the values based on it's going to have the data data based on what those three numbers are that we split that we sent okay so we split it and now if we print split packet it should it should print an array if i'm thinking about this right okay if i'm thinking about this right all right hopefully okay boom now look at that now the one thing you got to see is it's passing an array but it's passing three what it's passing three strings okay so you better keep that in mind that you get one array and that one array has three strings so what do we want to do at that point well we want to go ahead and do something useful so what we're going to say is x our variable x is equal to we got to float it because it's a string float of what float split packet the first element is element 0. so we're taking the first element of the array we're converting it to a float and we're putting it in x okay and now i think we can copy this and then we can paste it and then we can paste it and then character return and then this we will call y and then this we will call c right and then this is split packet element one and split packet element two and now we're going to do a print okay instead of print split packet we're going to print what we're going to print the string x equal ah quote x equal space and then comma what x and then comma what y equal space and then comma what y and then comma then quote z equal space comma what comma z okay so we're going to print x equal x y equal y c equals z so now let's run this thing and see if this actually works oh okay very good except i got a little i've got an extra put the end quote inside of that okay and that should not be there that should be x equal x there a little minor formatting problem okay let's run this thing okay x is equal to 5 y is equal to something z is equal to something okay so that data that we're passing over we grab it as a chunk we clean it up we split it we convert it to floats and now we have real numbers that we can actually do things with also look we went to floating point numbers we we started out with integers we passed them as a string and then we converted them to a float and so now we're actually printing them out as a float okay guys man this is pretty cool we're writing programs in arduino we're passing the data to python and then we're grabbing it in python and we're doing something with it okay let's see if we can make it a little bit more interesting and so for next week what your homework is is in your ela your most excellent elegu super starter kit i need you to get your little potentiometer out and then what i want you to do is build a circuit with this potentiometer i want you to take the potentiometer reading from that center leg and then i want you to pass that over to python and then i want you to print it out in python and then as you are changing the data here on arduino i want that on the potentiometer i want that to be reflected over in python now for you guys that just know arduino and really don't know a lot about python yet just print it because i've shown you everything that you need to know in order to do that and then for you guys that uh have taken my lessons on visual python i want you to make a visual python graphic that will show visually the data about where your potentiometer is okay now you could either like maybe you could convert it to voltage and then after converting it to voltage you could display the voltage in visual python or you could make some sort of little meter where you were showing you are showing the voltage based on uh based on where the position of the potentiometer is does that make sense okay i hope it does now for you guys that don't know how to do visual python don't worry because next week i will show you the solution to the homework based on what i've shown you here you ought to be able to at least you ought to be able to at least program up the potentiometer and pass the data over and print it in python and then you'll learn something when you see my solution for you guys that have taken visual python let's see just how slick of a demonstration how slick of a visual you can make based on this assignment okay you guys need to post your solutions to youtube for you guys that are new what you do is you just do a screen capture of your program running showing me what it does then you post it to youtube under your youtube account and then down below in the comments you leave a comment with a link over to your youtube video to your solution and then also when you post it on youtube make sure that you leave a link back to this video so anybody watching your video would know what you're doing they would have contacts you linked back to me and then from the comments below you link to your solution i hope that makes sense okay guys i hope you're having as much fun with this class as i am i am really looking forward to getting back into our arduino hardware and and linking up and getting arduino working with python and so this is going to be a lot of fun again this is paul mcquarter from toptechboy.com coming to you from the lovely shores of the mighty river now i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 53,479
Rating: undefined out of 5
Keywords:
Id: VN3HJm3spRE
Channel Id: undefined
Length: 34min 54sec (2094 seconds)
Published: Tue Jan 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.