Experiment Guide for the SparkFun Tinker Kit

This Tutorial is Retired!

This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.

View the updated tutorial: Activity Guide for SparkFun Tinker Kit

Pages
Contributors: D___Run___
Favorited Favorite 1

Experiment 11: Reading Serial Data

Introduction

In Experiment 3 you used an RGB LED to create a rainbow of fun. The problem is that, to define colors, you had to change your Arduino code. You have also used the Serial object in Arduino to print out data to your computer using Serial.print(); and Serial.println(). In this experiment you will send serial data the other direction – to the RedBoard! What data will you be sending? Comma-separated RGB values to change the color of your RGB, of course!

Let’s see what pot of gold lies on the other end of this data rainbow!

Parts Needed

You will need the following parts:

  • 1x Breadboard
  • 1x SparkFun RedBoard
  • 1x Common Cathode RGB LED
  • 3x 330Ω Resistors
  • 6x Jumper Wires

Didn’t Get the Tinker Kit?

If you are conducting this experiment and didn’t get the Tinker Kit, we suggest using these parts:

Breadboard - Self-Adhesive (White)

Breadboard - Self-Adhesive (White)

PRT-12002
$4.95
37
Jumper Wires - Connected 6" (M/M, 20 pack)

Jumper Wires - Connected 6" (M/M, 20 pack)

PRT-12795
$1.95
2
LED - RGB Clear Common Cathode

LED - RGB Clear Common Cathode

COM-00105
$2.05
2
Resistor 330 Ohm 1/6 Watt PTH - 20 pack

Resistor 330 Ohm 1/6 Watt PTH - 20 pack

COM-11507
$0.95
2

SparkFun RedBoard - Programmed with Arduino

DEV-12757
127 Retired

Hardware Hookup

Ready to start hooking everything up? Check out the wiring diagram and hookup table below to see how everything is connected.

Pay special attention to the component’s markings indicating how to place it on the breadboard. Polarized components can only be connected to a circuit in one direction.

Wiring Diagram for the Experiment

alt text

Having a hard time seeing the circuit? Click on the wiring diagram for a closer look.

Open the Sketch

Open the Arduino IDE software on your computer. Coding in the Arduino language will control your circuit.

You can type out or copy and paste the following code into the Arduino IDE. Hit upload, and see what happens!

language:cpp
/*
SparkFun Tinker Kit 
Example sketch 11

Serial Color Mixing

Read Serial data from your computer and use it to set 
the RGB values of the RGB LED.


This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about Arduino.
*/

//create variables for pin numbers. We are making them constants here, because they       //never change. 
const int RED_PIN = 5;
const int GREEN_PIN = 6;
const int BLUE_PIN = 9;

// How fast we plan to cycle through colors in milliseconds
int redVal = 0;
int greenVal= 0;
int blueVal = 0;


void setup()
{
//set the three pin variables as outputs
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);

//Start the Serial port at 9600 baud rate
Serial.begin(9600);
Serial.println("Please enter your RGB in CSV format(Example: 255,100,0)");
}


void loop()
{
analogWrite(RED_PIN, redVal);
analogWrite(GREEN_PIN, greenVal);
analogWrite(BLUE_PIN, blueVal);

if(Serial.available()>0)
 {
  redVal = Serial.parseInt();
  greenVal = Serial.parseInt();
  blueVal = Serial.parseInt();
 }
}

Code to Note

language:cpp
Serial.begin(9600);

Whether you are using serial communication as an input or an output, you need to use the begin() method to start your serial port. The baud rate can vary, but 9600 is the standard for most applications.

language:cpp
Serial.parseInt();

There are a number of ways to read and parse data coming in from the serial port. The simplest way is to format your data coming in as a Comma-Separated Value (CSV) string. In this format the parseInt() method captures the data as it comes in. Once the there is a non alpha-numeric character (a character that is not a letter or a number) parseInt() will stop capturing the value as an integer. As an example if we were to send the string 123,456,789 through parseInt() it would return the value of 123 because it would stop capturing at the comma (,).

In this experiment we use the parseInt() method three times, one right after the other, to capture the three comma-separated RGB values and place them in the three color variables.

What You Should See

You should see nothing at first when your upload is complete. Open up the Serial Monitor (The magnifying glass icon in the upper right-hand corner of the Arduino IDE). You should see the text “Please enter your RGB in CSV format(Example: 255,100,0)” print out in the serial monitor.

Enter an RGB value, (we are using 255,100,0 as an example) in the text entry area as shown below and click ‘Send’. Once you click the send button your RGB should turn a purple color. Congrats! You just sent data to your RedBoard from your computer!

Tinker Kit Circuit 11

Troubleshooting

LED Remains Dark or Shows Incorrect Color

With the four pins of the LED so close together, it’s sometimes easy to misplace one. Double check each pin is where it should be.

You are Sending Data but Nothing Happens

Make sure you are sending your data as integers between 0 and 255 in a comma-separated format.