Arduino-Basics



Introduction

Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs (e.g., a finger on a button or a Twitter message) and can turn them into an output (e.g., activating a motor or publishing something online). You can tell your board what to do by sending a set of instructions to the microcontroller on the board.

All Arduino boards are completely open-source, empowering users to build them independently and eventually adapt them to their particular needs. The software, too, is open-source, and it is growing through the contributions of users worldwide.

Thanks to its simple and accessible user experience, Arduino has been used in thousands of different projects and applications. The Arduino software is easy-to-use for beginners, yet flexible enough for advanced users. It runs on Mac, Windows, and Linux. (Source: Arduino Guide)

Arduino Uno

The following course provides you with basic understandings of microcontrollers using Arduino as an application example. Furthermore, a short introduction to C programming is provided.

Embedded Systems

Embedded systems are computer systems in which a processor is embedded in a technical device. The processor takes over all monitoring, control, and operating functions. In general, the processor of an embedded system is a microcontroller.

In contrast to PC processors, the memory as well as inputs and outputs are usually integrated into a single chip. This is a processor with different peripheral interfaces.

Microcontrollers can be separated by the number of bits of the internal data bus (4-bit, 8-bit, 16-bit, 32-bit, etc.). This number describes the length of data which can be processed by the microcontroller. The biggest 8-bit representable number is 255, thus an 8-bit microcontroller can only add numbers less than or equal to 255 in one step. Adding higher numbers results in a longer calculation time as several commands are necessary.

Embedded systems are not always required to have a user interface. However, they can have I/O interfaces to handle analog or digital data.

Characteristics

Systems are optimized for (examples)

Difference between Universal Computers and Embedded Systems

Universal Computer Embedded System
Optimized for high processing power and graphics performance Optimized system solutions where the microcontroller is often not visible
High compatibility with application software Provides comprehensive functions. Available with sensor and actuator interfaces
Uses a variety of programs for individual applications Is optimized for a specific application
Computers are interchangeable, software is the capital good Is often designed as a real-time system
Mostly uses standardized Operating Systems, e.g., MacOS

Software Development

In conventional, so-called "self-hosted" computers (like your Windows PC), the development system and the target system are identical. The development system generally has a highly specialized and highly integrated interface that includes all the tools of software development (e.g., Visual Studio).

In the case of embedded systems, the operating system of the target can offer only basic functionalities like, for example, loader, debugger, or scheduler. Therefore, the host system is different from the target system, which makes configuration and debugging more complicated than for "self-hosted" computers. Cross development is necessary.

In this case, the programming and compiling are done using development tools on the host computer. Furthermore, a connection to the target system is established using a communication interface (e.g., ISP or Ethernet interfaces).

Cross Development using a host computer and an embedded system

Programming in C

Variables

A variable is a name given to a storage area that programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

Standard Data Types for C Programming (Typical Values)

Integer Types

Datatype Size Range
signed char 1 Byte -128 to 127
unsigned char 1 Byte 0 to 255
unsigned short 2 Bytes 0 to 65535
signed short 2 Bytes -32768 to 32767
unsigned int 4 Bytes 0 to (2^{32}-1)
signed int 4 Bytes (-2^{31}) to (2^{31}-1)
unsigned long 8 Bytes 0 to (2^{64}-1)
signed long 8 Bytes (-2^{63}) to (2^{63}-1)

Floating Point Types

Datatype Size Range Precision
float 4 Bytes 1.2E-38 to 3.4E+38 6 decimal places
double 8 Bytes 2.3E-308 to 1.7E+308 15 decimal places

Void Type

The void type specifies that no value is available. It is used in three kinds of situations:

Variables can be defined like this (examples):

Operators

The C language has different types of operators:

Arithmetic Operators

Operator Description Example
+ Adding A + B = 30
- Subtracting A - B = -10
* Multiplication A * B = 200
/ Division B / A = 2
% Modulus B % A = 0
++ Increment value by one A++ = 11
-- Decrement value by one A-- = 9

Relational Operators

Comparison Operators
Comparison <, >, <=, >=
Equality ==
Inequality !=

