Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
DIY Microcontroller Projects for Hobbyists
DIY Microcontroller Projects for Hobbyists

DIY Microcontroller Projects for Hobbyists: The ultimate project-based guide to building real-world embedded applications in C and C++ programming

Arrow left icon
Profile Icon Garcia-Ruiz Profile Icon Pedro Cesar Santana Mancilla
Arrow right icon
NZ$14.99 NZ$51.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5 (17 Ratings)
eBook Jul 2021 320 pages 1st Edition
eBook
NZ$14.99 NZ$51.99
Paperback
NZ$64.99
Subscription
Free Trial
Arrow left icon
Profile Icon Garcia-Ruiz Profile Icon Pedro Cesar Santana Mancilla
Arrow right icon
NZ$14.99 NZ$51.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5 (17 Ratings)
eBook Jul 2021 320 pages 1st Edition
eBook
NZ$14.99 NZ$51.99
Paperback
NZ$64.99
Subscription
Free Trial
eBook
NZ$14.99 NZ$51.99
Paperback
NZ$64.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

DIY Microcontroller Projects for Hobbyists

Chapter 2: Software Setup and C Programming for Microcontroller Boards

In this chapter, you will review the basic configuration of the IDEs used for programming the Blue Pill and Curiosity Nano microcontroller boards, as well as learn the basics of the C programming language necessary for coding applications for the Blue Pill and the Curiosity Nano. This is by no means a comprehensive C tutorial. It contains important information to understand and complete the exercises explained in all the chapters of this book. In this chapter, we're going to cover the following main topics:

  • Introducing the C programming language
  • Introducing Curiosity Nano microcontroller board programming
  • Introducing Blue Pill microcontroller board programming
  • Example – Programming and using the microcontroller board's internal LED

By the end of this chapter, you will have received a solid introduction to the C programming language, including a set of programming instructions useful for developing many small and mid-sized microcontroller projects with the Blue Pill and Curiosity Nano microcontroller boards. This chapter also covers the use of the internal LED, which both the Blue Pill and the Curiosity Nano have. This can be very useful for quickly showing digital results (for example, confirming actions in your project).

Technical requirements

The software that we will use in this chapter is the Arduino and MPLAB X IDEs for programming the Blue Pill and the Curiosity Nano, respectively. Their installation process was described in Chapter 1, Introduction to Microcontrollers and Microcontroller Boards. We will also use the same code examples that were used in the aforementioned chapter.

In this chapter, we will also use the following hardware:

  • A solderless breadboard.
  • The Blue Pill and Curiosity Nano microcontroller boards.
  • A micro USB cable for connecting your microcontroller boards to a computer.
  • The ST-LINK/V2 electronic interface needed to upload the compiled code to the Blue Pill. Remember that the ST-Link/V2 requires four female-to-female DuPont wires.

These are fundamental hardware components that will suffice for the examples described in this chapter, and will also prove useful in other more complex projects explained in other chapters.

The code used in this chapter can be found at the book's GitHub repository here:

https://github.com/PacktPublishing/DIY-Microcontroller-Projects-for-Hobbyists/tree/master/Chapter02

The Code in Action video for this chapter can be found here: https://bit.ly/3xwFvPA

The next section explains a concise introduction to the C programming language.

Introducing the C programming language

The C programming language was initially created in the early seventies for developing the UNIX operating system, but it has been ported to practically all operating systems ever since. It is a mid-level programming language because it shares properties from high-level languages such as Python and low-level languages, for example, the assembly language. The C language is generally easier to program than low-level languages because it is very human-readable and there are many libraries available that facilitate the development of software applications, among other reasons. It is also very efficient for programming embedded systems. C is one of the most popular coding languages, and virtually all microcontrollers can be programmed with C compilers – Blue Pill and Curiosity Nano are no exceptions.

The C language is not completely portable among different families and manufacturers of microcontrollers. For example, the I/O ports and the interrupts are not programmed the same in both Blue Pill and Curiosity Nano. That is why two types of C compilers and different libraries are needed for programming both microcontroller boards. In fact, the Arduino IDE used for programming the Blue Pill uses a variant of C called C++. C++ is a powerful extension of the C programming language that incorporates features such as object-oriented and low-memory level programming.

The following section explains the basics of the C language structure. This section includes an explanation of the #include directive, writing comments, understanding variables, using constants, a keywords list, declaring functions, evaluating expressions, and writing loops in C.

The basic structure of the C language

As with other programming languages, C makes it possible to declare program elements such as constants, types, functions, and variables in separate files called header files, which end in .h. This can help to organize C instructions and reduce clutter in your main C code. A library is a header file containing program elements (such as functions) that can be shared with other C programmers or constantly used in different C programs. C language compilers contain important libraries that we will use in this book. The header files can be included (that is, linked and compiled) along your main program using the #include directive; hence, the programming elements declared in the header file will be called and used in your C program.

