# Cool Cap Engineer

## Projects: Arduino 24V Brushed DC Motor Controller Shield Update #2

In my last post, I talked about my newest project; a 24V DC brushed motor controller shield for the Arduino. I mentioned my setbacks in my post and that in my new post, I will give an update for the project. Unfournately, the only thing I could do last week was replacing my old Arduino Duemilanove with the Arduino Uno. Until I can test the circuit proper, I decided to spend this post discussing how I intend to control this 24V motor.

If you’re a regular visitor of this blog, an electrical engineer, or just an electronics hobbyist, then you’re probably familiar with an Hbridge. An Hbridge is an electronic circuit that changes the direction of current flowing to a motor in order to change the rotation its shaft. To change the flow of current, four switches are used. Using the figure 1 as a reference, the motor’s shaft rotates clockwise when S1/S4 turns on, and when S2/S3 turns on.

Today, these Hbridge switches are replaced with transistors. Lower voltage Hbridges can be made using Bipolar Junction Transistors (BJT) since they require little current to properly control lower voltage motors. However, if you’re control 12V and more motors, then BJTs would be horrible as they will require more current to control these motors. So instead, metal oxide semiconductor field effect transistors (MOSFET) are used since they “ideally” consume no power to turn them on. For motor control, Power MOSFETs are used. There are two types of MOSFETs: Pchannels and Nchannels. I will not discuss the physical difference between them, but I will mention that Nchannels turn on when you apply a positive voltage to its gate, and Pchannels turn off when you apply positive voltage to its gate.

Alas, MOSFETs also have problems to consider. One problem is that if you try to pass 12V or more through the Nchannel MOSFET, then you need at least 12V to turn them on. Also, to turn off a pchannel, you to apply the same voltage you’re sinking through the MOSFET to its gate. I seen many forum posts in which people blown out their MOSFET forgetting this important fact. Of course I’m no different since I made the same mistake senior year of college…and in my last post.

Now that I talked about the theory of this motor control, let me talk about my proposed circuit. In my Hbridge circuit, I have my upper switches comprised of pchannnel MOSFETs and my bottom switches made up of nchannel MOSFETs. To turn on just one pchannel MOSFET, the Arduino will activate a low current NPN transistor, which will turn on the pchannel MOSFET because of the voltage divider at its gate. However, when the NPN transistor is off, then the voltage at the pchannel’s gate will be equal to the voltage being applied to it. By having the voltage applied to equal to the voltage at its gate, the Pchannel turns off. This method is repeated for the other pchannel MOSFET.

Finally, to control whether an nchannel is off or on, 12V-20V must be applied to the gate. Unlike a pchannel MOSFET, the voltage applied to the gate can be a separate voltage from the vvoltage applied to it. To accomplish this, I used low side gate drivers (IR4427) to provide the right voltage to turn the bottom Nchannels on or off.

Now that I explained the theory the best way I could, let me discuss what’s next for this project. Although I did not test my Arduino to my motor control circuit yet, there’s a glaring problem with the circuit. There is no isolation between the Arduino and the circuits responsible for driving the MOSFETs. In order to achieve this, not only will have to look into optoisolators, but also isolated DC to DC DIP modules. But that’s another post. Another item that I will have to look into is setting up an undervoltage lockout circuit for the circuits driving the MOSFETs gates.  By adding an undervoltage lockout circuit, the motor will not turn on until the right amount of voltage is applied to the MOSFETs.

Anyway, that’s it for me today. If you have a question, comment, or concern, feel free to leave a comment. I will see you guys, next week.

## Projects: Arduino 24V Brushed DC Motor Controller Shield Update #1

So let’s rewind 7 months ago, when I was still in senior year of college. One of my final projects I was working on for my club was a drink dispensing robot. We were close to finishing the robot, but I could not get the motor drive working for it. No matter how much I tried, the circuitry for the motor controller kept burning out. I felt so ashamed because I could not figure out what was wrong with the circuit. Fast forward to today and I wanted to try to correct the mistakes I made for that project by working on a similar one. So, one of my new projects is a 24V brushed motor controller using Arduino.

