
Interfaces Project
Introduction
The embedded software that was used in this project is based in the software developed from August to December of 2017; nevertheless, the software in this release was substantially improved, and new features were implemented. The most important new feature in this release is the implementation of I2C communication and the reading of the value of the accelerations Ax, Ay, and Az sensed by a MPU6050 (Accelerometer + Gyroscpe).
Redesign and Migration from PIC18F25K80 to PIC18F45K80
In the August-December 2017 release, the software was implemented using a Microchip PIC18F25K80, a high performance 8-bit micro controller with 28 pins. This micro controller was enough for the first release because only 6 motors were controlled, no I2C communication was implemented, and no analog pins were required. But for the January-May 2018 release, the mechanical and electronic capabilities of the robotic arm kit were improved and expanded, demanding a redesign of the software.
While redesigning the embedded software, it was taken into account the following features required:
-
Control 7 stepper motor drivers (2 digital inputs required by each driver: 1 for the clock, 1 for the direction).
-
Reading 3 analog signals generated by a joystick module (x, y) and linear potentiometer (z).
-
Implementation of serial communication.
-
Implementation of I2C communication.
-
Implementation of an emergency stop button.
The following block diagram was designed in order to visualize all the interfaces connected to the micro controller:

Figure 0.
Block diagram of the PIC18F45K80 with its different interfaces.
The PIC18F25K80 didn´t have the sufficient number of pins for implementing the electronic and mechanic improvements; therefore, a new micro controller was selected. The performance of the PIC18F25K80 was sufficient for correctly performing every task, so the selected micro controller was preferably chosen from the same PIC18F family. After analyzing different options, a PIC18F45K80 was selected; this Microchip PIC18 has the following features that fulfilled our requirements:
-
4 Timers/Enhanced Compare/Capture/PWM modules for precision timing control.
-
11char, 12-bit ADC for advance analog interface
-
Operating voltage 1.8V – 5.5V
-
1 MSSP serial port for SPI or I2C™ communication
-
2 Enhanced USART modules
-
Program Memory Type: Flash
-
Program Memory Size: 32 KB
-
CPU Speed: 16 MIPS/DMIPS
-
Pin count: 40

Figure 1.
Pins diagram of the PIC18F45K80 PDIP.
Once the micro controller was selected, every pin was assigned to its corresponding external inout/output. As shown in the Figure 1, the majority of these pins can be configured to perform different functions, so depending on the capabilities of each pin the following pin layout (Figure 2) was conceptualized while designing and writing different short test codes:

Figure 2.
Pin layout that the robotic arm kit requires from the PIC18F45K80.
All the pins in Figure 2 that are assigned to an external input/output are in white, the pins 11, 12, 31 and 32 are connected to ground and an external 5V supply, the pins 1, 39 and 40 are connected to a PICKIT3 allowing the programming and debugging of the PIC while using MPLAB IDE. The pins 19, 20, 21, and 22 are multiplexed and further configuration has to be set in order to use these pins as general input/output pins (GPIOS); and while trying to use pins 34 and 35 as GPIOS, the compiler (XC8 compiler used) showed an unidentified error, so it was assumed that further configuration is also required to be set. Therefore, these to set of pins weren't used nor implemented in this release.
Components and Interfaces
MPU 6050
MotionInterface™ is becoming a “must-have” function being adopted by smartphone and tablet manufacturers due to the enormous value it adds to the end user experience. In smartphones, it finds use in applications such as gesture commands for applications and phone control, enhanced gaming, augmented reality, panoramic photo capture and viewing, and pedestrian and vehicle navigation. With its ability to precisely and accurately track user motions, MotionTracking technology can convert handsets and tablets into powerful 3D intelligent devices that can be used in applications ranging from health and fitness monitoring to location-based services. Key requirements for MotionInterface enabled devices are small package size, low power consumption, high accuracy and repeatability, high shock tolerance, and application specific performance programmability – all at a low consumer price point.
The MPU-60X0 is the world’s first integrated 6-axis MotionTracking device that combines a 3-axis gyroscope, 3-axis accelerometer, and a Digital Motion Processor™ (DMP) all in a small 4x4x0.9mm package. With its dedicated I2C sensor bus, it directly accepts inputs from an external 3-axis compass to provide a complete 9-axis MotionFusion™ output. The MPU-60X0 MotionTracking device, with its 6-axis integration, on-board MotionFusion™, and run-time calibration firmware, enables manufacturers to eliminate the costly and complex selection, qualification, and system level integration of discrete devices, guaranteeing optimal motion performance for consumers. The MPU-60X0 is also designed to interface with multiple non-inertial digital sensors, such as pressure sensors, on its auxiliary I2C port. The MPU-60X0 is footprint compatible with the MPU-30X0 family.
The MPU-60X0 features three 16-bit analog-to-digital converters (ADCs) for digitizing the gyroscope outputs and three 16-bit ADCs for digitizing the accelerometer outputs. For precision tracking of both fast and slow motions, the parts feature a user-programmable gyroscope full-scale range of ±250, ±500, ±1000, and ±2000°/sec (dps) and a user-programmable accelerometer full-scale range of ±2g, ±4g, ±8g, and ±16g.
Communication with all registers of the device is performed using either I2C at 400kHz or SPI at 1MHz (MPU-6000 only).
Where to buy? ($100.00 MX)
https://hetpro-store.com/mpu6050/

