element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Freedom development platform
  • Products
  • Dev Tools
  • Freedom development platform
  • More
  • Cancel
Freedom development platform
Blog [FRDM-KL05Z - mbed] Chapter 1 bis: Create more mbed projects !!
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Freedom development platform to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: FreescaleTools_and_Software
  • Date Created: 24 Jul 2014 5:23 PM Date Created
  • Views 756 views
  • Likes 1 like
  • Comments 1 comment
  • cortex-m0+
  • kl05z
  • Cortex-M
  • kinetis
  • frdm-kl05z
  • cortex
  • mbed
  • kinetis-l
  • cortex-m0
  • arm
  • freedom
Related
Recommended

[FRDM-KL05Z - mbed] Chapter 1 bis: Create more mbed projects !!

FreescaleTools_and_Software
FreescaleTools_and_Software
24 Jul 2014

image

 

This document is part of a series:

[FRDM-KL05Z] Full-Review and Getting-Started

[FRDM-KL05Z - mbed] Chapter 1: Create your first mbed project !!

[FRDM-KL05Z - mbed] Chapter 1 bis: Create more mbed projects !!

 

 

 

Hello Freedom users

 

Following to my previous tutorial entitled 'Create your first mbed project with the FRDM-KL05Z'  I would like to go further and propose additional mbed project creation ... still from scratch to show you the 'magic' of this free online tool.

 

 

 

1. Make a RED dimmer featuring the PWM timer embedded in the Kinetis-KL05Z

 

We will now create a project featuring the control of the Red LED with a PWM timer to vary its power (vs the bi-state Digital I/O control from the previous exercise)

 

As for the Lab 5 of my previous mbed tutorial, you must return to the mbed compiler web interface.

 
ef2f83.jpg
  

Again, select Create New Program from the toolbar

 

d2ba22.jpg

In the new program creation window, select the Template option Empty Program

As project will be a Red LED dimmer, so let's call it: FRDM-KL05Z_DimmeRed

Press OK

 

ff2137.jpg

 

Your new project will now be displayed in the Program Workspace between the projects frdm_rgbled from lab4 and FRDM-KL05Z_BlinkRGB from lab5.

 

5660d2.jpg

  

Expand the project frdm_rgbled

Copy the mbed folder and paste it in the FRDM-KL05Z_DimmeRed project (right click)

Then right click on the FRDM-KL05Z_DimmeRed project and Select New File

In the popup window, enter the file name: main.cpp

Open the main.cpp file for the project FRDM-KL05Z_DimmeRed and copy the text below

 

 

#include "mbed.h" //mbed libraries

 

PwmOut rled(LED_RED); //create a PWM signal represented by a floating value between 0.0 (LED FULL-ON) and 1.0 (LED OFF) connected to the specified pin

 

int main()

{

    while(1)

    {

        rled = rled + 0.01; //Decrease the power of the Red LED until turn the Red LED OFF

        wait(0.05);

        if (rled==1)

        {

        rled = 0;           //Restart the Red LED power at its maximum

        }

    }

}

 

  

Click on Save-All to save project modification then select Compile from the toolbar

  

1d0705.jpg

   

If you followed carefully the previous instructions, compilation should end successfully and the download of the FRDM-KL05Z_DimmeRed_KL05Z.bin file will start automatically in your web explorer.

Copy the FRDM-KL05Z_DimmeRed_KL05Z.bin file in the mbed drive from your file explorer, wait the two tones and press the reset button


CONGRATULATION !! Your FRDM-KL05Z is now Dimming down its red LED
 

 

We will now enhance a little bit the complexity of the exercise by making vary the power of the LED in both direction

We are still use the PWMOut function to transmite a PWM signal to the Red LED

Replace the content of the main.cpp by the text below

 

 

#include "mbed.h"         //mbed libraries

 

PwmOut rled(LED_RED); //create a PWM signal represented by a floating value between 0.0 (LED FULL-ON) and 1.0 (LED OFF) connected to the specified pin

 

int main()

