Cool Cap Engineer

Engineering by an anime nerd

1 Comment


Buck regulators are used for reducing the input voltage coming from a power supply to safely power microprocessors, gate drive circuitry, and other circuits. Today, I will show you how to use the LTC3639 – a buck regulator that can receive up to 150V- which comes with under-voltage and over-voltage lockout.

The LTC3639 should allows you to to easily change the output voltage, undervoltage lockout, overvoltage lockout, switching frequency, and the maximum peak current. I built 15V power supply using the LTC3639 and the DC1901A development board from Linear Technology.

Pin Functions


  • SW (Pin 1): Connects to the drain of the LTC3639’s internal MOSFET.
  • Vin (Pin 3): Supply pin for the IC
  • FBO (Pin 5): Feedback comparator output. Connect to VFB of other LTC3639’s in order to increase the output current. Otherwise, leave FBO floating.
  • VPRG2, VPRG1 (Pins 6, 7): Used for configuring the output of the LTC3639. These pins can be connected to either ground or the LTC3639’s SS pin.
GND GND Adjustable
SS SS 1.8V
  • GND (Pins 8, 16, 17): Ground connection. Pin 17 must be soldered to the PCB ground plane for rated thermal performance.
  • VFB ( Pin 9): Output voltage feedback. Connect to output voltage’s resistive divider for an adjustable output. Connect this pin to Vout for fixed output configurations.
  • SS (Pin 10): By connecting a capacitor from this pin to ground, the voltage ramp up time can be set. Leave floating to use internal 1ms soft start.
  • Iset (Pin 11): Leave this pin floating for 230ma peak current. Short this pin to ground for 25ma peak current. For a configurable peak current, connect a resistor from this pin to ground.
  • OVLO (Pin 12): Can be used to configure the overvoltage lockout using a resistive divider connected from the input supply. If the voltage on this pin is greater than 1.21V, then the overvoltage lockout is activated. The chip resumes operation when the voltage on the OVLO pin is less than 1.10V.
  • Run (Pin 14): Activates the chip when the voltage on this pin is greater than 1.21V. This pin shuts off the chip when the pin’s voltage is less than .7V. The Run pin is also used for setting the under voltage lockout.

Leaving  pin 12 floating sets the peak current to 230ma, while shorting this pin to ground sets the peak current to 25ma. For a different current peak current limit, a resistor should be connected from pin 11 to ground. The peak current cannot exceed 230ma and cannot be less than 20ma. The peak current is also twice the average current. The value of the peak current resistor can be found using the following equation…

The inductor is used to determine the LTC3639’s switching frequency when it is operating in burst mode, or when the LTC3639 is lightly loaded. The following equation is used for determining the inductance during burst mode.

If the LTC3639 will not be lightly loaded, then the inductor must meet the following two conditions…

Although the previous equations provide some insight on how the inductor will affect the switching frequency of the LTC3639, figure 2 gives a range of recommended inductor values given peak current for maximum efficiency.


It is recommended to use ferrite core inductors for their ability to handle a high switching frequency, and low core loss.

An input capacitor is necessary for filtering the trapezoidal current going into the source of the LTC3639’s internal high side MOSFET. If the maximum input voltage ripple is given, then the following equation can be used.

The output capacitor is needed to filter the inductor’s ripple current. If the desired output voltage ripple is given, the output capacitor can be selected as long as it follows the following two conditions…

How the output voltage is configured depends on the configuration of VPRG2 and VPRG1. For an adjustable output voltage, short VPRG2 and VPRG1 to ground, and connect VFB to an external resistive divider from the output. The output voltage can be set according to the following equation.

The only requirement on the resistive divider is to keep R2 less than 200K in order to keep output voltage variation less than 1%.


The undervoltage, and overvoltage lockout can be set using a three resistor divider.


All three resistors must satisfy the following condition…


By leaving pin 10 floating, the output voltage ramp time will typically be 1ms. If a longer ramp time is required, then connect a capacitor from pin 10 to ground. The value of capacitor can be computed from the following equation…

Although the ramp time can be calculated from C_ss, it must fulfill the following condition…


I used the LTC3639 to assemble 15V power supply circuit to power a gate drive circuit I was working on. The 15V power supply must perform according to the following requirements…

Overvoltage Lockout=97V
Undervoltage Lockout=18V

One of the first tests performed on the 15V circuit was loading it with a 100ma load and observing the start up time. It took 12ms for the output to start up after immediately applying 43V and 85V to the 15V power supply circuit.For more information about the LTC3639, look at the datasheet. Feel free to post a comment if you have a question or concern. Once again, I will see you guys next week!

1 Comment

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


Hey guys! Today’s post will be a relatively short post compared to the last post, but I just wanted to update you guys on my project. Well, the first thing I want to mention is that I finally tested the circuit with the new Arduino, and it works perfectly!  No short circuits, and the motor rotates properly. I included a picture of the schematic in the figure below. If you’re having a hard time viewing the picture, just click on it and it will expand.


So does that mean my project is finished? Nope. In fact, this project is just getting started. I want to make some serious improvements to this circuit. First obvious improvement is by making the circuit as cheap as possible. For example, the MOSFETs I used for the circuit are really nice and robust (they’re rated  around 150V/100A), but they cost $24 in total.  Second, I must include an overcurrent protection circuit as it will prevent the MOSFETs from getting damaged when the motor is stalled. The next improvement is the inclusion of a undervoltage and overvoltage lockout to protect the Arduino from any possible damage. Not to mention, by adding overvoltage and undervoltage lockout circuity, I can forget adding an  isolated DC-DC converter, which are really expensive. Finally, I will consider the project fully complete once I implement the circuit on an Arduino shield.