MPU 6050 showing all of its pins.

Axis of the MPU6050. It can measure the acceleration along the 3 axes, and around those same axis.
Bluetooth Module HC05 or HC06
HC‐05 module is an easy to use Bluetooth SPP (Serial Port Protocol) module,designed for transparent wireless serial connection setup.The HC-05 Bluetooth Module can be used in a Master or Slave configuration, making it a great solution for wireless communication.This serial port bluetooth module is fully qualified Bluetooth V2.0+EDR (Enhanced Data Rate) 3Mbps Modulation with complete 2.4GHz radio transceiver and baseband. It uses CSR Bluecore 04‐External single chip Rluetooth system with CMOS technology and with AFH (Adaptive Frequency Hopping Feature).
Where to buy? ($175.00 MX)
https://www.taloselectronics.com/producto/modulo-bluetooth-rs232-hc-06/

Bluetooth module HC 06 top view.

Bluetooth module HC 06 bottom view.
Stepper Motors and Drivers
A stepper motor is a brushless DC electric motor that divides a full rotation into a number of equal steps. The motor's position can then be commanded to move and hold at one of these steps without any position sensor for feedback (an open-loop controller), as long as the motor is carefully sized to the application in respect to torque and speed (Wikipedia).
Where to buy?
https://www.pololu.com/category/87/stepper-motors
Brushed DC motors rotate continuously when DC voltage is applied to their terminals. The stepper motor driver converts a train of input pulses (typically square wave pulses) into a precisely defined increment in the shaft position of the stepper motor. Each pulse moves the shaft through a fixed angle. The way these stepper motor drivers work was very for the design of the embedded software because there is a function that generates a square wave in each of the Pulse Motor # pins, this function named sendPulses(motor, direction) controls the pin that will generate the pulses and its frequency.
Where to buy?

NEMA 17 Stepper Motor

Stepper Motor Connected to Driver

Digital stepper driver for NEMA 17, 23, 24 stepper motors.

TB6560 Stepper Motor Driver Board


