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
Can$12.99 | ALL EBOOKS & VIDEOS
Save more on purchases! Buy 2 and save 10%, Buy 3 and save 15%, Buy 5 and save 20%
Arduino BLINK Blueprints
Arduino BLINK Blueprints

Arduino BLINK Blueprints: Get the most out of your Arduino to develop exciting and creative LED-based projects

By Utsav Shah
Free Trial
Book May 2016 134 pages 1st Edition
eBook
Can$33.99 Can$12.99
Print
Can$41.99 Can$28.99
Subscription
Free Trial
eBook
Can$33.99 Can$12.99
Print
Can$41.99 Can$28.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Arduino BLINK Blueprints

Chapter 1. Getting Started with Arduino and LEDs

Welcome to the exciting world of physical computing! Today, hobbyists and experts all over the world use Arduino to make interactive objects and to create cool prototypes. In this chapter, you will get introduced to different Arduino boards, followed by installation instructions for Arduino IDE. You will write a "Hello World" program using Arduino IDE and will learn about serial communication. By the end of this chapter, you will have a basic knowledge of Arduino and its IDE, which will be helpful in the remaining chapters of the book. In this chapter, we will cover the following:

  • Arduino boards

  • Arduino IDE

  • Before you start

  • "Hello World"

  • Using serial communication

  • The world of LED

Arduino boards


Arduino was originally created for artists and designers as an easy and quick prototyping tool. Designers were able to create sophisticated designs and artworks even without having knowledge of electronics and programming. So it is understood that the first few steps of learning Arduino are very easy. In this section, you will get introduced to different Arduino boards and learn how to choose an Arduino board for your project and some information on the Arduino UNO board, which you will be using throughout this book.

Different Arduino boards

Beginners often get confused when they discover Arduino projects. When looking for Arduino, they hear and read such terms as Uno, Zero, and Lilypad. The thing is, there is no such thing as "Arduino". In 2006, the Arduino team designed and developed a microcontroller board and released it under an open source license. Over the years, the team has improved upon the design and released several versions of the boards. These versions mostly had Italian names. There are numbers of boards that the team has designed over the past 10 years:

The Arduino team didn't just improve on design, they invented new designs for specific use cases in mind. For example, they developed Arduino Lilypad to embed a board into textiles. It can be used to build interactive T-shirts. The following table shows the capability of different Arduino boards. Arduino boards may differ in their appearance, but they have a lot in common. You can use the same tools and libraries to program:

Name

Processor

Dimension

Voltage

Flash(kB)

Digital I/O(PWM) pins

Analog input pins

Arduino Lilypad

ATmega168V

51 mm outer diameter

2.7-5.5 V

16

14(6)

6

Arduino YUN

Atmega32U4

68.6 mm × 53.3 mm

5 V

32

14(6)

12

Arduino Mega

ATmega2560

101.6 mm × 53.3 mm

5 V

256

54(15)

16

Arduino Due

ATSAM3X8E

101.6 mm × 53.3 mm

3.3 V

512

54(12)

12

Arduino Zero

ATSAMD21G18A

68.6 mm × 53.3 mm

3.3 V

256

14(12)

6

Arduino UNO

ATmega328P

68.6 mm × 53.3 mm

5 V

32

14(6)

6

As Arduino boards' design and schematics are open source, anyone can use and change the original board design and can create their own version of an Arduino-compatible board. Because of that, you can find countless Arduino clones on the web which can be programmed using the same tools and libraries used for original Arduino boards.

How to choose an Arduino board for your project

With so many options available, it becomes challenging for a person to decide which Arduino board to use for a project. The Arduino family is huge, and it is impossible to read about each and every board and decide upon which board to use for a particular project.

The following flowchart simplifies the process by providing a decision tree for widely-used Arduino boards and the most common use cases/applications:

If you are not sure what you will build and what hardware capabilities are required, start building your prototype using Arduino UNO. Arduino UNO has the best documentation and best support. It is also the most compatible of all Arduino boards. Most of the existing libraries and shields are compatible with Arduino UNO. And finally, most of the code that has been written on earlier versions of Arduino boards will also work with Arduino UNO.

