Interfacing FD04A and SKPS with Arduino

Introduction

Flexibot Driver 4 Channels (FD04A) is special designed to drive up to 4 DC brush motors. It offers low cost and easy to use DC motor driver capable of driving up 4 DC brush motors and the current can goes up to 3-Ampere. By interfacing with Arduino Duemilanove (Arduino-Due) – Obsolete, replace with Arduino UNO , the board is ready for driving up motors with direction, start, stop and speed control. As I know, Arduino Duemilanove is a microcontroller board based on the ATmega328 (datasheet). As shown in Figure 1, it has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. The pins mapping for ATmega328 was shown in the Figure 2. By choosing Arduino microcontroller, it allows users to process inputs and outputs going to and from the chip in simpler way in terms to control the motors easily. In addition, further interface with PS2 Controller Starter Kit (SKPS) will allow users to control the motors with PS2 controller through Universal Asynchronous Receiver/Transmitter (UART) which will establish a serial communication between Arduino and SKPS. Therefore, in this tutorial, I will explain some ideas and connections to interface FD04A and SKPS with Arduino. Thus, some guides to control the Flexibot move forward, backward, rotate clockwise, rotate counter-clockwise and also some more different direction movements will also be explain in this tutorial. Let’s begin!

Figure 1: Arduino-Due’s Overview

**Notes: Please refer to Arduino Starter Kit Manual for further informations. 

 Figure 2: ATmega 328 pin mapping

**Notes: For further informations, please refer here to study ATmega328 pin mapping and schematic. 

 

Requirements

Main Hardware List:

(i) Flexibot – using transwheel: 1 Unit                                (ii) FD04A (Flexibot Driver 4 Channels): (Rev1.0 )1 Unit

                                      

(iii) SKPS (PS2 Controller Starter Kit): 1 Unit                   (iv) Arduino-UNO

                                               

(v) PS2 Controller (PS-GP-1): 1 Unit

 

Main Components List:

(i) Male-to-Female Jumper Wire (WR-JW-MF-10)          (ii) 12V/7.0Ah Lead Acid (SLA-12-70): 1 Unit

                                    

(iii) Adapter 12V 2A (AD-12-2): 1 Unit                            (iv) USB Cable (WR-USB-B): 1 Unit

                                                  

(v) Screw driver: 1 Unit

 

Main Softwares List:

(i) Arduino IDE – download from the Arduino download page 

(ii) USB Driver – download from FTDI driver page if you are using Duemilanove or CT-UNO. If you are using Arduino UNO, the driver comes with the Arduino IDE.

 

Methodologies

In this tutorial, we need to use Flexibot (using 3 transwheel with 3 DC motors) as the robot base to demonstrate the functionality of the interfacing between FD04A and SKPS with Arduino-Due. Originally, the Flexibot was using PR19 DIY project circuit board with PIC16F777 as the microcontroller to control the motor driver but now we remove this circuit board and replace it with Arduino-Due as microcontroller so that we can interface FD04A and SKPS with Arduino.

Figure 3: Completed Connection to interface FD04A and SKPS with Arduino-Due

The following sections will explain separately the connection between FD04A with Arduino and SKPS with Arduino. After that, some simple source code with Arduino IDE and some programming ideas will be discussed to control the Flexibot.

 

PART A: Connection between FD04A and Arduino-Due

As explained earlier, there are 14 digital input output pins in the Arduino-Due which consist of 1 transmit data pin, 1 receive data pin, 6 PWM output pins and 6 usual digital I/O pins. To control the Flexibot, since we need to control 3 DC motors to move to different direction, so we need to use 6 usual digital output pins and 3 PWM output pins from Arduino-Due. As shown in the Figure 4, there are 4 channels can be used to connect to the motor but as for Flexibot it only need to use 3 Channels to control 3 DC motors. The channels that being used for Flexibot are Channel 2, Channel 3 and Channel 4. Firstly, I want to explain first about the pin connections of 2×7 IDE socket in the FD04A as shown in the Figure 5.

Figure 4: FD04A Overview

Figure 5: FD04A IDE socket pins with numbering

**Note: the labelled numbers represent the pin number.

The following table show the functions of each pins:

Pin numbersFunctions
Pin 1Motor 1 pin 1
Pin 2Motor 1 speed control
Pin 3Motor 1 pin 2
Pin 4Motor 2 pin 1
Pin 5Motor 2 speed control
Pin 6Motor 2 pin 2
Pin 7Motor 3 pin 1
Pin 8Motor 3 speed control
Pin 9Motor 3 pin 2
Pin 10Motor 4 pin 1
Pin 11Motor 4 speed control
Pin 12Motor 4 pin 2
Pin 135V
Pin 14GND