{

    rled.period(0.001f);

    while (true)

    {

        for (float i = 0.0f; i < 2.0f ; i += 0.001f)

        {

             if (i<1) 

             {

                rled = i;        //Decrease the power of the Red LED

                wait (0.0025f);

             }

             else

             {

                rled = 2-i;     //Increase the power of the Red LED

                wait (0.0025f);

             }

        }

    }

}

 

 

Click on Save-All to save project modification then select Compile from the toolbar

Copy the new FRDM-KL05Z_DimmeRed_KL05Z.bin file in the mbed drive from your file explorer, wait the two tones and press the reset button

 

CONGRATULATION !! Your FRDM-KL05Z board seems now ALIVE with this heartbeat demo image

 

 

 

2. Make an RGB motion sensor featuring the Accelerometer

  

We will now create a new project featuring the 3-axis Accelerometer with digital output, Freescale MMA8451Q, embedded in the FRDM-KL05Z.

Fortunately there is already a mbed library dedicated to the MMA8451Q component, so thanks to a simple include of the library MMA8451Q.h our code will be drastically simplified.

Then we will reuse the PwmOut function to drive the power of each Red Green Blue LED that will reflect the board inclination.

 

As previously, from the mbed compiler interface, create a New Empty Program that you could call FRDM-KL05Z_AccelerateRGB

Duplicate the mbed folder (from any other valid project) into your new project folder and create a New File main.cpp

According to the mbed Sensors map below the MMA8451Q is connected to the KL05Z via the I2C interface with the following pinout:

- SCL = PTB3

- SDA = PTB4

 

9dec24.jpg

 

In the main.cpp file of your project FRDM-KL05Z_AccelerateRGB copy the text below

  
 

#include "mbed.h"     //mbed standard libraries

#include "MMA8451Q.h" //mbed dedicated library for the Accelerometer MMA8451Q

 

#define MMA8451_I2C_ADDRESS (0x1d<<1)

 

int main(void)

{

    MMA8451Q acc(PTB4, PTB3, MMA8451_I2C_ADDRESS);   // activate the peripheral and allocate the I2C pins and Address of the Peripheral

    PwmOut rled(LED_RED);     //create a PWM signal represented by a floating value between 0.0 (LED FULL-ON) and 1.0 (LED OFF) connected to the specified pin

    PwmOut gled(LED_GREEN);

    PwmOut bled(LED_BLUE);

 

    while (true)

    {

        rled = 1.0 - abs(acc.getAccX());  //divide by 4096 b/c MMA8451Q output is 4096 counts per g so this f outputs accelerometer value formatted to g (gravity)

        gled = 1.0 - abs(acc.getAccY());  // abs will convert it into an absolute value (always positive)

        bled = 1.0 - abs(acc.getAccZ());  // LED will be full powered ON when PwmOut value will be O so we need to make this substraction 1-Acceleration

        wait(0.1);

    }

}

 

 

Click on Save-All to save project modification then select Compile from the toolbar

Compilation will return an error as we have not added the library MMA8451 to our project folder and it is not a standard mbed library

 

e2d8e5.jpg

 

In the compiler output window, click on Fix it!

A popup window will appear and automatically search the proper library to download

Select the first one named MMA8451Q which has been already imported 1846 times (in the description, you have some informations regarding the differentiating functions available in the library) and click OK

0d04ae.jpg

 

The library MMA8451 will be added to your project folder FRDM-KL05Z_AccelerateRGB and the compilation will restart automatically and generate the FRDM-KL05Z_AccelerateRGB_KL05Z.bin file.

 

64f188.jpg
 

Copy the new FRDM-KL05Z_AccelerateRGB_KL05Z.bin file in the mbed drive from your file explorer, wait the two tones and press the reset button

 

CONGRATULATION !! Your FRDM-KL05Z board is now able to detect its inclination and display it on its RGB LED  image

 

84c4ae.jpg cee222.jpg e6d983.jpg

   

 

  

3. Make an RGB Slider featuring the Touch controller embedded in the Kinetis-KL05Z

 

We will now create a new project featuring the touch controller TSI embedded in the KL05Z MCU.