Note

Arduino shields are modular circuit boards that can be plugged on top of the Arduino PCB, extending its capabilities. Want to connect your Arduino to the Internet? There is a shield for that. There are hundreds of shields available online, which makes Arduino more than just a development board.

Throughout this book, we will use the Arduino UNO board.

Arduino UNO

"UNO" means one in Italian, and it was chosen to mark the release of Arduino IDE v1.0. It is the first in a series of USB Arduino boards. As mentioned before, it is one of the most widely used boards in the Arduino Family:

In this section you will get introduced to different components of the Arduino UNO board.

  • A: USB plug: Every Arduino board needs a way to be connected to a power source. The Arduino UNO can be powered from a USB cable coming from your computer. The USB connection is also how you will load code onto your Arduino board.

  • B: Reset button: Pushing it will temporarily connect the reset pin to ground and restart any code that is loaded on the Arduino board.

  • C: ARE F: This stands for Analog Reference. In this book, you are not going to use this pin. It is sometimes used to set an external reference voltage (between 0 and 5) as the upper limit for the analog input pins.

  • D: GND: Digital ground.

  • E: Pin 0 to Pin 13: The area of pins under the DIGITAL label are digital pins. These pins can be used for both digital input (like telling if a button is pushed) and digital output (like powering an LED). Next to some of the pins (3, 5, 6, 9, 10, and 11) there is a tilde (~) sign, which means those pins can also act as pulse width modulation apart from normal digital pins. PWM (pulse width modulation) is a technique for getting an analogue signal with digital means by controlling on and off duration of the signal.

  • F: ON: This is a power LED indicator. This LED should light up whenever you plug your Arduino into a power source. If the LED doesn't turn ON, there is something wrong with your Arduino board.

  • G: In-circuit serial programmer: You will use these pins very rarely. Mostly, hardware manufacturers use these pins for debugging purposes. Also, these pins are used to program Arduino using other Arduino or other microcontrollers.

  • H: Main IC: This is an ATmega 328 microcontroller. Think of it as the brain of your Arduino board.

  • I: Pin A0 to Pin A5: The area of pins under the Analog In label are Analog In pins. These pins can read the signal from an analog sensor (like a temperature sensor) and convert it into a digital value that can be read by Arduino software and can be used for further processing.

  • J: Vin: Voltage In. Arduino can be supplied with power from a DC jack, the USB connector, or Vin pin. While supplying with Vin, you can give up to 12 V. The in-built regulator in Arduino will take care of regulating voltage to 5 V.

  • K: GND: Ground pin.

  • L: 5 V: Power pin which supplies 5 volts.

  • M: 3.3 V: Power pin which supplies 3.3 volts.

  • N: External power supply: Once you have uploaded your code to Arduino you don't need a computer just to draw power. You can use an external power supply and can use Arduino as a standalone device.

  • O: Voltage regulator: The voltage regulator is not actually something you can interact with on Arduino. It controls the amount of voltage that is let into the Arduino board.

  • P: Tx Rx LEDs: Tx is short for transmit, RX is short for receive. In electronics, TxRX are used to indicate the pins responsible for serial communication. These LEDs will give nice visual indications whenever our Arduino is receiving or transmitting data.

Arduino IDE


As, initially, Arduino was initially designed for artists and designers, the Arduino team has tried to develop Arduino software (IDE) as simply as possible without compromising on the power of the tool. Before you run your "Hello World" program, you need to install Arduino IDE on your computer.

Installing Arduino IDE

Arduino IDE is supported on all major operating systems, initially Windows, Mac, and Linux.

On Windows

  1. Go to https://www.arduino.cc/en/Main/Software.

  2. Download "Windows Installer" or "Windows ZIP file for non admin install".

  3. If you have downloaded "Windows Installer", double click on it and it will be installed.

  4. If you have downloaded "Windows ZIP file for non admin install", extract it and you will find arduino.exe. Double click on it to get started with the Arduino IDE.