**Note: Please refer to the FD04A User Manual (Rev1.0, Rev2.0)for further informations.

Therefore, by knowing the functions of each pins in FD04A’s IDE socket, now we can start to connect those pins to the Arduino-Due. The following tables show the connections that should be made in order to interface FD04A with Arduino-Due:

Arduino-Due PinsFD04A Pins
Digital pin 2Motor 2 pin 1 (Pin 4)
Digital pin 3 (PWM)Motor 2 speed control (Pin 5)
Digital pin 4Motor 2 pin 2 (Pin 6)
Digital pin 5 (PWM)No connected
Digital pin 6 (PWM)Motor 3 speed control ( Pin 8 )
Digital pin 7Motor 3 pin 1 (Pin 7)
Digital pin 8Motor 3 pin 2 (Pin 9)
Digital pin 9 (PWM)No connected
Digital pin 10 (PWM)No connected
Digital pin 11 (PWM)Motor 4 speed control (Pin 11)
Digital pin 12Motor 4 pin 1 (Pin 10)
Digital pin 13Motor 4 pin 2 (Pin 12)
5V Power5V (Pin 13)
GndGND (Pin 14)

**Notes:

1. Please use the Male-to-Female Jumper Wires to connect them.

2. To supply the motor power in FD04A, connect the PWR terminal with 12V Battery. Make sure the polarity of the battery is correctly connected to avoid damage on FD04A board.

3. Make sure the 5V power supply from Arduino-Due is connected to FD04A board’s 5V pin to power up it.

 

PART B: Connection between SKPS and Arduino-Due

The main purpose of SKPS in this tutorial is to enable us to control the Flexibot with PS2 controller. It can communicate with the host (Arduino-Due) through UART to execute various functionality such as controlling the Flexibot in different movement/direction by pressing different buttons on PS2 controller. Thus, the connection between SKPS and Arduino-Due is quite simple which there are only 5 pins need to be connected as shown in Figure 6.

Figure 6: SKPS Pins Name and Overview

**Note: Select baud rate as 9600 for this tutorial. 

The following table shows the connection of SKPS in order to communicate  with Arduino-Due through UART:

SKPS PinsArduino-Due Pins
5V5V Power Out
GNDGnd
RXDigital Pin 1 (TX)
TXDigital Pin 0 (RX)
RESETRESET

**Notes:

1. Please use Male-To-Female Jumper Wires to connect them as for FD04A.

2. The RX and TX pins must be cross connected to the microcontroller. In other words, RX should be connected to microcontroller’s Transmitter pin (TxD), while TX should be connected to microcontroller’s Receiver pin (RxD).

3. The connection for RESET pin is optional because there is already a reset button on SKPS. If the RESET pins is connected, the user is able to reset the microcontroller during run time.

4. Make sure that the 5V supply that connected to SKPS must be a stable supply of 5V. Any ripple higher that 5.5V will spoil the controller on SKPS. I suggest to use the 5V power out from Arduino-Due.

5. Please refer to SKPS User Manual for further informations. 

 

PART C: Programming the Arduino-Due to interface with FD04A and SKPS through UART

Figure 7: Completed Connection Diagram 

Once the connections that have been discussed on PART A and PART B are correctly completed as shown in the Figure 7, now it’s time for us to start the programming on Arduino microcontroller with Arduino IDE software. Here is the most interesting parts in this tutorial. 🙂

Firstly, I want to share some basic ideas to make certain motor to run in clockwise (CW), counter-clockwise (CCW) and stop.  Referring to the FD04A User Manual (Rev1.0, Rev2.0), we need to set the logic level of each pins that connected to the motor correctly to make the motor run in certain state as shown in the table below:

Pin 1Pin 2Motor State
00Brake to Ground
01CW (relative)
10CCW (relative)
11Brake to V motor

Besides that, we have one more pins in each channel of FD04A which is speed control pin (on IDE socket pins). For this pin, we need to connect to the Pulse Width Modulation (PWM) from the microcontroller. By doing this, we now can manipulate the programming to change the motor speed. Anyway, if we want only full speed motor, we can just connect it to the Vcc or 5V.

