Arduino BLINK Blueprints

By Samarth Shah , Utsav Shah
    What do you get with a Packt Subscription?

  • Instant access to this title and 7,500+ eBooks & Videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
About 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.

Publication date:
May 2016
Publisher
Packt
Pages
134
ISBN
9781785284182

 

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.

About the Authors
  • Samarth Shah

    Samarth Shah is a software engineer by profession and maker by heart. He leads maker activities at Pune Makers and heads Infosys Robotics Club. He loves building creative/innovative prototypes using the latest hardware/sensors (Raspberry Pi, Arduino, Kinect, Leap Motion, and many more) and software. He has given talks at various national and international conferences. He has authored a book on Raspberry Pi entitled Learning Raspberry Pi, Packt Publishing. During the day, he works on various data visualization techniques and UI frameworks. At night, he does blogging, reading, writing, and many more things. You can read more about him at http://samarthshah.com.

    Browse publications by this author
  • Utsav Shah

    Utsav Shah is an instrumentation engineer who loves to work on the latest hardware as well as software technologies. He has been featured on India's leading website http://yourstory.in and Ahmedabad Mirror (Times Group) for his research work on "Converting sign language into speech" using a Leap Motion controller. Apart from his regular work at Infosys Limited, he manages activities of Infosys Robotics Club. In his leisure time, he loves to read books and work on cutting-edge technologies.

    Browse publications by this author
Latest Reviews (2 reviews total)
of interesting LED projects
Arduino BLINK Blueprints
Unlock this book and the full library FREE for 7 days
Start now