Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
DIY Microcontroller Projects for Hobbyists

You're reading from   DIY Microcontroller Projects for Hobbyists The ultimate project-based guide to building real-world embedded applications in C and C++ programming

Arrow left icon
Product type Paperback
Published in Jul 2021
Publisher Packt
ISBN-13 9781800564138
Length 320 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Miguel Angel Garcia-Ruiz Miguel Angel Garcia-Ruiz
Author Profile Icon Miguel Angel Garcia-Ruiz
Miguel Angel Garcia-Ruiz
Pedro Cesar Santana Mancilla Pedro Cesar Santana Mancilla
Author Profile Icon Pedro Cesar Santana Mancilla
Pedro Cesar Santana Mancilla
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

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

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.

You have been reading a chapter from
DIY Microcontroller Projects for Hobbyists
Published in: Jul 2021
Publisher: Packt
ISBN-13: 9781800564138
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime