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

Learning C for Arduino

Book Description

Ultimate guide for programming Arduino with C

About This Book

  • Get hands-on experience with the Ardruino board and learn to control it with your programming skills
  • Learn the essential concepts of C such as variables, data structures, functions, loops, and pointers
  • Work with electronic devices such as LEDs, switches, and motors and connect them to Arduino using C

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.

What You Will Learn

  • Play with mathematical operations using C
  • Use logical operations and loops to play with LEDs and the Arduino board
  • Create custom functions using C and connect an SD card to the Arduino
  • Use Object-oriented Programming to connect a GSM module to the Arduino board
  • Play with an LCD board and Servo using standard Arduino libraries
  • Build projects using Arduino such as a LED cube, a smart weather system, and home security
  • Identify and fix common errors on an Arduino board

In Detail

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.

Style and approach

This comprehensive step-by-step guide starts with the basic concepts of C for your Arduino board. It will teach you how to leverage C to explore the capabilities of Arduino.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Learning C for Arduino
    1. Learning C for Arduino
    2. Credits
    3. About the Author
    4. About the Reviewer
    5. www.PacktPub.com
      1. Why subscribe?
    6. Customer Feedback
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Errata
        2. Piracy
        3. Questions
    8. 1. Getting Started
      1. Arduino
        1. Types of Arduino
          1. Arduino Uno
          2. Arduino Mega
          3. Arduino Nano
          4. Arduino Leonardo
          5. Arduino Mini
          6. Arduino Micro
      2. Exploring Arduino Uno
        1. USB port
        2. External power jack
        3. Analog pins
        4. Digital pins
        5. ICSP
        6. Microcontroller
        7. Reset button
        8. Connecting Arduino to PC
      3. Downloading and installing Arduino IDE
        1. Downloading Arduino IDE
        2. Installing Arduino IDE
          1. Installing Arduino IDE on Windows
          2. Installing Arduino IDE on Ubuntu/Linux
          3. Installing Arduino IDE on Mac
      4. Explore Arduino IDE
      5. Summary
    9. 2. Our First Program!
      1. Connecting your Arduino
      2. Hello Arduino!
        1. Dissecting our first code
        2. setup() function
        3. loop() function
      3. Things to remember
        1. Watch your case
        2. Don't forget your semicolon!
        3. Adding both Setup() and Loop() functions
        4. Minding the baud rate
        5. Formatting your code
        6. Turning the LED On
        7. Blink a LED
        8. Commenting
        9. Try at home
          1. Keyboard shortcuts
      4. Summary
    10. 3. Exploring C with Arduino IDE
      1. Variables
        1. Exercise
      2. Data types
        1. Integers
        2. Rational numbers or fractions
        3. Characters and strings
        4. Booleans
      3. Collecting and showing data through serial port
        1. Mathematical operations
          1. Addition
          2. Subtraction
          3. Exercise
          4. Multiplication
          5. Division
          6. Modulus
        2. Arrays
        3. Exercise
      4. Strings and operations with strings
        1. Conversion of strings
        2. Exercise
      5. Summary
    11. 4. Blinking with Operations and Loops
      1. Expression in C
        1. Logical operations in C
          1. If-statement
          2. Nested if
      2. Logical operators
        1. AND operator
        2. OR operator
        3. NOT operator
        4. If-else
        5. Switch-case
      3. Exercises
      4. Loops
        1. for loop
          1. Nested for loop
        2. While loop
        3. do-while loop
      5. Exercise
      6. Summary
    12. 5. Functions and Files with Arduino
      1. Functions
        1. Types of functions
          1. Functions with no arguments and a return value
          2. Function with arguments and no return value
          3. Functions with arguments and a return value
          4. Usages of functions
          5. Exercise
      2. Data logging
        1. File handling
      3. Connecting an SD card to your Arduino
        1. Formatting the SD/Micro SD card
        2. Connecting the module to the Arduino
        3. Naming your data file
        4. Reading a file
        5. Importing a library
        6. Setting CS/SS pin number
      4. Writing on a file
        1. Exercise
      5. Summary
    13. 6. Arduino and C++
      1. Object Oriented Programming
        1. Objects
        2. Class
      2. Going deeper into class
      3. Understanding OOP better
      4. Fundamentals of OOP
        1. Encapsulation
        2. Data Abstraction
          1. Constructor
          2. Virtual function
        3. Polymorphism
          1. Pointer and reference in polymorphism
          2. Method overload
          3. Method overriding
        4. Inheritance
        5. Exercises
        6. Benefits of OOP
        7. Using OOP with Arduino
      5. GSM modules
        1. Types of GSM module
        2. Getting to know SIM900A mini better
        3. Connecting the GSM Shield to the Arduino
      6. Arduino library
      7. Making a call using GSM module
        1. AT commands
          1. Usages and rules of AT Commands
      8. Sending an SMS
      9. Receiving an SMS
      10. Summary
    14. 7. Using Pointers and Structure
      1. Pointers
        1. Declaring a pointer
        2. Reference point
        3. Types of pointers
      2. Pointers and arrays
      3. Usages of pointers
      4. Learning about structure
      5. Structure and function
        1. Pass by value
        2. Pass by reference
      6. Nested structure
      7. Exercise
      8. Summary
    15. 8. Working with Arduino Libraries
      1. Arduino library
        1. In Windows
        2. In OSX
        3. Linux OS
        4. Types of libraries
          1. The Servo library
        5. Exploring Servo.h library
        6. Knob the Servo
        7. Sweep with Servo
      2. The Firmata library
      3. The SoftwareSerial library
        1. The LiquidCrystal library
      4. Uses of LiquidCrystal Library
        1. The Stepper library
      5. Installing a library
      6. Removing a library
      7. Creating our own library
      8. Summary
    16. 9. Lets Build Something Awesome
      1. LED cube
      2. Smart weather system
      3. Home security
      4. Summary
    17. 10. Few Error Handlings
      1. Few common errors
        1. Arduino is connected but cannot be found by the computer
        2. Cannot upload programs to the Arduino board
          1. Arduino software
          2. Update the drivers
          3. Access to the serial port
          4. Physical connection
      2. Auto-reset
      3. Bootloader
      4. java.lang.StackOverflowError
      5. Arduino software freeze when I try to upload a program
      6. Board doesn't turn on (the green power LED doesn't light up)
      7. Get an error when launching arduino.exe on Windows
      8. Could not find the main class
      9. Cygwin conflicts on Windows
      10. Tools | Serial Port menu is not visible
        1. gnu.io.PortInUseException on Mac
      11. Sketch appear to upload successfully but not do anything
      12. Undeclared functions or undeclared types error
      13. Invalid device signature when trying to upload a sketch
      14. 'xxx' does not name a type error
      15. Library is not Installed properly
      16. Wrong folder location
      17. Library dependencies
      18. Wrong library name
      19. Wrong folder name
      20. Forgot to close the Arduino IDE
      21. Check your hardware connections
      22. Check the coding syntax error
      23. Use serial monitor
      24. Stay connected with the Arduino forum
      25. Summary