To establish the serial communication between SKPS and Arduino-Due through UART, the basic idea is to send correct protocol to SKPS through serial port so that it will check the status of certain button of PS2 controller. But before that, we should set the beginning data rate in bits per second (baud) as 9600 for serial data transmission.

Now, let’s start to use Arduino IDE for programming!

Firstly, we need configure some setting first in Arduino IDE. Thus, we need to set the board (under tools) as Arduino Deumilanove w/ ATmega328.  

Note: For downloading the completed sample code, please click the link that given in the end of this tutorial.

Thus, some descriptions of the sample code are as shown in the followings:

Here are the sample code to define the constant and set the constant variable for this tutorial:

//skps protocol
#define p_Up 4
#define p_Right 5
#define p_Down 6
#define p_Left 7
#define p_L2 8
#define p_R2 9
#define p_L1 10
#define p_R1 11
#define p_Triangle 12
#define p_Circle 13
#define p_Cross 14
#define p_Square 15
//constant variable
const int rx = 0;
const int tx = 1;
const int mtr2_p1 = 2;
const int mtr2_p2 = 4;
const int mtr2_spd = 3;
const int mtr3_p1 = 7;
const int mtr3_p2 = 8;
const int mtr3_spd = 6;
const int mtr4_p1 = 12;
const int mtr4_p2 = 13;
const int mtr4_spd = 11;

Next, we write the setup code to set the pins as input or output and also set beginning serial baud rate. The sample code is as shown in the following:

void setup()
{
Serial.begin(9600); //Set serial baud rate as 9600
Serial.flush(); //Waits for the transmission of outgoing serial data to complete.
//Set the mode for each digital pins whether input or output
pinMode(rx, INPUT);
pinMode(tx, OUTPUT);
pinMode(mtr2_p1, OUTPUT);
pinMode(mtr2_p2, OUTPUT);
pinMode(mtr2_spd, OUTPUT);
pinMode(mtr3_p1, OUTPUT);
pinMode(mtr3_p2, OUTPUT);
pinMode(mtr3_spd, OUTPUT);
pinMode(mtr4_p1, OUTPUT);
pinMode(mtr4_p2, OUTPUT);
pinMode(mtr4_spd, OUTPUT);
}

After that, we write the source code inside the loop( ) function which is the main program that the Arduino microcontroller will implement during run time. Thus, in the loop( ) function, we need only call the desired functions to do something because we write separately the function codes to implement 10 different type of movement for Flexibot. The main program was as shown in the following sample code:

void loop()
{
if(skps(p_Up)==0) //Check whether Up button is pressed
{
move_up(); //Call move_up() function
}
else if(skps(p_Down)==0) //Check whether Down button is pressed
{
move_down(); //Call move_down() function
}
else if(skps(p_Left)==0) //Check whether Left button is pressed
{
move_left(); //Call move_left() function
}
else if(skps(p_Right)==0) //Check whether Right button is pressed
{
move_right(); //Call move_right() function
}
else if(skps(p_Triangle)==0) //Check whether Triangle button is pressed
{
deg_30(); //Call deg_30() function
}
if(skps(p_Circle)==0) //Check whether Circle button is pressed
{
deg_150(); //Call deg_150() function
}
else if(skps(p_Cross)==0) //Check whether Cross button is pressed
{
deg_210(); //Call deg_210() function
}
if(skps(p_Square)==0) //Check whether Square button is pressed
{
deg_330(); //Call deg_330() function
}
else if(skps(p_L1)==0) //Check whether L1 button is pressed
{
anticlockwise(); //Call anticlockwise function
}
else if(skps(p_R1)==0) //Check whether R1 button is pressed
{
clockwise(); //Call clockwise function
}
else //Check whether no button is pressed
{
stop(); //Call stop function
}
}

Here are the important sample function codes to send data and receive data for Arduino-Due:

unsigned char receive_data(void) //Function to wait for a byte receive from UART
{
unsigned char temp;
while(!Serial.available()); //Wait until data received
temp=Serial.read();
return temp;                    //Return the received data
}
unsigned char skps(unsigned char data) //Function to send out a byte via UART
{
Serial.write(data); //Send new data
return receive_data(); //Return received data
}

At this moment, we write the functions code to implement 10 different movements and one more function code to stop all the Flexibot’s motors as shown in the following sample code:

(a) move_up( ) function – make the Flexibot move forward