So, I want the final version to be implemented as an Arduino Uno shield. The shield will allow the user to control two 24V DC motors and limit the current 10 amps (5 amps per motor). If one of the motors draw more than 5 amps, then the shield will shut off power to both motors. The shield should also provide some level of isolation between the Arduino and the motor controller. By providing some isolation between the Arduino and the motor controller,  the Arduino will not get damaged during overcurrent condition. To control the direction of both motors, I will be making a homebuilt Hbridge using a combination of pchannel and nchannel mosfets. For my pchannels mosfets, I used Fairchild’s FQP27P06. As for nchannel mosfets, I used FQP20N06.

Normally, I would talk about the theory behind the brushed motor controller shield and talk about testing later, but I drew a lot of the schematics by hand and will need to be either re-scanned or converted to an Eagle Cad schematic. I will mention that for driving the pchannels and nchannels, I used IR’s IR4427 dual low side gate driver.

Arduino motor controller implemented on a breadboard

The figure above shows my first implementation of the motor control circuit. This motor control circuit should allow me to change the direction the motor is rotating. For testing the circuit, I used a 24V scooter motor I brought from Ebay. Despite the fact the motor is a 120W motor, the motor will not be loaded. By not loading the motor, the motor draws a couple of milli-amps (200-300ma) when I connected 24V to it using my 32V/5A DC power supply.  Because of this, I thought it would be unnecessary to heatsink some of the parts of the circuit.After building the circuit, I was able to safely power the motor from 12V-19V, but then one of the motor control transistors burned out. “Why?” you ask? Because by the time I put 18V into the circuit/motor, it was drawing 2.5A.

One of the pchannel mosfets was damaged around 18V.

How could this be? If I provided a heatsink to each of the transistors as I should of, I could of pump 24V into the motor easily. To fix this situation, I was going to have to either grab a couple of heatsinks, or grab a piece of metal to act like a giant heatsink and attach the transistors to the piece of metal with sol-pads on the back. I also going to need to buy some more p channel mosfets T_T. However, I could not wait a couple weeks to solve the issue. I wanted it fixed now.

A homemade heatsink for power mosfets.

Luckily, I had several pieces of pre-drilled metal and sol pads already available. After attaching each transistor to the piece of metal and using every available alligator clip at my disposal to connect each transistor to the circuit I built on the breadboard, I tested the circuit again. Again, I was still having problems as the circuit was still drawing a high amount of current (around 4.89A). I later found out that the way I was driving my pchannels was completely wrong. For  pchannel mosfets to be fully off, the gate voltage must be equal to the source voltage. Because of my inproper way of controlling the pchannel mosfets, this caused a short in my Hbridge. So now I have to completely revise my previous hbridge control circuit in favor of a new one.

New motor control circuit. Surprisingly used less parts than the last circuit.

After testing the new circuit, I still saw a high current draw (again, around 4.89A). It was later revealed that my arduino was turning on both pchannels of my Hbridge, even though I specify in my code that only one pchannel should be on at a time.  In other words, this caused yet another  short in my Hbridge.When I looked at the voltage Arduino’s onboard 5v regulator was outputing, I found it that instead of outputting close to 5V, it was outputting 3.5V. Long story short, I need to replace my Arduino.

To see if my Hbridge was still working, I provided the input voltages using an 7815 voltage regulator. Now my circuit was drawing the correct amount of current!

Correct current draw. The circuit/motor was drawing 280ma and not 2.80 amps.

Well, that’s it for me today. Not only will I post the parts I used and the theory of the motor controller circuit soon, but I will keep you guys updated on the progress on this project. If you have any questions, comments, or concerns, feel free to leave a comment!

## PSA: Kept You Waiting, Huh?