On Linux

  1. Go to https://www.arduino.cc/en/Main/Software.

  2. Download "Linux 32 bits" or "Linux 64 bits" depending on your OS type.

  3. Extract it and run the Arduino executables to get started with the Arduino IDE.

On Mac

  1. Go to https://www.arduino.cc/en/Main/Software.

  2. Download "Mac OS X 10.7 Lion or newer".

  3. Extract it and run the Arduino executables to get started with the Arduino IDE.

Understanding Arduino IDE

If you have used IDEs such as Visual Studio, XCode, and Eclipse before, you'd better lower your expectations, because Arduino IDE is very simple. It mainly consists of an editor, a compiler, a loader, and a serial monitor. It has no advanced features such as a debugger or code completion:

Let's look into each button separately:

  • A: Verify button: This will compile the program that's currently in the editor. It does not only verify the program syntactically. It also turns it into a representation suitable for the Arduino board.

  • B: Upload button: This will compile and upload the current program to the connected Arduino Board.

  • C: New button: This creates a new program by opening a new editor window.

  • D: Open button: With this button, you can open an existing program from the file system.

  • E: Save button: This saves the current program.

  • F: Serial monitor: Arduino can communicate with a computer via a serial connection. Clicking the Serial Monitor button opens a serial monitor window that allows you to watch the data sent by Arduino and also to send data back. You will learn more about it in the Using serial communication section.

  • G: Editor window: This is where you will write the code.

  • H: Error console: This is where you will see all the error messages of your code.

  • I: Status bar: This will show the connected Arduino board name along with the COM port number.

Before you start


After getting some understanding of Arduino UNO and IDE, there are a couple of things that you need to know before you dive into the world of Arduino.

Power supply

As mentioned in the Arduino UNO section, there are two ways you can power up your Arduino UNO. One is by using a USB cable connected to your computer and the second one is by a 12 V external power supply. Please make sure that you don't use a power supply greater than 20 volts as you will overpower and thereby destroy your Arduino Board. The recommended voltage for most Arduino models is between 6 and 12 volts.

Verifying connection

This is the last step before you write your "Hello World" program with your Arduino:

  1. Make sure you have selected Arduino UNO under the Tools | Board section. If you have some other Arduino board, make sure you select that board:

  2. Select the COM port from Tools | Port, to which your Arduino UNO board is connected. In the following image it is COM13, but it will vary from computer to computer:

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

Using serial communication


Serial communication is used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port which is also known as a UART. Serial data transfer is when we transfer data one bit at a time, one right after the other. Information is passed back and forth between the computer and Arduino by, essentially, setting a pin to high or low. Just like we used that technique to turn an LED on and off, we can also send data. One side sets the pin and the other reads it.

In this section, you will see two examples. In the first example, Arduino will send data to the computer using serial communication, while in the second example, by sending a command (serial) from the computer, you can control the functionality of the Arduino board.

Serial write

In this example, the Arduino board will communicate with the computer using the serial port, which can be viewed on your machine using the Serial Monitor.

Write the following code to your Arduino editor:

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  
  Serial.println("Hello world!");  // prints hello with ending line break 
}

void loop()                       // run over and over again
{
                                  // do nothing!
}

Tip

Even if you have nothing in the setup or loop procedures, Arduino requires them to be there. That way it knows you really mean to do nothing, as opposed to forgetting to include them!

Serial.begin sets up Arduino with the transfer rate we want, in this case 9600 bits per second. Serial.println sends data from Arduino to the computer.

Once you compile and upload it to your connected Arduino board, open Serial Monitor from the Arduino IDE. You should be able to see the Hello world! text being sent from the Arduino board:

Note

If you have trouble locating Serial Monitor, check the Understanding Arduino IDE section of this chapter.

Serial read

In the previous example, serial library was used to send a command from Arduino to your computer. In this example, you will send a command from the computer, and Arduino will do a certain operation (turn on/off LED) based on the command received:

int inByte; // Stores incoming command

