EasyManuals Logo

Arduino uno User Manual

Arduino uno
311 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #49 background imageLoading...
Page #49 background image
For uploading software, the Arduino has a serial port, and you can use it to
connect the Arduino to other devices, too. (In Compiling and Uploading Pro-
grams, on page 19, you learned how to look up the serial port your Arduino
is connected to.) In this section, youll use the serial port to control Arduinos
status LED using your computers keyboard. The LED should be turned on
when you press 1, and it should be turned off when you press 2. Heres all
the code you need:
Welcome/LedSwitch/LedSwitch.ino
const unsigned int LED_PIN = 13;
Line 1
const unsigned int BAUD_RATE = 9600;
-
-
void setup() {
-
pinMode(LED_PIN, OUTPUT);
5
Serial.begin(BAUD_RATE);
-
}
-
-
void loop() {
-
if (Serial.available() > 0) {
10
int command = Serial.read();
-
if (command == '1') {
-
digitalWrite(LED_PIN, HIGH);
-
Serial.println("LED on");
-
} else if (command == '2') {
15
digitalWrite(LED_PIN, LOW);
-
Serial.println("LED off");
-
} else {
-
Serial.print("Unknown command: ");
-
Serial.println(command);
20
}
-
}
-
}
-
As in our previous examples, we define a constant for the pin the LED is
connected to and set it to
OUTPUT
mode in the
setup
function. In line 6, we ini-
tialize the serial port using the
begin
function of the
Serial
class, passing a baud
rate of 9600. (You can learn what a baud rate is in Learning More About
Serial Communication, on page 253.) Thats all we need to send and receive
data via the serial port in our program.
So, lets read and interpret the data. The
loop
function starts by calling
Serial
s
available
method in line 10.
available
returns the number of bytes waiting on the
serial port. If any data is available, we read it using
Serial.read
.
read
returns the
first byte of incoming data if data is available and -1 otherwise.
If the byte we have read represents the character 1, we switch on the LED
and send back the message LED on over the serial port. We use
Serial.println
,
report erratum discuss
Using Serial Ports 29
www.it-ebooks.info

Table of Contents

Other manuals for Arduino uno

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Arduino uno and is the answer not in the manual?

Arduino uno Specifications

General IconGeneral
Form factorArduino
CertificationRoHS, FC, CE
Processor model-
Processor frequency- MHz
Microcontroller modelATmega328
Microcontroller frequency16 MHz
DC input voltage7-12 V
Operating voltage5 V
DC current per I/O pin40 mA
Flash memory0.032 MB
Maximum internal memory- GB
SRAM (Static Random Access Memory)2 KB
EEPROM (Electrically Erasable Programmable Read-Only Memory)1 KB
Wi-FiNo
Number of analog I/O pins6
Number of digital I/O pins14
Weight and Dimensions IconWeight and Dimensions
Board dimensions53.4 x 68.6 mm

Related product manuals