Ship sinking and all you have is an Arduino? Make your Arduino blink S.O.S!
Previously, I covered how to get your first sketch on an Arduino running – and how to blink off and on the board’s “L” labeled LED light.
Our original code looked something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
|
And contained a very basic set of instructions (as an example) so you (the user) could get familiar with writing code for Arduino micro-controllers.
Soon we are going to modify that code to give it some more worthwhile functionality. But before we can do that, we need the user to understand each line of code given here.
1
2
3
4
|
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
|
In the first method, we are telling the Arduino that this sketch will be targeting pin 13 for its output. This is documented in detail at http://arduino.cc/en/Tutorial/DigitalPins, and the pinMode() method is further documented in the reference (http://arduino.cc/en/Reference/PinMode). According to the Arduino reference, the syntax for setting a pin as output is pinMode(
Now that that is established, we must put our logic into the main loop (loop()). This loop will run continually, and provide whatever changes need to be made to the output or input pins.
1
2
3
4
5
6
7
|
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
|
In the example from my last tutorial, we use digitalWrite(13, High) to turn the pin #13′s voltage to high causing the LED to light up. After a 1 second delay (delay(1000)), we use digitalWrite(13, LOW) to turn the voltage low enough for the LED to flicker off. Finally we wait 1 more second (delay(1000)) and than repeat the same loop.
According to the official Arduino documentation, the syntax of digitalWrite() is as follows:
Description
Write a HIGH or a LOW value to a digital pin.
If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.
If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor. See the digital pins tutorial for more information.
NOTE: If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(HIGH), the LED may appear dim. Without explicitly setting pinMode(), digitalWrite() will have enabled the internal pull-up resistor, which acts like a large current-limiting resistor.
Syntax
digitalWrite(pin, value)
Parameters
pin: the pin number
Returns
none
Basically, digitalWrite() allows us to control the voltage of this LED. According to the Arduino reference, digitalWrite() takes two parameters, the first being the number of the pin and the second being the voltage (for example, digitalWrite(
I have followed your tutorials and my Arduino is blinking. How can I make a LED not attached to the board blink? How do I find the right pins?
Reply