Logical Operators

Operator Symbol
And &&
Or ||
Not !

Bitwise Operators

Bitwise operators work on bits and perform bit-by-bit operations.

Operator Symbol
Binary And &
Binary Or |
Binary XOR ^
Binary Left Shift <<
Binary Right Shift >>

Decision Making

Decision-making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Loops

Loops are used if a block of code must be executed several times. It is possible to use one or more loops inside any other loop.

Loop control statements change execution from its normal sequence.

Functions

A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.

The general form of a function definition in C programming language is as follows:

return_type function_name(parameter list)
{
    body of the function
}

The return type specifies the data type of the value which is returned by the function, for example int. If no value is returned, the return type is void. The function name is the actual name of the function. The parameters act like a placeholder to pass values to the function. The function body contains the statements which define what the function does.

Example

/* function returning the max between two numbers */
int max(int num1, int num2)
{
    /* local variable declaration */
    int result;

    if (num1 > num2)
        result = num1;
    else
        result = num2;

    return result;
}

To use the function, it must be called in the program. Therefore, the required parameters and the function name must be passed. If the function returns a value it must be stored in a variable.

/* calling a function to get max value */
ret = max(a, b);

Note: Variables inside a function or a block are called local variables. Variables outside of all functions are called global variables. Variables in the definition of function parameters are called formal parameters.

Arrays

Arrays can store a fixed-size sequential collection of elements of the same type.

In this case, the number of values between braces {} cannot be larger than the number of elements that we declare for the array between square brackets [].

If the array should just be big enough to hold the initialization, an empty square bracket is used: double size[] = {10.0, 2.0, 3.4, 9.4};

An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array: timSize = size[2];. This will take the third element from the array and assign it to the variable "timSize".

Pointers

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. It is used for example for dynamic memory allocation.

Strings

Strings are actually one-dimensional arrays of characters terminated by a null character \0. Thus a null-terminated string contains the characters that comprise the string followed by a null.

Example: char word[] = "Hello";

Structures

Structures is a user-defined data type available in C that allows combining data items of different kinds. For example, it might be helpful to track different information about a person like name, age, and city.

Structures can be defined like this (structure tag is optional):

struct [structure tag]
{
    member definition;
    member definition;
    ...
    member definition;
} [one or more struct variables];

Example

struct person
{
    char name[50];
    int age;
    char city[100];
};
/* Initialization */
struct person tim; /* Declare tim of type person */

/* Access member of structure */
timAge = tim.age;

To access any member of a structure, we use the member access operator (.).

Arduino Programming

An Arduino program, known as a "Sketch," consists of the setup function for one-time initialization during startup and the loop function for the endless loop.

Difference between Arduino and Conventional C

The Arduino has a minimal Operating System. The program consists of an initialization part and an endless loop. As it can be seen in examples, the Arduino development platform encapsulates many cryptic functions and the processor code. Additionally, the Arduino code is significantly larger in size than code written in conventional C.

Arduino vs C

Arduino vs. Microcontroller Programming

Microcontrollers can be programmed in different languages. A very hardware-related, low-level language is Assembler. It is used to create code for a specific device or a particular computer architecture and is typically selected for time or memory critical tasks.

In contrast to Assembler, Arduino has a minimal Operating System and library functions to interact with the hardware. Thus, it is portable across multiple systems (the generated code can be used for different microcontrollers). Programming of μC-Register is done with the help of the library functions. This leaves the programmer free to take care of their individual program.

Arduino Basic Commands

Read and Write

Useful Arduino Commands

Create Own Functions (Example)

void blink(int thePin, int time)
{
    digitalWrite(thePin, HIGH);
    delay(time);
    digitalWrite(thePin, LOW);
    delay(time);
}

// Calling the function
blink(3, 1000);

Switch-Case

Used to test if a variable is in a specific condition.

switch (myVariable)
{
    case 1:
        command1;
        break;
    case 2:
        command2;
        break;
    default:
        command3;
        break;
}

Note: For more information, please refer to the Arduino page.

Note: For most steps, Arduino examples are available in the Arduino IDE. Use these examples!