RGB LED Using Accelerometer and ARM

Hey Folks,
In this tutorial, we will learn, how to change the colour of RGB LED using accelerometer of the ARM (FRDM-KL25Z).

    • The FRDM-KL25Z is an ultra-low-cost development platform for Kinetis L Series KL1x (KL14/15) and KL2x (KL24/25) MCUs built on ARM® Cortex™-M0+ processor. 
    • The FRDM-KL25Z has been designed by NXP in collaboration with mbed for prototyping all sorts of devices, especially those requiring the size and price point offered by Cortex-M0+ and the power of USB Host and Device.
    • The FRDM-KL25Z is supported by a range of NXP and third-party development software.
    • It is packaged as a development board with connectors to break out to stripboard and breadboard and includes a built-in USB FLASH programmer.

    • NXP KL25Z Kinetis KL2x MCU (MKL25Z128VLK4)
      • High-performance ARM® Cortex™-M0+ Core
      • 48MHz, 16KB RAM, 128KB FLASH
      • USB (Host/Device)
      • SPI (2)
      • I2C (2)
      • UART (3)
      • PWM (TPM)
      • ADC (16 bit)
      • DAC (1x 12bit)
      • Touch Sensor
      • GPIO (66)
               So now we are familiar with the FRDM-KL25Z let’s use it.

   Components Required:
    • FRDM-KL25Z board
    • Data Cable (USB Mini-B)
    • MMA8451Q - 3-axis accelerometer (On Board)


#include "mbed.h"
#include "MMA8451Q.h"

#if   defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
  PinName const SDA = PTE25;
  PinName const SCL = PTE24;
#elif defined (TARGET_KL05Z)
  PinName const SDA = PTB4;
  PinName const SCL = PTB3;
#elif defined (TARGET_K20D50M)
  PinName const SDA = PTB1;
  PinName const SCL = PTB0;

#define MMA8451_I2C_ADDRESS (0x1d<<1)

int main(void)
    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
    PwmOut rled(LED1);
    PwmOut gled(LED2);
    PwmOut bled(LED3);

    printf("MMA8451 ID: %d\n", acc.getWhoAmI());

    while (true) {
        float x, y, z;
        x = abs(acc.getAccX());
        y = abs(acc.getAccY());
        z = abs(acc.getAccZ());
        rled = 1.0f - x;
        gled = 1.0f - y;
        bled = 1.0f - z;
        wait (0.1f);
        printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z);

After dumping the code the output is shown in the video below:-

Join our hands-on training courses.
To know more visit us at  Karkhana Training Portal.