This example shows how to send data from a personal computer to an Arduino or Genuino board to control the brightness of an LED. The data is sent in individual bytes, each of which ranges in value from 0 to 255. The sketch reads these bytes and uses them to set the brightness of the LED.

You can send bytes to the board from any software that can access the computer serial port. Examples for Processing and Max/MSP version 5 are shown below.

Hardware Required

  • Arduino or Genuino Board

  • LED

  • 220 ohm resistor

Software Required


Connect the 220 ohm current limiting resistor to digital pin 9, with an LED in series. The long, positive leg (the anode) of the LED should be connected to the output from the resistor, with the shorter, negative leg (the cathode) connected to ground.

click the image to enlarge

dimmer circuit3

image developed using Fritzing. For more circuit examples, see the Fritzing project page


click the image to enlarge

simplefade pin9 schem




  Demonstrates sending data from the computer to the Arduino board, in this case

  to control the brightness of an LED. The data is sent in individual bytes,

  each of which ranges from 0 to 255. Arduino reads these bytes and uses them to

  set the brightness of the LED.

  The circuit:

  - LED attached from digital pin 9 to ground.

  - Serial connection to Processing, Max/MSP, or another serial application

  created 2006

  by David A. Mellis

  modified 30 Aug 2011

  by Tom Igoe and Scott Fitzgerald

  This example code is in the public domain.



const int ledPin = 9;      // the pin that the LED is attached to

void setup() {

  // initialize the serial communication:


  // initialize the ledPin as an output:

  pinMode(ledPin, OUTPUT);

void loop() {

  byte brightness;

  // check if data has been sent from the computer:

  if (Serial.available()) {

    // read the most recent byte (which will be from 0 to 255):

    brightness = Serial.read();

    // set the brightness of the LED:

    analogWrite(ledPin, brightness);


/* Processing code for this example

  // Dimmer - sends bytes over a serial port

  // by David A. Mellis

  // This example code is in the public domain.

  import processing.serial.*;

  Serial port;

  void setup() {

    size(256, 150);

    println("Available serial ports:");

    // if using Processing 2.1 or later, use Serial.printArray()


    // Uses the first port in this list (number 0). Change this to select the port

    // corresponding to your Arduino board. The last parameter (e.g. 9600) is the

    // speed of the communication. It has to correspond to the value passed to

    // Serial.begin() in your Arduino sketch.

    port = new Serial(this, Serial.list()[0], 9600);

    // If you know the name of the port used by the Arduino board, you can specify

    // it directly like this.

    //port = new Serial(this, "COM1", 9600);


  void draw() {

    // draw a gradient from black to white

    for (int i = 0; i < 256; i++) {


      line(i, 0, i, 150);


    // write the current X-position of the mouse to the serial port as

    // a single byte




/* Max/MSP v5 patch for this example



























Processing Code

The Processing sketch in the code sample above will send bytes out the computer serial port to the board to dim the LED.

Max code

The Max/MSP patch in the code sample above looks like the image below. Copy it and paste it into a new patch window.


See Also:

  • serial()

  • serial.read()

  • analogRead()

  • analogWrite()

  • ASCIITable - Demonstrates Arduino's advanced serial output functions.

  • Graph - Send data to the computer and graph it in Processing.

  • Midi - Send MIDI note messages serially.

  • MultiSerialMega - Use two of the serial ports available on the Arduino and Genuino Mega.

  • PhysicalPixel - Turn a LED on and off by sending data to your board from Processing or Max/MSP.

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

  • SerialCallResponse - Send multiple variables using a call-and-response (handshaking) method.

  • SerialCallResponseASCII - Send multiple variables using a call-and-response (handshaking) method, and ASCII-encode the values before sending.

  • SerialEvent - Demonstrates the use of serialEvent().

  • VirtualColorMixer - Send multiple variables from Arduino to your computer and read them in Processing or Max/MSP.

Last revision 2015/07/29 by SM