Tag Archives: Arduino

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 13: Controlling RGB LED with Arduino

OK, we are ready to move forward and learn new circuit skills and new programming skills. In today’s lesson we will learn how to control an RGB (Red, Green, Blue) LED with an arduino microcontroller! This will introduce us to a new circuit component, and will require us to learn some new programming skills.

Circuit to control RGB LED with an Arduino

An RGB LED is basically three LED’s in one. It has 4 leads. One lead, the long lead, is the common ground. Then one lead controls the red LED, one lead controls the green LED, and one lead controls the blue LED. All three of the LED’s are connected to ground through the same pin. You can control the color you get out of the LED based on the voltages you write to the different control pins. A schematic will probably help you understand how the component works and how you should hook up to it.

This schematic shows how to work with a common cathode RGB LED

This picture shows the four pins for a common cathode RGB LED. “Common Cathode” just means that the LED’s share the ground pin. There are also “Common Anode” LED’s which share a common high voltage pin, and then each color has its own ground. I think these are much more confusing, but just mention them so that you know that this tutorial is for the common cathode type.  The Sparkfun Inventor Kit has the common cathode configuration, which is the type I prefer. Also note in the drawing you can see that the length of the pins is your clue as to which pin controls red, which green and which blue.

Now lets think about using this in a circuit. As you can see in the schematic, all three of the LED’s share a common ground pin. You can easily see that pin should be connected to your circuit ground. Now, think about how you would connect the control pins. To control a normal LED, you needed to connect to one arduino output pin. To control this LED, how many control pins will you need to use on the arduino? That’s right . . . you will need three control pins. Also, remember than you never connect an LED directly to a voltage source, you always use a series current limiting resistor (typically 330 ohms). For these new RGB LED’s how many current limiting resistors will we need? We will need three . . . each color control pin will need its own current limiting resistor. So each color control pin will connect to an arduino output pin through its own current limiting resistor. Once that is hooked up, we can control what color the LED is by writing voltages from the arduino to specific legs of the RGB LED. If we write a voltage to the red pin, the LED will be red. If we write a voltage to the blue pin, the LED will be blue. Also the exciting thing is that if you write voltages to multiple pins, you can get the in between blended colors. Basically by analogWrite-ing different values to the 3 different control pins, you can get any imaginable color. But first, lets go ahead and get our circuit set up. The following schematic controls red from arduino pin 6, green from arduino pin 10 and blue from arduino pin 11. Go ahead and hook this circuit up.

Arduino circuit to control the color of an RGB LED.

Now lets play around with a program that will independently turn on the different colors. We will start simple so we can get an intuitive feel for how the LED works.

 With the code above, what color do you anticipate the LED will be? Hook up the circuit and type in the code, and see what happens. It is important that you type in the code. Do not cut and paste my code. You need to type it in. When you type it in, you will probably make mistakes and when you do you will have to troubleshoot or debug your code. That means you have to find your mistakes. All programmers make mistakes, and it is important very early on to learn how to find your mistakes.

Modify the code above so that the LED turns green.

Now modify it again so that the LED turns red.

Now try for the in between colors. How would you get the LED to turn orange? Play around with achieving different colors. Try to get the following colors:

Cyan

Magenta

Yellow

Orange

Purple