Search icon CANCEL
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arduino BLINK Blueprints

You're reading from  Arduino BLINK Blueprints

Product type Book
Published in May 2016
Publisher Packt
ISBN-13 9781785284182
Pages 134 pages
Edition 1st Edition
Languages
Author (1):
Utsav Shah Utsav Shah
Profile icon Utsav Shah

"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.

You have been reading a chapter from
Arduino BLINK Blueprints
Published in: May 2016 Publisher: Packt ISBN-13: 9781785284182
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}