There are many useful standard and non-standard libraries. We will review and use both. The #include directive is a special instruction for the C compiler and not a regular C instruction. It should be written at the beginning of the program and without a semicolon at the end. Only the C statements have a semicolon at the end. There are three ways to write and apply the #include directive. These are as follows:

  • #include <file_name.h>: This type of directive uses the less than and greater than symbols, meaning that the header file (.h) is placed in the compiler path. You don't need to write the complete path to the header file.
  • #include "file_name.h": This type of directive uses double quotes. The header file is stored in the project's directory.
  • #include "sub_directory_name/file_name.h": This directive type tells the compiler that the header file is placed in a sub-directory. Please note that the slash symbol is applied depending on the operating system that you are using. For example, Windows computers use a backslash (\) symbol as a directory separator. Linux and Mac computers use the forward-slash (/) symbol.

The next sub-section shows how to define and use header files.

Example of the #include directive

The following program example shows how to include a header file that is placed in the project's directory:

#include "main_file.h"
int main(void)
{
    x = 1;
    y = 2;
    z = x+y;
}

In the preceding example, the x, y, and z variables were declared in the main_file.h header file, so they are not declared in the main program. The header file (file.h) contains the following code declaring the three variables used in the main code:

int x;
int y;
int z;

We could declare the variables in the main program and not declare the variables in a header file (.h). It is up to you whether you want to write program elements in header files. We will learn more about variables later in this chapter.

Note

The C language is case sensitive, so be careful when writing C code. Most C language instructions are written in non-capitalized letters. Be careful when you declare a variable, too. For example, the variables x and X are different in C.

There are standard libraries that come with the C language and many programmers make good use of them. The stdio.h library (stored as a header file) is widely used in C programming. It defines several macros, variable types, and also specialized functions for performing data input and output; for example, taking input letters from a keyboard or writing text to the console. The console is a text-based area provided by the IDE where reading data from a keyboard or writing text or special characters happens.

This is a short C program example using the <stdio.h> directive:

// program file name: helloworld.c
#include <stdio.h>  
int main()  
{  // start main block of instructions
     printf("Hello world!"); 
     return 0;
} 

C program files are stored with the .c extension (such as mainprogram.c). The C++ program files are generally stored with the .cpp extension (for example, mainprogram.cpp).

The American National Standards Institute (ANSI) C language defines a number of useful built-in functions, such as printf(), which displays characters (for example, a text message) on the IDE's console. As you can see from the preceding program example, we wrote some comments explaining each line of code. The next section shows the different ways of writing comments in the C language.

Using comments in C

