Start up the Arduino software IDE (Integrated Development Environment) and open the Blink example sketch (reported below) that is available in the File>Examples>Analog menu of the Arduino/Genuino software. Sketches are written in Arduino C.

When you select Sketch>Verify/Compile from the menu, the Arduino software looks over the document and translates it to Arduino-machine.

/*
 * Blink
 * Turns on an LED on for one second, then off for one second, and so on... 
 * We use pin 13 because, depending on your Arduino board, it has either a built-in
 * LED or a built-in resistor so that you need only an LED.
*/

int ledPin = 13;                // LED connected to digital pin 13

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}


♦ Variables

int ledPin = 13;                // LED connected to digital pin 13

Blink skematicThis is the first line of actual instruction code, which turns out to be a sentence telling the computer that we would like it to create a box named ledPin and to put the number 13 in that box:

  • The first part of this sentence is int, which is short for integer.
  • The second part of this sentence is ledPin which is the name of the box.
  • The third part is an =, which basically says that the variable (box) named ledPin should start out equaling whatever is after the =.
  • The fourth part is 13, a whole number (integer) which is assigned to ledPin

NB: Generally you should prefer to use the component #define to give a name to a constant value before the program is compiled. Defined constants in Arduino don't take up any program memory space on the chip. The compiler will replace references to these constants with the defined value at compile time. The first line of code thus changes:

#define ledPin 13               // LED connected to digital pin 13 

 

Special Procedures - setup() and loop()
These two procedures are extra-special in that when the Arduino first wakes up after being reset, it always does what's in the setup() procedure first. Then it does whatever is in the loop() procedure over and over and over… forever! Or at least until you turn it off.


Procedures

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

This bunch of code is an example of a procedure, that is a collection of statements, it's used to group statements together so that we can refer to them all with one name.
We see that it is named setup() and it has no input values and it returns void. That's a computer way of saying nothing: this procedure doesn't return anything.
pinMode() is a procedure that set one of the Arduino digital pins in INPUT or in OUTPUT.


♦ Procedure calls

void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}

The loop() procedure has no inputs or output. It's has multiple statements, one after the other.
The first and the third statements are procedures call. The procedure digitalWrite() turn a pin on the Arduino chip: on, using HIGH costant, and off, using LOW costant, and that pin is powering the LED so in essence its turning the LED on and off.
The second and fourth statements are the same, and are delay(). These statement says to Arduino to stop what it's doing for a short amount of time, and all we have to do is tell it how many milliseconds (1/1000th of a second) to wait and it will do the job for us.

♦ Exercises
Now it's time for you to make modifications to the sketch and to experiment with different delay values.

Exercise 1. Modify the code so that the light is on for 100 ms and off for 900 ms. Write the code.
 

Exercise 2. Modify the code so that the light is on for 50 ms and off for 50 msec. What happens?
     

  • Intense strobe action
  • The light is no longer blinking
  • The light is blinking slowly


Exercise 3. Modify the code so that the light is on for 10 ms and off for 10 ms. What happens?

  • Intense strobe action
  • The light is no longer blinking
  • The light is blinking slowly


Exercise 4. Modify the previous code so that the LED is connected on pin 10, and trace the connections between devices in the figure below.   

Blink components


♦ Practice
Practice 1. Realize a lights effect with 4 LEDs that turn on and turn off them sequentially. Program an Arduino sketch to do this.

Practice 2. Program an Arduino sketch to represent numbers on a 7 segment display.

7 segment displayPractice 3. Program an Arduino sketch to make a decadic counter that displays the digits (from 0 to 9) sequentially on a 7 segment display.

You can find answers to the exercises and practice proposed in this page by clicking on this link: Arduino Blink - Answers.


Advertising

Advertising

Advertising

We use cookies

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.