Skip to content

Supplementary Signposting Document to Accompany The Introductory Exercises

Pre-face

This document is designed for students with little or no C programming experience, though it serves as a useful signposted reference for all students. Beginners can use this guide alongside the Introductory Exercises document. It provides structured links to the Learn-C tutorial website, C-Programming.com, the Arduino programming reference, and relevant sections of the Introductory Exercises document.

Students with existing C programming experience may skip this document and proceed directly to the Introductory Exercises, or advance straight to the first assessed exercise: Basic: LED Pattern

Introduction

In previous years, it has become evident that some students enrolled in the Mechatronics module have no prior experience programming in C/C++ or working with Arduino and microcontroller-based embedded systems. Consequently, these students often struggle with the fundamentals of the laboratory exercises due to a lack of introductory C programming skills.

Providing comprehensive C programming instruction is beyond the scope of this module and the allocated teaching contact hours. Since numerous high-quality online learning resources already exist, this document provides signposts to key resources covering essential C programming concepts required for the laboratory activities.

Note

This document does not provide an exhaustive list of all programming topics required for the module. You may need to explore additional resources independently to further develop your programming knowledge and skills.

This guide highlights curated tutorial resources from Learn-C, reference guides from C-Programming.com, and official language references and tutorials from the Arduino website.

You are expected to self-guide your learning to build the required skills for the laboratory exercises and coursework project. If you discover additional high-quality programming resources that would benefit future cohorts, please share them with the teaching team so they can be integrated into future course updates.

The remaining subsections outline key topics and resources to support you throughout the laboratory activities and project.

External tutorial websites linked from this document

The tutorials on Learn-C focus on standard C programming for desktop operating systems (Windows, Linux, macOS) rather than embedded systems like the Arduino platform. Consequently, there are minor structural differences between standard C desktop programs and Arduino programs (referred to as "sketches"). Nevertheless, Learn-C provides a solid foundation in core C programming concepts, which we supplement with signposted material from the Arduino website.

Each Learn-C tutorial includes a brief concept overview and an embedded online compiler, allowing you to execute and test code snippets directly within your browser.

Another valuable resource is C-Programming.com. While also focused on desktop C, it offers a more in-depth explanation of core syntax. We recommend focusing primarily on the lessons within the ”Introduction and Basic C Features” section of the C-Tutorial

Note

The following sections curate specific lessons from Learn-C and C-Programming.com, pairing them directly with corresponding concepts in the Arduino Language Reference. We recommend reviewing these resources in the order presented below.

Arduino Language Reference

The Arduino programming referenceis an official documentation hub detailing core Arduino functions, variables, and programming structures. While the full reference may initially appear complex to beginner programmers, it is an essential resource for reviewing function syntax as your skills develop.

Note

Relevant entries from the Arduino programming reference are linked directly throughout this document, so you do not need to read through the entire reference page before starting.

Arduino Build-in Examples within the Arduino IDE

The Arduino Tutorials Portal provides a wide range of learning materials, from beginner guides on hardware and software setup to detailed walkthroughs of the Arduino IDE’s build-in examples. It also features links to community projects on the Arduino Project Hub, and advanced hardware integration guides.

Many of the guides in the built-in examples and tutorials sections will serve as helpful technical references when designing your coursework project.

Introduction to Programming an Arduino in C.

The remainder of this document provides a structured set of signposted links to help you build a solid understanding of C programming fundamentals. It interweaves tutorials from Learn-C with corresponding entries from the Arduino programming referenceand exercises from the Introductory Exercises guide.

The Arduino API consists of a series of C/C++ functions and libraries that you can invoke within your code. While the native language underlying the Arduino framework is C++, this module focuses exclusively on procedural C programming.

For interested students, a brief overview and history of the C programming language is available on Wikipedia site.

Hello World Example

A standard entry point when learning any programming language is the "Hello World" program (see the “Hello World” tutorial on Learn-C. This example demonstrates basic program structure and syntax by printing the string "Hello World" to the Serial Monitor output.

The Standard Output in Arduino

Unlike full desktop operating systems, microcontrollers do not have a built-in terminal display or standard console output. In Arduino development, standard output text streams are directed over USB to the Serial Sonitor within the Arduino IDE. This will be discussed further in a later section.

Basic Syntax Elements

The following fundamental syntax elements are required for valid C/Arduino code execution:

  • Semicolon (;): Terminates individual statements and instruction lines.
  • Curly braces ({ }): Defines statement blocks, function bodies, and control flow scopes.

Code Comments

Comments provide human-readable explanations within your source code. You should write clear, concise comments to explain complex implementation details and design choices. Comments are essential for debugging and collaborating, and they must be kept up to date whenever code is modified.

Preprocessor Directives

Preprocessor directives configure your program before compilation:

  • #define (Macro Definition): Replaces literal values or code snippets across your sketch before compilation. Defining global constants with #define improves readability and simplifies maintenance. However, complex logic macros should generally be replaced by inline functions or typed const variables.

  • #include (Include Library): Links external software libraries or header files into your sketch (for instance, including <Servo.h> to control servo motors).

Variables and Numbers

A critical aspect of embedded systems programming is selecting appropriate variable data types to manage limited SRAM memory efficiently. The Variables and Types tutorial on Learn-C provides a basic introduction to declaring primitive types. However, embedded microcontrollers require careful consideration of variable ranges and type casting. For a comprehensive overview of data types, bit-widths, and conversion utility functions (such as byte(), int(), and float()), refer to the variables section of Arduino Language Reference

For additional background on declaration syntax and variable scope, see the Introduction to C lesson from C-Programming.com)

Arrays and Data Structures

