Book Image

DIY Microcontroller Projects for Hobbyists

By : Miguel Angel Garcia-Ruiz, Pedro Cesar Santana Mancilla
Book Image

DIY Microcontroller Projects for Hobbyists

By: Miguel Angel Garcia-Ruiz, Pedro Cesar Santana Mancilla

Overview of this book

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.
Table of Contents (16 chapters)

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.