Menu Close

How to Sending Data from an Arduino to Python

0 0
Read Time:7 Minute, 13 Second

Sending Data from an Arduino to Python

Hi everyone so I recently start on a new project involving Internet of Things IOT cyber-physical systems and I found myself having to collect a significant amount of data process it run it through various algorithms before pushing it to the cloud so I just thought of sharing the work I did now you guys might be able to use some of these methods if you have similar projects but for now let’s start off with the very basics and that’s how to send data from an Arduino to Python using Py serial now  welcome to that’s engineering so when using an Arduino.

If you want to collect a significant amount of sensor data crosses it maybe run it through various algorithms use it in machine learning or AI or even to develop things like web pages or dashboards then the Arduino ide isn’t the friendliest thing to use we are better off using something like MATLAB or Python for example now MATLAB has an Arduino library and add-on which you can download and use it to control the Arduino and read data directly off its ports Python on the other hand isn’t that straightforward but we do have access to the Py serial library which allows us to read data from the serial port of the Arduino and that’s what we are going to have a look at in this Article today.

Now before we get started. let’s have a quick look at the physical system so I’m using over here adht11 temperature and humidity sensor now you can use whatever sensor you want or any number of sensors that you want and you would have to connect it as such the Arduino on the other hand has to be connected to the computer using a USB cable so that you can transmit data and have it picked up by python so for this sensor the positive is connected to five volts the negative is connected to ground and the data pin is connected to any digital pin on the Arduino I have used pin number four alright guys so now let’s go through the Arduino code this code along with the python script will be available in a link down in the description below.

So you can have a look at that but I will run through these codes very briefly so you guys know what’s going on so I started by including the library for my DHT sensor and in the setup I started serial communication at a baud rate of 9600 in the loop. I initialized the DHT sensor and collected the temperature and humidity readings and stored them in their respective variables so this is the main part of the code now for python to pick up the data you have to place the data on the serial port and place it on the serial port you just have to simply write the data on to the serial monitor which is exactly what I’ve done here.

I have the temperature value followed by an X followed by the humidity value so the X actually acts as a delimiter so that later on we can split the temperature and humidity data and identify them separately so what happens is when the data is put onto the serial port and when Python reads the data it has to decode it the decoded value can be stored as a string and this string has to be split so that we know what the temperature value is and what the humidity reading is and we will be using this X to split it up right now I’ll just plug in my Arduino and go to the serial monitor I’m already uploaded the code and you should see the temperature followed by X followed by the humidity reading all right now.

let’s have a quick look at the Python script so this is the Python script I’m using three libraries the serial which is the Py serial library time and schedule now video is just used for automation of this code you don’t necessarily need to use it but I’m used it so I can periodically collect data values without having to come back and run this code every time I want to collect a new data point now you can find these libraries online just go to pypi.org and type in the name of the library for example Py serial and you would get the library over here you can install this by running this command on your command or terminal window and you can find the documentation as well over here similarly you can do the same for the shitty old library which is for job scheduling and you can install it by running this command.

So I’m using pi charm but you can use whatever Python editor you want to use you just go to your command or terminal window and type in tip install py serial and you would get. so I’m already installed it so it tells me is already been satisfied but if you haven’t installed it the installation process should start similarly you can install the shield library as well and the installation for that should proceed right now let’s have a quick look at the code so the code starts all the way at the bottom over here at this particular command so it starts machining the command and I’m said shiny moon every 10 seconds and every 10 seconds I’ve ask edit to run this particular function which is the function I’ve defined up over here I’ve then used while loop and I’ve told it to run the pending functions what happens here is it will just keep checking to see if the 10 seconds has expired and once it does expire it will run this particular function that I am specified right.

So let’s have a quick look at this function I’m started by establishing a serial connection to the Arduino and I’ve specified the serial port and the board wait now if you go back to your Arduino ide go to tools and you can check your serial port by going to port so – con 3 right so the serial port and the baud rate you can then use this attribute read line to get the serial data and that’s what I’ve done and store it in this variable Arduino data the data has to be decoded which is what’s happening in this line and it’s then converted into a string right so here based on the X we can split the temperature and humidity readings so everything on either side of the data X is split into two separate values and stored in this variable list values now the data is in the form in the format of a string.

So I’ve used a for loop to store the data to change the format of the data into a floating point number and append it to this variable list influence and then I’ve just printed out the data I in that variable which is the temperature and the humidity ready so you can actually add whatever functions you want to add over here too depending on what sort of application you are trying to develop so you can call other functions over here to run whatever code you want to do it after that just clear your variables and close the connection with your Arduino right so that’s about it let’s go ahead and run this code all right and the program started so every 10 seconds it should get a new reading so that’s weird for 10 seconds right so that’s the first reading and again after 10 seconds it should get a new reading and the second radio.

So similarly this will just keep on going until well until you stop the code but if we collect data every 10 seconds you can change that over here by specifying for example 10 minutes or 10 hours days weeks so on and so forth all right guys so that’s all for this video if you guys liked it don’t forget to give me a thumbs up by clicking the like button below now if you guys have any questions or clarifications on the work we just did involving sending data fro man Arduino to Python please leave a comment and I will do my best to get back to you if you want access to the chords or the circuit diagram that I’ve used in.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: