Create a LED Dimmer

Move the mouse to change the brightness of an LED.

This example shows how to send data from a personal computer to an Arduino 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 Board

  • LED

  • 220 ohm resistor

Software Required

Circuit

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.

circuit

Schematic

schematic

Code

1/*
2
3 Dimmer
4
5 Demonstrates sending data from the computer to the Arduino board, in this case
6
7 to control the brightness of an LED. The data is sent in individual bytes,
8
9 each of which ranges from 0 to 255. Arduino reads these bytes and uses them to
10
11 set the brightness of the LED.
12
13 The circuit:
14
15 - LED attached from digital pin 9 to ground.
16
17 - Serial connection to Processing, Max/MSP, or another serial application
18
19 created 2006
20
21 by David A. Mellis
22
23 modified 30 Aug 2011
24
25 by Tom Igoe and Scott Fitzgerald
26
27 This example code is in the public domain.
28
29 https://www.arduino.cc/en/Tutorial/Dimmer
30
31*/
32
33const int ledPin = 9; // the pin that the LED is attached to
34
35void setup() {
36
37 // initialize the serial communication:
38
39 Serial.begin(9600);
40
41 // initialize the ledPin as an output:
42
43 pinMode(ledPin, OUTPUT);
44}
45
46void loop() {
47
48 byte brightness;
49
50 // check if data has been sent from the computer:
51
52 if (Serial.available()) {
53
54 // read the most recent byte (which will be from 0 to 255):
55
56 brightness = Serial.read();
57
58 // set the brightness of the LED:
59
60 analogWrite(ledPin, brightness);
61
62 }
63}
64
65/* Processing code for this example
66
67 // Dimmer - sends bytes over a serial port
68
69 // by David A. Mellis
70
71 // This example code is in the public domain.
72
73 import processing.serial.*;
74
75 Serial port;
76
77 void setup() {
78
79 size(256, 150);
80
81 println("Available serial ports:");
82
83 // if using Processing 2.1 or later, use Serial.printArray()
84
85 println(Serial.list());
86
87 // Uses the first port in this list (number 0). Change this to select the port
88
89 // corresponding to your Arduino board. The last parameter (e.g. 9600) is the
90
91 // speed of the communication. It has to correspond to the value passed to
92
93 // Serial.begin() in your Arduino sketch.
94
95 port = new Serial(this, Serial.list()[0], 9600);
96
97 // If you know the name of the port used by the Arduino board, you can specify
98
99 // it directly like this.
100
101 //port = new Serial(this, "COM1", 9600);
102
103 }
104
105 void draw() {
106
107 // draw a gradient from black to white
108
109 for (int i = 0; i < 256; i++) {
110
111 stroke(i);
112
113 line(i, 0, i, 150);
114
115 }
116
117 // write the current X-position of the mouse to the serial port as
118
119 // a single byte
120
121 port.write(mouseX);
122
123 }
124
125*/
126
127/* Max/MSP v5 patch for this example
128
129----------begin_max5_patcher----------
130
1311008.3ocuXszaiaCD9r8uhA5rqAeHIa0aAMaAVf1S6hdoYQAsDiL6JQZHQ2M
132
133YWr+2KeX4vjnjXKKkKhhiGQ9MeyCNz+X9rnMp63sQvuB+MLa1OlOalSjUvrC
134
135ymEUytKuh05TKJWUWyk5nE9eSyuS6jesvHu4F4MxOuUzB6X57sPKWVzBLXiP
136
137xZtGj6q2vafaaT0.BzJfjj.p8ZPukazsQvpfcpFs8mXR3plh8BoBxURIOWyK
138
139rxspZ0YI.eTCEh5Vqp+wGtFXZMKe6CZc3yWZwTdCmYW.BBkdiby8v0r+ST.W
140
141sD9SdUkn8FYspPbqvnBNFtZWiUyLmleJWo0vuKzeuj2vpJLaWA7YiE7wREui
142
143FpDFDp1KcbAFcP5sJoVxp4NB5Jq40ougIDxJt1wo3GDZHiNocKhiIExx+owv
144
145AdOEAksDs.RRrOoww1Arc.9RvN2J9tamwjkcqknvAE0l+8WnjHqreNet8whK
146
147z6mukIK4d+Xknv3jstvJs8EirMMhxsZIusET25jXbX8xczIl5xPVxhPcTGFu
148
149xNDu9rXtUCg37g9Q8Yc+EuofIYmg8QdkPCrOnXsaHwYs3rWx9PGsO+pqueG2
150
151uNQBqWFh1X7qQG+3.VHcHrfO1nyR2TlqpTM9MDsLKNCQVz6KO.+Sfc5j1Ykj
152
153jzkn2jwNDRP7LVb3d9LtoWBAOnvB92Le6yRmZ4UF7YpQhiFi7A5Ka8zXhKdA
154
1554r9TRGG7V4COiSbAJKdXrWNhhF0hNUh7uBa4Mba0l7JUK+omjDMwkSn95Izr
156
157TOwkdp7W.oPRmNRQsiKeu4j3CkfVgt.NYPEYqMGvvJ48vIlPiyzrIuZskWIS
158
159xGJPcmPiWOfLodybH3wjPbMYwlbFIMNHPHFOtLBNaLSa9sGk1TxMzCX5KTa6
160
161WIH2ocxSdngM0QPqFRxyPHFsprrhGc9Gy9xoBjz0NWdR2yW9DUa2F85jG2v9
162
163FgTO4Q8qiC7fzzQNpmNpsY3BrYPVJBMJQ1uVmoItRhw9NrVGO3NMNzYZ+zS7
164
1653WTvTOnUydG5kHMKLqAOjTe7fN2bGSxOZDkMrBrGQ9J1gONBEy0k4gVo8qHc
166
167cxmfxVihWz6a3yqY9NazzUYkua9UnynadOtogW.JfsVGRVNEbWF8I+eHtcwJ
168
169+wLXqZeSdWLo+FQF6731Tva0BISKTx.cLwmgJsUTTvkg1YsnXmxDge.CDR7x
170
171D6YmX6fMznaF7kdczmJXwm.XSOOrdoHhNA7GMiZYLZZR.+4lconMaJP6JOZ8
172
173ftCs1YWHZI3o.sIXezX5ihMSuXzZtk3ai1mXRSczoCS32hAydeyXNEu5SHyS
174
175xqZqbd3ZLdera1iPqYxOm++v7SUSz
176
177-----------end_max5_patcher-----------
178
179*/

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.

maxDimmer

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.