Reader small image

You're reading from  Mastering Arduino

Product typeBook
Published inSep 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788830584
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Jon Hoffman
Jon Hoffman
author image
Jon Hoffman

Jon Hoffman has over 25 years of experience in the field of information technology. Over these years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems. Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon's other interests are spending time with his family, robotic projects, and 3D printing. Jon also really enjoys Tae Kwon Do, where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter Kara is currently working towards her black belt.
Read more about Jon Hoffman

Right arrow

Arduino IDE

I have been programming computers, as a hobby or professionally, for over 37 years. In that time, I have used many different Integrated Development Environments (IDE) and text editors to write code. I wrote my first Hello World program on a teletype that did not use either an IDE or a text editor. When I bought my first computer, which was a Commodore VIC-20, I used the BASIC programming language in programming mode to write my programs. When you were in programming mode, you entered the code line by line and each line went into memory as you entered it, there wasn't a nice editor or IDE. I wasn't until I learned to program in the C programming language, on an IBM PCjr, that I used my first real IDE. Now I wonder how I managed to write anything with one.

In this chapter, you will learn:

  • What an Arduino Sketch is
  • What the Arduino IDE is
  • What the Arduino Web...

Arduino Sketch

When we program the Arduino, the code is put into a project. These projects are called sketches and a sketch is stored in a Sketchbook. A sketch is designed to be as simple and straightforward as possible by abstracting away a lot of the technical aspects of programming the Arduino by using the prebuilt functions.

The coding language used to program the Arduino is very similar to the C programming language. We will be looking at how to program the Arduino in Chapter 6, Programming the Arduino – The Basics and Chapter 7, Programming the Arduino – Beyond the Basics. This chapter is to get you familiar with what sketches are and to look at the tools we can use.

Before a sketch can be uploaded to an Arduino, the Arduino IDE or Web Editor must go through several steps to build the application. The first step to building a sketch is to perform some...

Arduino IDE

The Arduino IDE is an integrated development environment that can be installed locally on Windows, macOS and Linux-based computers. The IDE can be downloaded from the Arduino software page at this URL: https://www.arduino.cc/en/Main/Software. At the time this book is written, the latest stable version of the IDE is 1.8.5.

The following images show what the IDE will look like when it is first run:

We will start using the IDE at the end of this chapter where we will build our first sketch. We will be using it, as well as the Web Editor extensively throughout the rest of this book. For now, let's briefly explore the IDE so we can familiarize ourselves with some of its basic functionality.

Exploring the IDE

There...

Arduino web editor

The Arduino Web Editor enables us to create and upload sketches within most web browsers. Officially the Web Editor is supported with the Chrome, Firefox, Safari and Edge browsers with the installation of a plugin.

The Web Editor is part of and can be accessed from the Arduino Create project here: https://create.arduino.cc

Before you can install the plugin and use the Web Editor, you will need to create a free Arduino account. Once we are logged in to our account, the site will walk you through installing the plugin. Once the plugin is installed, you should see a page similar to this:

Let's explore the Web Editor to see how to use it.

Exploring

The four main areas of the Web Editor are shown in the...

Examples

There are numerous examples that are included with the Arduino IDE and the Web Editor. These examples are simple sketches that demonstrate the Arduino commands and how to use them. These examples range from the very basic sketches that demonstrate how to read and write digital I/O to more advance sketches that show how to use sensors. While these examples are designed to demonstrate the Arduino commands they can also be used as examples of how to write good code for the Arduino.

To access the examples within the Arduino IDE, click on the File option in the menu bar and then go to the Examples option. You will see a list of categories for the examples that look similar to the following screenshot:

If you go to any of the categories, you will see a list of examples for that categories. If you select an example, such as the DigitalReadSerial example under the Basics category...

Arduino libraries

Like most development platforms, the Arduino environment can be extended with libraries. These libraries provide extra functionality, that we can use in our sketches, such as providing access to specific hardware, manipulating data and adding extra features like a task scheduler (Arduino Cron Library). There are numerous libraries that are built in to the IDE and Web Editor, but we can also download other libraries or build our own.

 

To access the libraries in the Arduino IDE, we select the Sketch option from the menu bar and then select the Include Library option. This will show another menu that lets you load a library or manage libraries. This menu should look similar to the following screenshot:

If you select any of the built-in libraries, the header files will automatically be included in your sketch. We will learn more about header files in Chapter...

Serial monitor

The serial monitor sends and/or receive text, usually through the USB cable. This enables us to receive debug messages or send text from the keyboard within the Web Editor or the Arduino IDE. We will see how to do both of these when we create our first sketches at the end of this chapter.

To use the serial monitor with the Arduino IDE or with the Web Editor, you must first connect an Arduino to the computer and establish communication between the Arduino and the IDE or Editor.

To begin using the serial monitor within the Arduino IDE, click on the serial monitor icon in the upper right-hand corner of the IDE. The following screenshot highlights the serial monitor icon:

The serial monitor will open up in a separate window, as shown in the following screenshot:

The input section enables us to send text to the Arduino. To do this, type the text into the input box...

Hello World

For our first Sketch, we will create the traditional Hello World application with the Arduino. This application will output the words "Hello World" to the serial monitor; however, before we create this application we need to understand what the setup() and loop() functions do.

The setup() function is run once and only once when the application first starts. This function enables us to initiate any variables or hardware when the application first starts. After the setup() function completes, the loop() function is then called for the first time. When the loop() function finishes it will be called again and will continue to loop until the Arduino is powered down.

Let's demonstrate how these functions work. We will need to start off by creating a new sketch in either the Arduino IDE or the Web Editor. To create a new sketch with the Arduino IDE we can use...

Echo

An echo application will read the text in from the Serial Monitor and will then output it back.

The text will be entered into the input field, as shown in the following screenshot:

And the text will be echoed, as shown in the following screenshot:

We will start off by creating a new sketch and add the following code to it:

byte bytesIn;
 
void setup() { 
  Serial.begin(9600);   
}
 
void loop() { 
  if (Serial.available()) { 
    bytesIn = Serial.read(); 
    Serial.write(bytesIn); 
  }
}

In this code, we start off by defining a variable named bytesIn of the byte type. Then within the setup() function the data rate for the serial data transmission is set to 9600 baud.

Within the loop() function we use the Serial.available() function to see if there is any data stored in the serial queue. The Serial.available() function returns the number of bytes available for reading...

Summary

In this chapter, we saw how to set up the Arduino IDE and the Web Editor. We also learned the basic functionality of both. At the end of this chapter, we saw how to use the Serial Monitor to send and receive data to and from the Arduino.

In the next chapter, we will begin to learn how to program the Arduino.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Arduino
Published in: Sep 2018Publisher: PacktISBN-13: 9781788830584
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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Jon Hoffman

Jon Hoffman has over 25 years of experience in the field of information technology. Over these years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems. Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon's other interests are spending time with his family, robotic projects, and 3D printing. Jon also really enjoys Tae Kwon Do, where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter Kara is currently working towards her black belt.
Read more about Jon Hoffman