Episode 6: Stepper Motor Control with GUI using Python – Arduino Com
Using parameters defined in python script well be using some of the concepts we learned in our earlier series, such as transmitting data from python to arduino parsing data, once its received and using functions to control the speed direction and angle of the stepper motor. So lets get started. First of all, let me run through a little demonstration here. We have the python script and lets run. The script gui should appear, as illustrated in the middle of the screen, and we should have the arduino output. Let me just activate the arduino. Okay, now, if we run the gui lets set on the interface gu interface, we have the image of the stepper motor itself, the speed and the slider, which controls the speed in rpm and the angle which is measured in degrees, so lets set the speed to 8 Rpm and angle lets make it 90 degrees if we rotate. If we press clockwise, the data is sent and the stepper motor rotates by 90 degrees lets have a look again here and we can also run the stepper motor in anti clockwise direction. This can be seen. We can reduce the speed to 5 rpm if you want to and set the angle to 180 degrees and let the stepper motor rotate in clockwise direction by 180 degrees same applies to anti clockwise direction. On the right hand of the screen, we have the outputs. So this is the the first line indicate the data thats been transmitted from arduino from python to arduino uh.
So we have the first set of data, which is one, and then we have the separator a. Then we have the second set of data, which is the speed of the arduino, and we have separator b. Then we have the third set of data, which is the angle that we weve set earlier and then separator c and anti clockwise direction is being applied. Then separated once we do the parsing we again separate the sets of data to 1 580 and anti clockwise and wed use that data to control the motion of the stepper motor okay, guys so lets run through the script on arduino ide. First, we are defining the global variables here same as before that are used in our program. We got the connection. Port weve got the temporary variables that is used to control the script, as well as the variables to capture the parameters. So, for example, weve got indices a to d here that separate the data sets and in string format we have the variables used to control the motor, but these are converted into these variables, one below which are here in order to capture the right data format, which Is used in our step motor, so in the void setup we have our serial open and the voltage is set to 9600. All the connecting ports are set to output and were using the pin, mod and its set to output because were sending data through them to the motor in our void loop.
So initially we are waiting for the data from the serial, which is transmitted from python once recognized that there is a more than zero characters. Each of the characters from the string or from the serial is assigned to a variable called car data, which is char data. As shown here, so we are checking if each of the variables whether equal to backlash n or not backlash and usually indicates that we press enter. So if it doesnt equal to backlash n, we take the um the character in car data and assign it to indexed variable mod data, and we are using a variable called index underscore post to navigate through the characters. The navigation variable is increasing by one every loop. Otherwise, if the variable car data is equal to backlash n, we are making the last character backlash 0, indicating at its end of the string and the navigation variable is zero is assigned to the navigation variable. Once its done. We run the function called parse. The data and we send the string string value assigned to mod data so lets lower our interface and see whats in the function parse the data so the earlier mod deer variable here, a mod data variable is sent to the function and assigned to data in and The type is a string for the data in parsing of the data is same as before, where we capture the letters, a b, c and d from our string, which separates the data sets, then we extract the data between the separators, which is here and assign them To a variable – and please note, some of the parameters are still in string format, hence require additional line to convert from string to appropriate format depending on its purpose.
Once the data is parsed and the parameters are assigned to global variables, we print them in our serial allowing us to confirm if they are accurate, then we convert the angle of the motor to number of corresponding steps which is here and convert the speed of the Motor to the required time delay between the steps which is here this way we achieve the desired speed of the motor. If we find that the assigned direction in the motor dear string, which is this variable, is equal to clockwise or anticlockwise, we run an appropriate function here and add one more line towards the end to de energize. The last port thats been activated in the function, if otherwise, which is here if the mod dear string variable neither clockwise or anti clockwise. We get a message in the serial saying that the data is invalid and to try again for the user lets go through the functions, so the functions to run the motor in a clockwise and the anti clockwise direction has been discussed in our earlier session. Just a brief overview here, so here in this function we energize the boards in sequential manner, and we repeat this process until the number of steps correspond to the desired angle. So initially we have sequence number one where one of the ports is high and the remaining ports are low and that energizes one of the coils in the stepper motor. Then in the second sequence we have the second port high and the remaining is low.
That energizes, the second coil in the stepping water and the sequence repeats a number of times so between the sequences. We are pausing for certain interval to achieve the desired speed for the motor. The process in clockwise is the same as in anti clockwise. Just the sequence is reversed here: okay, once we finish setting up our arduino ide lets jump to python script. So let me just disable this and enable the python in python. There are two main actions happening here is which are creating functional gui and sending data to the serial port. So here above we are starting off by importing pi serial module time and tk inter libraries and defining the variables that will be used in our script, which are here. Then we are defining the serial port, starting from here all the way to there and opening it up before sending any data before going through the functions lets run through the main script first, which is down here here. What here we are defining the parameters of the gui interface and assigning functions to the widgets that we assigned in the gui? First, we create the image folded gui as it as it will be positioned at the top of the interface, then were creating a label and a slider widget that will be used to adjust the speed of the motor. The speed is measured in revolutions per minute and this the slider increments from 1 to 9 Music and expands across across the widths of the interface as we move forward.
We create a label and an entry box that is used to set the rotating angle of the motor. The angle is measured in degrees and for the final step we have two widgets, which is clockwise and anti clockwise buttons, so with the widgets right here. These buttons set the direction and transmit the data to the serial using a command method. It assigns a functions to the button. Lets move up the screen to access the functions, um Music, so its here rotate clockwise and anti clockwise. The directions are assigned to a variable called motor deer, so they actually have the clockwise or anti clockwise, which is used as the input function to as an input variable to function sent data okay. So, as for the send data function, lets move up to line 29 thats where the function starts. We are starting off with a variable mod run and a constant value of 1 is assigned, so were not changing much uh. This variable at this point, but it could be used later on. If we want to indicate if the power is on or off or any other kind of navigation inputs, then we have variable mod speed, int, which is retrieved from the slider using method get and it retrieves retrieves integer value, which is then converted to string value in Line 34 and as for the mod motor angle, which is here on this on this line, we are also using a get function.
I mean method from the entry box based on this data. We are using, if statement, to check if the input data either empty or not empty, so if its not empty, then we encode the data and send it to the serial. If not, if, if its empty, then we display a message in one of the label areas saying please input angle, the message is displayed for one second and for the final part, we have more deer variable, which is determined in the earlier steps and sent here as An input so all the data here is being encoded to utf 8 and being sent to the serial with the corresponding separators. As we define all the functions, all the variables i mean we Music run. This function confirm transfer. What this does is displays a message saying: data is has been sent on the image that weve inserted earlier. Okay, so lets try to run this again. Let me bring this slightly down and activate the gui as well as have the arduino output displayed. This enables us to see what are the data thats coming in okay, if i set the speed to 9 rpm and angle 90, so we can run the stepper motor clockwise. We can also run anti clockwise. Here we can change the speed of the motor to 5 lets, say and angle lets, set it 360 one revolution and let it rotate clockwise on the right hand of the screen, and we can see the data is coming into our arduino and its being parsed one By one lets run the stepper motor in anti clockwise direction same as before it runs.
Okay, great, i think, well just wrap this up here um. It was very interesting project to work on. I hope you guys enjoyed make sure to like this video and subscribe for further series.