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)

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.