Pages: [1]   Go Down
Author Topic: variable pwm frequency and duty cycle using potentiometer  (Read 2587 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using timer 1 library to vary the frequency and duty cycle using two potentiometers. i am a beginner and do not have much programming knowledge. please help me regarding this problem asap. i only have a very limited time for my project.
thanksftp://

Code:
#include "TimerOne.h"
#include <Servo.h>
Servo myservo;
int potpin = 0;
int potpin2 = 1; // analog pin used to connect the potentiometer
int val1;    // variable to read the value from the analog pin
int duty;
int freq;
int period;
void setup()
{
 
  freq=1/period;
 
 
 
  pinMode(13, OUTPUT);
  Timer1.initialize(freq);         // initialize timer1, and set a 1/2 second period
  Timer1.pwm(13, duty, freq);                // setup pwm on pin 9, 50% duty cycle
  Timer1.attachInterrupt(callback);  // attaches callback() as a timer overflow interrupt
}

void callback()
{
  digitalWrite(13, digitalRead(13) ^ 1);
}

void loop()
{
   duty = analogRead(potpin2);
    duty = map(duty, 0, 1023, 0, 100);
   period = analogRead(potpin);
    period = map(period, 0, 1023, 20, 500000);
  Timer1.pwm(13, duty, freq);
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

please reply asap thanks. if anyone know how to vary the frequency please tell me
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 643
Posts: 50441
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
please help me regarding this problem asap.
This isn't a homework help line.

It also isn't a psychic hot line. You need to tell us what the problem you are having is.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Waiting 3 minutes and asking for a reply ASAP will not win you points here.

You have a pretty obvious flaw.  How many times does your sketch update the variable "freq"?

Does fixing that fix your problem?  We don't know.  You haven't stated:  what you expected to happen and what is actually happening.
« Last Edit: February 16, 2011, 06:55:59 pm by James C4S » Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your reply james and paul. i dont know how many times my sketch updates the variable "freq"? i am not asking you to solve this for me paul. all i am asking is that i am a beginner in programming and need serious help.
to start with:
this is my idea of varying the frequency. i am using a potentiometer at input 0 and input 1. from these input i am trying to map the input to 20 to 500000 i.e. freq of 50Khz-1khz. im not sure if this is right? but your help would be much appreciated.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 643
Posts: 50441
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i dont know how many times my sketch updates the variable "freq"?
Look through your code. Find everywhere where freq is assigned a value. (freq = something). How many times does that happen? I see it only in setup(), which executes exactly once.

freq is defined as an int. Unless period is 1, 1/period, as an int, will be 0.

As currently set up, period is actually 0. So, freq = 1/0. What is the result of that division?

Why do you need to change the frequency of PWM?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for replying paul.

How can i fix this problem?
 I am an electrical engineering student and have no programming skills at all. I am trying to build a motor controller. I chose arduino because i heard it was easy to program. I am willing to learn and if you suggest me any good books, i will study them.
The motor controller is based on a hip4081 chip and it is fed by pwm signal. My supervisor has asked to to vary both frequency and duty cycle using two potentiometers. This is my first ever coding experience. So, i apologize if i ask any stupid questions.
Moreover i think if arduino make a library for varying pwm frequency, that would be helpful to many people.
Moreover, how can i later on make an interface serially and hence make a program which can control this motor from computer?
your help much appreciated.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 643
Posts: 50441
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Moreover i think if arduino make a library for varying pwm frequency, that would be helpful to many people.
There are ways of varying the pwm frequency, though I have no first hand experience with doing so. Google the forum for some ideas. Encapsulating that capability in a library is a good idea.

Be sure to use proper variable types, and compute the frequency (or, more likely, period) at the appropriate times in your library.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

can anyone tell me how do you call a function from library. i am using timer1 library and am wondering if i am calling the timer1.pwm() correctly?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Moreover i think if arduino make a library for varying pwm frequency, that would be helpful to many people.
There are ways of varying the pwm frequency, though I have no first hand experience with doing so. Google the forum for some ideas. Encapsulating that capability in a library is a good idea.

Be sure to use proper variable types, and compute the frequency (or, more likely, period) at the appropriate times in your library.

i intend to use a potentiometer pluged into one of the analogue pins of arduino mega2560. this will then be converted into a digtal value by onboard adc. i want to put this value of the adc, hence the potentiometer, into a register(which i do not know which one?) to change the timer settings. this will eventually lead to a chage in frequency. can anyone tell me if the concept is correct? and which register do i chance.
By just changing the prescalar, i can only get 5 different sets of frequencies on each timer. I need a continuous range of frequencies please.
Logged

Pages: [1]   Go Up
Jump to: