O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Beginning C for Arduino: Learn C Programming for the Arduino and Compatible Microcontrollers

Book Description

Beginning C for Arduino is written for those who have no prior experience with microcontrollers or programming but would like to experiment and learn both. This book introduces you to the C programming language, reinforcing each programming structure with a simple demonstration of how you can use C to control the Arduino family of microcontrollers. Author Jack Purdum uses an engaging style to teach good programming techniques using examples that have been honed during his 25 years of university teaching.

Beginning C for Arduino will teach you:

  • The C programming language

  • How to use C to control a microcontroller and related hardware

  • How to extend C by creating your own library routines

During the course of the book, you will learn the basics of programming, such as working with data types, making decisions, and writing control loops. You'll then progress onto some of the trickier aspects of C programming, such as using pointers effectively, working with the C preprocessor, and tackling file I/O. Each chapter ends with a series of exercises and review questions to test your knowledge and reinforce what you have learned.

What you'll learn

  • The syntax of the C programming language as defined for the Arduino

  • Tried and true coding practices (applicable to any programming language)

  • How to design, code, and debug programs that drive Arduino microcontrollers

  • How to extend the functionality of C

  • How to integrate low cost, off-the-shelf, hardware shields into your own projects

Who this book is for

The book is aimed at a complete novice with no programming background. It assumes no prior programming or hardware design experience and is written for creative and curious people who would like to blend a software and hardware learning experience into a single, enjoyable endeavor.

Table of Contents

  1. Titlepage
  2. Dedication
  3. Contents at a Glance
  4. Contents
  5. About the Author
  6. About the Technical Reviewer
  7. Acknowledgments
  8. Introduction
  9. Chapter 1: Introduction
    1. Assumptions About You
    2. What You Need
    3. Verifying the Software
    4. Verifying the Hardware
    5. Loading and Running Your First Program
    6. Summary
  10. Chapter 2: Arduino C
    1. The Building Blocks of All Programming Languages
    2. The Five Program Steps
    3. A Revisit to the Blink Program
    4. Summary
    5. Exercises
  11. Chapter 3: Arduino C Data Types
    1. The boolean Data Type
    2. The char Data Type
    3. The byte Data Type
    4. The int Data Type
    5. The word Data Type
    6. The long Data type
    7. The float and double Data Types
    8. The string Data Type
    9. String Data Type
    10. The void Data Type
    11. The array Data Type
    12. Defining versus Declaring Variables
    13. Using the cast Operator
    14. Summary
    15. Exercises
  12. Chapter 4: Decision Making in C
    1. Relational Operators
    2. The if Statement
    3. A Modified Blink Program
    4. Software Modifications to the Alternate Blink Program
    5. The if-else Statement
    6. Cascading if Statements
    7. The Increment and Decrement Operators
    8. The switch Statement
    9. The goto Statement
    10. Getting Rid of Magic Numbers
    11. The C Preprocessor
    12. Heads or Tails
    13. Something to Think About
    14. Summary
    15. Exercises
  13. Chapter 5: Program Loops in C
    1. The Characteristics of Well-Behaved Loops
    2. Using a for Loop
    3. The while Loop
    4. The do-while Loop
    5. The break and continue Keywords
    6. A Complete Code Example
    7. Loops and Coding Style
    8. Summary
    9. Exercises
  14. Chapter 6: Functions in C
    1. The Anatomy of a Function
    2. What Makes a “Good” Function
    3. Writing Your Own Functions
    4. Logical Operators
    5. Writing Your Function
    6. Leap Year Calculation Program
    7. Passing Data Into and Back From a Function
    8. Summary
    9. Exercises
  15. Chapter 7: Storage Classes and Scope
    1. Hiding Your Program Data
    2. Statement Block Scope
    3. Local Scope
    4. Global Scope
    5. Scope and Storage Classes
    6. The volatile keyword
    7. Summary
    8. Exercises
  16. Chapter 8: Introduction to Pointers
    1. Defining a Pointer
    2. Using a Pointer
    3. Summary of Pointer Rules
    4. Why Are Pointers Useful?
    5. Pointers and Arrays
    6. Summary
    7. Exercises
  17. Chapter 9: Using Pointers Effectively
    1. Relational Operations and Test for Equality Using Pointers
    2. Pointer Arithmetic
    3. Two-Dimensional Arrays
    4. Two-Dimensional Arrays and Pointers
    5. Pointers to Functions
    6. The Right-Left Rule
    7. Summary
    8. Exercises
  18. Chapter 10: Structures, Unions, and Data Storage
    1. Structures
    2. Unions
    3. EEPROM Memory
    4. Data Logging
    5. Other Storage Alternatives
    6. Summary
    7. Exercises
  19. Chapter 11: The C Preprocessor and Bitwise Operations
    1. Preprocessor Directives
    2. Parameterized Macros
    3. Summary
    4. Exercises
  20. Chapter 12: Arduino Libraries
    1. Libraries
    2. Writing Your Own Library
    3. Setting the Arduino IDE to Use Your Library
    4. A Sample Program Using the Dates Library
    5. Summary
    6. Exercises
  21. Appendix A: Suppliers and Sources
    1. Suppliers
    2. Sources
  22. Appendix B: Electronic Components for Experiments
    1. Microcontroller Board
    2. Solderless Breadboard
    3. Electronic Components
    4. Online Component Purchases
    5. Experiment!
  23. Answers to Exercises
    1. Chapter 2
    2. Chapter 3
    3. Chapter 4
    4. Chapter 5
    5. Chapter 6
    6. Chapter 7
    7. Chapter 8
    8. Chapter 9
    9. Chapter 10
    10. Chapter 11
    11. Chapter 12
  24. Index