How to control any sensor using only Arduino and Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys my name is joseph and welcome to my channel today i'm gonna show you how we can control our arduino board using python with few lines of code this tutorial will be divided into two parts so uh in the first part i'll show you how we can send information to our arduino board and then i'll show you how we can get data from the sensors connected to the arduino board using python so as far as i can say today's tutorial is very exciting oh yeah let's get started [Music] okay guys let's begin the only thing we need for now is our arduino board okay because i will be controlling the led on our arduino board by sending some commands on the serial communication and then we'll we'll see the the python code that we'll be using to control the same led so here i have the code this is the code road and i call i call it led so i'm going to connect the the cable okay the usb cable and upload the code so before uploading the code you have to check the port okay okay mine is connected to this port now i can upload the code okay um it's done uploading i'm going to open the serial monitor okay you can see you can see that it's saying type on to turn on the led and after turn off the led so i'm going to type on here once i type on it should turn on the led and then i press enter i'm going to type again i type the wrong one because it should be this this way on the o should be like a cap top a capital o okay so i'm going to type again on [Music] you see that the led is on if i type off the led is off so now let's see the python code that we'll be using to do the same the same same work okay okay here we have the python code is also a very simple code the first thing i did was to import the this library pi pi serial i'm just commencing so that you can [Music] use install okay use this command to install the library okay so if you don't have this library installed you just use this command here install pi serial now you can open your cmd and then type the same pip install by sierra and then press enter it's going to install this library and then i created a variable okay that will be responsible to store the informations of our arduino board all the informations of the serial common communication between python and the arduino board and then we have here this function i called it led and i gave an argument to it i in they called i called it data and then i said if on found in data then it should on or turn on the led and if it's off found in data and then it should turn off the led and then here we have the the white loop but we we will be typing the the the data or the information to turn on or turn off the led so let me save this and run it i'm going to run this python file in terminal okay i guess something is on here no okay okay now it's okay so type on to turn on the led and after turn off so i'm going to type on so once i press enter to turn on the led you see that the led is on if i type off the led is off okay guys so this is how we can control our arduino you can uh instead of using the the instead of controlling the led this one you can control a relay or you can control a mod or something like that okay now uh we're going to see how we can get data from some sensors using arduino and python okay now that we have seen how we can control our arduino board using python now let's uh start getting data from sensors okay so the first sensor are using is this one the ultrasonic sensor so uh this sensor is used to measure distance suppose that you want to make a project in which you want to measure a certain distance then this is the right sensor you can use for this project so i'm going to make the connections remember that you can download all the files the diagrams and the source code on my blog page okay the link is in the description so i'm going to make connections okay uh the connections are done okay now that's it code okay this is the code we're going to to use you see that what what i did was uh to include this library a new ping you can also download and install this library and then i defined the two pins okay the two pins of the sensor for data one is trigon eco okay eco is connected to pin number to the port number two of the arduino board and trig is connected to the pin number three of the arduino board and here i find the maximum distance okay and i created an object from the library and i gave i gave to it all these informations the tree equal and the maximum distance and then here on the in the setup function i initiated the serial communication and i printed this thing the distance from the sensor just to know that the values we are getting is the distance from the sensor and then after running the code or after opening server monitor it's going to uh have a small delay uh and here i created this this this uh a variable uh that will be storing the data's the informations from the library and then here we have these things and all this so it's a very simple code so let's uh i'm going to upload this code and see how it's going to work okay uh it's done uploading so i'm going to open the serial monitor and you see that it is measuring the distance oh okay i made a mistake sorry i forgot to connect the the power plug to connect the these the sensor um five volts in gmd okay i forgot so now we can open the serial monitor and see that we're getting the distance see getting the distance from our sensor okay so i'm going to let's see the code in python okay this is the code in python it it is a very simple chord we did the same thing that uh we've done on on first first code we i imported the library and then i initiated the cr communication and here we have a while loop okay so this is this code actually you you can use it to read any values getting from the server communication or the c for example if you if you connected if you've connected another sensor here and you're getting the data on serial monitor you can use this code without doing any any any change to see the same the same values are the same informations so i'm going to run this code and you see that we're getting the same informations here okay we're getting the same information here so this is how you can check or you can get the data from this sensor using python okay so i'm going to stop this and let's see another one okay let's see another sensor i'm going to remove all these and i'm going to use this one here at the dht11 but you can use the one you have i'm going to connect this power and gnd 5 volts and 10d and here i have uh okay gnd and five volts and this one this pin it is for for data okay i'm going to connect it in number six so i'm going to open the arduino code i've written for this project okay this one this 11. okay here we have the code and this code is also a simple code i included this library dht and then all these so take take take your time okay take a time to study all these codes the the code is not that that hard that long so i believe you can get to understand all this codes so i'm going to upload this one going to upload this one [Music] okay and then i'm going to open the serial monitor so see that we're getting the temperature and humidity okay we have we see that it's getting this so i'm going to open the same code we use for the this sensor okay i'm going to use that same code in python to read this information because that code the only thing it does is to read the same value from the values we're getting on the serial communication or here on the serial monitor to well we can use the same tool to read the these values too so i can just come here and and run this so you see we're getting temperature and humidity see temperature is the same information we're getting there all right guys this is the first part of this tutorial in the next video will show you how we can use the data we're getting from the sensors to do a certain task so please do not forget to subscribe to my channel and activate the the notification button so so you don't miss the next video thanks for watching and see you next time
Info
Channel: JOSEPH DREAM
Views: 965
Rating: undefined out of 5
Keywords: arduino, how to make
Id: eeXr6swRJkA
Channel Id: undefined
Length: 14min 50sec (890 seconds)
Published: Wed Aug 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.