Arduino and Python Serial Communication with PySerial Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to my first video tutorial on how we can interface our arena with Python Arduino is a great development platform that's easy to use but still very versatile interfacing with Python lets us expand our applications create user interfaces and data displays as well as many cases take the signal processing load off the Arduino and let us post process data to make it more useful we'll use the Arduino to collect the data and then hand that data off the Python via a serial communication the Python module I'll be using to do this will be PI serial it's free open source and a great tool and before we get started I'd like to point something out there's going to be two sets of programs for our development one is the code that's running on the Arduino and the other is running on the computer for Python we need to make sure that these are both in alignment there'll be some certain parameters and values that need to be equal and I'll make sure to point these out but once we get that nailed down it becomes pretty streamlined well first start by looking at the Arduino code this is how we get the information in the first place well utilize the ADC the analog to digital converter on the Arduino to collect our information the ADC takes a voltage input and then produces a digital output first I'll create a variable for the analog pin we'll use the third pin then I created another variable called data this will be the input information and I'm initializing it to 0 moving on we have void setup the purpose of this is to set up and initialize serial communication note the speed we're using 9600 moving on to the void loop or the main part of this code very simple and two lines of code data equals the analog value read off the annele log pen so this is the information that the ADC is getting and we're telling it where to look to get it the next line of code is a serial.println what this does is puts the data or prints it on the cereal bus the print Ln is unique in the fact that it prints a new line as well as the data this acts as a delimiter which lets us separate one data point from the other and lets us organize it a little better if you just use serial print you'll get each value one after the other and it gets messy particularly in the Arduino IDE serial monitor you want to include that new line the println as we see taking that new line out makes it hard to interpret what information is coming out of the serial bus now moving on to the Python code you can think of the Python code as automating that Arduino IDE serial monitor you can pass an information and communicate with the microcontroller as well as receive information from it let's take a look at some very basic code that allows us to view the data that's coming off the serial bus as noted in the beginning of the video we're going to be using the PI serial module to connect between our Arduino and our Python code and to do this we need to import the module we also need to setup our serial communication port and some additional information in order to get valid data inside serial serial there's three parameters one is the serial communication port this port is assigned by the computer and we need to make sure that we have the correct one because if not we'll be looking at the wrong port and you won't get any data in fact you'll get an error next you have the baud rate it's critical that this parameter matches what we established inside the arduino code if these two are mismatching you'll get garbage data or possibly an error the last parameter is timeout and what we're assigning to it is the number of seconds we should wait before we accept the serial information so in other words if we don't get any serial information after a second we transmit what is on the bus which could be nothing across it this allows us not to get hung up and wait for data that's not there what it tries to prevent is an infinite loop type scenario where we're just sit there waiting for something that isn't going to happen next we enter the main part of our code which is a while true loop it contains two lines of code one is the data point and the second prints out the data point in the first line of code we have the Arduino data point in this we make sure to look at the specific port which we specified above and then utilize the read line function what the read line function does is looks at the information that's coming in serially and then reads that until the end of line character and by default that's the newline character like in the Arduino IDE serial monitor we're specifying that we want to delimit our information this makes it more useful readable and more practical and after this we simply print the information now that we have this python code set up let's check out our Adreno data here is the raw data that we are receiving notice how the data is prefixed by a B and single quotes this just means that the incoming information or bytes then we have our data point followed by a carriage return character in a newline character it's very simple to clean this information up but it's good to note that you can view it in this form Python is particularly good with having premade functions that can clean up data here we can easily clean this data up by adding decode and inside that function pass it the parameter ASCII this tells us how we should decode the information and what type of information it is looking at in running this code we see that it removed the byte prefix and the newline and carriage return characters this puts us in a better position to understand the information that we're receiving from the Arduino thanks for watching and I hope you've learned more about both Arduino and Python in this video we went over the basics but there's a lot more to come there's some really neat applications and data post-processing and plotting in the future I'd really like to explore these thanks for watching again stay tuned for more content and if you'd like to give some input please let me know in the comment section
Info
Channel: WaveShapePlay
Views: 112,426
Rating: undefined out of 5
Keywords: pyserial, python, arduino, serial, programming
Id: iKGYbMD3NT8
Channel Id: undefined
Length: 7min 21sec (441 seconds)
Published: Fri Jun 15 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.