Fortunately like for the Accelerometer, there is already a mbed library dedicated to this peripheral, so thanks to a simple include of the library tsi_sensor.h this project programming will be simplified.

Then we will reuse the PwmOut function to drive the power of each Red Green Blue LED that will reflect the touch area of the slider.

 

As previously, from the mbed compiler interface, create a New Empty Program that you could call FRDM-KL05Z_TouchRGB

Duplicate the mbed folder (from any other valid project) into your new project folder and create a New File main.cpp

 

According to the FRDM-KL05Z schematics (available at www.freescale.com/frdm-kl05z) below the capacitive touch slider electrodes are connected to the KL05Z with the following TSIO_IN pinout:

- TSI0_IN8/PTB12

- TSI0_IN9/PTA13

 

b505c0.jpg

 

In the main.cpp file of your project FRDM-KL05Z_TouchRGB copy the text below

 

 

#include "mbed.h"        //mbed libraries

#include "tsi_sensor.h" //mbed dedicated library for the Touch Sensor Controller embedded in the KL05Z

 

int main(void)

{

    DigitalOut rled(LED_RED);    //create a PWM signal represented by a floating value between 0.0 (LED FULL-ON) and 1.0 (LED OFF) connected to the specified pin

    DigitalOut gled(LED_GREEN);

    DigitalOut bled(LED_BLUE);

 

    TSIAnalogSlider tsi(9, 8, 40);  // definition of the TSI_IN pins connected to the Touch Slider electrodes and the resolution of 40 steps

 

    while (true)

    {

        if (tsi.readPercentage()<1 and tsi.readPercentage() >0.7)  // definition of the Touch Slider area to power the Red LED

        {

           rled = 0;

           gled = 1;

           bled = 1;

           wait(0.1);

        }

        else if (tsi.readPercentage()<0.7 and tsi.readPercentage() >0.3)  // definition of the Touch Slider area to power the Green LED

        {

           rled = 1;

           gled = 0;

           bled = 1;

           wait(0.1);

        }

        else if (tsi.readPercentage()<0.3 and tsi.readPercentage() >0)  // definition of the Touch Slider area to power the Blue LED

        {

           rled = 1;

           gled = 1;

           bled = 0;

           wait(0.1);

        }

        else // when no detection switch off the LED

        {

           rled = 1;

           gled = 1;

           bled = 1;

           wait(0.1);

        }

    }

}

 

 

Click on Save-All to save project modification then select Compile from the toolbar

Compilation will return an error as we have not added the library TSI_sensor to our project folder and it is not a standard mbed library

 

51504e.jpg

 

In the compiler output window, click on Fix it!

A popup window will appear and automatically search the proper library to download

Select the first one named tsi_sensor which has been already imported many times and click OK

 

219b6e.jpg

 

The library TSI_sensor will be added to your project folder FRDM-KL05Z_TouchRGB and the compilation will restart automatically and generate the FRDM-KL05Z_TouchRGB_KL05Z.bin file.

 

Copy the new FRDM-KL05Z_TouchRGB_KL05Z.bin file in the mbed drive from your file explorer, wait the two tones and press the reset button

 

CONGRATULATION !! Your FRDM-KL05Z board is now able to detect touch on the slider and display it on its RGB LED  image

 

483f90.jpg 4c56fb.jpg 1fac12.jpg

 

    

 

I hope that those additional project examples were useful for your comprehension of the mbed added value for the FRDM-KL05Z evaluation.

 

mbed is a free tool with of course some limitations.

Even if mbed is producing a nice handbook as workaround, you may have probably already understood that the Debug capabilities of this online tool are really limited.

This is the reason why, I will post soon more tutorials featuring other ARM IDE toolchains compatible with the FRDM-KL05Z to get an alternative option when you are going to develop your own project ...

... but definitely this mbed tool is a nice free solution to quickly evaluate the performances/capabilities of a new MCU

 

You should be now ready to create your own mbed project just limited by your own creativity

Please share your experience and publish your project examples in this Freedom group of the Element14 community to help the community of Freedom users.


Happy mbed Experience image

  • Sign in to reply
  • DAB
    DAB over 11 years ago

    Another great tutorial.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube