Hardware Reference
In-Depth Information
Try It
/* Blink
Context: Arduino
Here's your first program.
Blinks an LED attached to pin 13 every half second.
Connections:
Pin 13: + leg of an LED (- leg goes to ground)
*/
int LEDPin = 13;
void setup() {
pinMode(LEDPin, OUTPUT); // set pin 13 to be an output
}
void loop() {
digitalWrite(LEDPin, HIGH); // turn the LED on pin 13 on
delay(500); // wait half a second
digitalWrite(LEDPin, LOW); // turn the LED off
delay(500); // wait half a second
}
In order to see this run, you'll need to connect an
LED from pin 13 of the board to ground (GND),
as shown in Figure 1-13. The positive (long) end
of the LED should go to 13, and the short end to ground.
Binary sketch size: 1010 bytes (of a 32256 byte maximum)
Once the sketch is uploaded, the LED you wired to the
output pin will begin to blink. That's the microcontroller
equivalent of “Hello World!”
Then type the code into the editor. Click on Tools Board
to choose your Arduino model, and then Tools Serial
Port to choose the serial port of the Arduino module. On
the Mac or Linux, the serial port will have a name like this:
/dev/tty.usbmodem241241 . If it's an older board or a
Wiring board, it will be more like this: /dev/tty.usbserial-
1B1 (the letters and numbers after the dash will be slightly
different each time you connect it). On Windows, it should
be COM x , where x is some number (for example, COM5).
NOTE: If it doesn't work, you might want to seek out some external
help. The Arduino Learning section has many tutorials ( www.arduino.
cc/en/Tutorial ). The Arduino ( www.arduino.cc/forum ) and Wiring
( http://forum.wiring.co ) forums are full of helpful people who love
to hack these sort of things.
Serial communication
One of the most frequent tasks you'll use a microcontroller
for in this topic is to communicate serially with another
device, either to send sensor readings over a network or
to receive commands to control motors, lights, or other
outputs from the microcontroller. Regardless of what
device you're communicating with, the commands you'll
use in your microcontroller program will be the same. First,
you'll configure the serial connection for the right data
rate. Then, you'll read bytes in, write bytes out, or both,
depending on what device you're talking to and how the
conversation is structured.
NOTE: On Windows, COM1-COM4 are generally reserved for
built-in serial ports, regardless of whether your computer has
them.
Once you've selected the port and model, click Verify
to compile your code. When it's compiled, you'll get
a message at the bottom of the window saying Done
compiling . Then click Upload. This will take a few
seconds. Once it's done, you'll get a message saying
Done uploading , and a confirmation message in the serial
monitor window that says:
 
Search WWH ::




Custom Search