Yah…it’s been quite a long time since my last post. A lot has happened since June: I graduated from RPI, moved to Kingston, NY, and started working at a company with a focus with motor control circuitry and power electronics, which I honestly did not expect. But enough about my long hiatus, now it’s time to talk about new blog updates, and when you guys can start seeing new posts.

First things, first: projects. I cannot believe I’m saying this, but my face tracker project will have to be cancelled, due to my lack of Open CV coding knowledge. I feel embarrassed cancelling this project, since this is the third project I will cancel on this blog. HOWEVER, I am working on a power electronics project, which is 70% finished (I just need to buy some parts and to test the project).  I will talk about this power electronics project on the blog very soon.

Second thing: categories. Now that I’ve been given a stellar electrical engineering education, I will be writing basic electronic posts. These posts will not only give you a premier on basic electronic parts, but more in-depth topics to consider.  Although I talked about the PIC18F here on the blog, I was introduced to the dsPIC30F family of microcontrollers quite some time ago. In other words, I will no longer be writing microcontroller tutorials on the PIC18F but rather on the dsPIC30F.

So when can you guys start seeing new posts? Right now, I am devising several new posts as we speak. I would say December 2nd. I will publish new posts once a week so that way it will not affect work life.

If you guys still want to hear what I’ve been doing, then visit my Twitter page. Here I will be posting some cool engineering projects I’ve found, as well as the projects I’m currently working on. Again, I apologize for the long hiatus, and expect some new content soon!

## Projects: Face Tracking Turret Update #3

And you guys thought I forgot about this project! Oh no. I just been busy with school that I had to put the project on the back burner. However, I did buy the rocket turret from ThinkGeek.com. Here’s what I found from my dissection from this rocket turret.

Underneath the rocket turret was a couple of rubber stickers, which actually hid the screws for the base. After unscrewing the base, I found the main controller for the turret. I could not tell what was the microcontroller that was used, because I spent most of my time learning which wire controls the three motors inside the turret.

After I unscrewed the bottom of the base, I proceeded to  the top of the base. It seems like the rocket turret is able to move 0 degrees to 180 degrees due to the mechanical system shown above. The top base can also tell whether or not it will not exceed 0 or 180 degrees due to the switches inside the top of the base.

So I proceeded to tear apart  the bottom base some more.  Inside the base contains two motors. These motors are responsible for rotating the head of the turret as well as moving the head 0 to 180 degrees.

The final part I looked at was the main turret head. This was the part I ran into. The problem I was having was figuring out how the firing mechanism work. The firing mechanism uses a small dc motor, but there seems to be a certain sequence that must be achieved before the missile can be launched. Because of this, I’m changing the face tracker to use a small water pump I found on Nerdkits.com.

Well, that’s all I have for you guys today. I’ll let you know any new developments I have for the project.

## Analog Discovery: An Electrical Engineer’s Best Friend

If there’s anything I learned in college, then it is this: you cannot solve a circuit related problem without having the proper debugging tools to do so.  There were numerous times in which I could not solve any problems in my circuit because I could not properly see a waveform, or could not measure the current correctly.  Then in my last semester of senior year, one of my professors gave to me Digilent’s Analog Discovery for free (I got a lot of free stuff during college).  After obtaining that piece of engineering beauty from my professor, I instantly fell in love with the device.

# What is the Analog Discovery?

The Analog Discovery is a USB multitool to properly debug or power your circuit.  This little device has an oscilloscope, function generator, DC power supply, digital outputs, and logic analyzer in one package.  All you need to use the device is a computer with a good ole’ USB connection.  So what tools are available? I’ll talk about the tools I used on the Analog Discovery thus far.

1) Oscilloscope: You will find yourself working with signals that will not always output a simple square pulse. Sometimes, you’ll work with sinusoidal, triangular, and even sawtooth waveforms. To make sure the circuit is outputing the correct waveform, you need to see it. This is where an oscilloscope comes in. Although I saw waveforms moving at 1KHZ (as seen in my implementing PWM on PIC18F tutorial), the fastest signal you can see on the scope is 5MHZ.