Joystick Module
This module has 5 pins: Vcc, Ground, X, Y, Key. The thumbstick is analog and provides more accurate readings than simple ‘directional’ joysticks tat use some forms of buttons, or mechanical switches. Additionally, you can press the joystick down to activate a ‘press to select’ push-button. To get stable readings from the Key /Select pin, it needs to be connected to Vcc via a pull-up resistor.
When Vcc connected to 5V and Ground to a grounds, this module produces an output of around 2.5V from X and Y when it is in resting position. Moving the joystick will cause the output to vary from 0V to 5V depending on its direction. If you connect this module to amicrocontroller, you can expect to read a value of around 512 in its resting position (expect small variations due to tiny imprecisions of the springs and mechanism) When you move the joystick yous hould see the values change from 0 to 1023 depending on its position.
Where to buy? ($40.00 MX)
https://www.taloselectronics.com/producto/modulo-joystick-xy-de-eje-dual/
Serial Communication Using the EUSART1 Module
The Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART) module is one of two serial I/O modules. (Generically, the EUSART is also known as a Serial Communications Interface or SCI.)
The EUSART can be configured as a full-duplex, asynchronous system that can communicate with peripheral devices, such as CRT terminals and personal computers. It can also be configured as a half-duplex synchronous system that can communicate with peripheral devices, such as A/D or D/A integrated circuits, serial EEPROMs, etc.
As mentioned before, the PIC45K80 is equipped with two independent EUSART modules, referred to as EUSART1 and EUSART2. They can be configured in the following modes:
-
Asynchronous (full duplex) with:
-
Auto-wake-up on character reception
-
Auto-baud calibration
-
12-bit Break character transmission
-
-
Synchronous – Master (half duplex) with selectable clock polarity
-
Synchronous – Slave (half duplex) with selectable clock polarity
It was important to take into account that the pins of EUSART1 and EUSART2 are multiplexed with the functions with the following ports.
The operation of each Enhanced USARTx module is controlled through three registers:
-
Transmit Status and Control (TXSTAx)
-
Receive Status and Control (RCSTAx)
-
Baud Rate Control (BAUDCONx)
In this project, we used the USART1 with the following configuration:
TXSTA1=0b00100000;
RCSTA1=0b10010000;
BAUDCON1=0b00000000;
SPBRG1=12;
Since all the code was implemented using interruptions, it was necessary to set the following priorities as high:
//Receiver priorities
RC1IE=1;
RC1IP=1;
RC1IF=0;
//Transmitter priorities
//TXBIEbits.TX1IE=1;
PIE1bits.TX1IE=0;
TX1IP=1;
PIR1bits.TX1IF=0;
And the code within the interuption is the following:
void interrupt ISR_alta (void) //high priority{
//Transmitter
if ((PIE1bits.TX1IE==1) && (PIR1bits.TX1IF==1)){
PIE1bits.TX1IE = 0;
banderaTX=1;
}
//Receiver
if ((RC1IE==1) && (RC1IF==1)){
recepcion[k] = RCREG1;
banderaRC=1;
}
}
The other functions related to the serial communication are:
-
sendMsg();
-
getInstruction();
Implementing I2C communication with the MSSP module for the MPU6050
I2C Communication
The Master Synchronous Serial Port (MSSP) module is a serial interface, useful for communicating with other peripheral or microcontroller devices. These peripheral devices may be devices such as serial EEPROMs, shift registers, display drivers and A/D Converters. The MSSP module can operate in either of two modes:
-
Serial Peripheral Interface (SPI)
-
Inter-Integrated Circuit (I2C™)
-
Full Master mode
-
Slave mode (with general address call)
-
The I2C interface supports the following modes in hardware:
-
Master mode
-
Multi-Master mode
-
Slave mode with 5-bit and 7-bit address masking (with address masking for both 10-bit and 7-bit addressing)
The MSSP module has three associated control registers. These include a status register (SSPSTAT) and two control registers (SSPCON1 and SSPCON2). The use of these registers and their individual configuration bits differ significantly depending on whether the MSSP module is operated in SPI or I2C mode.
The MSSP module in I2C mode fully implements all master and slave functions (including general call support), and provides interrupts on Start and Stop bits in hardware to determine a free bus (multi-master function). The MSSP module implements the standard mode specifications, as well as 7-bit and 10-bit addressing.
Two pins are used for data transfer:
-
Serial Clock (SCL) – RC3/REFO/SCL/SCK
-
Serial Data (SDA) – RC4/SDA/SDI
We had to configure these pins as inputs by setting the associated TRIS bits, which in the case of the PIC18F45K80 were:
-
TRISC4 = 1;
-
TRISC3 = 1;
The MSSP module has seven registers for I2C operation. These are:
-
MSSP Control Register 1 (SSPCON1)
-
MSSP Control Register 2 (SSPCON2)
-
MSSP Status Register (SSPSTAT)
-
Serial Receive/Transmit Buffer Register (SSPBUF)
-
MSSP Shift Register (SSPSR) – Not directly accessible
-
MSSP Address Register (SSPADD)
-
I2C Slave Address Mask Register (SSPMSK)
SSPCON1, SSPCON2 and SSPSTAT are the control and status registers in I2C mode operation. The SSPCON1 and SSPCON2 registers are readable and writable. The lower 6 bits of the SSPSTAT are read-only. The upper two bits of the SSPSTAT are read/write. SSPSR is the shift register used for shifting data in or out. SSPBUF is the buffer register to which data bytes are written to or read from.
For this project, the configuration of these mentioned registers is the following:
SSPSTATbits.SMP = 0;
SSPSTATbits.CKE = 1;
SSPCON1 = 0b00101000;
SSPCON2 =0b00000000;
SSPADD = BITRATE;
For a correct implementation of the I2C communication is necessary to enable the following interruption priority levels:
SSPIE = 1;
SSPIF = 0;
The functions related to the I2C communication are the following:
void I2C_Ready(); // Check weather I2C is ready/idle or not
void I2C_Init(); //Initialize I2C configuration
char I2C_Start(char); //Send START pulse with slave device write address
void I2C_Start_Wait(char); //Send START pulse with slave device write address until acknowledge
char I2C_Repeated_Start(char); //Send REPEATED START pulse with slave device read address
char I2C_Stop(); //Send STOP pulse
char I2C_Write(unsigned char); // Write data/address to slave device
void I2C_Ack(); //Send acknowledge to slave for continue read
void I2C_Nack(); //Send negative acknowledge to slave for stop current communication
char I2C_Read(char); //Read data from slave devices with 0=Ack & 1=Nack
Interfacing MPU6050 Accelerometer - Gyroscope
As mentioned before, the MPU6050 generates 7 different values:
-
Acceleration along the x-axis
-
Acceleration along the y-axis
-
Acceleration along the z-axis
-
Acceleration around the x-axis
-
Acceleration around the y-axis
-
Acceleration around the z-axis
-
Temperature
The MPU6050 can communicate with the PIC18F45K80 micro controller using the previously I2C communication protocol.
How does the gyroscope work?
When the gyros are rotated about any of the sense axes, the Coriolis Effect causes a vibration that is detected by a MEM inside MPU6050. This voltage is digitized using 16-bit ADC to sample each axis. The full-scale range of output are +/- 250, +/- 500, +/- 1000, +/- 2000.
How does the accelerometer work?
Acceleration along the axes deflects the movable mass. This displacement of moving plate (mass) unbalances the differential capacitor which results in sensor output. Output amplitude is proportional to acceleration. A 16-bit ADC is used to get digitized output. The full-scale range of acceleration are +/- 2g, +/- 4g, +/- 8g, +/- 16g. It measured in g (gravity force) unit. When device placed on flat surface it will measure 0g on X and Y axis and +1g on Z axis
In this project, we currently use only the accelerometer, e.g., the values 1, 2 and 3. We communicate with the PIC18 to the MPU6050 using the following functions:
MPU6050_Init();
MPU_Start_Loc();
I2C_Stop();
readIMU();
The MPU6050_Init() function is only used once at the beginning of the program. It is necessary for correctly initializing the MPU6050.
The functions MPU_Start_Loc() and I2C_Stop() are used within the the readIMU() function. You can see how this functions are implemented in the following piece of code:




