Read ASCII String

Parse a comma-separated string of integers to fade an LED.

This sketch uses the Serial.parseInt() function to locate values separated by a non-alphanumeric character. Often people use a comma to indicate different pieces of information (this format is commonly referred to as comma-separated-values or CSV), but other characters like a space or a period will work too. The values are parsed into integers and used to determine the color of a RGB LED. You'll use the Arduino Software (IDE) serial monitor to send strings like "5,220,70" to the board to change the light color.

Hardware Required

  • Arduino Board

  • common anode RGB LED

  • 3 220 ohm resistors

  • hook-up wires

  • breadboard

Circuit

circuit

Schematic

schematic

You'll need four wires to make the circuit above. A wire connects the 5V from the POWER connector of the board to the longest pin of the RGB LED. You should turn the LED so that the longest pin is the second from the left..

Place the RGB LED on your breadboard with the longest pin as the second from the top. Check the datasheet for your specific LED to verify the pins, but they should be R, V+, G and B. The wire from 5V should therefore connect that second pin from top, as in the connection scheme above.

With your remaining wires, connect your red cathode to pin 3, green cathode to pin 5, and blue cathode to pin 6 in series with the resistors.

RGB LEDs with a common anode share a common power pin. Instead of turning a pin HIGH to illuminate the LED, you need to turn the pin LOW, to create a voltage difference across the diode. So sending 255 via analogWrite() turns the LED off, while a value of 0 turns it on at full brightness. In the code below, you'll use a little bit of math on the sketch side, so you can send values which correspond to the expected brightness. Essentially, instead of using analogWrite(pin, brightness), you'll be calling analogWrite(pin, 255-brightness).

Code

You'll first set up some global variables for the pins your LED will connect to. This will make it easier to differentiate which one is red, green, and blue in the main part of your program:

1const int redPin = 3;
2const int greenPin = 5;
3const int bluePin = 6;

In your setup(), begin serial communication at 9600 bits of data per second between the board and your computer with the line:

1Serial.begin(9600);

Also in the setup, you'll want to configure the pins as outputs:

1pinMode(redPin, OUTPUT);
2pinMode(greenPin, OUTPUT);
3pinMode(bluePin, OUTPUT);

In the loop(), check to see if there is any data in the serial buffer. By making this a while() statement, it will run as long as there is information waiting to be read :

1while (Serial.available() > 0) {

Next, declare some local variables for storing the serial information. This will be the brightness of the LEDs. Using Serial.parseInt() to separate the data by commas, read the information into your variables:

1int red = Serial.parseInt();
2int green = Serial.parseInt();
3int blue = Serial.parseInt();

Once you've read the data into your variables, check for the newline character to proceed:

1if (Serial.read() == '\n') {

Using constrain(), you can keep the values in an acceptable range for PWM control. This way, if the value was outside the range of what PWM can send, it will be limited to a valid number. By subtracting this value from 255 you will be formatting the value to use with a common anode LED. As explained above, these LEDs will illuminate when there is a voltage difference between the anode and the pin connected to the board:

1red = 255 - constrain(red, 0, 255);
2green = 255 - constrain(green, 0, 255);
3blue = 255 - constrain(blue, 0, 255);

Now that you have formatted the values for PWM, use analogWrite() to change the color of the LED. Because you subtracted your value from 255 in the step above:

1analogWrite(redPin, red);
2analogWrite(greenPin, green);
3analogWrite(bluePin, blue);

Send the value of each LED back to the serial monitor in one string as HEX values :

1Serial.print(red, HEX);
2Serial.print(green, HEX);
3Serial.println(blue, HEX);

Finally, close up your brackets from the if statement, while statement, and main loop :

1}
2}
3}

Once you have programmed the board, open your Arduino Software (IDE) serial monitor. Make sure you have chosen to send a newline character when sending a message. Enter values between 0-255 for the lights in the following format : Red,Green,Blue. Once you have sent the values to the board, the attached LED will turn into the color you specified and you will receive back the HEX values in the serial monitor.

1/*
2
3 Reading a serial ASCII-encoded string.
4
5 This sketch demonstrates the Serial parseInt() function.
6
7 It looks for an ASCII string of comma-separated values.
8
9 It parses them into ints, and uses those to fade an RGB LED.
10
11 Circuit: Common-Cathode RGB LED wired like so:
12
13 - red anode: digital pin 3
14
15 - green anode: digital pin 5
16
17 - blue anode: digital pin 6
18
19 - cathode: GND
20
21 created 13 Apr 2012
22
23 by Tom Igoe
24
25 modified 14 Mar 2016
26
27 by Arturo Guadalupi
28
29 This example code is in the public domain.
30
31*/
32
33// pins for the LEDs:
34
35const int redPin = 3;
36
37const int greenPin = 5;
38
39const int bluePin = 6;
40
41void setup() {
42
43 // initialize serial:
44
45 Serial.begin(9600);
46
47 // make the pins outputs:
48
49 pinMode(redPin, OUTPUT);
50
51 pinMode(greenPin, OUTPUT);
52
53 pinMode(bluePin, OUTPUT);
54
55}
56
57void loop() {
58
59 // if there's any serial available, read it:
60
61 while (Serial.available() > 0) {
62
63 // look for the next valid integer in the incoming serial stream:
64
65 int red = Serial.parseInt();
66
67 // do it again:
68
69 int green = Serial.parseInt();
70
71 // do it again:
72
73 int blue = Serial.parseInt();
74
75 // look for the newline. That's the end of your sentence:
76
77 if (Serial.read() == '\n') {
78
79 // constrain the values to 0 - 255 and invert
80
81 // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
82
83 red = 255 - constrain(red, 0, 255);
84
85 green = 255 - constrain(green, 0, 255);
86
87 blue = 255 - constrain(blue, 0, 255);
88
89 // fade the red, green, and blue legs of the LED:
90
91 analogWrite(redPin, red);
92
93 analogWrite(greenPin, green);
94
95 analogWrite(bluePin, blue);
96
97 // print the three numbers in one string as hexadecimal:
98
99 Serial.print(red, HEX);
100
101 Serial.print(green, HEX);
102
103 Serial.println(blue, HEX);
104
105 }
106
107 }
108}

Learn more

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last revision 2015/07/29 by SM

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.