void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT); // LED pin
Serial.println("Ready"); // Ready to receive commands
}
void loop() {
  if(Serial.available() > 0) { // A byte is ready to receive
    inByte = Serial.read();
    if(inByte == 'o') { // byte is 'o'
      digitalWrite(13, HIGH);
      Serial.println("LED is ON");
      }
      else 
      { 
        // byte isn't 'o'
        digitalWrite(13, LOW);
        Serial.println("LED is OFF");
      }
   }
}

The inByte function will store the incoming serial byte. From the previous example, you should be familiar with the commands written in the setup function. In the loop function, first you need to know when a byte is available to be read. The Serial.available() function returns the number of bytes that are available to be read. If it is greater than 0, Serial.read() will read the byte and store it in an inByte variable. Let's say you want to turn on the LED when the letter 'o' is available. For that you will be using the if condition, and you will check whether the received byte is 'o' or not. If it is 'o', turn on the LED by setting pin 13 to HIGH. Arduino will also send an LED is ON message to the computer, which can be viewed in Serial Monitor:

If it is any other character, then turn off the LED by setting pin 13 to LOW. Arduino will also send an LED is OFF message to the computer, which can be viewed in Serial Monitor:

The world of LED


LED stands for light emitting diode, so it emits light when sufficient voltage is provided across the LED anode and cathode. Today's LEDs are available in many different types, shapes, and sizes – a direct result of the tremendous improvements in semiconductor technology over recent years. These advancements have led to better illumination, longer service life, and lower power consumption. They've also led to more difficult decision making, as there are so many types of LED to choose from.

LEDs can be categorized into miniature, high power, and application-specific LEDs:

  • Miniature LEDs: These LEDs are extremely small and usually available in a single color/shape. They can be used as indicators on devices such as cell phones, calculators, and remote controls.

  • High power LEDs: Often referred to as high output LEDs, these are a direct result of improved diode technology. They offer a much higher lumen output than standard LEDs. Typically, these LEDs are used in car headlights.

  • Applicatio n-specific LEDs: As the name suggests, there are many LEDs that fall under this category. These are flash LEDs, RGB LEDs, seven segment display, LED lamps, and LED bars.

Summary


In this chapter, an overview of different Arduino boards was covered, with a detailed explanation of an Arduino UNO board. Arduino IDE was explained, with installation instructions for Windows, Mac, and Linux machines. "Hello World" and a serial communication Arduino sketch were developed.

In the next chapter, we will develop an LED mood lamp and you will learn some artistic stuff apart from programming LEDs.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn to control TV backlighting using an IR remote
  • Get introduced to sound visualization so you are able to use sound-controlled LEDs
  • Build an exciting persistence of vision wand

Description

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.

What you will learn

[*] Set up Arduino boards to run a basic ‘Hello World’ program [*] Develop a mood lamp and expand it to become an LED night lamp [*] Control TV backlight color and intensity using an IR remote [*] Develop an IR-controlled 4*4 LED cube [*] Use sound visualization to develop a sound-controlled LED Christmas tree [*] See a fun way to create interesting long exposure photographs and light displays using persistence of vision (POV) wands

Product Details

Country selected

Publication date : May 30, 2016
Length 134 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781785284182
Vendor :
Arduino
Category :

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : May 30, 2016
Length 134 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781785284182
Vendor :
Arduino
Category :

Table of Contents

14 Chapters
Arduino BLINK Blueprints Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Authors Chevron down icon Chevron up icon
About the Reviewer Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
1. Getting Started with Arduino and LEDs Chevron down icon Chevron up icon
2. Project 1 – LED Night Lamp Chevron down icon Chevron up icon
3. Project 2 – Remote Controlled TV Backlight Chevron down icon Chevron up icon
4. Project 3 – LED Cube Chevron down icon Chevron up icon
5. Sound Visualization and LED Christmas Tree Chevron down icon Chevron up icon
6. Persistence of Vision Chevron down icon Chevron up icon
7. Troubleshooting and Advanced Resources Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Top Reviews
No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.