




























































































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!





























































































Foreword by Daniel Jebaraj
Each author was carefully chosen from a pool of talented experts who shared our vision. The book you now hold in your hands, and the others available in this series, are a result of the authors’ tireless work. You will find original content that is guaranteed to get you up and running in about the time it takes to drink a few cups of coffee.
Syncfusion will be working to produce books on several topics. The books will always be free. Any updates we publish will also be free.
There is no catch here. Syncfusion has a vested interest in this effort.
As a component vendor, our unique claim has always been that we offer deeper and broader frameworks than anyone else on the market. Developer education greatly helps us market and sell against competing vendors who promise to “enable AJAX support with one click” or “turn the moon to cheese!”
If you have any topics of interest, thoughts or feedback, please feel free to send them to us at [email protected].
We sincerely hope you enjoy reading this book and that it helps you better understand the topic of study. Thank you for reading.
Please follow us on Twitter and “Like” us on Facebook to help us spread the word about the Succinctly series!
About the Author
Marko Švaljek works as a software developer in the machine networks department of the Kapsch Group. For the majority of his career, he didn’t have much to do with electronics, and his code consisted of mostly showing something to users on a screen, or it moved data around storage systems on the backend.
One evening, out of curiosity, he ordered an Arduino board. Although he wasn’t aware of it at the time, this little board changed a lot in his life. One of the first and very basic things he did with it was measure the temperature and the light level on his work desk. Having a background in software development, the first thought he had was about saving the data that came in from the sensor and analyzing it.
Sensors around the world actually generate a lot of data that is difficult to handle with the traditional technologies. So, at about the same time, he started to learn about Big Data technologies and especially about time series data (i.e., basically storing the sensor readings together with the timestamp when the measurements were made). Then he started to write about both Big Data and Electronics on his blog.
This opened a lot of doors, such as being invited to attend and talk at conferences that use those two technologies. Shortly after he published the book Cassandra Succinctly , he always felt that the technology that started the whole thing went a little under the radar. So he decided to write the book that will help others get into the world of hacking with the electronics and especially the Arduino board.
The purpose of having the Arduino Board is to program it to do something, so it is desirable that you have at least basic programming knowledge. It might be a bit hard for you to follow along without basic programming knowledge. Knowledge of the C programming language or any other similar language is also desirable.
We’ll also try to keep the programs simple and will make many comments in the code so that you can follow along. Getting started with Arduino programming is relatively easy. There is a free Arduino integrated development environment (IDE) available for download on all of today’s most popular operating systems.
We will use the IDE because it will make the upload of our programs to the Arduino board easy. Basically, there will be just a couple of clicks needed and then we’ll see the magic happen as our code will actually start to control real-world objects such as lights and buzzers.
Before doing anything with the board, we have to install some software that will enable us to program it. In the next couple of sections, we’ll go through the installation steps for the most popular operating systems. We’ll start by using Microsoft Windows.
Installing Arduino IDE on Windows
At the moment of writing this book, Microsoft Windows 7 still seems to be the most popular Windows operating system (OS) on the market. The next most popular Windows OS is Windows XP. So we’ll concentrate on how to install Arduino IDE for Windows 7. There is an installer for Windows available on the Arduino website here. Download the installer and run it. There will be a couple of steps to take in order to install it on the Windows platform:
Figure 1: Allow the application to make changes to the computer by clicking Yes
Figure 2: Agree with the License Agreement by clicking “I Agree”
Figure 3: Select Installation Options and click Next
Figure 6: After the installation completes, click Close
If you followed the steps shown in previous figures, by now you should have a functional Arduino IDE on your Windows machine. Start the Arduino IDE by clicking on Arduino in the Start menu or by double-clicking the Arduino icon on the desktop. You should see something like the following:
Figure 7: Arduino IDE
When you plug the Arduino in for the first time, you might wait for some time until Windows recognizes and installs the drivers for the Arduino Uno board. This is pretty common; just wait for a while, and you should be up and running with your Arduino in no time. This is as far as we’ll go for now. At this stage, the most important thing is that we have an installed Arduino IDE. Later we will cover how to actually hook up the Arduino Uno board with the Arduino IDE and how to upload the code to Arduino.
Installing Arduino IDE on Linux
The installation on the Linux platform is not as straightforward as it is on the Windows platform. There is a prerequisite needed before installing and running the Arduino IDE. This prerequisite involves having an installed Java runtime environment. There are a lot of Linux versions available. Around one-third of desktop Linux machines run on Ubuntu. We’ll cover how to install Arduino IDE on Linux Ubuntu in this section. The most basic Java installation for the Linux Ubuntu is to start the terminal and then run the following commands:
After you’ve checked if Java is present on your system (or after you installed it from scratch), it’s time to download the IDE from the Arduino website here. Click the download link for the Linux version.
Be careful to check if your system is 32- or 64-bits and download the appropriate version from the website. If the versions of Linux are not matching your system, you will not be able to upload the programs to Arduino, and you will get an exception when trying to do so.
The Arduino IDE download for Linux is compressed. Open it after downloading it and extract it to a folder where you usually install your applications. Make sure that you remember the folder where you extracted the Arduino IDE, and then navigate to the folder where you extracted the Arduino IDE. Run the arduino executable file by double-clicking it. The system will ask you the following question:
[Update the package index]
[Check if Java is already there, skip to Arduino installing if it’s installed]
[Install Java if it’s not present on your system]
[install all the required packages and check if Java is installed]
Installing Arduino IDE on Mac OS X
On Mac OS X, Java is a prerequisite for running Arduino IDE. Java is not available on the Mac OS X by default—at least not with the most recent version. There are multiple ways you can bring Java to your Mac. Some of the previous versions of Mac had it installed by default. The easiest way to get started with Java on Mac is to go to the Apple support page available here, and download and install the provided files. As with the Linux and Windows versions, the Arduino IDE is available on the Arduino website here.
The Mac OS X Arduino IDE is compressed by default. Extract the archive to a folder and remember where you extracted it. The rest of the installation is easy; just take the extracted application and copy it to Applications folder. Then, run the Arduino the same way as you would run the other applications installed on your Mac:
Figure 10: Drag the extracted application to the Applications folder
Once the Arduino IDE is started, it’s pretty much identical no matter what platform you are using. In the next section, we will cover the basic Hello World of Arduino programming.
Arduino Uno Hello World
This is the first section in which you will need the Arduino Uno board to follow along. Besides the board, you will also need an A to Mini-B USB cable. There are two approaches that you can go by at the moment. One approach is to hook the Arduino to the USB and see what’s going on when you plug it in to your computer.
A second approach is that you can get to know the parts of the board before you plug it in. Getting to know the components on the Arduino Uno board might not seem as important in the beginning, but as you progress, you will have to get to know the parts well. It might seem a bit complicated at first, but there are a couple of things of which you should be aware. Let’s take a look at the Arduino:
Figure 11: An Arduino Uno board
Take a moment to get acquainted with the board. Try to locate the elements located near the letters marked in the previous figure.
The following are Arduino Uno board elements that we will use:
A – USB cable socket. Through this cable we power Arduino and upload programs. B – Reset button. We use this button to restart the uploaded program. C – Arduino digital pins. How these work will be explained in later chapters. D – Arduino power pins. These will be used to power electronic components. E – Arduino analog pins. How to use them will be explained in later chapters. F – TX and RX Light Emitting Diodes (LEDs) indicate Arduino communicating over USB. L is programmable. G – Power-on light. Arduino should ignite this LED as soon as it has power.
It’s perfectly fine if you don’t remember all of these elements in the beginning. The more you work with the board, the more you will be acquainted with it. At the moment, the only important element is the USB cable socket. Plug the USB cable to your computer and then to the USB cable socket on Arduino. Once again, you should use the USB A to Mini-B cable. If you are not sure what kind of cable that is, ask somebody working at your nearby electronics store. The cable should look exactly like (or similar to) the following:
We’ll explain the code a bit before we continue. The onboard LED is bound to the digital pin 13. Now, you might wonder what a digital pin is. Well, there is a “digital” in its name. That means that the pin can have two states: HIGH and LOW. HIGH means that there is electrical current on the pin while LOW means that there is no electrical current on the pin. Arduino can output the current to a pin or it can read a current from a pin. In this example, we are sending an electrical current to pin 13. If the current on pin 13 is high, the onboard LED will be on. If pin 13 is in a low state, the onboard LED will be turned off.
The second part of the code is the loop function. The code inside the loop function runs for as long as there is a power source attached to the Arduino, and it will send the current to pin 13. It will wait for a second and then will stop sending the current to the pin. Then it will wait for another second and start over from the beginning. The previous code will, essentially, give the current and then take it away on pin 13 in regular one-second intervals. The unit for pausing on Arduino is a millisecond, so a one-second pause is equal to one thousand milliseconds. Now that you’ve typed the code, it would be a good idea to verify the code. To do that, just click Verify:
Figure 13: The Verify function in action
void loop() {
// turn the LED on with HIGH voltage level digitalWrite(13, HIGH);
// wait for a second delay(1000);
// turn the LED off by making the voltage LOW digitalWrite(13, LOW);
// wait for a second delay(1000); }
Now that the code is verified, it’s time to upload it to the Arduino. We do this by clicking Upload. After the click, the code is compiled and uploaded:
Figure 14: Upload functionality in action
If everything went fine, you should see the LED blinking. Now, it might not seem like a lot, but this is actually a fully functioning program on your Arduino board right now, and your software is actually interacting with the real world through this LED. Now, there is no useful use for this LED right now other than you learning how to control it. But the output from this pin could be used to control a lot of things. We’ll talk about it through the rest of the book.
Serial Monitor
Arduino is capable of serial communication and can be used to communicate with the computer or other devices that understand serial communication. Serial communication has been around for quite some time and is often used when building various kinds of electronics projects. Perhaps it would be best to take a look at a simple Arduino example:
void setup() { // initializes serial communication with a speed of 9600 bit/s Serial.begin(9600); }
// this loop runs for as long as the Arduino has power void loop() { // send a message to the serial port Serial.println("Hello I'm your Arduino Board!");