Book Image

Arduino BLINK Blueprints

By : Utsav Shah
Book Image

Arduino BLINK Blueprints

By: Utsav Shah

Overview of this book

Arduino is an open-source prototyping platform based on easy-to-use hardware and software. Arduino has been used in thousands of different projects and applications by a wide range of programmers and artists, and their contributions have added up to an incredible amount of accessible knowledge that can be of great help to novices and experts alike. Want to build exciting LED projects with Arduino? This book will be your companion to bring out the creative genius in you. To begin with, you will get introduced to the maker movement and the open source hardware development Arduino boards. You will then move on to develop a mood lamp and a remote-controlled TV backlight. As you progress through the book, you will develop an LED cube and will learn to use sound visualization to develop a sound-controlled LED Christmas tree. You will then move on to build a persistence of vision wand. At the end of each chapter, you’ll see some common problems, their solutions, and some workarounds.
Table of Contents (14 chapters)

"Hello World"


You must be aware of "Hello World" programs from computer science, where you write a piece of code which will display "Hello World". In electronics hardware board space, "Hello World" refers to blinking an LED by writing a simple program.

The resistor will block the flow of current in both directions. A diode is a two-terminal electronics component that has low resistance in one direction and high resistance in the other direction. Diodes are mostly made up of silicon. LEDs are the most commonly used diodes in any electronics circuit. LED stands for light emitting diode, so it emits light when sufficient voltage is provided across the LED anode and cathode.

The longer lead of the LED is the anode and the other end is the cathode. The color of the light depends on the semiconductor material used in the LED, as shown in the following diagram:

Connect the longer lead of the LED to pin 13 and shorter lead of the LED to GND (ground pin) and write the following code in a new editor window:

// 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(1000 millisecond)
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second(1000 millisecond)
}

You will notice from the preceding code that there are two important functions in each program. The first one is setup, which runs only once when you power up the board or press the reset button. The second function is loop, which runs over and over forever.

In the setup function, you should write a code that needs to be executed once, like defining a variable, initializing a port as INPUT or OUTPUT. In the preceding code, digital pin 13 is defined as OUTPUT. In the loop function, the first line will put the HIGH voltage on pin 13, which will turn on the LED connected to pin 13.

The "Hello World" program will turn the LED on for one second and turn off the LED for one second. The delay(1000) function will induce a delay of one second and after that, digitalWrite(13, LOW) will put the low voltage on pin 13, which will turn off the LED. Again, before you turn the LED on, you need to wait for one second by putting delay(1000) at the end of the code.