FRDM-KL25Z with the Arduino Motor Shield

The great thing with the Freedom FRDM-KL25Z board is its compatibility with Arduino Shields: a great set of board available at very reasonable prices. I had my hands on the Adafruit Data Logger shield, and now it was time to use the original Arduino Motor Shield R3.

Freedom FRDM-KL25Z with Arduino Motor Shield and Arexx Chassis

Freedom FRDM-KL25Z with Arduino Motor Shield and Arexx Chassis

I already had an Arexx Robo Chassis available: a simple platform with two DC motors. So I added the FRDM-KL25Z with the Arduino Motor Shield R3 on top of it:

Top View with Arduino Motor Shield on top of Freedom board and motor chassis

Top View with Arduino Motor Shield on top of Freedom board and motor chassis

Power Supply

The battery pack (4 AAA NiMH rechargeable batteries) provide 5V to the motor shield. That 5V is powering the FRDM-KL25Z through trace on the Motor Shield. There is an outstanding issue: the Motor Shield expects that the CPU board provides both 3.3V and 5V. 3.3V is provided, but the FRDM-KL25Z only provides 5V is either the KL25Z or the K20 is USB powered. So for now I need to power the system as well with a USB cable until I find a different solution.

Current Sensing

The shield provides two analog pins for motor current sensing. According to the documentation 3.3V should be full-scale for 2A. However, I’m measuring around 60 mV even with no current. It is not clear to me from the shield schematics if the analog signal depens on the AREF signal or not: the problem could be because the Freedom board does *not* route that signal to the header without hardware modification. So not sure where I am now with this, as I’m measuring the wrong values.

Console/Shell Interface

To manually control the motor, I have added a simple shell interface:

Shell Interface

Shell Interface

That way I can manually control the motors and get status information:

Shell Motor Commands and Status

Shell Motor Commands and Status

Processor Expert

The CodeWarrior for MCU10.3 project is using Processor Expert components to abstract from the hardware, and runs FreeRTOS:

Arduino Motor Processor Expert Components

Arduino Motor Processor Expert Components

Motor Driver

The motor driver functionality is in Motor.c and Motor.h. The interface is as below:

/*
* Motor.h
*
*      Author: Erich Styger
*/

#ifndef MOTOR_H_
#define MOTOR_H_

#include "PE_Types.h"
#include "FSSH1.h"

typedef enum {
  MOT_DIR_FORWARD,  /*!< Motor forward direction */
  MOT_DIR_BACKWARD  /*!< Motor backward direction */
} MOT_Direction;

typedef int8_t MOT_SpeedPercent; /* -100%...+100%, where negative is backward */

typedef struct MOT_MotorDevice_ {
  bool brake; /* if brake is enabled or not */
  MOT_SpeedPercent currSpeedPercent; /* our current speed in %, negative percent means backward */
  uint16_t currPWMvalue; /* current PWM value used */
  LDD_TError (*SetRatio16)(LDD_TDeviceData*, uint16_t);
  LDD_TDeviceData *PWMdeviceData; /* LDD device handle for PWM */
  void (*DirPutVal)(LDD_TDeviceData *, bool); /* function to set direction bit */
  LDD_TDeviceData *DIRdeviceData; /* LDD device handle for direction */
  void (*BrakePutVal)(LDD_TDeviceData *, bool); /* function to enable/disable brake */
  LDD_TDeviceData *BRAKEdeviceData; /* LDD device handle for brake pin */
  uint16_t currentValue; /* current AD current sensor value */
  uint16_t offset; /* current AD sensor offset value */
  LDD_TDeviceData *SNSdeviceData; /* LDD current AD device handle */
} MOT_MotorDevice;

/*!
* \brief Sets the PWM value for the motor.
* \param[in] motor Motor handle
* \param[in] val New PWM value.
*/
void MOT_SetVal(MOT_MotorDevice *motor, uint16_t val);

/*!
* \brief Return the current PWM value of the motor.
* \param[in] motor Motor handle
* \return Current PWM value.
*/
uint16_t MOT_GetVal(MOT_MotorDevice *motor);

/*!
* \brief Change the direction of the motor
* \param[in] motor Motor handle
* \param[in] dir Direction to be used
*/
void MOT_SetDirection(MOT_MotorDevice *motor, MOT_Direction dir);

/*!
* \brief Returns the direction of the motor
* \param[in] motor Motor handle
* \return Current direction of the motor
*/
MOT_Direction MOT_GetDirection(MOT_MotorDevice *motor);

