
DIY Microcontroller Projects for Hobbyists
By :

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.
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:
Figure 2.3 – The Blue Pill (top) and the Curiosity Nano's internal LEDs
/* 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 }
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.
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:
16F15376_Curiosity_Nano_LED_Blink_Delay.zip
file.Source Files
folder.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 } }
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.