2) Multimeter:  Although most electrical engineers should have at least a multimeter in their possession, the Analog Discovery includes a multimeter as well! Although the device claims it can measure up to 20V, I usually play it safe and measure up to 5V.

# How do you use it?

Although I mentioned you need a USB micro cable, you also need to download Waveforms from Digilent’s website.  Underneath the Analog Discovery is a bunch of wires. Based on what tool you want to use, you’ll need to use a specified  wire. For example, if I wanted to use the tool’s oscilloscope, you need to connect Analog Discovery’s 1+ to your signal, and 1- to ground. Finally, if I wanted to use the 5V DC power supply, then I’ll connect my devices requiring 5V to V+ and my devices requiring ground to V-. If you want a more concrete look at Analog Discovery’s pinout diagram, then look at the picture below, which can also be found on Digilent’s website.

Unfortunately, I cannot say the device is free. Although I got this device for free……this device is quite expensive. Without a student discount, the Analog Discovery costs $200, which is enough to get you a really good oscilloscope, function generator, or logic analyzer for that price.If for you are a college student, then you can get a 50% student discount, which will make the Analog Discovery cost$100.

In my opinion, if you have \$200 laying around, I rather get standard EE equipment. However, if you move alot, and do not want to carry EE equipment with you where you go, then buy the Analog Discovery. You buy the Analog Discovery from Digilent’s website.

## Controlling A Seven Segment Display Using Mojo

Remember a couple weeks ago when I said that I will be working on tutorials for the Mojo? Today’s post will be the first post that will be a Mojo tutorial. Although I originally planned to show you guys how to create a simple half adder with a 7 segment display, I realized we have to learn how to control the seven segment display first.  Today, I will show you not only how to connect your seven segment display to the Mojo, but how to implement a digital design to control the display on the Mojo.

# What Are Seven Segment Displays?

A seven segment display is just seven LEDs arranged to display a number ranging from 0-9.  However, seven segment displays have two different connections: common anode and common cathode.Seven segment displays with a common anode connection means that all of the LEDs have their anodes connected together.  However, seven segment displays with common cathode connection means all of the LEDs cathodes are connected to each other. The picture on the bottom shows common anode and common cathode seven segment displays.

When you have a common anode display, you just need one current limiting resistor, but 7 devices to sink the current for each LED. A common cathode display requires multiple current limiting resistors, but one ground connection to sink the current for all of the LEDs. For today’s tutorial, we will use a common cathode seven segment display.

# What Will You Need?

1) (1x) Mojo Board

2) (1x) LTS-4301JR 7 Segment Display

3) (7x) 330 ohm resistor

4) (1x) Micro USB cable

# Wiring Connection

The picture below shows how to connect the 7 segment display to the Mojo.

# How To Implement Your Digital Design Onto The Mojo?

For the next part, you’ll probably need to visit my getting started with FPGAs guide as alot of the steps from that tutorial can be applied for this one such as downloading the Xilnix Web ISE.

After downloading and extracting the Mojo project base, open Xilnix Web ISE. Once you open up the Xilnix ISE, go to file->open project. Navigate to your Mojo Project base and select the Xilnix ISE Project file inside the folder.

From the hierarchy menu in the Xilnix Web ISE, double click on the Mojo Top.v file, and edit it to look like this.

module mojo_top(
input clk,
input rst_n,
input cclk,
output[7:0]led,
output spi_miso,
input spi_ss,
input spi_mosi,
input spi_sck,
output [3:0] spi_channel,
input avr_tx,
output avr_rx,
input avr_rx_busy,
output [6:0] display
);

wire rst = ~rst_n;

assign spi_miso = 1'bz;
assign avr_rx = 1'bz;
assign spi_channel = 4'bzzzz;

