Servo Control with NXP FRDM-KL25Z Board
This example shows how to use Simulink Coder Support Package for NXP FRDM-KL25Z Board to control a servo motor connected to FRDM-TFC Shield.
Simulink Coder Support Package for NXP FRDM-KL25Z Board enables you to create and run Simulink® models on a FRDM-KL25Z Board.
In this example, you will learn how to create a Simulink model that controls a standard servo motor which is connected to FRDM-TFC Shield.
We recommend completing Getting Started with Simulink Coder Support Package for NXP FRDM-KL25Z Board.
To run this example you will need the following hardware:
NXP FRDM-KL25Z Board
Standard servo motor
Task 1 - Hardware Connections
1. Place the FRDM-TFC Shield firmly on the FRDM-KL25Z Board as shown in the following figure.
2. Connect the NXP FRDM-KL25Z Board to your computer with a USB cable.
3. Connect your servo motor to the FRDM-TFC Shield. There is a header on the Shield labeled with an image of a servo motor. This is where you should connect the servo. Servo motors have three wires: power, ground, and PWM signal. Connect them as described below and shown in the following figure.
Connect the power wire (red) to the +6V pin.
Connect the ground wire (black) to the GND pin.
Connect the PWM signal wire (white) to the servo PWM pin.
The header's pins are labeled on the bottom of the board.
4. Connect the battery supply to FRDM-TFC Shield.
Task 2 - Set the Servo Motor Position
In this task you will set the position of the servo motor shaft using a sine wave. The shaft angle will vary between -20 and 20 degrees.
1. Open the Servo Control model.
2. Double-click on the Sine Wave block to see that it is set to vary between -20 and 20, with a step size of 0.01. This means that the Servo block gets a new angle for the servo motor shaft every 0.01 second.
3. In your Simulink model, click the Build Model button on the toolbar.
4. When the model starts running on the NXP FRDM-KL25Z board, observe the servo's motor shaft position sweep between -20 and 20 degrees.
Task 3 - Control the Servo Motor Position via a Potentiometer
In this task you will set the position of the servo motor shaft manually, using a potentiometer on the FRDM-TFC Shield.
1. Open the Servo Control Using Potentiometer model.
2. Notice that the Potentiomter block's output is multiplied with the value 20, using a gain block. This is done to scale the Potentiometer's output, normally -1 to 1, to -20 to 20.
3. Double-click on the Potentiometer block to see that potentiometer A has been selected, and that it will be updating the servo's values every 0.01 seconds.
4. In your Simulink model, click the Build Model button on the toolbar. The model will now be deployed to the NXP FRDM-KL25Z Board.
5. When the model starts running on the NXP FRDM-KL25Z Board, observe the servo's motor shaft position sweeps between -20 and 20 degrees based on the position of Potentiometer A.
Other Things to Try
Experiment with other blocks in the NXP FRDM-KL25Z block library. For example:
Use Digital Input block to adjust the motor shaft position depending on the external control signal or signals.