/*!
* \brief Shell command line parser.
* \param[in] cmd Pointer to command string
* \param[out] handled If command is handled by the parser
* \param[in] io Std I/O handler of shell
*/
uint8_t MOT_ParseCommand(const unsigned char *cmd, bool *handled, const FSSH1_StdIOType *io);

/*!
* \brief Initialization function.
*/
void MOT_Init(void);

#endif /* MOTOR_H_ */

Summary

It was very easy to use the Motor Shield with the help of CodeWarrior for MCU10.3 and Processor Expert. The basic functionality with the exception of current sensing works and with the shell interface it is easy to explore and add further functionality. I still have an ultrasonic sensor to integrate :-).

The CodeWarrior project and sources are available from this link.

Happy Motoring 🙂

13 thoughts on “FRDM-KL25Z with the Arduino Motor Shield

  1. Erich

    First let me say I am a big fan of yours! I love the content you post on mcuoneclipse.com.

    In context of Arduino-compatible shields, one to watch-out for is Avnet’s new “Wi-Go” (Wi-Fi on the Go…) wireless-datalogger “shield” designed specifically as a companion board to Freescale’s FRDM-KL25Z Freedom board. It has an 800mAH Li-Poly battery, Wi-Fi, multiple sensors plus low-power SPI serial Flash memory. Details available here: http://www.em.avnet.com/Wi-Go

    CE certification recently completed and a number of boards sent over for the dozen or so Kinetis-L workshops being hosted by Silica there in Europe (based on this Wi-Go kit). Their workshops started this week, more details on that available at:
    https://www.silica.com/seminar/view/low-power-freescale-kinetis-l-microcontrollers-spe.html

    Regards

    Peter Fenn | Global Technical Marketing Manager, Microcontrollers
    (O) 949.789.4308 | (M) 949.922.3161
    Avnet Electronics Marketing | 430 Exchange, Ste. 100 | Irvine, CA 92602
    http://www.em.avnet.com

    Like

    • Hi Peter,
      thanks! that “Wi-Go” module really looks interesting. I checked the web site, but it looks that board is only available in the Americas? How can I get one in Europe? I would love to get my hands on it.
      Best regards,
      Erich

      Like

  2. Pingback: The Freedom Robot | MCU on Eclipse

  3. Very interested in this project. I have a similar motor shield from Pololu that I want to try. I have imported the project source from the link. After resolving some issues with missing PE components and different versions I was able to get the PE code to generate. Now when I build the project it is hanging on ‘Undefined reference to __copy_rom_sections_to_ram in __arm_start.c Line 231. Additionally, there are errors on __init_ccp, __init_registers and __init_user all in __arm_start.c. Is this a project property setting or a code warrior setting? How can I resolve the errors? Any help will be appreciated. Thanks!

    Like

  4. Pingback: Pololu Line Following Robot with Freedom Board | MCU on Eclipse

  5. Hi Erich,

    Have you tried using Embedded Software and Motor Control Libraries (http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=FSLESL) on KL25Z Freedom Board? There are some issues when importing the library into CodeWarrior v10.3.

    ~~~~~~
    Issue 1:
    ~~~~~~
    I create a New Bareboard Project for Kinetis KL25(without PE), drag & drop all the necessary files into “Project_Headers” (linking), add all the PATHs into “Includes” and “Libraries” under project properties (every steps provided in the User Guide). 2 Problems occur when I tried to build the project.

    ————–
    Problem 1:
    ————–
    #if defined(__CWCC__)

    Suppose the __CWCC__ should be defined automatically by the compiler. Therefore, in order to mute the error temporarily, I simply put everything outside the #if statement. Then, the 2nd problem occurs:

    ————–
    Problem 2:
    ————–
    intrinsic_cw.h – error – expected ‘(‘ before ‘{‘ token

    The error occurs everywhere in the header file where the line contain the “asm{ }”.

    ~~~~~~
    Issue 2:
    ~~~~~~
    So, I try to dig deep into the problem by revisiting the User Guide again. This time I create a New Project for Kinetis K40 family which is shown exactly in the UG. Then I realize that I am able to choose the “ARM Build Tools” in the wizard (GCC or Freescale) which is not available back then for Kinetis KL25. If I choose GCC the same problems occur and hence I select Freescale.

    Next, I just follow all the steps (as described earlier) and compile the project. Surprisingly, no error(s) is encountered this time. (the problem still occurs with the #if defined(__CWCC__), so it is bypassed temporarily)

    Please advise. Thanks.

    Like

  6. Pingback: 5V Generation from V_IN on the Freedom Board RevE | MCU on Eclipse

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.