/
2011-02-13-send-data-to-arduino.html
50 lines (39 loc) · 2.82 KB
/
2011-02-13-send-data-to-arduino.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
---
layout: post
title: Send data from a computer to an Arduino
date: 2011-02-13 22:48:29
updated: 2011-03-01 21:32:15
description: Most tutorials will explain how to send data from an Arduino to a computer, what about the opposite?
---
<p>I’m a programmer, not an electrical engineer. I bought an Arduino to learn how to connect the physical world with the computing one where I know my way around. I hope to post a few howtos when I learn something new.</p>
<h2>Serial Communication</h2>
<p>Most tutorials will explain how to send data from an Arduino (or a connected sensor) to a computer. I want the opposite: send data from my computer to an Arduino Uno board. Talking with an Arduino over a <abbr title="Universal Serial Bus">USB</abbr> cable is called ‘serial communication’ which means you can send one instruction at a time. This may sound slow — and it is — but it's the easiest way to learn how it works.</p>
<h2>Sketch</h2>
<p>Connect the Arduino with your computer using a <abbr title="Universal Serial Bus">USB</abbr> cable and open the editor (read the <a href="http://arduino.cc/en/Tutorial/BareMinimum" title="Bare minimum Arduino code needed to get started">Bare Minimum</a> tutorial if you don't know how). Now load the sketch below and open the Serial Monitor (last button in the Arduino editor toolbar).</p>
{% highlight c++ %}
int ledPin = 13;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
char value = Serial.read();
if (value == '1') {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
Serial.println(value);
}
delay(1000);
}
{% endhighlight %}
<p>Using the input field in the Serial Monitor you can send a 1 to the Arduino board to turn the built-in <abbr title="Light-Emitting Diode">LED</abbr> on, anything else will turn it off. The Serial Monitor displays the value the Arduino sent back (the one it got as the input).</p>
<h2>How?</h2>
<ol>
<li>The first line specifies the <abbr title="Light-Emitting Diode">LED</abbr> we want to use, pin 13 is the built-in <abbr title="Light-Emitting Diode">LED</abbr> on an Arduino Uno, change this if you use another board.</li>
<li>The Serial.begin line tells the Arduino what <a href="http://www.ladyada.net/learn/arduino/lesson4.html" title="What does baud rate mean?">baud rate</a> to use.</li>
<li>The loop function will look if there is some input waiting and if so read it in the <em>value</em> variable (only one char at the time). If the value is 1 it will turn the <abbr title="Light-Emitting Diode">LED</abbr> on, if not it will turn it off.</li>
<li>The Serial.print sends the input back to the computer (optional but nice to see what the Arduino received) and wait for 1 second before checking for new input.</li>
</ol>