An array is a indexed collection of variables of the same data type stored sequentially in memory.

  • Single-Dimensional Arrays: Learn how to declare, initialize, and access zero-indexed data elements using the Learn-C Arrays Tutorial.

  • Multidimensional Arrays: Used to store grid-based or tabular data structures using multiple indices (e.g., rows and columns in a 2D matrix). Review the Learn-C Multidimensional Arrays Tutorial.

For an in-depth reference on array manipulation, pointer-array relationships, and memory allocation in C, consult lesson-8 of C Tutorial in C-Programming.com.

Functions

Functions are self-contained blocks of code designed to perform specific tasks. They modularize code, improve readability, and eliminate code duplication by allowing repeated execution from multiple call sites. When writing Arduino sketches, you will utilize both built-in API functions (such as pinMode() and digitalWrite()) and custom functions created for your own application logic.

For introductory walkthroughs on function prototypes, parameters, and return types, see thethe Learn-C, Functions tutorial and the Functions in C lesson from C-Programming.

The "Blink" sketch serves as the initial test exercise in the Introductory Exercises guide You can open it directly within the Arduino IDE via:

Files \(\rightarrow\) Examples \(\rightarrow\) 01.Basics \(\rightarrow\) Blink

Refer to the official Arduino Blink Built-in Example Guide for circuit diagrams and line-by-line code breakdowns.

Resistor Value Difference

While the standard Arduino online tutorial specifies a \(220\,\Omega\) current-limiting resistor, Exercise 1 instructs you to use a \(470\,\Omega\) resistor to protect the lab kit LEDs.

The Blink example validates that your software environment, compiler, and USB communications interface are functioning correctly. An Arduino sketch is structured into three primary regions:

  • Global Scope: Located at the top of the sketch, this area is used for defining global variables, importing libraries via #include, and creating preprocessor macros using #define.
  • setup() function: Executes once when the board powers on or resets. It is used to initialize GPIO pin modes (pinMode()), set up hardware peripherals, and configure communication baud rates.
  • loop() function: - Executes repeatedly after setup() completes. This function contains your main control algorithm, continuously sampling inputs and updating outputs.

Program Execution and Timing Delays

The Blink sketch utilizes the delay() function to pause program execution for a specified duration in milliseconds.

To implement precise iterative or conditional timing in your code, review the following iteration structures:

  • while loops: Iterates a block of code continuously as long as a specified conditional condition remains true.
  • For Loops: Iterates a block of code a predetermined number of times using an internal counter variable.

For detailed syntax examples, consult the Learn-C For Loops Tutorial and While Loops Tutorial, the Loops in C lesson from C-Programming.com or the official Arduino entries for the for loop and while loop.

Decision Making and Control Flow

A core requirement of embedded control systems is making operational decisions based on sensor inputs, system states, and logical conditions. The Conditions tutorial introduces conditional execution using the if statement in C.

For Arduino-specific syntax and conditional logic, consult the official documentation for the if statement, as well as the relational and boolean logical operators detailed in the Structure of the Arduino language Reference. A detailed discussion on conditional structures is also available in the if statement

Switch-Case Control Structures

The switch...case statement provides an efficient alternative to lengthy if...else if chains when branching execution based on specific discrete integer values of a control variable:

Finite State Machines (FSM)

switch...case structures are widely used to implement Finite State Machines (FSM). A Finite state machines is an architectural model that organizes complex, event-driven, or sequential system behavior into a set of discrete operational states. While advanced state machine implementations fall outside the core scope of the laboratory exercises, utilizing an FSM architecture can greatly simplify software development for your final coursework project.

Serial Monitor and Communications

As noted previously, Arduino microcontrollers lack a traditional desktop console display for standard output. Instead, serial communication is used to transmit debug text, system messages, and real-time variable values between the microcontroller and a connected host computer. The built-in Serial Monitor in the Arduino IDE serves as an integrated terminal for viewing these text streams.

For a practical guide to configuring and using the Serial Monitor, see the Arduino IDE Serial Monitor Tutorial

Serial API Functions

Transmitting data to the Serial Monitor requires initializing and calling functions from the hardware Serial Class:

  • Serial.begin(baudRate): Initializes serial communication and sets the data transmission rate in bits per second (e.g., Serial.begin(9600);). This must be called inside setup().

  • Serial.print(val): Prints data to the serial port as human-readable ASCII text.

  • Serial.println(val): Prints data to the serial port followed by a carriage return and newline character (\r\n).

The string data type

Before outputting text data to the Serial Monitor, it is essential to understand how text strings are represented and stored in memory. Review the Strings Tutorial on Learn-C for core concepts.

In embedded C and Arduino development, text can be represented using two distinct approaches:

  • C-Style Character Arrays (char[ ]): Null-terminated arrays of characters. This is the memory-efficient standard for resource-constrained microcontrollers.

  • Arduino String Object (String): A dynamic object wrapper that provides convenient helper methods for string manipulation. Note: Frequent dynamic allocation of String objects can fragment limited SRAM; C-style character arrays are preferred for complex sketches.

, and being aware of the String as an array of characters section and the String DataType object section of the Arduino language reference.

Introductory Exercise 2: Push Button and LED

The Push button and LED Exercise builds upon the standard built-in Arduino Button Tutorial.

The code provided in the laboratory worksheet demonstrates digital input sampling (digitalRead()) and illustrates how to use the Serial Monitor for real-time state logging and variable debugging.

Final Comment

The signposted background C programming resources provided in this guide cover the essential syntax and concepts required to complete the laboratory exercises in this module.

You are encouraged to explore additional online or print resources to strengthen your C programming skills independently. If you find high-quality tutorials or documentation that would enrich this signposting guide, please notify the teaching staff so we can consider integrating them into future course revisions.