Learning C for Arduino: A comprehensive guide that will help you ace C's fundamentals using the powerful Arduino board
()
About this ebook
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.
Read more from Syed Omar Faruk Towaha
Building Smart Drones with ESP8266 and Arduino: Build exciting drones by leveraging the capabilities of Arduino and ESP8266 Rating: 0 out of 5 stars0 ratings
Related to Learning C for Arduino
Related ebooks
Learning C for Arduino Rating: 0 out of 5 stars0 ratingsArduino Essentials Rating: 5 out of 5 stars5/5Learn Arduino Prototyping in 10 days Rating: 0 out of 5 stars0 ratingsProgramming Arduino with LabVIEW Rating: 3 out of 5 stars3/5Building a Home Security System with Raspberry Pi Rating: 0 out of 5 stars0 ratingsArduino BLINK Blueprints Rating: 0 out of 5 stars0 ratingsEmbedded Programming with Modern C++ Cookbook: Practical recipes to help you build robust and secure embedded applications on Linux Rating: 0 out of 5 stars0 ratingsArduino Electronics Blueprints Rating: 4 out of 5 stars4/5Intel Galileo Essentials Rating: 0 out of 5 stars0 ratingsSmart Internet of Things Projects Rating: 4 out of 5 stars4/5Learning BeagleBone Python Programming Rating: 0 out of 5 stars0 ratingsAndroid for the BeagleBone Black Rating: 0 out of 5 stars0 ratingsArduino Home Automation Projects Rating: 3 out of 5 stars3/5ESP8266 Home Automation Projects: Leverage the power of this tiny WiFi chip to build exciting smart home projects Rating: 0 out of 5 stars0 ratingsESP8266 Robotics Projects: DIY Wi-Fi controlled robots Rating: 0 out of 5 stars0 ratingsArduino by Example Rating: 4 out of 5 stars4/5Mastering Arduino: A project-based approach to electronics, circuits, and programming Rating: 0 out of 5 stars0 ratingsIndustrial Cybersecurity: Efficiently secure critical infrastructure systems Rating: 5 out of 5 stars5/5Raspberry Pi Home Automation with Arduino - Second Edition Rating: 0 out of 5 stars0 ratingsIntel Edison Projects Rating: 0 out of 5 stars0 ratingsRaspberry Pi LED Blueprints Rating: 0 out of 5 stars0 ratingsComputer Vision with Python 3 Rating: 0 out of 5 stars0 ratingsMastering Linux Device Driver Development: Write custom device drivers to support computer peripherals in Linux operating systems Rating: 0 out of 5 stars0 ratingsHands-On Internet of Things with MQTT: Build connected IoT devices with Arduino and MQ Telemetry Transport (MQTT) Rating: 0 out of 5 stars0 ratingsLearning Swift Rating: 5 out of 5 stars5/5Mastering PyCharm Rating: 5 out of 5 stars5/5
Hardware For You
iPhone 16 Pro Max User Manual: The Complete Step-By-Step Guide to Maximize your New iPhone 16 Pro Max and iOS 18 Rating: 0 out of 5 stars0 ratingsCompTIA A+ Complete Review Guide: Exam Core 1 220-1001 and Exam Core 2 220-1002 Rating: 5 out of 5 stars5/5macOS Sonoma For Dummies Rating: 0 out of 5 stars0 ratingsCompTIA A+ Complete Review Guide: Core 1 Exam 220-1101 and Core 2 Exam 220-1102 Rating: 5 out of 5 stars5/5Exploring Arduino: Tools and Techniques for Engineering Wizardry Rating: 4 out of 5 stars4/5Chip War: The Fight for the World's Most Critical Technology Rating: 4 out of 5 stars4/5Electrical Engineering | Step by Step Rating: 0 out of 5 stars0 ratingsProgramming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5Apple Watch For Dummies Rating: 0 out of 5 stars0 ratingsiPhone For Dummies Rating: 0 out of 5 stars0 ratingsChromebook For Dummies Rating: 5 out of 5 stars5/5Amazon Web Services (AWS) Interview Questions and Answers Rating: 5 out of 5 stars5/5Unlock Any Roku Device: Watch Shows, TV, & Download Apps Rating: 0 out of 5 stars0 ratingsMastering Apple MacBook - MacBook Pro, MacBook Air, MacOS Ultimate User Guide Rating: 1 out of 5 stars1/5Raspberry Pi Electronics Projects for the Evil Genius Rating: 3 out of 5 stars3/5Build Your Own PC Do-It-Yourself For Dummies Rating: 4 out of 5 stars4/5Raspberry Pi Cookbook for Python Programmers Rating: 0 out of 5 stars0 ratingsTroubleshooting and Maintaining Your PC All-in-One For Dummies Rating: 5 out of 5 stars5/5Exploring Apple iPad: iPadOS 15 Edition: The Illustrated, Practical Guide to Using your iPad Rating: 0 out of 5 stars0 ratingsAfter Steve: How Apple Became a Trillion-Dollar Company and Lost Its Soul Rating: 4 out of 5 stars4/5CompTIA A+ Complete Study Guide: Core 1 Exam 220-1101 and Core 2 Exam 220-1102 Rating: 0 out of 5 stars0 ratingsArduino Electronics Blueprints Rating: 4 out of 5 stars4/5Upgrading and Fixing Computers Do-it-Yourself For Dummies Rating: 4 out of 5 stars4/5Linux All-in-One For Dummies Rating: 3 out of 5 stars3/5Raspberry Pi Home Automation with Arduino - Second Edition Rating: 0 out of 5 stars0 ratingsDancing with Qubits: How quantum computing works and how it can change the world Rating: 5 out of 5 stars5/5MacBook For Dummies Rating: 4 out of 5 stars4/5
Reviews for Learning C for Arduino
0 ratings0 reviews
Book preview
Learning C for Arduino - Syed Omar Faruk Towaha
Table of Contents
Learning C for Arduino
Credits
About the Author
About the Reviewer
www.PacktPub.com
Why subscribe?
Customer Feedback
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Errata
Piracy
Questions
1. Getting Started
Arduino
Types of Arduino
Arduino Uno
Arduino Mega
Arduino Nano
Arduino Leonardo
Arduino Mini
Arduino Micro
Exploring Arduino Uno
USB port
External power jack
Analog pins
Digital pins
ICSP
Microcontroller
Reset button
Connecting Arduino to PC
Downloading and installing Arduino IDE
Downloading Arduino IDE
Installing Arduino IDE
Installing Arduino IDE on Windows
Installing Arduino IDE on Ubuntu/Linux
Installing Arduino IDE on Mac
Explore Arduino IDE
Summary
2. Our First Program!
Connecting your Arduino
Hello Arduino!
Dissecting our first code
setup() function
loop() function
Things to remember
Watch your case
Don't forget your semicolon!
Adding both Setup() and Loop() functions
Minding the baud rate
Formatting your code
Turning the LED On
Blink a LED
Commenting
Try at home
Keyboard shortcuts
Summary
3. Exploring C with Arduino IDE
Variables
Exercise
Data types
Integers
Rational numbers or fractions
Characters and strings
Booleans
Collecting and showing data through serial port
Mathematical operations
Addition
Subtraction
Exercise
Multiplication
Division
Modulus
Arrays
Exercise
Strings and operations with strings
Conversion of strings
Exercise
Summary
4. Blinking with Operations and Loops
Expression in C
Logical operations in C
If-statement
Nested if
Logical operators
AND operator
OR operator
NOT operator
If-else
Switch-case
Exercises
Loops
for loop
Nested for loop
While loop
do-while loop
Exercise
Summary
5. Functions and Files with Arduino
Functions
Types of functions
Functions with no arguments and a return value
Function with arguments and no return value
Functions with arguments and a return value
Usages of functions
Exercise
Data logging
File handling
Connecting an SD card to your Arduino
Formatting the SD/Micro SD card
Connecting the module to the Arduino
Naming your data file
Reading a file
Importing a library
Setting CS/SS pin number
Writing on a file
Exercise
Summary
6. Arduino and C++
Object Oriented Programming
Objects
Class
Going deeper into class
Understanding OOP better
Fundamentals of OOP
Encapsulation
Data Abstraction
Constructor
Virtual function
Polymorphism
Pointer and reference in polymorphism
Method overload
Method overriding
Inheritance
Exercises
Benefits of OOP
Using OOP with Arduino
GSM modules
Types of GSM module
Getting to know SIM900A mini better
Connecting the GSM Shield to the Arduino
Arduino library
Making a call using GSM module
AT commands
Usages and rules of AT Commands
Sending an SMS
Receiving an SMS
Summary
7. Using Pointers and Structure
Pointers
Declaring a pointer
Reference point
Types of pointers
Pointers and arrays
Usages of pointers
Learning about structure
Structure and function
Pass by value
Pass by reference
Nested structure
Exercise
Summary
8. Working with Arduino Libraries
Arduino library
In Windows
In OSX
Linux OS
Types of libraries
The Servo library
Exploring Servo.h library
Knob the Servo
Sweep with Servo
The Firmata library
The SoftwareSerial library
The LiquidCrystal library
Uses of LiquidCrystal Library
The Stepper library
Installing a library
Removing a library
Creating our own library
Summary
9. Lets Build Something Awesome
LED cube
Smart weather system
Home security
Summary
10. Few Error Handlings
Few common errors
Arduino is connected but cannot be found by the computer
Cannot upload programs to the Arduino board
Arduino software
Update the drivers
Access to the serial port
Physical connection
Auto-reset
Bootloader
java.lang.StackOverflowError
Arduino software freeze when I try to upload a program
Board doesn't turn on (the green power LED doesn't light up)
Get an error when launching arduino.exe on Windows
Could not find the main class
Cygwin conflicts on Windows
Tools | Serial Port menu is not visible
gnu.io.PortInUseException on Mac
Sketch appear to upload successfully but not do anything
Undeclared functions or undeclared types error
Invalid device signature when trying to upload a sketch
'xxx' does not name a type error
Library is not Installed properly
Wrong folder location
Library dependencies
Wrong library name
Wrong folder name
Forgot to close the Arduino IDE
Check your hardware connections
Check the coding syntax error
Use serial monitor
Stay connected with the Arduino forum
Summary
Learning C for Arduino
Learning C for Arduino
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
Credits
About the Author
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.
About the Reviewer
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.
www.PacktPub.com
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 service@packtpub.com 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.
www.PacktPub.comhttps://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.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
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 customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Preface
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.
What this book covers
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.
What you need for this book
Software: Arduino IDE and Fritzing.
Who this book is for
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.
Conventions
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.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
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 feedback@packtpub.com, 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.
Customer support
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.
Errata
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
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 copyright@packtpub.com with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this book, you can contact us at questions@packtpub.com, and we will do our best to address the problem.
Chapter 1. Getting Started
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.
Note
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
Arduino is a microcontroller board. People also call it a prototype