Comments are either blocks or lines of text that don't affect the functioning of C programs. Writing comments in C programming is useful because they can be used to explain and clarify the meaning or the functioning of instructions, functions, variables, and so on. All the comments that we write in the program are ignored by the compiler. There are a couple of ways of writing comments in C:

  • Using double slashes (//): This makes a single-line comment.
  • Using slashes and asterisks (/*  */): This makes a comment with a block of text.

This code example demonstrates how to use both types of comments:

/**********************************************************
Program: Helloworld.c 
Purpose: It shows the text "Hello, world!!" on the IDE's console. 
Author: M. Garcia.
Program creation date: September 9, 2020.
Program version: 1.0 
**********************************************************/
#include <stdio.h>  //standard I/O library
int main(void)
{
    int x; // we declare an integer variable
                  
    printf("Hello, world!!"); 
    x=1; // we assign the value of 1 to variable x.
}

Tip

It is a good programming practice to write the code's purpose, version number and date, and the author's name(s) as comments at the beginning of your C program.

The next section describes how to declare and use variables in C programming. Variables are very useful, and you will use them in most of the chapters of this book.

Understanding variables in C

A variable is a name (also called an identifier) assigned via programming to a microcontroller memory storage area that holds data temporarily. There are specific types of variables in C that hold different types of data. The variable types determine the layout and size of the variable's assigned microcontroller memory (generally, its internal random-access memory or RAM).

We must declare a variable in the C language first to use it in your code. The variable declaration has two parts – a data type and an identifier, using this syntax: <data_type> <identifier>. The following explains both:

  • A data type (or just type) defines the type of data to be stored in the variable (for example, an integer number). There are many data types and their modifiers. The following table describes the four main types:
Table 2.1 – The main four data types used in the C language

Table 2.1 – The main four data types used in the C language

  • Each type from Table 2.1 has the modifiers unsigned, signed, short, and long, among others. For example, we can declare a variable that holds unsigned integers as unsigned int x;.
  • There is another type named void. This type has no value and is generally used to define a function type that returns nothing.
  • An identifier is a unique name identifying the variable. Identifiers can be written with the letters a..z or A..Z, the numbers 0..9, and the underscore character: _. The identifier must not have spaces, and the first character must not be a number. Remember that identifiers are case-sensitive. In addition, an identifier should have fewer than 32 characters according to the ANSI C standard.

For example, let's declare a variable named x that can hold a floating-point number:

float x;

In the preceding line of code example, the C compiler will assign variable x a particular memory allocation holding only floating-point numbers.

Now, let's use that variable in the following line of code:

x=1.10;

As you can see, we store the floating-point value of 1.10 in the variable named x. The following example demonstrates how to use a variable in a C program:

/* program that converts from Fahrenheit degrees to Celsius degrees. Written by Miguel Garcia-Ruiz. Version 1.0. Date: Sept. 9, 2020
*/
#include <stdio.h> // standard I/O library to write text
int main(void) // It won't return any value
{
    float celsius_degrees;
    float fahrenheit_degrees=75.0;
    // Calculate the conversion:
    celsius_degrees=(fahrenheit_degrees-32)*5/9;
    // printf displays the result on the console:
    printf("%f",celsius_degrees); 
}

You can initialize a variable with a value when it is declared, as shown in the preceding example for the fahrenheit_degrees variable.

We can also store strings in a variable using double quotes at the beginning and end of the string. Here's an example:

char  name = "Michael";

The preceding example shows how a string is stored in a char variable type, which is an array of characters.

Declaring local and global variables

There are two types of variables in C depending on where they are declared. They can have different values and purposes:

  • Global variables: These are declared outside all the functions from your code. These variables can be used in any function and through the whole program.
  • Local variables: Local variables are declared inside a function. They only work inside the function that were declared, so their value cannot be used outside that function. Have a look at this example containing both global and local variables:
#include<stdio.h>
// These are global variables:
int y;
int m;
int x;
int b;
int straight_line_equation() {
    y=m*x+b;
    return y;
}
int main(){
    int answer;  // this is a local variable
    m=2;
    x=3;
    b=5;
    answer = straight_line_equation();
    printf(" %d\n  ",answer);
    return 0;  // this terminates  program
}

In the preceding example, the global variables y, m, x, and b work in all programs, including inside the straight_line_equation() function.

Using constants

Constants (also called constant variables) can be used to define a variable that has a value that does not change throughout the entire program. Constants in C are useful for defining mathematical constants. This is the syntax for declaring a constant:

const <data_type> <identifier>=<value>;

Here, the data type can be either int, float, char, or double, or their modifiers, for example:

const float euler_constant=2.7183;
const char A_character='a';

You can also declare variables using the #define directive. It is written at the beginning of a program, right after the #include directive, without a semicolon at the end of the line, using this syntax: #define <identifier> <value> .

We don't need to declare the constant's data type. The compiler will determine that dynamically. The following examples show how to declare constants:

#define PI 3.1416
#define value1 11
#define char_Val 'z'

The next section deals with keywords from the C language that are widely used in C programs.

Applying keywords

The ANSI C standard defines a number of keywords that have a specific purpose in 
C programming. These keywords cannot be used to name variables or constants. These are the keywords (statements) that you can use in your C code:

auto, break, case, char, const, continue, default, do, double, else,  enum, extern, float, for, goto, if, int, long, register, return, short, signed sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while.

The compilers used to compile programs for the Blue Pill and Curiosity Nano boards have additional keywords. We will list them in this chapter. The following section explains what functions in C are.

Declaring functions in C

A function is a block that contains a group of C instructions, or just a single instruction, that will perform a task, or a number of tasks, with a particular purpose. The block of instructions is encompassed with curly brackets. A C program has at least one function, which is main(). This function is written in C programs, and other functions are called from it. You can logically divide your code up into functions to make it more readable and to group instructions that are related to the same task, giving the instructions some structure. Functions in C are defined more or less like algebraic functions where you have a function name, a function definition, and a function parameter(s).

The general form for defining a function in C is the following:

<return_data_type> <function_name> (parameter list) {    <list of instructions>
    return <expression>; //optional
}

The parameters (also called arguments) input data to a function. The parameters are optional since a function cannot have parameters at all. We need to declare the type of each parameter. The variables declared as parameters behave like local variables for the function where they were declared. The variables declared as parameters are also called the function's formal parameters, and these are also termed call by value, meaning that the changes made to the parameters (the variables) inside their function do not have any effect on them. In other words, the instructions from inside a function cannot alter the values of its function's parameters. The return statement allows a value from a function to be returned, and this returned value is used in other parts of the program. The return statement is optional since you can code a function that does not return a value.

Tip

It is a good programming practice to indent the instructions contained in a function block. This gives the function more visual structure and readability.

The following function example shows how to use parameters and how data is returned from a function, where number1 and number2 are the function parameters:

int maxnumber(int number1, int number2) {
    /* Declaring a local variable to store the result: */
    int result1;
    if (number1 > number2)
        result1 = number1;
    else
        result1 = number2;
    return result1; 
}

In the preceding example, the function returns the results of the comparison between the two numbers.

Tip

Make sure that the function's data type has the same type as the variable used in the return statement.

If, for some reason, you don't need to return a value from a function, you can use the void statement instead of defining the function's data type, for example:

void error_message ()
{
    printf("Error.");
}

In the preceding example, we are not using the return 0 statement in the function because it's not returning any value. We can then call the function by its name: error_message();.

Calling a function

Once we declare a function, we need to call it, that is, run it in another part of your code. This transfers the program control to the called function and it will run the instruction(s) contained in it. After executing all the instructions from the function, the program control resumes, running instructions from the main program.

To call a function, you will need to write the function name and the required values for the parameters. If your function returns a value, you can store it in a variable. For example, let's call the max() function that we explained previously:

int result2;
result2=maxnumber(4,3);

In this example, the result of the number comparison made by the maxnumber() function will be stored in the result2 variable.

Evaluating expressions (decision statements)

The C language provides a way to declare one or more logic conditions that can be evaluated (tested) by the program, as well as some statements that need to be executed according to the result of that evaluation, that is, if the condition is either true or false.

The C programming language assumes that the true value is any non-null or non-zero value. It is false if the value is zero or null. C has the following decision-making statements:

  • if (expression_to_evaluate) {statements}: This has a Boolean expression in the decision that is followed by one or more statements to be run if the decision is true, for example:
    #include <stdio.h>
    void main(){
    	int x;
    	x=11;
    	if (x>10) {
    		printf("yes, x is greater than 10");
    	}
    }
  • if (decision) {statements} else {statements}: The else component can be used after an if statement and can be useful when running one or more statements if the decision is false, for example:
    #include <stdio.h>
    void main(){
      int x;
      x=5;
      if (x>10) {
         printf("yes, x is greater than 10");
      }
      else {
        printf("no, x is not greater than 10");
      }
    }

    In the preceding example, the x variable is analyzed, and if x is greater than 10, it will print out this message on the IDE's console: yes, x is greater than 10, otherwise it will print out no, x is not greater than 10.

    Tip

    Be careful when you evaluate two variables with the if statement. Use double equal signs for that (==). If you use only one equal sign, the compiler will raise an error. Do it like this: if (x==y) {statements}

  • The switch statement compares the value of a variable against a number of possible values, which are called cases. Each case from the switch statement has a unique name (identifier). If a match is not found in the list of cases, then the default statement will be executed and the program control goes out of the switch with the list of cases. The optional break statement is used to terminate the program control outside of the switch block. This is useful if, for some reason, you don't want the switch statement to keep evaluating the rest of the cases. The following is the syntax for the switch statement:
switch( expression_to_evaluate)
{
    case value1:
        <statement(s)>;
        break;
    case value_n:
        <statement(s)>;
        break;
}

The preceding code shows the syntax for the switch statement, including its break sentence. The following code is an example of using switch, which will compare the variable age against three cases. In case the variable has a value of 10, it will print out the following text: the person is a child:

#include <stdio.h>
void main(){
	int age;
	age=10;
	switch (age)
	{
		case 10:
			printf ("the person is a child");
			break;
		case 30:
			printf ("the person is an adult");
			break;
		case 80:
			printf ("the person is a senior citizen");
			break;
	}	
}

So far, we have reviewed how to logically evaluate an expression. The next section explains how to run one or more statements repeatedly. This can be useful for some repetitive tasks for the microcontroller board, such as reading data from an input microcontroller port continuously.

Understanding loops

A loop in C executes a single line of code or a block of lines of code a number of times, and if programmed, it could run endlessly. The C programming language provides three ways of making loops using the keywords for, while, and do..while:

for loop

The for loop repeats one or more statements contained in its block until a test expression becomes false. This is the syntax of the for loop:

for (<initialization_variable>;         <test_expression_with_variable>; <update_variable>)
{
    <statement(s)_to_run>;
} 

In the preceding syntax, the counter_variable initialization is executed once. Then, the expression is evaluated with counter_variable. If the tested expression is false, the loop is terminated. If the evaluated expression is true, the block statement(s) are executed, and counter_variable is updated. counter_variable is a local variable that only works in the for loop. This example prints out a list of numbers from 1 to 10 on the IDE's console:

for (int x=1; x<=10; x++)
{
    printf("%d ", x);
}

Please note that the x++ statement is the same as writing x=x+1.

while loop

The while loop repeats one or more statements from its block while a given condition is true, testing its condition prior to executing the statements. When its condition tests false, the loop terminates. Here is the syntax for the while loop statement:

while (<test_expression>) 
{
    statement(s); 
}

The preceding code is the syntax for the while loop. The following is example code that uses the while loop, counting from 0 to 10:

int x = 0;
while (x <= 10)
{
    // \n it will display the next number in a new 
    // line of text:
    printf("%d \n", x); 
    x=x+1;
}

do..while loop:

This type of loop is very similar to the while loop. The do..while loop executes its block statement(s) at least once. The expression is evaluated at the end of the block. The process continues until the evaluated expression is false.

The following is the syntax for the do..while loop:

do
{
    statement(s);
}
while (<test_expression>);

The following example uses the do..while loop, counting numbers from 5 to 50, while the sum is < 50:

int number=5;
do
{
    number=number+5;
    printf("%d ", number);
}
while (number < 50);

In the preceding code, the variable called number has the value 5 added to it and the variable is printed out on the IDE's console at least once, and then the variable is evaluated.

The infinite loops

You can also program an infinite loop, which, of course, will run endlessly (the loop does not terminate) until we abort the program (or disconnect the power from the microcontroller board!). Infinite loops can be useful for showing a result from a microcontroller continuously, reading data from a microcontroller board continuously without stopping it, and so on.

You can do this using any of the three types of loops. The following are some examples of infinite loops:

for(; ;)
{
    printf("this text will be displayed endlessly!");
}
while(1) 
{
    printf("this text will be displayed endlessly!");
}
do
{
    printf("this text will be displayed endlessly!");
}
while (1);

As you can see from the preceding code, programming endless loops is easy and simple.

The break and continue keywords in loops

You can break running a block of statements from a loop using the break keyword. The break statement of the following example will stop the for loop, but the statement will run only once:

for (int x=1; x<=10; x++)
{
    printf("%d ", x);
    break;
}

You can use the break statement in any of the three types of loops.

The continue keyword will force the next iteration of the loop to run, skipping any statement written after the continue keyword. This example will not print out the second line of text:

for (int x=1; x<=10; x++)
{
    printf("%d ", x);
    continue;
    printf("this line won't be displayed.");
}

The preceding code displays the value of x without displaying the next line of text because of the continue statement, moving the program control to the beginning of the for loop.

The next section deals with a number of C statements and functions that were created specifically for the Curiosity Nano microcontroller board and that are slightly different from those for the Blue Pill board.

Introducing Curiosity Nano microcontroller board programming

As you learned from Chapter 1, Introduction to Microcontrollers and Microcontroller Boards, the Curiosity Nano can be programmed using ANSI C language, explained in this chapter, using the MPLAB X IDE.

The basic structure of a C program for the Curiosity Nano is similar to the one explained above using the main() function, but its declaration changes. You have to include the keyword void in it, as follows:

//necessary IDE's library defining input-output ports:
#include "mcc_generated_files/mcc.h"
void main(void) //main program function
{
    // statements
}

The file 16F15376_Curiosity_Nano_IOPorts.zip from the book's GitHub page contains the necessary input-output (I/O) functions for the Curiosity Nano to work. Each port's I/O functions contain the port name. For example, the IO_RD1_GetValue() function will read an analog value from the Curiosity Nano's RD1 port.

The following are useful functions that you can use for programming the Curiosity Nano, which is already defined by the MPLAB X compiler. Note that xxx means the Curiosity Nano's port name. Please read Chapter 1, Introduction to Microcontrollers and Microcontroller Boards, to familiarize yourself with the Curiosity Nano's I/O port names and their respective chip pins:

  • IO_xxx_SetHigh();: This function writes the logic HIGH (3.3 V) value on the specified pin (port).
  • IO_xxx_SetLow();: This function writes the logic LOW (0 V) value on the specified pin (port).
  • IO_xxx_GetValue();: This function returns the logic (digital) value (either HIGH or LOW) that is read from the specified port. HIGH is returned as 1. LOW is returned as 0.
  • ADC_GetConversion(xxx);: This function reads an analog value from the specified port and returns a value from 0 to 1023 corresponding to the analog-to-digital conversion done on the read value.
  • SYSTEM_Initialize();:  This function initializes the microcontroller ports.
  • __delay_ms(number_milliseconds);: This function pauses the program for a number of milliseconds (there are 1,000 milliseconds in one second).
  • IO_xxx_Toggle();: This function toggles the port's value to its opposite state of the specified port. If the port has a logic of HIGH (1), this function will toggle it to 0, and vice versa.

We will use some of the preceding functions in an example explained later in this chapter.

Figure 2.1 shows the Curiosity Nano's pins. Bear in mind that many of them are I/O ports:

Figure 2.1 – Curiosity Nano's pins configuration

Figure 2.1 – Curiosity Nano's pins configuration

We have configured the following ports from the Curiosity Nano microcontroller board as I/O ports. We did this in all the Curiosity Nano's software project files from this book. The ports' pins can be seen in Figure 2.1. Some of them are used throughout this book:

RA0, RA1, RA2, RA3, RA4, RA5, RB0, RB3, RB4, RB5, RC0, RC1, RC7, RD0, RD1, RD2, RD3, RD5, RD6, RD7, RE0, RE1, and SW0.

The following section explains the basic programming structure and important functions for the Blue Pill board microcontroller board coding, which are somewhat different from the Curiosity Nano board.

Introducing Blue Pill microcontroller board programming

As you learned from Chapter 1, Introduction to Microcontrollers and Microcontroller Boards, you can program the Blue Pill board using the Arduino IDE, along with a special library installed in the IDE. Remember that this IDE uses C++ language, which is an extension of C. Programs are also called sketches in Arduino IDE programming. All the sketches must have two functions, called setup() and loop().

The setup() function is used to define variables, define input or output ports (board pins), define and open a serial port, and so on, and this function will run only once. It must be declared before the loop() function.

The loop() function is the main block of your code and will run the main statements of your program. This loop() function will run repeatedly and endlessly. Sketches do not require the main() function.

This is the main structure for your sketches (programs):

void setup() 
{
    statement(s);
}
void loop() 
{
    statement(s);
}

Here is how to define pins (a microcontroller board's ports) either as inputs or outputs:

void setup ( ) 
{
 // it sets the pin as output.
    pinMode (pin_number1, OUTPUT);
 // it sets the pin as input 
    pinMode (pin_number2, INPUT); 
}

An input port will serve to read data from a sensor or switch, and an output port will be used to send data to another device or component, turn on an LED, and suchlike.

Tip

Programming in the Arduino IDE is case-sensitive. Be careful when you write function names, define variables, and so on.

As you can see from the preceding code, each block of statements is enclosed in curly brackets, and each statement ends with a semicolon, similar to ANSI C. These are useful functions that can be used for programming the Blue Pill:

  • digitalWrite(pin_number, value);: This function writes a HIGH (3.3 V) or LOW (0 V) value on the specified pin (port); for example, digitalWrite(13,HIGH); will send a HIGH value to pin (port) number 13.

    Note

    You must previously declare pin_number as OUTPUT in the setup() function.

  • digitalRead(pin_number);: This function returns either a logic HIGH (3.3 V) or logic LOW (0 V) value that is read from a specified pin (port), for example, val = digitalRead(pin_number);.

    Note

    You must previously declare pin_number as INPUT in the setup() function.

  • analogWrite(pin_number, value);: This function writes (sends) an analog value (0..65535) to a specified PIN (output port) of the Blue Pill.
  • analogRead(pin_number);: This function returns an analog value read from the specified PIN. The Blue Pill has 10 channels (ports or pins that can be used as analog inputs) with a 12-bit analog-to-digital conversion (ADC) resolution. Their input range is 0 V – 3.3 V. So, the analogRead() function will map input voltages between 0 and 3.3 volts into integer numbers between 0 and 4095, for example:
  • int val = analogRead(A7);
  • delay(number_of_milliseconds);: This function pauses the program for the specified amount of time defined in milliseconds (remember that there are one thousand milliseconds in a second).

    Tip

    You can also use the C language structure explained in this section for programming the Arduino microcontroller boards, with the only difference being that the range of values for analogWrite() will be 0...255 instead of 0...65535, and analogRead() will have a range of 0 to 1023 instead of 0 to 4095.

Figure 2.2 shows the I/O ports and other pins from the Blue Pill:

Figure 2.2 – The Blue Pill's pins configuration

Figure 2.2 – The Blue Pill's pins configuration

The ports' pins can be seen in Figure 2.2. Some of them are used in this book's chapters. The Blue Pill has the following analog ports: A0, A1, A2, A3, A4, A5, A6, A7, B0, and B1. The following are digital I/O ports: C13, C14, C15, B10, B11, B12, B13, B14, B15, A8, A9, A10, A11, A12, A15, B3, B4, B5, B6, B7, B8, and B9.

Just remember that in the code, the ports are referenced as PA0, PA1, and so on, adding a letter P.

We will use some of the preceding functions in an example in the next section.

Example – Programming and using the microcontroller board's internal LED

In this section, we will use common statements from C/C++ languages for controlling an internal LED from the Blue Pill and the Curiosity Nano boards. The internal LED can be very useful for quickly verifying the state of I/O ports, showing data from sensors, and so on, without the need to connect an LED with its respective resistor to a port. The next section will show how to compile and send a piece of code to the microcontroller boards using their internal LED.

Programming the Blue Pill's internal LED

This section covers the steps for programming the internal LED. You don't need to connect any external electronic component, such as external LEDs. Using the internal LED from the Blue Pill is useful for quickly testing out and showing the result or variable value from a program. You will only need to use the microcontroller boards. The following steps demonstrate how to upload and run the program to the Blue Pill:

  1. Connect the ST-LINK/V2 interface to the Blue Pill, as explained in Chapter 1, Introduction to Microcontrollers and Microcontroller Boards.
  2. Connect the USB cable to the Blue Pill and your computer. Insert the Blue Pill into the solderless breadboard. Figure 2.3 shows the internal LED from the Curiosity Nano and the Blue Pill boards:
    Figure 2.3 – The Blue Pill (top) and the Curiosity Nano's internal LEDs

    Figure 2.3 – The Blue Pill (top) and the Curiosity Nano's internal LEDs

  3. Open Arduino IDE. Write the following program in its editor:
    /*
      Blink
      This program turns on the Blue Pill's internal LED   on for one second, then off for two seconds,   repeatedly.
      Version number: 1.
      Date: Sept. 18, 2020.
      Note: the internal LED is internally connected to   port PC13.
      Written by Miguel Garcia-Ruiz.
     */
    void setup() 
    {
      pinMode(PC13, OUTPUT);
    }
    void loop() 
    {
      digitalWrite(PC13, HIGH);
      delay(1000);
      digitalWrite(PC13, LOW);
      delay(2000);             // it waits for two seconds
    }
  4. Click on the Upload button from the IDE and see how the program is compiled and sent to the Blue Pill. Once it's done, you should see the Blue Pill's small LED blinking.

    Tip

    You can use the preceding code for blinking the internal LED of the Arduino microcontroller boards. Just swap PC13 for LED_BUILTIN.

You could leave the Blue Pill without inserting it in a solderless breadboard because we are not connecting any component or wire to the Blue Pill's ports in the preceding example.

Programming the Curiosity Nano's internal LED

Similar to the Blue Pill, you can use the Curiosity Nano's internal LED to quickly show data from sensors, and so on, without connecting an LED to a port. The whole project containing this example and other supporting files necessary for compiling it on the MPLAB X IDE is stored on the GitHub page. It is a zip file called 16F15376_Curiosity_Nano_LED_Blink_Delay.zip.

Follow these steps to run the program on the MPLAB X IDE:

  1. Connect the USB cable to the Curiosity Nano and insert the board in the solderless breadboard. Unzip the 16F15376_Curiosity_Nano_LED_Blink_Delay.zip file.
  2. On the MPLAB X IDE, click on File/Open Project and then open the project.
  3. Double-click on the project folder and click on the Source Files folder.
  4. Click on main.c and you will see the following source code:
    /*
    This program makes the on-board LED to blink once a second (1000 milliseconds).
    Ver. 1. July, 2020. Written by Miguel Garcia-Ruiz
    */
    //necessary library generated by MCC:
    #include "mcc_generated_files/mcc.h" 
    void main(void) //main program function
    {
        // initializing the microcontroller board:
        SYSTEM_Initialize(); 
        //it sets up LED0 as output: 
        LED0_SetDigitalOutput();
        while (1) //infinite loop
        {
            LED0_SetLow(); //it turns off the on-board LED
            __delay_ms(1000); //it pauses the program for                           //1 second 
            LED0_SetHigh(); //it turns on on-board LED and                         //RE0 pin
            __delay_ms(1000); //it pauses the program for                           //1 second 
        }
    }
  5. Compile and run the code by clicking on the run icon (colored green), which is on the top menu. If everything went well, you will see Curiosity Nano's internal LED blinking.

As you can see from the preceding example, it has useful C functions specifically created for the Curiosity Nano board, such as the following:

SetLow(), SetHigh() and __delay_ms().

Those functions are essential for making projects with microcontroller boards, and they are used in other chapters of this book.

Summary

In this chapter, we learned how to properly configure and set up the MPLAB X and the Arduino IDEs for the C microcontroller board programming. We were introduced to the C programming language, and in particular, a set of C language instructions necessary for programming the Blue Pill and microcontroller boards. To practice what you have learned about the C language, we looked at a number of practical circuits using the boards' internal and external LEDs. The instructions and structure learned in this chapter can be applied to the rest of this book.

Chapter 3, Turning an LED On and Off Using a Push Button, will focus on how to connect a push button with a pull-up resistor to a microcontroller board, as well as how to minimize electrical noise when using the push button. It will also explain how to set up a microcontroller board's input port via software, along with possible applications of push buttons.

Further reading

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Discover how to apply microcontroller boards in real life to create interesting IoT projects
  • Create innovative solutions to help improve the lives of people affected by the COVID-19 pandemic
  • Design, build, program, and test microcontroller-based projects with the C and C++ programming language

Description

We live in a world surrounded by electronic devices, and microcontrollers are the brains of these devices. Microcontroller programming is an essential skill in the era of the Internet of Things (IoT), and this book helps you to get up to speed with it by working through projects for designing and developing embedded apps with microcontroller boards. DIY Microcontroller Projects for Hobbyists are filled with microcontroller programming C and C++ language constructs. You'll discover how to use the Blue Pill (containing a type of STM32 microcontroller) and Curiosity Nano (containing a type of PIC microcontroller) boards for executing your projects as PIC is a beginner-level board and STM-32 is an ARM Cortex-based board. Later, you'll explore the fundamentals of digital electronics and microcontroller board programming. The book uses examples such as measuring humidity and temperature in an environment to help you gain hands-on project experience. You'll build on your knowledge as you create IoT projects by applying more complex sensors. Finally, you'll find out how to plan for a microcontroller-based project and troubleshoot it. By the end of this book, you'll have developed a firm foundation in electronics and practical PIC and STM32 microcontroller programming and interfacing, adding valuable skills to your professional portfolio.

Who is this book for?

This STM32 PIC microcontroller book is for students, hobbyists, and engineers who want to explore the world of embedded systems and microcontroller programming. Beginners, as well as more experienced users of digital electronics and microcontrollers, will also find this book useful. Basic knowledge of digital circuits and C and C++ programming will be helpful but not necessary.

What you will learn

  • Get to grips with the basics of digital and analog electronics
  • Design, build, program, and test a microcontroller-based system
  • Understand the importance and applications of STM32 and PIC microcontrollers
  • Discover how to connect sensors to microcontroller boards
  • Find out how to obtain sensor data via coding
  • Use microcontroller boards in real life and practical projects

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 30, 2021
Length: 320 pages
Edition : 1st
Language : English
ISBN-13 : 9781800569522
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jul 30, 2021
Length: 320 pages
Edition : 1st
Language : English
ISBN-13 : 9781800569522
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just NZ$7 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just NZ$7 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total NZ$ 248.97
Developing IoT Projects with ESP32
NZ$102.99
DIY Microcontroller Projects for Hobbyists
NZ$64.99
Mastering Embedded Linux Programming
NZ$80.99
Total NZ$ 248.97 Stars icon
Banner background image

Table of Contents

15 Chapters
Chapter 1: Introduction to Microcontrollers and Microcontroller Boards Chevron down icon Chevron up icon
Chapter 2: Software Setup and C Programming for Microcontroller Boards Chevron down icon Chevron up icon
Chapter 3: Turning an LED On or Off Using a Push Button Chevron down icon Chevron up icon
Chapter 4: Measuring the Amount of Light with a Photoresistor Chevron down icon Chevron up icon
Chapter 5: Humidity and Temperature Measurement Chevron down icon Chevron up icon
Chapter 6: Morse Code SOS Visual Alarm with a Bright LED Chevron down icon Chevron up icon
Chapter 7: Creating a Clap Switch Chevron down icon Chevron up icon
Chapter 8: Gas Sensor Chevron down icon Chevron up icon
Chapter 9: IoT Temperature-Logging System Chevron down icon Chevron up icon
Chapter 10: IoT Plant Pot Moisture Sensor Chevron down icon Chevron up icon
Chapter 11: IoT Solar Energy (Voltage) Measurement Chevron down icon Chevron up icon
Chapter 12: COVID-19 Digital Body Temperature Measurement (Thermometer) Chevron down icon Chevron up icon
Chapter 13: COVID-19 Social-Distancing Alert Chevron down icon Chevron up icon
Chapter 14: COVID-19 20-Second Hand Washing Timer Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5
(17 Ratings)
5 star 64.7%
4 star 29.4%
3 star 0%
2 star 5.9%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Yujie Oct 06, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book provides an easy and interesting approach to understand the basics of embedded systems for beginners. There are plenty of simple and well-designed DIY projects with easy-to-understand instructions and steps, which is very important for anyone with an interest in microcontrollers but have no electrical background to understand and follow. Overall, it is a great book to teach hobbyists down a path to future embedded system projects.It could be even better if it can include more programming languages in the future to accommodate various development environments and needs. Also, the code documentation can be further improved by including more comments.
Amazon Verified review Amazon
Omar Jul 31, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you are interested in learning and easily make projects with microcontrollers without being an expert in the subject. If you have general knowledge about programming and electronics, this book will guide you step by step in the development of basic projects to implement the Internet of Things (IoT).You can download all the code files from GitHub and the code action videos can be viewed in YouTube, so you can complete the 12 suggested projects easily.If you are interested in learning how to implement solutions using microcontrollers in a simple and fun way, this book is an excellent buy.As an especially interesting point, chapters 12, 13 and 14 highlight, where you can build a body temperature meter, an alert for social distancing and even a fun time control to wash your hands for 20 seconds, all of these projects are particularly helpful in times of Covid-19.Do not think anymore, if you were looking for a DIY guide to program microcontrollers with real applications, in this book you have everything you need to start exploiting your creativity and to continue in this interesting area.
Amazon Verified review Amazon
Jesus Aug 15, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
As someone who had to dust out his C++ skills, this book made it very easy to start coding swiftly. Starting is pretty straightforward, but if you do not have much coding experience, I recommend getting the kindle version, as you can access the links and complementary materials more smoothly.This book truly lives to my expectations as a "soon to be" hardware hobbyist. But, as a data scientist, I'm obsessed with logging everything around me to make data-driven decisions. So the chapter on data logging and the previous one on different sensors and how to set them up lowered the entry barrier.One of the things that I appreciate is social responsibility and how the authors show us how we can put our skills to the betterment of others. The Covid-19 projects were interesting. In particular, the hands washing one, as an Apple watch user, I got obsessed with always completing the 20 seconds, and I see how this easy-to-set timer can encourage more people to wash their hands for the appropriate time.
Amazon Verified review Amazon
Daniel Jul 30, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
They have a series of project and explain how to complete them with examples and theory. Entry level theory is discussed. Perfect to get your feet wet with the science.
Amazon Verified review Amazon
L. Castro Aug 09, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is a good way to start coding microcontroller projects. If you are an enthusiast and get excited about hands-on projects, this book may be just for you.One of the things I liked about the book is the writing. It is streamlined and concise, focused on an entry level. Another aspect that I liked is that it provides several actual examples that can be implemented using two types of boards.On the flipside, some of the things that could be enhanced throughout the book is documenting and explaining the code. The snippets are shown, and explain what they do, but it took me some time to understand what each line of code did. The first two chapters (the basics on electronics and C programming language) may not be sufficient to implement some of the projects if you have no previous knowledge of electronics or low-level programming languages such as the C programming languages. Finally, a troubleshooting section could be of help to those who are really starting to implement these types of projects.Overall: I recommend this book to those who want to get hands-on experience on implementing simple microcontroller projects but be aware that you may need to invest additional time to learn some of the concepts presented in this book, if you do not have previous experience in implementing such projects.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.