assign led = 8'b0;
seven_segment_display_decoder(.clk(clk),.number(9),.seven_segment_display_control_lines(display));
endmodule


Don’t worry about line 24. We’re going to add the seven_segment_display_decoder module right now. Right click on Mojo Top.v and add a new verilog file. We’re going to call it “seven_segment_display_decoder.” After creating the file, add the following code to the file.

timescale 1ns / 1ps
module seven_segment_display_decoder(clk,number,seven_segment_display_control_lines);
input clk;
input [8:0] number;
output reg [6:0] seven_segment_display_control_lines;

always @ (posedge clk)
begin
case(number)
1:seven_segment_display_control_lines = 7'b0110000;
2:seven_segment_display_control_lines = 7'b1101101;
3:seven_segment_display_control_lines = 7'b1111001;
4:seven_segment_display_control_lines = 7'b0110011;
5:seven_segment_display_control_lines = 7'b1011011;
6:seven_segment_display_control_lines = 7'b1011111;
7:seven_segment_display_control_lines = 7'b1110000;
8:seven_segment_display_control_lines = 7'b1111111;
9:seven_segment_display_control_lines = 7'b1110011;
default:seven_segment_display_control_lines = 7'b1111110;
endcase
end
endmodule


Finally, select your mojo.ucf file. Edit it to make sure it look like this.

#Created by Constraints Editor (xc6slx9-tqg144-3) - 2012/11/05
NET "clk" TNM_NET = clk;
TIMESPEC TS_clk = PERIOD "clk" 50 MHz HIGH 50%;

NET "clk" LOC = P56;
NET "rst_n" LOC = P38;

NET "cclk" LOC = P70;

NET "led<0>" LOC = P134;
NET "led<1>" LOC = P133;
NET "led<2>" LOC = P132;
NET "led<3>" LOC = P131;
NET "led<4>" LOC = P127;
NET "led<5>" LOC = P126;
NET "led<6>" LOC = P124;
NET "led<7>" LOC = P123;

NET "spi_mosi" LOC = P44;
NET "spi_miso" LOC = P45;
NET "spi_ss" LOC = P48;
NET "spi_sck" LOC = P43;
NET "spi_channel<0>" LOC = P46;
NET "spi_channel<1>" LOC = P61;
NET "spi_channel<2>" LOC = P62;
NET "spi_channel<3>" LOC = P65;

NET "avr_tx" LOC = P55;
NET "avr_rx" LOC = P59;
NET "avr_rx_busy" LOC = P39;
NET "display<0>" LOC =P87;
NET "display<1>" LOC =P85;
NET "display<2>" LOC =P84;
NET "display<3>" LOC =P83;
NET "display<4>" LOC =P82;
NET "display<5>" LOC =P81;
NET "display<6>" LOC =P80;


So we finished all of the code for the Mojo. Now we need to create the bit file. Before we create the bit file, make sure you save everything. Once you save everything, select mojo_top.v. Underneath the hierarchy menu is the processes menu. Look for “Generate Programming File” in the Processes menu. Click on it. Xilnix will now try to create a bitfile aka your digital design.

Finally, select load file. When the bit file is uploaded to your Mojo, you should see a 9 on the seven segment display.

Well that’s it for today you guys! I’ll leave you to figure out how to change the number displayed on the display. If you guys have any more questions or concerns, then please leave a comment!

## Implementing PWM On PIC18F Microcontroller

PWM output with 20% duty cycle.

# What is PWM?

As you guys know, microcontrollers, FPGAs, and other embedded processors can only output a high or low signal based on the power supplied to it. In other words, embedded processors cannot output a variety of voltages. Despite this, we have the processor “pretend” to output different voltages using a series of pulses and varying the width of the pulses. The technique of varying the width of a series of pulses is called Pulse Width Modulation (PWM).

