Blink LED: Arduino, ESP32, STM32, LPC2148, PIC16f877a



Here's a simple "Blink LED" code example for various microcontroller platforms along with explanations for each. This example will make an LED connected to a digital pin blink on and off.


Arduino (Using Arduino IDE and Arduino Uno):

int ledPin = 13;  // LED connected to digital pin 13

void setup() {

  pinMode(ledPin, OUTPUT); // Set the LED pin as an output

}

void loop() {


  digitalWrite(ledPin, HIGH); // Turn the LED on

  delay(1000);                // Wait for 1 second

  digitalWrite(ledPin, LOW);  // Turn the LED off

  delay(1000);                // Wait for 1 second

}

Explanation for Arduino:

- The LED is connected to pin 13.

- In the `setup()` function, we set `ledPin` as an output using `pinMode()`.

- The `loop()` function alternates between turning the LED on and off using `digitalWrite()` and introduces delays using `delay()` to control the blinking rate.


ESP32 (Using Arduino IDE and ESP32 Dev Board):

int ledPin = 2;  // LED connected to GPIO pin 2

void setup() {

pinMode(ledPin, OUTPUT); // Set the LED pin as an output

}

void loop() {

  digitalWrite(ledPin, HIGH); // Turn the LED on

  delay(1000);                // Wait for 1 second

  digitalWrite(ledPin, LOW);  // Turn the LED off

  delay(1000);                // Wait for 1 second

}

Explanation for ESP32:

- Similar to the Arduino example, the LED is connected to `ledPin`.

- GPIO pins are used to control the LED.

- The `setup()` function configures `ledPin` as an output.

- The `loop()` function blinks the LED on and off with delays.


STM32 (Using STM32CubeIDE and STM32 Discovery Board):

#include "main.h"

int main(void) {

  HAL_Init();            // Initialize HAL library

  SystemClock_Config();  // Configure the system clock

  MX_GPIO_Init();        // Initialize GPIO pins

  while (1) {

    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);   // Turn the LED on

    HAL_Delay(1000);                                      // Wait for 1 second

    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); // Turn the LED off

    HAL_Delay(1000);                                      // Wait for 1 second

  }

}


Explanation for STM32:

- The STM32 code structure involves initialization using HAL (Hardware Abstraction Layer).

- The LED is connected to pin GPIOB_PIN_0.

- `HAL_Init()` initializes HAL library, `SystemClock_Config()` sets system clock, and `MX_GPIO_Init()` initializes GPIO pins.

- The main loop alternates LED state using `HAL_GPIO_WritePin()` and adds delays using `HAL_Delay()`.

(Note: STM32 code structure varies based on the STM32Cube configuration and the board used.)


Renesas (Using Renesas e2 studio and Renesas Synergy Board):

#include "hal_data.h"

int main(void) {

  halInit();          // Initialize HAL library

  g_ioport.p_api->pinWrite(IOPORT_PORT_06_PIN_00, IOPORT_LEVEL_HIGH); /

  while (1) {

    g_ioport.p_api->pinWrite(IOPORT_PORT_06_PIN_00, IOPORT_LEVEL_LOW);  // Turn the LED off

    R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS);           // Wait for 1 second

    g_ioport.p_api->pinWrite(IOPORT_PORT_06_PIN_00, IOPORT_LEVEL_HIGH); // Turn the LED on

    R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS);           // Wait for 1 second

  }

}

Explanation for Renesas:

- Initialization is done using `halInit()` in the main function.

- The LED is controlled using `g_ioport.p_api->pinWrite()` function.

- The `R_BSP_SoftwareDelay()` function adds delays.

(Note: Renesas code structure can vary depending on the Synergy configuration and the board used.)


LPC2148 (Using Keil uVision and LPC2148 Development Board):

#include <LPC214x.H>

int main(void) {

  IO1DIR |= (1 << 16); // Set P1.16 as output

  while (1) {

    IO1SET = (1 << 16); // Turn the LED on

    for (int i = 0; i < 1000000; i++); // Delay

    IO1CLR = (1 << 16); // Turn the LED off

    for (int i = 0; i < 1000000; i++); // Delay

  }

}


Explanation for LPC2148:

- `IO1DIR` is used to configure the direction of the pin (input/output).

- `IO1SET` and `IO1CLR` are used to set and clear the pin.

- A simple for loop acts as a delay.

Note: The LPC2148 example assumes familiarity with the LPC2148 development environment and configuration.)


PIC16F877A (Using MPLAB X IDE and PIC16F877A Development Board):

#include <xc.h>

void main(void) {

  TRISC2 = 0; // Set RC2 as output

  while (1) {

    RC2 = 1; // Turn the LED on

    __delay_ms(1000); // Delay for 1 second

    RC2 = 0; // Turn the LED off

    __delay_ms(1000); // Delay for 1 second

  }

}

Explanation for PIC16F877A:

- `TRISC2` is used to set the direction of the pin.

- `RC2` is used to control the pin.

- `__delay_ms()` provides a delay in milliseconds.

(Note: The PIC16F877A example is written in assembly-like code using XC8 compiler syntax.)


Comments

Popular posts from this blog

Investigating the Universe of Stepper Engines

Programming Fundamentals for Arduino Beginners

Making an Essential Home Mechanization Framework with Arduino