void move_up()
{
digitalWrite(mtr2_p1, HIGH);
digitalWrite(mtr2_p2, LOW);
analogWrite(mtr2_spd, 255);
digitalWrite(mtr3_p1, LOW);
digitalWrite(mtr3_p2, HIGH);
analogWrite(mtr3_spd, 255);
digitalWrite(mtr4_p1, LOW);
digitalWrite(mtr4_p2, LOW);
analogWrite(mtr4_spd, 0);
}

(b) move_down( ) function – make the Flexibot move backward

void move_down(void)
{
digitalWrite(mtr2_p1, LOW);
digitalWrite(mtr2_p2, HIGH);
analogWrite(mtr2_spd, 255);
digitalWrite(mtr3_p1, HIGH);
digitalWrite(mtr3_p2, LOW);
analogWrite(mtr3_spd, 255);
digitalWrite(mtr4_p1, LOW);
digitalWrite(mtr4_p2, LOW);
analogWrite(mtr4_spd, 0);
}

(b) move_left( ) function – make the Flexibot move toward left direction

void move_left(void)
{
digitalWrite(mtr2_p1, LOW);
digitalWrite(mtr2_p2, HIGH);
analogWrite(mtr2_spd, 190);
digitalWrite(mtr3_p1, LOW);
digitalWrite(mtr3_p2, HIGH);
analogWrite(mtr3_spd, 190);
digitalWrite(mtr4_p1, HIGH);
digitalWrite(mtr4_p2, LOW);
analogWrite(mtr4_spd, 255);
}

(c) move_right( ) function – make the Flexibot move toward right direction

void move_right(void)
{
digitalWrite(mtr2_p1, HIGH);
digitalWrite(mtr2_p2, LOW);
analogWrite(mtr2_spd, 190);
digitalWrite(mtr3_p1, HIGH);
digitalWrite(mtr3_p2, LOW);
analogWrite(mtr3_spd, 190);
digitalWrite(mtr4_p1, LOW);
digitalWrite(mtr4_p2, HIGH);
analogWrite(mtr4_spd, 255);
}

(d) deg_30( ) function – make the Flexibot move toward 30 degree direction

void deg_30(void)
{
digitalWrite(mtr2_p1, HIGH);
digitalWrite(mtr2_p2, LOW);
analogWrite(mtr2_spd, 255);
digitalWrite(mtr3_p1, LOW);
digitalWrite(mtr3_p2, LOW);
analogWrite(mtr3_spd, 0);
digitalWrite(mtr4_p1, LOW);
digitalWrite(mtr4_p2, HIGH);
analogWrite(mtr4_spd, 255);
}

(e) deg_150( ) function – make the Flexibot move toward 150 degree direction

void deg_150(void)
{
digitalWrite(mtr2_p1, LOW);
digitalWrite(mtr2_p2, LOW);
analogWrite(mtr2_spd, 0);
digitalWrite(mtr3_p1, HIGH);
digitalWrite(mtr3_p2, LOW);
analogWrite(mtr3_spd, 255);
digitalWrite(mtr4_p1, LOW);
digitalWrite(mtr4_p2, HIGH);
analogWrite(mtr4_spd, 255);
}

(f) deg_210( ) function – make the Flexibot move toward 210 degree direction

void deg_210(void)
{
digitalWrite(mtr2_p1, LOW);
digitalWrite(mtr2_p2, HIGH);
analogWrite(mtr2_spd, 255);
digitalWrite(mtr3_p1, LOW);
digitalWrite(mtr3_p2, LOW);
analogWrite(mtr3_spd, 0);
digitalWrite(mtr4_p1, HIGH);
digitalWrite(mtr4_p2, LOW);
analogWrite(mtr4_spd, 255);
}

(g) deg_330( ) function – make the Flexibot move toward 330 degree direction

void deg_330(void)
{
digitalWrite(mtr2_p1, LOW);
digitalWrite(mtr2_p2, LOW);
analogWrite(mtr2_spd, 0);
digitalWrite(mtr3_p1, LOW);
digitalWrite(mtr3_p2, HIGH);
analogWrite(mtr3_spd, 255);
digitalWrite(mtr4_p1, HIGH);
digitalWrite(mtr4_p2, LOW);
analogWrite(mtr4_spd, 255);
}

(h) anticlockwise( ) function – make the Flexibot rotate counterclockwise in the same position

