Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
52 lines (40 sloc) 2.81 KB
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
<div class="narrow-col">
<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 &lsquo;serial communication&rsquo; which means you can send one instruction at a time. This may sound slow &mdash; and it is &mdash; but it's the easiest way to learn how it works.</p>
<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="" title="Bare minimum Arduino code needed to get started" class="ext">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() {
pinMode(ledPin, OUTPUT);
void loop() {
if (Serial.available() > 0) {
char value =;
if (value == '1') {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
{% 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>
<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="" title="What does baud rate mean?" class="ext">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>
You can’t perform that action at this time.