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
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:
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
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
Each
Another valuable resource is
Note
The following sections curate specific lessons from
Arduino Language Reference
The
Note
Relevant entries from the
Arduino Build-in Examples within the Arduino IDE
The
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
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
Hello World Example
A standard entry point when learning any programming language is the "Hello World" program (see the "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
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.
Single Line Comment (//) : Ignores all text from the//sequence to the end of the line.Block Comment (/ ... /) : Ignores all text enclosed across single or multiple lines.
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#defineimproves readability and simplifies maintenance. However, complex logic macros should generally be replaced by inline functions or typedconstvariables. -
#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 byte(), int(), and float()), refer to the
For additional background on declaration syntax and variable scope, see the
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
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
Introductory Exercise 1: Blink Example
The "Blink" sketch serves as the initial test exercise in the
Files \(\rightarrow\) Examples \(\rightarrow\) 01.Basics \(\rightarrow\) Blink
Refer to the official
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 aftersetup()completes. This function contains your main control algorithm, continuously sampling inputs and updating outputs.
Program Execution and Timing Delays
The Blink sketch utilizes the
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
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 if statement in C.
For Arduino-specific syntax and conditional logic, consult the official documentation for the
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:
- Arduino Reference:
Switch/Case Syntax Guide . - C-Programming.com Guide:
Switch/Case Statements in C .
Finite State Machines (FSM)
switch...case structures are widely used to implement Finite State Machines (FSM). A
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
Serial API Functions
Transmitting data to the Serial Monitor requires initializing and calling functions from the hardware
-
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 insidesetup(). -
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
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 ofStringobjects can fragment limited SRAM; C-style character arrays are preferred for complex sketches.
, and being aware of the
Introductory Exercise 2: Push Button and LED
The
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.