Arduino

[Jun 21, 2013]  So I've had an Arduino Uno for over a year now and haven't had budgeted the time to do anything with it.  We, the Tarrant Makers (aka Forth Worth Makers Space), are holding an Arduino 101 class on June 29th, so I've decided to dust off the board and put together a project to demo.
Arduino Uno

My goal is to interface a SRF04 Ultrasonic Sensor to a simple Python game using the Uno.

Here are the steps I went through to bring my idea to fruition.
  1. Get the Arduino up and running on my Windows PC
    1. Install the latest version of the Arduino Software. This is pretty straight forward, just follow the instructions on the site.
    2. SRF04 Ultrasonic Sensor 
    3. The IDE comes with quite a few example programs to get you started.  I uploaded the 'Blink' program to the board, just to make sure everything was working. You should see the LED on the board blink.
  2. Measure distance with the Ultrasonic Sensor
    1. This sensor has been around for a while so I figured someone must have created an Arduino library for this device.  Sure enough, by the power of Google, I found one.  The download, instructions, and how to connect it to the Arduino can be found here.
    2. The DistanceSRF04 library has example code.  I uploaded the Centimeter (just because I like working in metric units) project and everything worked as expected.  To see the output open the Tools-> Serial Monitor in the Arduino IDE.  
    3. Now that I have the sensor working and detecting distance, lets figure out how to interface the hardware with software.  It's real easy to send and receive serial data using the Arduino so I'll go down this path to get the Arduino hardware talking with the Python software.
  3. Setting up serial communications in Python
    1. First I wanted to update to the latest version of IDLE (Python's IDE) which is version 3.3.2 currently.  The IDE can be downloaded here
    2. When installing Python be sure to select the 'Add python.exe to Path' option when the Customize Python window appears. What this option does is add a path to the python.exe to the 'environment variables' which allows one to run python commands from the windows command line.  You can also manually add the path via the System Properties if you forget to select this option or your version of the python install doesn't have it.
    3. Download the 'pyserial-2.6' library found here.  This is the library I'll be using in my python code to read the serial data from the Arduino.
    4. Extract the pyserial-2.6.tar file. Copy the pyserial-2.6 directory in C:\Python33\Lib\site-packages.  
      1. I didn't have an application installed on my PC to unpack tar files so I installed Cygwin Teminal to do this.  Cygwin can be downloaded here and help on unpacking a tar files can be found by typing 'tar ?' in the Cygwin Terminal or you can always go online. 
    5. Open a command line in windows and change directory to: C:\Python33\Lib\site-packages\pyserial-2.6
    6. Execute the PySerial setup by typing: python setup.py install
    7. We should be good to go to use the PySerial library. 

No comments:

Post a Comment