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:
- Connect the ST-LINK/V2 interface to the Blue Pill, as explained in Chapter 1, Introduction to Microcontrollers and Microcontroller Boards.
- 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:
- 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 }
- 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
forLED_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:
- 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. - On the MPLAB X IDE, click on File/Open Project and then open the project.
- Double-click on the project folder and click on the
Source Files
folder. - 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 } }
- 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.