34,79 €
This book will start with the fundamentals of C programming and programming topics, such data types, functions, decision making, program loops, pointers, and structures, with the help of an Arduino board. Then you will get acquainted with Arduino interactions with sensors, LEDs, and autonomous systems and setting up the Arduino environment. Moving on you will also learn how to work on the digital and analog I/O, establish serial communications with autonomous systems, and integrate with electronic devices. By the end of the book, you will be able to make basic projects such as LED cube and smart weather system that leverages C.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 265
Veröffentlichungsjahr: 2017
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: March 2017
Production reference: 1220317
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78712-009-9
www.packtpub.com
Author
Syed Omar Faruk Towaha
Copy Editor
Safis Editing
Reviewer
Francesco Balducci
Project Coordinator
Kinjal Bari
Commissioning Editor
Veena Pagare
Proofreader
Safis Editing
Acquisition Editor
Mansi Sanghavi
Indexer
Mariammal Chettiyar
Content Development Editor
Trusha Shriyan
Graphics
Kirk D'Penha
Technical Editor
Naveenkumar Jain
Production Coordinator
Aparna Bhagat
Syed Omar Faruk Towaha has degrees in physics and computer engineering. He is a technologist, tech speaker, and physics enthusiast from Shahjalal University of Science and Technology (SUST), Bangladesh. He has passion for programming, tech writing, and physics experiments. His recent books include Easy Circuits for Kids, Fundamentals of Ruby, How You Should Design Algorithms, and JavaScript Projects for Kids. He is an Oracle-certified professional developer who is currently involved with a number of projects that serve both physics and computer architecture. He is currently working as the CTO of an IT company.
Francesco Balducci is a software engineer, working on Digital Signal Processing, micro-controllers, systems-on-chip, and ASIC products, and is currently employed at ST Microelectronics. He received his degree in Electronic Engineering at Politecnico di Torino in 2005. His thesis was about software security and methods to measure the strength of copy protection systems.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1787120090.
If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Have you ever thought about making a robot? Or an electronic device that can be programmed for serving mechanical or logical purposes? If yes, then this book is the perfect guide for you. Throughout this book, you will be guided to learn C programming, which is required to program the microcontroller used in a logical device. You will also learn how you can develop your ideas about making an intelligent device. You will be able to program Arduino for your projects or play with Arduino for fun.
Chapter 1, Getting Started, in this chapter, you will learn about Arduino, types of Arduino, Install the Arduino IDE and know the Arduino IDE. This chapter will also have details about the functions of the Arduino IDE software.
Chapter 2, Our First Program! in this chapter, you will learn how you can connect your Arduino to the computer and write the first program for the Arduino. You will also learn how you can format your code. A simple Arduino project will also be discussed in this chapter.
Chapter 3, Exploring C with Arduino IDE, in this chapter, you will learn about C programming for Arduino. You will learn how you can declare variables, take inputs from the Serial Monitor, few mathematical operations, String, Arrays and many other things related to C programming.
Chapter 4, Blinking with Operations and Loops, in this chapter, you will learn logical operations and loops. You will also learn where you can use these techniques in your programs.
Chapter 5, Functions and Files with Arduino, in this chapter, you will learn about functions, functions types, and file handlings. You will also learn how you can connect an SD card to your Arduino and program for it to read and write.
Chapter 6, Arduino and C++, in this chapter you will learn basic things about object oriented programming, the benefits of OOP, and how you can use OOP in Arduino programming. At the end of this chapter you will be able to connect GSM module to your Arduino and make call or send and receive SMS with the module.
Chapter 7, Using Pointers and Structure, in this chapter you will go deeper into the C programming. You will learn the usages of pointer and structure. You will also learn how you can use them in your code.
Chapter 8, Working with Arduino Libraries, in this chapter, you will learn about Arduino libraries. You will learn how you can install a library and use it in your code. You will learn about few famous Arduino libraries.
Chapter 9, Let’s Build Something Awesome, in this chapter, you will build a number of projects including LED cube, a smart weather system, and a home security system. You will also learn how you can make your own projects.
Chapter 10, Few Error Handlings, in this chapter, you will learn how you can solve few common Arduino errors. You will also learn techniques for fixing errors that can occur in building any project or code.
Software: Arduino IDE and Fritzing.
This book is for hobbyists who have no knowledge about programming and microcontrollers, but are keen to learn C programming using a very affordable hardware device.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, path names, dummy URLs, user input, and Twitter handles are shown as follows: "On the last code we have written Serial.print("Hello Arduino!\n")."
A block of code is set as follows:
void setup() { Serial.begin(9600); } void loop() { Serial.print("Hello Arduino!\n"); }New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "If you go to Tools | Port you will see a bunch of port list."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at [email protected] with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.
Since you have picked this book, this means you want to learn both about programming and electronics, especially microcontrollers. The book is designed to enable you to learn C programming easily and implement the language with an Arduino. I would suggest you do every code of the book by yourself and run them on the Arduino IDE. Microcontroller programming was hard before, but since the innovation of Arduino and the use of C programming on the Arduino board, microcontroller programming has become easy and fun.
Before going any further, let's understand the programming language C better. You may wonder why C programming is called C.
Well, before the creation of C, there was its predecessor programming language, called B. So, you should be able to guess from this where the name came from. The B language was developed by Ken Thompson at Bell Labs. At the same lab, the C language was developed in 1972 by Dennis Ritchie. The main purpose of creating this language was to design a UNIX operating system. Ken Thomson and Dennis Ritchie were the main developers of the UNIX operating system, so Dennis developed C to design UNIX.
C is not just a powerful language but a flexible one too. It is a portable language, because we can write a C program on one computer and compile to another with almost no modification.
To compile the source code of C, we need to have an IDE installed to our computers. Throughout this book we will learn C programming using the Arduino IDE. We will also be introduced to the Arduino IDE, its installation process, and how we can run our very first C program using an Arduino and the Arduino IDE.
IDE stands for Integrated Development Environment. IDEs are used for software development. An IDE usually consists of an editor (where we can type code and comments), and a few tools along with a debugger (which is used to test the code to see if it has any errors). Most IDEs have a built in compiler (which converts source code/programming language into machine language).
Let's get introduced to an Arduino now.
Arduino is a microcontroller board. People also call it a prototype board. In a nutshell, an Arduino board is a small computer with a processor, RAM, ROM etc. Arduino is open source hardware - you might have heard of open source software. Open source software is computer software distributed with a license that gives the users the right to see its source code. A Free Open Source Software license provides the rights to study, change and distribute the software to anyone for any kind of purpose. Open source software is usually developed by a group of open source enthusiasts who develop and gradually fix bugs of the software.
Since Arduino is open source hardware, we can build our own Arduino and also develop it for the purpose of our goals. We can change it to whatever we want.
Since Arduino is open source hardware, people around the world modify the board according to their needs. There are a few companies that produce different versions and types of Arduino. Let's get introduced to a few famous types of Arduinos.
Arduino Uno is the most-used Arduino board. It is based on the ATmega328 microcontroller. It has 14 digital pins and six analog inputs. We will know more about analog and digital pins in the next chapters.
The ATmega328 has 32Kbytes flash memory, 32 pins, 20MHz maximum operating frequency and 8-bit AVR CPU. The following image is an Arduino Uno board and an ATmega328 microcontroller with its pin out:
Arduino Mega is a microcontroller board that is famous for providing more pins. It uses ATmega2560, and has 54 digital pins and 16 analog inputs.
The ATmega2560 has 256Kbytes of flash memory, 100 pins, 16MHz operating frequency and 8-bit AVR CPU. The following image is an Arduino Mega and an ATmega2560 microcontroller with its pin out:
Arduino Nano is usually chosen for prototypes with a small form factor. It is based on the ATmega328 microcontroller, which is same as the Arduino Uno. It has 14 digital pins and eight analog inputs. The following image is an Arduino Nano:
Arduino Leonardo is similar to Arduino Uno. The main difference is that it has a soldered microcontroller instead of a removable one, and the model of the microcontroller is ATmega32U4. It has 20 digital pins and 12 analog inputs. The following image is an Arduino Leonardo with the ATmega32U4 microcontroller's pin out:
Arduino Mini is a small prototyping device that is based on the ATmega168. It does not come with a USB port. Arduino Mini has 14 digital pins and eight analog inputs. The ATmega168 has 16Kbytes of flash memory, 20MHz maximum operating frequency and an 8-bit AVR CPU. The following image is an Arduino Mini and an ATmega168 microcontroller with its pin out. The following image is image of an Arduino menu with the ATmega168's pin out:
Arduino Micro is based on the ATmega32u4 microcontroller. It uses the same microcontroller as the Arduino Leonardo. There are fewer digital and analog pins than in the Arduino Leonardo - the Micro has 20 digital pins. It comes with a micro USB port and ICSP pins. The following image is an Arduino Micro:
Throughout this book, we will use the most famous board, Arduino Uno. Arduino Uno has a replaceable microcontroller, ATmega 328. Before connecting an Arduino to our PC, let's get to know our Arduino better.
An Arduino Uno consists of a number of parts. The important ones are as follows:
Let's know about them in detail.
This port is used to power up the Arduino board and upload programs into the microcontroller:
On the Arduino Uno, the USB port is situated in between the reset button and the voltage regulator.
A-B cable is needed for powering up the board and uploading code to the microcontroller. The following picture is an A-B cable:
The 'A' side is connected to the Arduino board and the 'B' side is connected to the USB port of the computer.
Via this port, we can power the Arduino, but we cannot upload a program using it. This port is usually a 7-12 volt DC input. The following picture is a spare port and an adapter of 7-12 volts. The following picture is an external power jack and a port of 3.5mm power:
On an Arduino Uno, there are six analog pins (A0, A1, A2, A3, A4 and A5). The analog pins are used for reading analog values. We will discuss the uses of the analog pins in the next chapter.
The following image shows the analog inputs of the Arduino Uno:
On an Arduino Uno there are 14 digital pins (0-13). The digital pins are used for reading digital values from sensors, generating digital signals and communicating with other devices through digital interfaces. The following image shows the position of the digital pins of the Arduino Uno:
There are two sets of ICSP (In Circuit Serial Programming) pins on an Arduino Uno board. On the image below, you can see both of them (A and B). These ICSP pins are used for updating the firmware or reinstalling the bootloader, which is something you should not worry about in this book:
Firmware is permanent software that is programmed on the read-only memory, and bootloader is a computer program that helps to load the operating system.
There is a main microcontroller on our Arduino. By microcontroller we mean a single computer or a collection of processor core, memory and input/output peripherals. On our Arduino Uno we have ATmega328. Let's see the pin out of the microcontroller:
The pins are defined as follows:
Pin number
Pin name on the microcontroller
Pin name on Arduino
1
PC6
Reset
2
PD0
0 (RX)
3
PD1
1 (TX)
4
PD2
2
5
PD3
3 (PWM)
6
PD4
4
7
VCC
VCC
8
GND
GND
9
PB6
Crystal
10
PB7
Crystal
11
PD5
5 (PWM)
12
PD6
6 (PWM)
13
PD7
7
14
PB0
8
15
PB1
9 (PWM)
16
PB2
10 (PWM)
17
PB3
11 (PWM)
18
PB4
12
19
PB5
13
20
AVCC
VCC
21
AREF
AREF
22
GND
GND
23
PC0
A0
24
PC1
A1
25
PC2
A2
26
PC3
A3
27
PC4
A4
28
PC5
A5
PWM means Pulse Width Modulation. This is a process by which we get analog results by digital means. GND is Ground, and VCC is the power (3.5V or 5V).
On Arduino Uno there is a reset button between the USB port and the digital pins. The following pins show a reset button of Arduino Uno. This button is used to refresh the board or restart the system:
Now we know all we need about Arduino Uno, let's connect it to the computer and program for our Arduino Uno.
To connect Arduino Uno to our system we will need an A-B cable, about which we have already learned. Let's connect the Arduino as shown in the following image:
You will hear a sound on your system after connecting the Arduino Uno to the PC. A green light on the Arduino Uno will glow. If the green light does not glow, something went wrong. You need to check the connection or see Chapter 10, Few Error Handlings:
Now, we will download the IDE for coding for the Arduino and uploading to the board.
Arduino IDE is an Integrated Development Environment where we can write code, debug and upload to our Arduino Uno or any other board.
To download Arduino IDE, we need to go to https://www.arduino.cc/en/Main/Software and you will see the following page:
We will download the package associated with our Operating System.
Let's install Arduino IDE on our system.
To install Arduino IDE on Windows, go to the previous link and download Windows Installer or Windows Zip file for non admin install. I would recommend you choose the installer file because the other file is a portable IDE, which may show errors with the drivers of the Arduino.
You will see the following page after clicking on any of the files:
You may Contribute and Download or Just Download.
After downloading the Windows Installer file, you will have an .exe file. Double click on the file to get started. I would recommend right clicking on the .exe file and selecting Run as Administrator,as shown in the image below:
The rest of the processes are similar to an ordinary installation.
On most modern Linux distributions you can download the Linux 32 bits or Linux 64 bits depending on your system architecture.
On Ubuntu and most Debian-based distributions you can use the command line to download and install the IDE from official package sources. Open your terminal (Ctrl+Alt+T) and write the following line and press Enter.
sudo apt-get install arduino arduino-coreYou will be prompted to enter your password. Enter your password and hit Enter:
You will be shown the following message on your terminal. Type Y and hit Enter:
If no error occurs, you will see the following screen:
To install Arduino IDE for mac, download the Mac OS X 10.7 Lion or newer file. You will see the Arduino.app file in your download folder. Double click on the file and you will first see a message that says verifying the Arduino.app. Then you will see a warning as the following image:
Click Open to open the Arduino IDE.
You may not see the option Open. All you need is to press the Control/Option Key of your mac and click on the Arduino.app file, and you will see the Open option.
You may copy the Arduino.app file into your Application folder for frequent access.
Now we have downloaded and installed the Arduino IDE on our system, let's explore the IDE.
In Windows, just open the installed Arduino software. On Ubuntu/Linux, type the following line in the terminal and hit Enter:
sudo ArduinoOn the mac, just open the Arduino.app file.
No matter which operating system you use, you will see almost the following screen on every platform:
At the top of the screen you will find all the menus for the Arduino Uno. Before learning anything, let's configure our Arduino Uno with the Arduino IDE. We have already connected our Arduino to our PC via an A-B cable. Now, from the menu, go to Tools | Board and select Arduino Uno. Then go to port and select the port on which the Arduino is connected:
If you find difficulty in selecting the port, please have a look at Chapter 10, Few Error Handlings.
If everything looks good, then we are now ready to write code for Arduino and upload it to the board.
Let's explore all the menus and their functions. In the File menu you will see the following options:
Line numbers option for the debugging suitability:
In the Edit menu, you will see the following options:
In the Sketch menu you will see the following options:
