Tag Archives: Python

Python with Arduino LESSON 6: Installing PIP on Windows

In this series of lessons you have learned to send data from Arduino to Python, and then do some pretty cool things in Python. We have created little virtual worlds, and have done neat dynamic graphics. Unfortunately not all Python libraries are as easy to install as vPython and pySerial. Some are next to impossible to install. The good news is that there is a free program called PIP that will install just about any Python library very easily. We need to pause and install PIP. Many of the future lessons will require you to have PIP installed on your machine. Please follow along with me on the video, which shows you how to install PIP on your windows machine. These links will be useful. You can download pip at this link:

https://pip.pypa.io/en/latest/installing.html

Go to the section on PIP install as seen here:

Install PIP

Right mouse click on get-pip.py and download to your desktop. You will then want to run the program in python. We show you how to do this in the video above. Then, you need to edit your system path file by going to the control panel, select system, select advanced settings, and under environmental parameters select the path. Update your path file to show your system where your python folder is and where your python script folder is. If you are unsure of this, watch the video where I show you exactly how to do it. If you are adept with computers, you can just do it from this description. Add the two elements to your path. Adjust the parameters to reflect where your python installation is and where your python script folder is. For me, these are the two things added to my path:

C:\Python27

and

C:\Python27\Scripts

If you do not know exactly what I am saying, then watch the video for more detail.

Once you have these in your system path, you can test your PIP as follows.

Open a CMD box.

Type:

pip install -U pip

This asks pip to update itself. You should see it come up and indicate it is either up to date or is updating. This will tell you that you have the PIP installed correctly.

Your life with Python will now be much easier because your system now knows the path to both your Python program and your PIP installer.

Python with Arduino LESSON 5: Finishing our Virtual Reality Example

This Lesson finishes the work that was begun in Python with Arduino LESSON 4. In that lesson we built the circuit and programmed the arduino to measure the distance to a target and the color of the target. The program then output that data to the serial port. In today’s lesson we will use python to read that data stream, and use the data to dynamically update a virtual world we create.

You will need to start with the work in LESSON 4 to get your circuit working, and your arduino programmed up. Once you have done that, you are ready to use Python to program up your virtual world. Remember you will need to have the pyserial and the vPython libraries loaded. We showed how to install the software in Python with Arduino LESSON 2.

In the video we will go through the process step-by-step to create a virtual world. The code we end up with is posted below. You should not copy and paste the code, but just glance at it if you get stuck. In the end, you should develop your own virtual world and just use mine as a guide if you need more help.

 The video explains each line of the code.  Play around and tweak the values and see the effect on your virtual scene. Now your assignment is to take what you have learned here, and continue to expand your virtual world. Add objects to your virtual scene. Perhaps build an object for the breadboard, color sensor and arduino. I will give you several days to do this, and then when I come around for a project grade, I will want to see who has built the most impressive virtual scene. You should go well beyond the simple demonstration I have done here.

Python with Arduino LESSON 4: Expanding your Virtual World

In this lesson we will expand the virtual world we created in Python with Arduino LESSON 3. We will be creating a virtual world that will track a simple scene in the real world. In this project, the virtual world will track both the position and the color of a target in the real world. This lesson requires that you have the Python software and libraries installed, which we explained in LESSON 2.

Arduino Circuit
This is our circuit with the HC-SR04 ultrasonic sensor and the TCS230 Color Sensor

This Lesson will be a bit more involved, and I will take you through it step-by-step. I will need to break things into two parts. In today’s lesson we will cover the Arduino side. We will develop the software that will measure distance and color, and then send those numbers over the serial port. Then in tomorrows lesson, we will develop the Python software to create a really cool virtual graphic to display the data in a virtual world.

For this project you will need the HC-SR04 ultrasonic sensor, the TCS230 Color Sensor, the Arduino Microcontroller, and some male/female jumper wires to connect to the color sensor.

The Ultrasonic Sensor can be attached per the schematic below:

Ultrasonic Sensor Circuit
Simple Circuit for Measuring Distance

Detailed tutorial on using this sensor was described in Arduino LESSON 18, so we will not go through all the details of using the sensor here. Review that lesson if you need more help. Key point here is to connect it as seen in diagram above.

You will also need to connect up the Color Sensor.

Connecting the Color Sensor to the Arduino

Color Sensor Pin
Arduino Pin
S0GND
S15V
S2pin 7
S3pin 8
OUTpin 4
VCC5V
GNDGND

Use of the color sensor was described in detail in Arduino LESSON 15.  You should be able to develop to write the software yourself based on earlier lessons to make measurements from both the Color Sensor, and Ultrasonic Sensor, but if you get stuck, you can glance at my code below. Again, it is important for you to write your own code and not copy and paste mine. Mine is just a reference if you get stuck.

The key point to notice with this code is the print statements, summarized below:

 Notice that we are printing  our color strengths and distance on one line separated by commas. It is important to note the order of the data. When we read this in Python, we will read it in as one line of text, and then we will parse it into its individual values. So, we must make note and remember the order the data is arranged in in this line.

Remember when you have your python program reading this data, you must have your serial monitor closed. For now though, run your program and look at the serial monitor to verify you are getting correct data in the expected format.

In the next Lesson, LESSON 5, we will build the Python program to create a virtual world from this data.

Python with Arduino LESSON 2: Installing the Software

There are some really incredible things we can do when we get our little Arduino to talk to the big bad Python programming language. To do that, we have to start by downloading some software. Never fear . . . it is all free and I will take you step by step through the installation. The video above shows you how to do it. If you are the impatient and technically adept type, you can download these three software packages:

1) Download and Install Python 2.7.8. Please note all my tutorials use this flavor of python. If you want to follow my tutorials, do not download a Python 3.x. Also download 32 bit version of the software, even if you have a 64 bit windows machine.

2) Download and Install Pyserial version 2.7. Again, download the 32 bit version of the software.

3) Download and install the Vpython library for 32 bit windows.

Now, lets get python and arduino talking together. First up, we need a simple program to get the Arduino sending data over the serial port. The following program is a simple program that will have the Arduino count and send the data to the serial port.

Now, open the VIDLE environment which you downloaded with the Vpython library. Once you have done that, you are ready to write a Python Program that will go out and read the data over the serial port. Full explanation is in the video. Do note, however, that the line:

This is for a windows machine. Also, the ‘com11’ has to be adjusted and set to whatever com port your arduino is talking on. You can figure that out by looking at Tools – Port on the Arduino. Set this parameter to whatever port your Arduino is talking on. Then, the baud rate needs to match as well. You can use whatever you want by Arduino and Python need to be on the same baud rate, which you set on this line of code. OK, the complete Python code to read the Arduino data from the serial port is here:

Simple as that! Welcome to the world of having the Power of Python now at your fingertips.

LESSON 1: Using Python With Arduino

Today we are going to start a new series of lessons that will allow you to take your Arduino projects up to the next level. Hopefully you have been through my 20 lessons on using the Arduino. If you are not familiar with the Arduino, you should start with those lessons. If you are familiar with Arduino, you can just right into this series of Lessons.

One of the major limitations of the Arduino IDE is its limited ability to interact with the user.  You can print text to a simple serial monitor text box, or get text input from the user. You can open up a new world of possibilities by using the programming language python to interact with the Arduino. Python combined with Arduino is a powerful combination what will drastically increase the WOW factor of your projects.

The video above will show a cool project that will hopefully motivate you to undertake this series of lessons. Python is a really cool language, and now that you know Arduino, it will be a breeze to learn Python!