Generating a PWM Signal Based on an Analog Input - miKroC

If you like to experiment with microcontrollers you've probably tried out analog-to-digital conversion and generating PWM signals. Using a popular PIC microcontroller like the 16F877A, which has both ADC and CCP (Capture/Compare/PWM) modules, you can test out both of these easily because mikroC provides easy to use library functions for both of them. (Most of you will be familiar with these libraries, so I won't be going in to much detail about them here. Leave a comment if you like to learn more.. :) )

Now, think of a scenario like this: You want to change the duty cycle of your PWM signal based on a analog signal. It can be from a variable resistor that you want to use to increase or decrease the duty cycle, or it can be a feedback from a sensor which is analog.

So, how can we do it?... we simply use the ADC to convert the analog signal and use that value as the duty cycle value. All we need is a microcontroller that has both a CCP and a PWM module. As I said before, the popular 16F877A fits the criteria.

So, now that we have our microcontroller, let's get down to the code.

Here's the sample code
unsigned int temp_res;
unsigned char temp_duty;

void main() {
  ADCON1 = 0x80;  // Configure analog inputs and Vref
  TRISA  = 0xFF;  // Set PORTA as an input
  TRISC = 0; // Set PORTC as an Output
  PORTC = 0;
  // Initialize and start the PWM unit

    temp_res = Adc_Read(2); // Get results of AD conversion
                                from channel 2 (RA2 pin)

    temp_duty = (temp_res/4); // Convert the 10 bit value
                                to 8 bits

    PWM_Change_Duty(temp_duty); // Set the duty value

    Delay_ms(50); // Slow everything down a little

Let's see what's happening in the code,
  • First, we declare 2 variables - temp_res to store the result of AD conversion and temp_duty to store the 8 bit value for the duty cycle.
  • We use the ADC_Read(2) function to read the analogue input from AN2 pin, convert it to digital and store the 10 bit value in temp_res. 
  • Since the value for the duty cycle should be 8 bits (0 to 255) we simply divide the temp_res by 4 and store it in temp_duty.
  • Finally, we use the PWM_Change_Duty() function and set the duty cycle to the value of temp_duty.
There's nothing new in this code that you don't already know :)

Here's the basic circuit for testing this code
Use the R2 variable resistor to get the varying analog signal. The brightness of the LED will change based on the PWM.

You can use this same method to generate a PWM based on any analog signal. Just think of the possibilities...



  1. What is different about the pic16f887 that this code doesnt work with it?

  2. i tried your code, its working but as the analog voltage is changed the pulse width changes but the frequency shown in the DSO remain same, whats the problem?????

  3. So, before you play, make sure you stretch the muscles
    used in swinging your golf club. Don't jump cold, warm up first until you break a light sweat. Most of us love it; going to Mac - Donald or Burger King from time to time.

    Feel free to visit my web site - women fitness tips abs

  4. I've listened to this a dozen or more times and still find it delightful and sooooo original in concept. I have more stuffings in me from yesterday than I care to think about. All kinds of sports news today are offered live through the television.

    my web blog - Latest Daily News

  5. Hi there, all the time i used to check web site posts here in
    the early hours in the dawn, for the reason that i like to learn more and more.

    my homepage: fitness tips of the day

  6. This study shows that progression of AIDS
    become rapid in the presence of negativity due to less social gathering and stress in life.

    Some pictures will remind you of your ambitions about your life while other humorous ones will relieve your stress and make your
    life more comfortable and easier. However, for the
    next several miles until we got to our hotel,
    he smelled pretty dog-gone good, not.

    Stop by my webpage - really funny pictures and sayings

  7. We are a group of volunteers and opening a new scheme in our community.
    Your site provided us with valuable information to work on.

    You have done an impressive job and our whole community will be
    thankful to you.

    my web page Read �

  8. But it's always wise to check in with a doctor before you start using a supplement regularly. By now it's a well established scientific fact that outdoor
    exercises are really good for your mental and emotional, as well as your physical
    health. Menopause weight gain is not inevitable, but it can be more difficult to maintain a healthy weight as your metabolism slows.

    my web-site visit the following web site


Post a Comment

Popular posts from this blog

What is the meaning of the numbers on Brushless Motors?

Li-Po Batteries Explained - Part 4