The picture above shows an example of a simple pulse train.  The time it takes for each pulse to start again is referred as its period (Ts).  In each pulse train, the pulse is on for a certain amount of time before turning off. The percentage which the pulse’s on time compared to its time off is called the duty cycle.  Duty Cycle is expressed as…

$D=\frac{T_{on}}{T_{on}+T_{off}}$

# PIC18F And PWM

As you’re already aware of, all microcontrollers feature a set number of PWM pins. But what pins are specifically designed for PWM? When reading your PIC18F datasheet, look for pins that have CCP (compare capture pwm) labeled. So let’s look at a picture featuring the PIC18F4553

As you see in the picture, RC1, RC2 and RB3 can be configured as PWM pins.

# PWM Calculations

So there are three crucial registers you must take in account for PWM: PR2, and CCPRXL:CCPXCON[5:4]. Configuring the PR2 register, will allow you to configure the PWM period while selecting a proper the value for CCPRXL:CCPXCON[5:4] will allow you to choose the right duty cycle. For choosing the right value for the PR2 register, use the following equation…

$PR2=\frac{PWM Period}{4*T_{osc}*TMR Prescale Value}-1$

Keep in mind that Tosc is the the inverse of the frequency that your microcontroller is running at. Also, we will use a prescale of 16 as we will use this in the upcoming code example. For CCPRXL:CCPXCON[5:4], we will use the following equation…

$CCPRXL:CCPXCON[5:4]=\frac{PWM Duty Cycle}{T_{osc}*TMR Prescale}$

CCPRXL:CCPXCON[5:4] is a 10 bit number. To make things easier on you guys, make sure you convert your number to a 10 bit binary number. After you convert the number to a binary number, set your CCPRXL register to the first 8 MSB numbers. Finally, take the last 2 LSB and you use it for your CCPXCON[5:4].

# PWM Coding Procedure

Now that I talked about the calculations, let’s talk about how to code your PIC18F microcontroller to use PWM. For this tutorial, we will use the PIC18F4553′s  CCP1 pin, set the PWM period at 1KHZ, a time prescale of 16, a clock frequency of 8*10^6 and a 50% duty cycle.Parts of the final code can be found in my Beginner’s Guide To PIC18F Microcontroller.

First we need to configure the CCP1 pin as an output pin,which is RC2.To do this we set the TRISCbits.RC2 to 0 and PORTCbits.RC2 to 0.

TRISCbits.RC2=0;
PORTCbits.RC2=0;


Now, we need to choose a value for PR2 register. Here is my calculation for PR2.
$PR2=\frac{10^{-3}}{4*16*\frac{1}{8*10^{6}}}-1=124$
Now that we found the number, let’s convert it to a binary number and use it.

PR2 = 0b1111011;

PWM uses timer2 to work. So we need to not only turn on timer2, but we must set the time scale to 16. This can be done by properly configuring T2CON by setting T2CON’s TMR2ON to 1, and T2CKPS1 to 1.

T2CON = 0b00000111 ;

CCPR1L:CCP1CON[5:4] is the last thing to compute.
$CCPR1L:CCP1CON[5:4]=\frac{\frac{1}{10^{3}}*.5}{\frac{1}{8*10^{6}}*16}=250$
When we convert 250 to its binary value, we get 0011111010. However, we take the first 8 MSB for CCPR1L and the last 2 LSB for CCP1CON bit 5 and CCP1CON bit 4.

CCPR1L = 0b00111110;
CCP1CON = 0b00101100;


Since you guys been so swell, here’s the final code for this example.

#include <p18f4553.h>
#include <stdio.h>
#include <stdlib.h>
#include <usart.h>
#include <delays.h>
#include <portb.h>

#pragma config FOSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config PWRT = ON
#pragma config DEBUG=OFF

int main(void)
{
TRISCbits.RC2=0;
PORTCbits.RC2=0;
PR2 = 0b01111100 ;
T2CON = 0b00000111 ;
CCPR1L = 0b00111110;
CCP1CON = 0b00101100;

while(1);
}
`