Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
C Programming for Arduino
C Programming for Arduino

C Programming for Arduino: Building your own electronic devices is fascinating fun and this book helps you enter the world of autonomous but connected devices. After an introduction to the Arduino board, you'll end up learning some skills to surprise yourself.

eBook
$9.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

C Programming for Arduino

Chapter 2. First Contact with C

In my life as a programmer, I encountered a lot of compiler-based as well as scripting languages. One of the lowest common denominators has always been the C language.

In our case, this is embedded system programming, which is another name for hardware programming; this first statement is also true.

Let's check what C programming really is and let's enter into a new world, that is, the realm of Arduino programming. We'll also use a very necessary feature called serial monitoring. This will help us a lot in our C learning, and you'll understand that this feature is also used in real-life projects.

An introduction to programming


The first question is, what is a program?

A program is text that you write using a programming language that contains behaviors that you need a processor to acquire. It basically creates a way of handling inputs and producing outputs according to these behaviors.

According to Wikipedia (http://en.wikipedia.org/wiki/Computer_programming):

Programming is the process of designing, writing, testing, debugging and maintaining the source code of computer programs.

Of course, this definition is very simple and it also applies to microcontrollers, as we already know that the latter are basically a type of computers.

Designing a program is the fact you have to think about first, before you begin coding it. It generally involves writing, drawing, and making schematics of all the actions you want your processor to make for you. Sometimes, it also implies to write what we call pseudocode. I hope you remember that this is what we created in the previous chapter when we wanted...

C and C++?


Dennis Ritchie http://en.wikipedia.org/wiki/Dennis_Ritchie) at Bell Labs developed the C programming language from 1969 to 1973. It is often defined as a general-purpose programming language and is indeed one of the most used languages of all times. It had been used initially to design the Unix operating system (http://en.wikipedia.org/wiki/Unix) that had numerous requirements, especially high performance.

It has influenced a lot of very well known and used languages such as C++, Objective-C, Java, JavaScript, Perl, PHP, and many others.

C is to both imperative and structured. It is very appropriate for both 8-bit and 64-bit processors, for systems having not only several bytes of memory but also terabytes too, and also for huge projects involving huge teams, to the smallest of projects with a single developer.

Yes, we are going to learn a language that will open your mind to global and universal programming concepts!

C is used everywhere

Indeed, the C language provides a lot of advantages...

The Arduino native library and other libraries


A programming library is a collection of resources that are available for use by programs.

They can include different types of things, such as the following:

  • Configuration data

  • Help and documentation resources

  • Subroutines and reusable part of code

  • Classes

  • Type definitions

I like to say that libraries provide a behavior encapsulation; you don't have to know how the behavior is made for using it but you just use it.

Libraries can be very specific, or can have a global purpose.

For instance, if you intend to design firmware that connects the Arduino to the Internet in order to grab some information from a mail server, and react by making an LED matrix blink in one way or another according to the content of the mail server's response, you have the following two solutions:

  • Code the whole firmware from scratch

  • Use libraries

Even if we like to code things, we are happier if we can focus on the global purpose of our designs, aren't we?

In that case, we'll try...

Checking all basic development steps


We are not here together to understand the entire details of code compilation. But I want to give you a global explanation that will help you to understand better how it works under the hood. It will also help you to understand how to debug your source code and why something wouldn't work in any random case.

Let's begin by a flowchart showing the entire process.

From the source code to the binary executable code

The following steps are executed to take the code from the source to the executable production stage:

  1. The C and C++ source code is just the type of code you already wrote for the Blink250ms project in Chapter 1, Let's Plug Things.

  2. Headers are usually included at the beginning of your code, and they refer to other files with the extension .h in which there are some definitions and class declarations. This kind of design, in which you have separate files for the source code (the program you are currently writing) and the headers (already made elements...

Using the serial monitor


The Arduino board itself can communicate easily using basic protocols for serial communication.

Basically, serial communication is the process of sending data elements over a channel, often named a bus. Usually, data elements are bytes, but it all depends on the implementation of the serial communication.

In serial communication, data is sent sequentially, one after the previous one. This is the opposite of parallel communication, where data are sent over more than one channel, all at the same time.

Baud rate

Because the two entities that want to communicate using serial communications have to be okay about the answer to the question "Hey, what is a word?", we have to use the same speed of transmission on both sides. Indeed, if I send 001010101010, is it a whole word or are there many words? We have to define, for instance, that a word is four-digits long. Then, we can understand that the previous example contains three words: 0010, 1010, and 1010. This involves a clock...

Making the Arduino talk to us


Imagine that you have followed carefully the Blink250ms project, everything is wired correctly, you double-checked that, and the code seems okay too, but it doesn't work.

Our LED isn't blinking at all. How to be sure that the loop() structure of your code is correctly running? We'll modify the code a bit in order to trace its steps.

Adding serial communication to Blink250ms

Here, in the following code, we'll add serial communication for the LED to blink every 250 ms:

  1. Open your previous code.

  2. Use Save As to create another project under the name TalkingAndBlink250ms.

    Note

    It is good practice to start from an already existing code, to save it under another name, and to modify it according to your needs.

  3. Modify the current code by adding all rows beginning with Serial as follows:

    /*
      TalkingAndBlink250ms Program
      Turns a LED connected to digital pin 8 on for 250ms, then off for 1s, infinitely.
      In both steps, the Arduino Board send data to the console of the IDE for...

Summary


In this chapter, we learned about programming using C language. We also learned how to use the serial monitoring feature of our Arduino IDE in order to know a bit more about what is happening in real time in our Arduino processor using traces.

I spoke about serial communication because it is very useful and is also used in many real-life projects in which you need a computer and an Arduino board to communicate among themselves. It can also be used between two Arduino boards or between Arduino boards and other circuits.

In the next chapter, we'll enter C code by using the serial monitoring window in order to make things a bit less abstract.

Left arrow icon Right arrow icon

Key benefits

  • Use Arduino boards in your own electronic hardware and software projects
  • Sense the world by using several sensory components with your Arduino boards
  • Create tangible and reactive interfaces with your computer
  • Discover a world of creative wiring and coding fun!

Description

Physical computing allows us to build interactive physical systems by using software & hardware in order to sense and respond to the real world. C Programming for Arduino will show you how to harness powerful capabilities like sensing, feedbacks, programming and even wiring and developing your own autonomous systems. C Programming for Arduino contains everything you need to directly start wiring and coding your own electronic project. You'll learn C and how to code several types of firmware for your Arduino, and then move on to design small typical systems to understand how handling buttons, leds, LCD, network modules and much more. After running through C/C++ for the Arduino, you'll learn how to control your software by using real buttons and distance sensors and even discover how you can use your Arduino with the Processing framework so that they work in unison. Advanced coverage includes using Wi-Fi networks and batteries to make your Arduino-based hardware more mobile and flexible without wires. If you want to learn how to build your own electronic devices with powerful open-source technology, then this book is for you.

Who is this book for?

This book is great for people who want to learn how to design & build their own electronic devices. From interaction design art school students to the do-it-yourself hobbyist, or even simply people who want to learn electronics, this book will help by adding a new way to design autonomous but connected devices.

What you will learn

  • Understand what an Arduino board is
  • Sense the world through a variety of digital inputs
  • Feel the world with analog sensors
  • Design a visual output feedback system
  • Use Max6 and Processing with Arduino
  • Connect your Arduino to wired and wireless networks
  • Add GPS localisation modules to your Arduino

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 17, 2013
Length: 512 pages
Edition : 1st
Language : English
ISBN-13 : 9781849517591
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : May 17, 2013
Length: 512 pages
Edition : 1st
Language : English
ISBN-13 : 9781849517591
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 129.97
Arduino Home Automation Projects
$25.99
Arduino Robotic Projects
$48.99
C Programming for Arduino
$54.99
Total $ 129.97 Stars icon
Banner background image

Table of Contents

13 Chapters
Let's Plug Things Chevron down icon Chevron up icon
First Contact with C Chevron down icon Chevron up icon
C Basics – Making You Stronger Chevron down icon Chevron up icon
Improve Programming with Functions, Math, and Timing Chevron down icon Chevron up icon
Sensing with Digital Inputs Chevron down icon Chevron up icon
Sensing the World – Feeling with Analog Inputs Chevron down icon Chevron up icon
Talking over Serial Chevron down icon Chevron up icon
Designing Visual Output Feedback Chevron down icon Chevron up icon
Making Things Move and Creating Sounds Chevron down icon Chevron up icon
Some Advanced Techniques Chevron down icon Chevron up icon
Networking Chevron down icon Chevron up icon
Playing with Max 6 Framework Chevron down icon Chevron up icon
Improving your C Programming and Creating Libraries Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.8
(11 Ratings)
5 star 27.3%
4 star 9.1%
3 star 18.2%
2 star 9.1%
1 star 36.4%
Filter icon Filter
Top Reviews

Filter reviews by




Bloomington Bookworm Jun 24, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a great resource for Arduino users. It provides users with in depth information about the Arduino. Unlike most books on programming, Julien provides us the rationale for why many of the things are done the way they are. He starts with simple concepts, and takes them to a moderately complex level.The final chapters cover some adanced ground, though not so much as to overwhelm. Here is a nice tidbit that I don't think is documented in the Arduino reference: you can address the ports directly using the Arduino IDE. For example, pins 8-15 are in port B. They can be set by putting in a line like:PORTB=B00111111 (p 486).This will make pins 8-13 outputs, using just a single line of code, instead of a for loop.There are many other useful tips as well as explanations.The example files can be downloaded from packpub.com.I found this book very enjoyable to read. I had no trouble understanding the English. Occasionally, the grammar comes across as odd. But this is first and foremost a technical book. And we are fortunate that the Arduino is part of a great International community.
Amazon Verified review Amazon
val m Apr 01, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I found this "big book" really useful, it teaches you the basics to wire, code and develop projects; it explains concepts, gives examples and helps understand C programming. Also very interesting the part dedicated to Max6.Personally, I carry it with me and used it as a reference when I'm working on my own projects.Julien is very professional, his method is very comprehensive and his approach is friendly and funny.In my opinion, this book is highly recommended to anyone interested in physical computing, C programming, Arduino, Interaction Design...
Amazon Verified review Amazon
Dannobe Jul 20, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
No Problems whatever....good Product !
Amazon Verified review Amazon
DamionWaltermeyer Aug 27, 2013
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I received a copy in exchange for a review.I'm not sure what the big deal is in a lot of the reviews I've seen. The grammar is no worse than many technical books I've read. It's like talking to someone who is really good at English, but not a native speaker; Occasionally you find yourself catching on a word or phrase, but usually you can figure it out pretty easily.I'm only a little more than halfway through it's 512 pages, but skimming to the end, I see it's much more in-depth than many of the Arduino books I've read. Simon Monk's intro to arduino is actually harder to read to my eyes(Though still a good book). The author spells out many things that are often glossed over and goes much farther than any of the introductory level Arduino books I've seen so far.Summary: A good book to dive into if you can handle some language issues and minor typos.
Amazon Verified review Amazon
Kevin Partridge Mar 03, 2015
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
I'm not going to quibble about grammar. What annoyed me was the lack of focus on C. In a booked entitled "C Programming for Arduino." I'm actually surprised no one else mentioned this.There isn't really a discussion of C per se. Programming the Arduino requires you to write in C/C++ so all books about Arduino discuss programming in C. A book that specifies it is about programming in C should emphasize actually learning C. The reader is left with a broken understanding of C. There's no mention of what the full C would look like coming out of the IDE - where's my main()? Can I alter memory addresses directly? Why might I do that?. How do I program the Arduino with a typical C toolset? This page supplies more of that information in 6 steps: http://www.wikihow.com/Write-Arduino-Software-in-C This is also a nice page: https://balau82.wordpress.com/2011/03/29/programming-arduino-uno-in-pure-c/ Both of these references were found with a quick google search. I expected a discussion of C and how it is used to program an Arduino. What I got was a general discussion of programming an Arduino.I would not have any real issue with this book if the title were something more general like, "Rapid Programming for Arduino from the Ground Up." The content is useful. The author covers material not discussed in the pages I noted above. He leads the newcomer through the Arduino world. Although he does so in a somewhat muddled way. There is good material here and if you are ok with a very informal approach then the book is fine. It just isn't a book on programming an Arduino in C. It's a fairly nice book about programming an Arduino in general. I really hope the author wasn't mislead by Packt into using a title that is incomplete at best and purposefully misleading in the worst case.The author should have included some discussion of topics such as that discussed in the previously mentioned links. If he had then most of the material could have remained the same. Leave out the other languages and frameworks. I'd give bonus points for showing how to implement code from Processing using C instead. Use QT or some other GUI framework for visualization.Other quibbles:Leave out the very brief summary of electricity. If the reader needs that then they really need more than is provided. There are other books for that. That section should have maybe been a glossary or appendix if that.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.