Intro: Arduino and Python
For a project (check out my blog for updates) I'm working on I needed to be able to communicate with my arduino, luckily the arduino can communicate though serial. It turns out almost any programming language can be used for serial communication but python seems to be the easiest so far.
I had some problems getting it all to work and finding a basic tutorial online proved very difficult. So hopefully this guide will explain the basics to anyone that is looking to start using the serial functions that the arduino provides.
I'm going to assume that if you're reading this you have some knowledge of how an arduino works and how to upload sketches and what not, however I will explain the arduino code when we get there. The python code is very basic but I will do my best to explain it as well.
The code is not that hard to understand, the hard part is understanding how the serial communication works.
So, I'm going to show you how to tell your arduino to blink using your computer. Once you understand this you should be able to expand both the python code and the arduino code to fit your own projects.
Step 1: Sorting Out Python
Now, obviously, we're going to need python if we want to do anything so we better get that! If you don't have it installed, head over to the python website and download it! (http://python.org/download/)
Once we have python installed were going to need a new library called PySerial. This is going to provide all the functions and methods we will need to talk to our arduino!
If you're using a windows machince check out their source forge page for the windows installer.
If you're using MacOS/linux your going to have to look around the PySerial website.
Also, if you're familiar with using Eclipse you might be intersted in the Python add-on for eclipse. Check it out if you would like to program in the eclipse environment.
Now if everything is installed we can actually start writing our Python program!
Step 2: Python Code!
Now we can actually start programming!
So, in order to actually use the PySerial methods we need to import the serial library before we try to use it.
Next I declare a variable that will act as a flag.When serial connections are opened with the arduino it takes a little while to sort things out. So we wont try to send anything to the arduino until it sends something to us.
Next we initialize a serial variable, "ser", that will be communicating with the arduino. Two parameters are sent when initializing a serial variable.
First you have to port that it will be communicating with. In my case It was COM11, but yours may differ. To find out what port your arduino is using, connect it to your computer and open up device manager. The arduino IDE will also tell you which port it is using.
The second parameter that is sent is the baud rate. The baud rate is the speed that the serial controller will send and receive at, the important thing is this baud rate matches the baud rate you use in the arduino sketch. I chose 9600 since it is a middle of the road speed and we don't need anything too fast for this example. If you want to use a faster or slower speed, use Google to figure out which speeds to use.
We want to tell the arduino to blink! So I have a write function that sends the number 1 to the arduino.
When the arduino sees this it's going to blink twice!
Now we want to wait until the arduino tells us that it has blinked twice. By having the while loop the program will loop (do nothing) until it receives a message. If we were to leave this while loop out, the program would close the serial port and the arduino wold stop blinking.
When we receive the message from the arduino we can close the serial port and end the program.
So that's all we need for the python program, just 10 lines of code!
Step 3: Setting Up the Arduino
Im going to assume everyone has the adruino software installed and working.
Since we just want to make a light blink we can just use the light that is on the arduino or connect and LED to pin 13 and ground.
Like the circuit the Code isn't too scary either.
In void setup () we start the serial monitor with a baud rate of 9600. The rate doesnt matter, just make sure it matches the baud rate in the python program.
Next, we make an output pin that out LED is connected to.
Lastly, we write something over serial so the python program knows we are ready.
In void loop() we have one big if statement. Basically this is waiting for the python program to send something over serial.
Once the arduino receives something the LED will blink twice.
After two blinks the arduino sends a message back saying it is finished blinking. The python program will see this and then stop.
Step 4: Test It Out!
Upload the sketch to your board and then run the python program. If everything was done properly you should see the light blink twice!!
If not make sure the programs are the same as mine or leave a comment here or message me, I'll be happy to help!
If you want start messing around this the code, try adding in another LED and make them alternate, try sending different things over serial. If you get stuck just remember Google is your friend!
Thanks for reading!