MSSP CONTROL REGISTER 1

MSSP CONTROL REGISTER 2

MSSP STATUS REGISTER
Implementation
After confirming the pins layout, and taking into account a similar arrangement to the block diagram of figure 0, a PCB was designed and manufactured for an easy plug-and-play of all the interfaces. In Figure 4, the PCB can be appreciated with the PIC18 mounted on it, and connected to the Bluetooth, 7 stepper motor drivers, MPU6050 and a USB TTL.
As you can appreciate, the implementation differs a little bit from the original block diagram: the USB TTL was never considered in the original design, and the joystick was not implemented; the explanation of this is that the way of operating the robotic arm was slightly modified due to technical issues that were unsolved. A graphic user interface (GUI) was designed in MATLAB for entering the desired x, y, and z coordinates of the end effector of the robotic arm, this GUI then sends those coordinates to the back-end where the inverse kinematics of the robotic arm is solved generating the number of degrees each of the articulations (proportionally related to the rotation of each motor) has to rotate. This set of quantity of degrees is then sent through the a COM port of the PC using serial communication with a USB TTL. Subsequently, once the micro controller receives each ASCII string, it processes the strings and turn them into different instructions for moving each of the 6 motors. The PIC18 is reading the information of the MPU6050 through I2C every 200 milliseconds. And the complication comes when the PIC18 tries to send back the values of MPU6050 to the GUI in the PC through serial communication because the program in MATLAB never receives the information. We tried to solve this issue with different

Figure 2.
Pin layout that the robotic arm kit requires from the PIC18F45K80.
approaches but at the end we chose to use a Bluetooth for sending information (in this case the acceleration values of the MPU6050) from the PIC18 to the PC displaying the information in Tera Term, and implement the USB TTL to send information (in this case the degrees of each articulation has to rotate) from the PC to the PIC18.
We believe that the PC we were using was not 100% compatible with the USB TTL, or that MATLAB was already executing a lot of operations and didn't support the reading of the serial port at the same time.

PCB Buttom Layer

PCB Butom Layer

PCB with all of its electronics components welded.

PCB with all of its electronic components, the PICC18 mounted, and all the interfaces connected.