void anticlockwise()
{
digitalWrite(mtr2_p1, LOW);
digitalWrite(mtr2_p2, HIGH);
analogWrite(mtr2_spd, 200);
digitalWrite(mtr3_p1, LOW);
digitalWrite(mtr3_p2, HIGH);
analogWrite(mtr3_spd, 200);
digitalWrite(mtr4_p1, LOW);
digitalWrite(mtr4_p2, HIGH);
analogWrite(mtr4_spd, 200);
}

(i) clockwise( ) function – make the Flexibot rotate clockwise in the same position

void clockwise()
{
digitalWrite(mtr2_p1, HIGH);
digitalWrite(mtr2_p2, LOW);
analogWrite(mtr2_spd, 200);
digitalWrite(mtr3_p1, HIGH);
digitalWrite(mtr3_p2, LOW);
analogWrite(mtr3_spd, 200);
digitalWrite(mtr4_p1, HIGH);
digitalWrite(mtr4_p2, LOW);
analogWrite(mtr4_spd, 200);
}

(j) stop( ) function – make the Flexibot stop to move

void stop()
{
digitalWrite(mtr2_p1, LOW);
digitalWrite(mtr2_p2, LOW);
analogWrite(mtr2_spd, 0);
digitalWrite(mtr3_p1, LOW);
digitalWrite(mtr3_p2, LOW);
analogWrite(mtr3_spd, 0);
digitalWrite(mtr4_p1, LOW);
digitalWrite(mtr4_p2, LOW);
analogWrite(mtr4_spd, 0);
} 

By following the sample code that is given in the above, we will have 10 different functions for Flexibot as shown below:

Figure 8: PS2 Controller Buttons

PS2 Controller button (pressed) Flexibot Functions
Up buttonMove forward with full speed
Down buttonMove backward with full speed
Left buttonMove toward left direction
Right buttonMove toward right direction
Triangle buttonMove toward 30 degree direction with full speed
Circle buttonMove toward 150 degree direction with full speed
Cross buttonMove toward 210 degree direction with full speed
Square buttonMove toward 330 degree direction with full speed
L1 buttonRotate in anti-clockwise on original position
R1 buttonRotate in clockwise on original position

 

In a nutshell, we can further control the Flexibot in more patterns after certain PS2 Controller is pressed such as move forward and backward continuously or do more than one type of function on table above at a time. It is the interesting part that we can manipulate with by programming. Furthermore, the Arduino programming environment is easy-to-use for beginners, yet flexible enough for advanced users to take advantage of as well. Thus, it is a right decision by choosing the Arduino to interface with FD04A and SKPS because it is easier to manipulate with if compare to PIC microcontroller.

That’s all from me, THANK YOU! See you again next time 🙂

Attachment

For download completed sample source code, click the link here.

Buy

, , ,

Related Post

Getting Started with Encoder Sensor Module (SN-ENC-MOD)

Getting Started with 3A Motor Driver Shield and Arduino Uno

Peak Current vs Continuous/Rated Current

Smartphone Controlled Wheel Robot

6 thoughts on “Interfacing FD04A and SKPS with Arduino

  1. hey, i would like to ask about the coding of joystick PS2 in ARDUINO. As above, there is no terms or declaration for joystick… is there any coding of joystick ps2 using this SKPS ??
    thanks in advance…

  2. Hi, you can download the sample code at the bottom of the tutorial. Including the sketch for Arduino. If you have further question, please feel free to discuss in our technical forum.

  3. hi, i m upload this code to my arduino uno board…
    after done uploading, the command window appear the word below:
    avrdude: stk500_getsync(): not in sync: resp=0x00

    so what is the problem??
    very very thank^^

  4. is there any new library need to be installed for Arduino in order to use SKPS protocols?
    is the cvoding same if Arduino Mega used instead of Arduino-due?
    Thanks

  5. [email protected] says:

    Hi Jonathan Law
    If U want to use serial 1 on arduino MEGA that involve pin (RX1,TX1)
    you just need to add number 1 after the word serial.

    example:

    Serial1.begin(9600); //Set serial baud rate as 9600
    Serial1.flush(); //Waits for the transmission of outgoing serial data to complete.

    unsigned char receive_data(void) //Function to wait for a byte receive from UART
    {
    unsigned char temp;
    while(!Serial1.available()); //Wait until data received
    temp=Serial1.read();
    return temp; //Return the received data
    }

    unsigned char skps(unsigned char data) //Function to send out a byte via UART
    {
    Serial1.write(data); //Send new data
    return receive_data(); //Return received data
    }

    Thats All

    =)

Leave a Reply

Your email address will not be published. Required fields are marked *