There’s another thing I want to look into. When I was running the motor control circuit with the motor attached,  I noticed that my power supply went into current limit whenever I commanded the motor to make a sudden turn.  This is due to the large amount of power needed to apply a torque large enough to change the rotation of the motor’s shaft. However, this solution can easily be fixed by implementing motor soft start code on the Arduino, which involves applying an increasing.decreasing PWM signal to the gate of the upper transistors to limit the power following through the motor.

Sorry for the short post today guys, but I spent most of last week preparing for my trip back to Philadelphia next week. I’m not sure if I will post a new update or a completely different article. Anyway, if you guys have any questions, comments, or concerns, feel free to post a comment. See you guys next week!

Leave a comment

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.

Leave a comment

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.

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!

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!

Leave a comment

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 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

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

1 Comment

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.


What’s The Bad News?

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.

Leave a comment

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.
Afterwards, you’ll need to download the Mojo project base.

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 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;

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)
	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;	

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%;

# PlanAhead Generated physical constraints 
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.


Now we need to burn the bitfile onto the Mojo board. If you have not done so, download the Mojo Loader. Once your download and extract the mojo loader and configure your computer to recognize the Mojo, open the application file inside the Mojo Loader folder. From the Mojo Loader select “Open Bin File.” Navigate to your Mojo Project Base and then to Mojo-Base->syn. Select the mojo_top.bit file.

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…



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.


Now, we need to choose a value for PR2 register. Here is my calculation for PR2.
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.
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 PBADEN = OFF
#pragma config LVP = OFF
#pragma config PWRT = ON
#pragma config DEBUG=OFF

int main(void)
    PR2 = 0b01111100 ;
    T2CON = 0b00000111 ;
   CCPR1L = 0b00111110;
    CCP1CON = 0b00101100;


Well, that’s it for me today! Did I miss something in my tutorial? Still have questions about the tutorial? Then please leave a comment below!

1 Comment

Mojo Makes FPGA Board Design Open Source


Last January, I’ve been learning how to use FPGAs, which I talked about in my Getting Started With FPGAs post.  To learn how to use FPGAs, I brought FPGA development boards, which can cost up to $110, from Fast forward to March, and I found Mojo, an open source $75 FPGA development board, on Based on what I’ve seen and tested thus far, I honestly think the Mojo board is a great introduction to digital design for electronic hobbyists and electrical engineering students.

What’s Under The Hood?
Unlike the Basys 2 that I commonly use, the Mojo uses a Spartan 6 XC6SLX9 FPGA. The Spartan 6 uses a 50MHZ clock, which is 10MHZ higher than the maximum clock frequency that a PIC18F can run.  In layman terms, it can run faster than most of my microcontroller/FPGA boards. The board can accept between 5V-12V, but can only output 3.3V for each digital I/O pin. Because of the Mojo’s 3.3V limitation, you’ll need extra hardware to use electronic items like servos, solenoids, etc. Finally, the Mojo uses an Atmega16U4 for the USB connection among other things.

What’s So Special About The Mojo?


The Mojo board is geared towards making digital design easy for hobbyists and electrical engineering students by taking advantage of the board’s Atmega16U4 microcontroller, which is used to implement your digital designs on to the board. However, the Atmega16U4 is not just used for uploading your designs to the Mojo. The Atmega16U4 is used to communicate serial objects like serial LCDs, XBee wireless modules, and even Sparkfun’s FTDI breakout boards, with the Mojo.  You can even use the analog to digital converters included with the Atmega16U4 to allow the Mojo to take in analog signals.

What I Love About The Mojo ?

Although this seems like a petty thing, one of the key qualities I look for in a FPGA development board these days is the number of accessible I/O pins.  When I first heard that the Mojo will offer 84 accessible I/O lines, I immediately gave my pledge for the project  In fact, the lack of accessible pins for the Basys 2 FPGA board severely limited me in terms of FPGA projects. Embedded Micro even added 8 LEDs on the board for FPGA beginners to use. These LEDs are prefect for beginners as they implement simple modules using these LEDs. During the early stages of testing the board, not only did I create a blinking module for one LED, but I also used the same module to control the other seven LEDs on the board.

Despite the sheer number of I/O pins available, one cannot forget the fact that the Mojo is open hardware, which simply means that the Mojo’s schematic is available to view. This is crucial if you want to create your own FPGA development board, but do not know where to start. In fact, I will probably use the Mojo’s schematic in the future.

Work Done Thus Far ?

mojoservoCurrently, I do not have any project plans for the Mojo board. My focus has been learning how to effectively use the Mojo board. Recently, I learned how to control a servo using the Mojo board, even though most servos can only powered using 5V. I was able to control the servo by connecting one of the Mojo’s pins to the optoisolator’s input pin, and the servo’s signal pin to the optoisolator’s Vo pin. Although I could not find a power supply around, I used my Arduino board to provide the right power to the servo.

Where Can I Learn How To Use The Mojo Board?


Although I am planning to write tutorials on how to use the Mojo board for this blog, you can visit, where you can read tutorials on how to implement serial communication, servo control, and other digital modules onto the Mojo. If you want to purchase your own Mojo board, then visit this URL. Please keep in mind that the Mojo board only cost $75.

Did anyone buy a Mojo board aside from me? Feel free to leave a comment about your impressions of the Mojo board!