Arduino Logo

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:

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.

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.

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

value: HIGH or LOW

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(, ). Delay(), on the other-hand takes only one parameter which is always time in milliseconds (1/1000 second). In our example we are forcing the loop to delay in increments of 1 second by use of delay(1000), where delay(

Now that we have an understanding of what is actually going on here, head over to part three and we will finish up by sequencing the blinks in a way such that they spell out SOS (or anything else you want to write in Morse code).

 

 

Tags: arduino, arduino basics, c, learning arduino, sos
3 replies
  1. Jsmith91
    Jsmith91 says:

    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

Trackbacks & Pingbacks

  1. […] my second tutorial, we broke apart the example code and learned about the functionality of all the methods hidden […]

    Reply
  2. […] Next up, I will be covering how to edit this code in order to blink morse code messages. […]

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply