Physical Pixel

Turn a LED on and off by sending data to your Arduino from Processing or Max/MSP.

This example example uses the Arduino board to receive data from the computer. The board turns on an LED when it receives the character 'H', and turns off the LED when it receives the character 'L'.

The data can be sent from the Arduino Software (IDE) serial monitor, or another program like Processing (see code below), Flash (via a serial-net proxy), PD, or Max/MSP.

Hardware Required

  • Arduino Board

  • LED (optional)

  • 220 ohm resistor (optional)

Software Required

Circuit

Many Arduino boards have a built-in LED connected to pin 13; if your board has no built-in LED, attach an external LED to pin 13. The long leg, or anode, goes to pin 13 through a 220 resistor. The short leg, or cathode, goes to ground.

circuit

Schematic

schematic

Code

1/*
2
3 Physical Pixel
4
5 An example of using the Arduino board to receive data from the computer. In
6
7 this case, the Arduino boards turns on an LED when it receives the character
8
9 'H', and turns off the LED when it receives the character 'L'.
10
11 The data can be sent from the Arduino Serial Monitor, or another program like
12
13 Processing (see code below), Flash (via a serial-net proxy), PD, or Max/MSP.
14
15 The circuit:
16
17 - LED connected from digital pin 13 to ground
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/PhysicalPixel
30
31*/
32
33const int ledPin = 13; // the pin that the LED is attached to
34int incomingByte; // a variable to read incoming serial data into
35
36void setup() {
37
38 // initialize serial communication:
39
40 Serial.begin(9600);
41
42 // initialize the LED pin as an output:
43
44 pinMode(ledPin, OUTPUT);
45}
46
47void loop() {
48
49 // see if there's incoming serial data:
50
51 if (Serial.available() > 0) {
52
53 // read the oldest byte in the serial buffer:
54
55 incomingByte = Serial.read();
56
57 // if it's a capital H (ASCII 72), turn on the LED:
58
59 if (incomingByte == 'H') {
60
61 digitalWrite(ledPin, HIGH);
62
63 }
64
65 // if it's an L (ASCII 76) turn off the LED:
66
67 if (incomingByte == 'L') {
68
69 digitalWrite(ledPin, LOW);
70
71 }
72
73 }
74}
75
76/* Processing code for this example
77
78 // Mouse over serial
79
80 // Demonstrates how to send data to the Arduino I/O board, in order to turn ON
81
82 // a light if the mouse is over a square and turn it off if the mouse is not.
83
84 // created 2003-4
85
86 // based on examples by Casey Reas and Hernando Barragan
87
88 // modified 30 Aug 2011
89
90 // by Tom Igoe
91
92 // This example code is in the public domain.
93
94 import processing.serial.*;
95
96 float boxX;
97
98 float boxY;
99
100 int boxSize = 20;
101
102 boolean mouseOverBox = false;
103
104 Serial port;
105
106 void setup() {
107
108 size(200, 200);
109
110 boxX = width / 2.0;
111
112 boxY = height / 2.0;
113
114 rectMode(RADIUS);
115
116 // List all the available serial ports in the output pane.
117
118 // You will need to choose the port that the Arduino board is connected to
119
120 // from this list. The first port in the list is port #0 and the third port
121
122 // in the list is port #2.
123
124 // if using Processing 2.1 or later, use Serial.printArray()
125
126 println(Serial.list());
127
128 // Open the port that the Arduino board is connected to (in this case #0)
129
130 // Make sure to open the port at the same speed Arduino is using (9600bps)
131
132 port = new Serial(this, Serial.list()[0], 9600);
133
134 }
135
136 void draw() {
137
138 background(0);
139
140 // Test if the cursor is over the box
141
142 if (mouseX > boxX - boxSize && mouseX < boxX + boxSize &&
143
144 mouseY > boxY - boxSize && mouseY < boxY + boxSize) {
145
146 mouseOverBox = true;
147
148 // draw a line around the box and change its color:
149
150 stroke(255);
151
152 fill(153);
153
154 // send an 'H' to indicate mouse is over square:
155
156 port.write('H');
157
158 }
159
160 else {
161
162 // return the box to its inactive state:
163
164 stroke(153);
165
166 fill(153);
167
168 // send an 'L' to turn the LED off:
169
170 port.write('L');
171
172 mouseOverBox = false;
173
174 }
175
176 // Draw the box
177
178 rect(boxX, boxY, boxSize, boxSize);
179
180 }
181
182*/
183
184/* Max/MSP version 5 patch to run with this example:
185
186----------begin_max5_patcher----------
187
1881672.3oc2ZszaaiCD9ryuBBebQVCQRYao8xhf1cQCPVfBzh8RRQ.sDsM2HSZ
189
190HQmlzh9eu7gjsjsEk7y0oWjiHoHm4aluYHGlueUmtiDuPy5B9Cv8fNc99Uc5
191
192XZR2Pm726zcF4knDRlYXciDylQ4xtWa6SReQZZ+iSeMiEQR.ej8BM4A9C7OO
193
194kkAlSjQSAYTdbFfvA27o2c6sfO.Doqd6NfXgDHmRUCKkolg4hT06BfbQJGH3
195
1965Qd2e8d.QJIQSow5tzebZ7BFW.FIHow8.2JAQpVIIYByxo9KIMkSjL9D0BRT
197
198sbGHZJIkDoZOSMuQT.8YZ5qpgGI3locF4IpQRzq2nDF+odZMIJkRjpEF44M3
199
200A9nWAum7LKFbSOv+PSRXYOvmIhYiYpg.8A2LOUOxPyH+TjPJA+MS9sIzTRRr
201
202QP9rXF31IBZAHpVHkHrfaPRHLuUCzoj9GSoQRqIB52y6Z.tu8o4EX+fddfuj
203
204+MrXiwPL5+9cXwrOVvkbxLpomazHbQO7EyX7DpzXYgkFdF6algCQpkX4XUlo
205
206hA6oa7GWck9w0Gnmy6RXQOoQeCfWwlzsdnHLTq8n9PCHLv7Cxa6PAN3RCKjh
207
208ISRVZ+sSl704Tqt0kocE9R8J+P+RJOZ4ysp6gN0vppBbOTEN8qp0YCq5bq47
209
210PUwfA5e766z7NbGMuncw7VgNRSyQhbnPMGrDsGaFSvKM5NcWoIVdZn44.eOi
211
2129DTRUT.7jDQzSTiF4UzXLc7tLGh4T9pwaFQkGUGIiOOkpBSJUwGsBd40krHQ
213
2149XEvwq2V6eLIhV6GuzP7uzzXBmzsXPSRYwBtVLp7s5lKVv6UN2VW7xRtYDbx
215
2167s7wRgHYDI8YVFaTBshkP49R3rYpH3RlUhTQmK5jMadJyF3cYaTNQMGSyhRE
217
218IIUlJaOOukdhoOyhnekEKmZlqU3UkLrk7bpPrpztKBVUR1uorLddk6xIOqNt
219
220lBOroRrNVFJGLrDxudpET4kzkstNp2lzuUHVMgk5TDZx9GWumnoQTbhXsEtF
221
222tzCcM+z0QKXsngCUtTOEIN0SX2iHTTIIz968.Kf.uhfzUCUuAd3UKd.OKt.N
223
224HTynxTQyjpQD9jlwEXeKQxfHCBahUge6RprSa2V4m3aYOMyaP6gah2Yf1zbD
225
226jVwZVGFZHHxINFxpjr5CiTS9JiZn6e6nTlXQZTAFj6QCppQwzL0AxVtoi6WE
227
228QXsANkEGWMEuwNvhmKTnat7A9RqLq6pXuEwY6xM5xRraoTiurj51J1vKLzFs
229
230CvM7HI14Mpje6YRxHOSieTsJpvJORjxT1nERK6s7YTN7sr6rylNwf5zMiHI4
231
232meZ4rTYt2PpVettZERbjJ6PjfqN2loPSrUcusH01CegsGEE5467rnCdqT1ES
233
234QxtCvFq.cvGz+BaAHXKzRSfP+2Jf.KCvj5ZLJRAhwi+SWHvPyN3vXiaPn6JR
235
2363eoA.0TkFhTvpsDMIrL20nAkCI4EoYfSHAuiPBdmJRyd.IynYYjIzMvjOTKf
237
2383DLvnvRLDLpWeEOYXMfAZqfQ0.qsnlUdmA33t8CNJ7MZEb.u7fiZHLYzDkJp
239
240R7CqEVLGN75U+1JXxFUY.xEEBcRCqhOEkz2bENEWnh4pbh0wY25EefbD6EmW
241
242UA6Ip8wFLyuFXx+Wrp8m6iff1B86W7bqJO9+mx8er4E3.abCLrYdA16sBuHx
243
244vKT6BlpIGQIhL55W7oicf3ayv3ixQCm4aQuY1HZUPQWY+cASx2WZ3f1fICuz
245
246vj5R5ZbM1y8gXYN4dIXaYGq4NhQvS5MmcDADy+S.j8CQ78vk7Q7gtPDX3kFh
247
2483NGaAsYBUAO.8N1U4WKycxbQdrWxJdXd10gNIO+hkUMmm.CZwknu7JbNUYUq
249
2500sOsTsI1QudDtjw0t+xZ85wWZd80tMCiiMADNX4UzrcSeK23su87IANqmA7j
251
252tiRzoXi2YRh67ldAk79gPmTe3YKuoY0qdEDV3X8xylCJMTN45JIakB7uY8XW
253
254uVr3PO8wWwEoTW8lsfraX7ZqzZDDXCRqNkztHsGCYpIDDAOqxDpMVUMKcOrp
255
256942acPvx2NPocMC1wQZ8glRn3myTykVaEUNLoEeJjVaAevA4EAZnsNgkeyO+
257
2583rEZB7f0DTazDcQTNmdt8aACGi1QOWnMmd+.6YjMHH19OB5gKsMF877x8wsJ
259
260hN97JSnSfLUXGUoj6ujWXd6Pk1SAC+Pkogm.tZ.1lX1qL.pe6PE11DPeMMZ2
261
262.P0K+3peBt3NskC
263
264-----------end_max5_patcher-----------
265
266*/

Processing Code

Copy the Processing code from the code sample above. As you mouse over the center square, the LED on pin 13 should turn on and off. The Processing applet looks like this:

physicalPixel output

Mouse over the square to turn the LED on and off.

Max patch

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

max physicalPixel

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.