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.
Again, select Create New Program from the toolbar
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
Your new project will now be displayed in the Program Workspace between the projects frdm_rgbled from lab4 and FRDM-KL05Z_BlinkRGB from lab5.
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
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
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
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
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
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.
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
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
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
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
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
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.