By long tradition (going back to 2006), the first Arduino sketch you write is to make an LED blink.
Arduino pins can be used for input and output, as long as you tell the computer which is which. So in this sketch, we tell the Arduino to set pin 13 to be the LED OUTPUT pin, and then we alternately send electricity to pin 13 (setting the pin HIGH) and cut off the electricity to pin 13 (setting the pin LOW). With each alternation, the LED turns on and off.
We’ll write all the sketches in this book using the Arduino “integrated development environment” (IDE), which, simply put, is special software for writing and uploading code to Arduino.
Download the Arduino IDE from http://arduino.cc/en/Main/Software, and follow the provided instructions to install it on your computer.
Once you’ve installed the software, open the IDE. You should see a screen that looks something like Figure 1-3.
The circuit portion of this project is very simple:
Take an LED and place the long lead into pin 13 on Arduino, as you can see in the Figure 1-4 breadboard view.
You can find this code in the Arduino IDE under File → Examples or on the EMWA GitHub Repository | chapter-1 | blink.
/* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is based on example code that is in the public domain. */ void setup() { // initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards: pinMode(13, OUTPUT); } void loop() { digitalWrite(13, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(13, LOW); // set the LED off delay(1000); // wait for a second }
In this sketch, the code in loop()
simply tells Arduino to set pin 13 HIGH—taking it up to 5 volts—for 1000 milliseconds (one second), followed by setting it LOW—taking it down to 0 volts—for another 1000 milliseconds.
Notice the /* ... */ sections and the // lines in the example above? Those are ways to put comments into your code to explain to others (and to yourself) what the code does: /* and */ tell the computer that everything between those marks should be ignored while running the program. // tells the computer that everything afterward on that line is a comment.
Get Environmental Monitoring with Arduino now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.